首先看一下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 :")