介绍
按某个维度求tensor的和(缩小维度求和)
别名
- tf.reduce_sum 最常用
语法
tf.math.reduce_sum(
input_tensor,
axis=None,
keepdims=False,
name=None
)
参数:
- input_tensor:数字类型的tensor
- axis:按某个维度求和(缩小某个维度求和)。默认值None,缩小所有的维度
- keepdims:默认值False。如果为true,则保留长度为1的缩小尺寸
例子
axis为None
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x) # 6
axis为[0,1]
当前形状是2维,所以等同于 axis=None
tf.reduce_sum(x, [0, 1]) # 6
axis=0
tf.reduce_sum(x, 0) # [2, 2, 2]
axis=1
tf.reduce_sum(x, 1) # [3, 3]
keepdims=True
tf.reduce_sum(x, 1, keepdims=True) # [[3], [3]]


