首先看一下reduce_sum及其参数的注释 :
def tf.reduce_sum(input_tensor, axis=None, keepdims=False, name=None)
Computes the sum of elements across dimensions of a tensor.
Reduces input_tensor along the dimensions given in axis. Unless keepdims is true, the rank of the tensor is reduced by 1 for each entry in axis. If keepdims is true, the reduced dimensions are retained with length 1.
If axis is None, all dimensions are reduced, and a tensor with a
single element is returned.
1 参数只有一个矩阵 input_tensor,其余参数为默认
下面我们用三个张量为例(下统一称为矩阵),对一维向量,二维矩阵,三维矩阵分别执行其余参数为默认的reduce_sum()运算。
x_dim_1 = tf.constant([1,2,3])
z = tf.reduce_sum(x_dim_1)
print("1D sum :")
print(z)
x_dim_2 = tf.constant([[1,2,3],[4,5,6]])
z = tf.reduce_sum(x_dim_2)
print("2D sum :")

这篇博客详细解析了TensorFlow中的tf.reduce_sum()函数,介绍了如何在不同维度上进行求和操作。当axis参数为None时,函数对所有元素求和并返回一个数值。当设置axis并保持keepdims为False时,函数会沿着指定轴进行求和,改变了矩阵的维度。文章通过实例解释了axis参数在高维矩阵中的作用,展示了不同设置下的运算结果。
最低0.47元/天 解锁文章
160

被折叠的 条评论
为什么被折叠?



