(编辑:jimmy 日期: 2025/1/12 浏览:2)
tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
以一个维度是2,形状是[2,3]的tensor举例:
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 # output: 2.0 print m_0 # output: [ 1. 2. 3.] print m_1 #output: [ 2. 2.]
如果设置保持原来的张量的维度,keep_dims=True ,结果:
print m_a # output: [[ 2.]] print m_0 # output: [[ 1. 2. 3.]] print m_1 #output: [[ 2.], [ 2.]]
类似函数还有: