1.作用
tf.reduce_mean函数的作用是:计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
2.表达式
reduce_mean(input_tensor, axis=None,
keep_dims=False,
name=None)
第一个参数input_tensor: 输入的待降维的tensor;
第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
第四个参数name: 操作的名称;
举例:
import tensorflow as tf
x = [[1, 2, 3],
[1, 2, 3]]
x = tf.cast(x, tf.float32)
mean_all = tf.reduce_mean(x, keep_dims=False)
mean_0 = tf.reduce_mean(x, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(x, 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(x)
print(m_a)
print(m_0)
print(m_1)
输出:
Tensor("Cast:0", shape=(2, 3), dtype=float32)
2.0
[1. 2. 3.]
[2. 2.]
Process finished with exit code 0