tf.reduce_mean

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)表示计算行向量平均值;
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值