tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
作用:沿着张量不同的数轴进行计算平均值。
参数:input_tensor: 被计算的张量,确保为数字类型。
axis: 方向数轴,如果没有指明,默认是所有数轴都减小为1。
keep_dims: 如果定义true, 则保留维数,但数量个数为0.
name: 操作过程的名称。
reduction_indices: 为了旧函数兼容的数轴。
返回值:降低维数的平均值。
如:
import tensorflow as tf
#创建张量
x = tf.Variable([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]);
#显示
init = tf.global_variables_initializer();
with tf.Session() as sess:
sess.run(init);
#tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
y = tf.reduce_mean(x);
y01 = tf.reduce_mean(x, axis=0, keep_dims=False);
y02 = tf.reduce_mean(x, axis=0, keep_dims=True);
y1 = tf.reduce_mean(x, axis=1);
print("x = ", x.eval());
print("tf.reduce_mean(x) = ", y.eval());
print("tf.reduce_mean(x, axis=0, keep_dims=False) = ", y01.eval());
print("tf.reduce_mean(x, axis=0, keep_dims=True) = ", y02.eval())
print("tf.reduce_mean(x, axis=1) = ", y1.eval());
输出:
('x = ', array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]], dtype=float32))
('tf.reduce_mean(x) = ', 5.0)
('tf.reduce_mean(x, axis=0, keep_dims=False) = ', array([ 4., 5., 6.], dtype=float32))
('tf.reduce_mean(x, axis=0, keep_dims=True) = ', array([[ 4., 5., 6.]], dtype=float32))
('tf.reduce_mean(x, axis=1) = ', array([ 2., 5., 8.], dtype=float32))
即:
tf.reduce_mean(x)表示计算所有元素平均值;
tf.reduce_mean(x, axis=0)表示计算列向量平均值;
tf.reduce_mean(x, axis=1)表示计算行向量平均值;