zl程序教程

您现在的位置是:首页 >  其它

当前栏目

tf.reduce_sum 用法

用法 sum TF reduce
2023-09-14 09:15:50 时间
import tensorflow as tf
# x has a shape of (2, 3) (two rows and three columns):
x = tf.constant([[1, 1, 1], [1, 1, 1]])
x.numpy()
array([[1, 1, 1],
       [1, 1, 1]], dtype=int32)
# sum all the elements
# 1 + 1 + 1 + 1 + 1+ 1 = 6
tf.reduce_sum(x).numpy()
# reduce along the first dimension
# the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
tf.reduce_sum(x, 0).numpy()
 array([2, 2, 2], dtype=int32)
# reduce along the second dimension
# the result is [1, 1] + [1, 1] + [1, 1] = [3, 3]
tf.reduce_sum(x, 1).numpy()
 array([3, 3], dtype=int32)
# keep the original dimensions
tf.reduce_sum(x, 1, keepdims=True).numpy()
array([[3],
       [3]], dtype=int32)