tf.reduce_sum()
tf.reduce_sum()可以理解为压缩求和,用于降维。
例子:
# 'x' is [[1, 1, 1]
# [1, 1, 1]]
#求和
tf.reduce_sum(x) ==> 6
#按列求和
tf.reduce_sum(x, 0) ==> [2, 2, 2]
#按行求和
tf.reduce_sum(x, 1) ==> [3, 3]
#按照行的维度求和
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
#行列求和
tf.reduce_sum(x, [0, 1]) ==> 6
Reference:
https://blog.csdn.net/arjick/article/details/78415675
tf.reduce_mean()
tf.reduce_mean()用于计算tensor沿着指定数轴上的平均值,主要用作降维或者计算tensor的平均值。
tf.reduce_mean(input_tensor, #输入的待降维的tensor
axis=None, #指定的轴,如果不指定,则计算所有元素的均值
keep_dims=False, #是否降低维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度
name=None, #操作的名称
reduction_indices=None) #在以前的版本中用来指定轴,已弃用
例子:
import tensorflow as tf
x = [[1, 2, 3],
[1, 2, 3]]
xx = tf.cast(x, tf.float32)
mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)
with tf.Session() as sess:
m_a , m_0, m_1 = sess.run([mean_all, mean_0, mean_1])
print(m_a)
print(m_0)
print(m_1)
输出:
[[2.]]
[[1., 2., 3.]]
[[2.], [2.]]