tf.nn.moments笔记: axes到底咋回事?

测试代码:

x = np.arange(12,dtype=np.float32).reshape(3,4)
a = tf.nn.moments(tf.constant(x),[0])
b = tf.nn.moments(tf.constant(x),[1])
# 相当于展开[0,1]维度, 在所有数上计算均值和方差,等于x.mean(),x.var()
c = tf.nn.moments(tf.constant(x),[0,1])
d = tf.nn.moments(tf.constant(x),None) # 同上
e = tf.nn.moments(tf.constant(x),[]) # 在实数上(0维tensor上)计算 
print(0, x,x.mean(),x.var())
with tf.Session() as sess:
    print(1, sess.run(a))
    print(2, sess.run(b))
    print(3, sess.run(c))
    print(4, sess.run(c)==sess.run(d),)
    print(5, sess.run(e))

执行结果:

0 [[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]] 5.5 11.916667
1 (array([4., 5., 6., 7.], dtype=float32), array([10.666667, 10.666667, 10.666667, 10.666667], dtype=float32))
2 (array([1.5, 5.5, 9.5], dtype=float32), array([1.25, 1.25, 1.25], dtype=float32))
3 (5.5, 11.916667)
4 True
5 (array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32), array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]], dtype=float32))

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值