Tensorflow函数:tf.zeros ,tf.argmax

本文深入解析TensorFlow中tf.zeros函数的使用方法,包括如何创建全零张量及其参数详解,同时对tf.argmax函数进行阐述,解释其在计算最大值索引的应用场景,特别是axis参数的作用。

tf.zeros函数

tf.zeros(
    shape,
    dtype=tf.float32,
    name=None
)

定义在:tensorflow/python/ops/array_ops.py.

创建一个所有元素都设置为零的张量. 

该操作返回一个带有形状shape的类型为dtype张量,并且所有元素都设为零.

例如:

tf.zeros([3, 4], tf.int32)  # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

函数参数:

  • shape:整数、整数元组或类型为int32的1维Tensor的列表.
  • dtype:结果Tensor中元素的类型.
  • name:操作的名称(可选).

函数返回值:

tf.zeros函数返回将所有元素设置为零的张量

 

 

 

tf.argmax是tensorflow用numpy的np.argmax实现的,它能给出某个tensor对象在某一维上的其数据最大值所在的索引值,常用于metric(如acc)的计算

tf.argmax()函数中有个axis参数(轴),该参数能指定按照哪个维度计算。
如 在矩阵的结构中,axis可被设置为0或1,分别表示
0:按列计算,1:行计算

下面是具体的示例,便于直观理解(因tf.argmax()的axis的用法与numpy中的argmax相同,为方便起见,本文使用了numpy.argmax()用于示例)

  1.  

axis=0

 

当axis=0时,以列为单位,第一列的最大值为索引为3的8,所以第一个值为3,以此类推

 

  1.  

axis=1

 

当axis=1时,以行为单位,第一行的最大值为索引为2的3,所以第一个结果为2

 

 



 

using NumSharp; using System; using System.Linq; // 用于Enumerable.Range using Tensorflow; // 核心TensorFlow命名空间 using Tensorflow.Keras; // Keras API using Tensorflow.Keras.ArgsDefinition; // 模型参数定义 using Tensorflow.Keras.Engine; // 模型基类 using static Tensorflow.Binding; // 静态绑定tf对象 using static Tensorflow.KerasApi; namespace DNN_Keras { public class LinearRegressionKeras { class Program { static void Main() { TFNET tfnet = new(); tfnet.Run(); } } public class TFNET { public void Run() { Tensor x = tf.constant (new[,] { { 1.0f,2.0f,3.0f}, { 4.0f,5.0f,6.0f} });// 输入数据(2个样本,3特征) Tensor y = tf.constant (new[,] { { 10.0f }, { 20.0f} }); // 真实标签 var model = new Linear(new ModelArgs { InputShape = (3) }); // 初始化模型(需指定输入形状) var optimizer = keras.optimizers.SGD(learning_rate: 0.01f); // Keras优化器 // 训练循环(20轮) foreach (var step in Enumerable.Range(0, 20)) { using var g = tf.GradientTape(); var y_pred = model.Apply(x);// 前向传播 var loss = tf.reduce_mean(tf.square(y_pred - y)); // MSE损失 var grads = g.gradient(loss, model.TrainableVariables);// 计算梯度(自动收集model的可训练变量) // 手动更新参数(配对梯度与变量) for (int i = 0; i < grads.Length; i++) optimizer.apply_gradients([(grads[i], model.TrainableVariables[i])]); Console.WriteLine($"step:{step},loss:{loss.numpy()}"); } Console.WriteLine(model.TrainableVariables.ToArray()); Console.ReadKey(); } } public class Linear:Model { private readonly Layer dense; public Linear(ModelArgs args):base(args) { dense = (Layer)keras.layers.Dense(1, activation: null, kernel_initializer: tf.zeros_initializer, bias_initializer: tf.zeros_initializer); StackLayers(dense); } protected virtual Tensors Call(Tensor inputs,bool training = false) { var outputs = dense.Apply(inputs); return outputs; } } } } 运行如下step:0,loss:145.16667 step:1,loss:145.16667 step:2,loss:145.16667 step:3,loss:145.16667 step:4,loss:145.16667 step:5,loss:145.16667 step:6,loss:145.16667 step:7,loss:145.16667 step:8,loss:145.16667 step:9,loss:145.16667 step:10,loss:145.16667 step:11,loss:145.16667 step:12,loss:145.16667 step:13,loss:145.16667 step:14,loss:145.16667 step:15,loss:145.16667 step:16,loss:145.16667 step:17,loss:145.16667 step:18,loss:145.16667 step:19,loss:145.16667 Tensorflow.IVariableV1[]
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值