[tensorflow] which dimension to reduce in tf.reduce_sum()

部署运行你感兴趣的模型镜像

I began to learn tensorflow.import tensorflow as tf, firstly of course. When encountered with tf.reduce_sum(array) or tf.reduce_mean(array), I felt a little doubt about which is the exact dimension to be reduced. Directly digging the answer in the unfamiliar tensorflow world(for me), somehow, made me confused.

the answer

Think about this. When create a array applying numpy, we use a = numpy.random.rand(3,4,5). That means the length of the first dimension is 3, the length of the second dimension is 4 and 5 for the third dimension.
if applying sess.run(tf.reduce_sum(a,0)), the product’s shape will be (4,5), the dimension length of which is 3 is reduced.

appendix

import tensorflow as tf
import numpy as np
a = np.random.rand(3,4,5)
a
Out[138]: 
array([[[ 0.67386161,  0.25891236,  0.15750355,  0.99356577,  0.62401749],
        [ 0.44104193,  0.14171052,  0.92457023,  0.3475105 ,  0.46261946],
        [ 0.74483249,  0.22724867,  0.61261517,  0.20140201,  0.11718528],
        [ 0.53023319,  0.65372313,  0.34679634,  0.7626164 ,  0.47658279]],
       [[ 0.22209199,  0.57104615,  0.94053357,  0.10663142,  0.96630193],
        [ 0.87147539,  0.29464845,  0.41552753,  0.05044025,  0.92632825],
        [ 0.78404338,  0.42560083,  0.91265402,  0.37281405,  0.25450812],
        [ 0.00306304,  0.74638202,  0.19689413,  0.65906257,  0.46627029]],
       [[ 0.46042323,  0.48506186,  0.73388123,  0.50179246,  0.3163692 ],
        [ 0.33435115,  0.01610695,  0.98188888,  0.77100164,  0.7795511 ],
        [ 0.24383665,  0.28206927,  0.09408851,  0.90500411,  0.69718288],
        [ 0.40164087,  0.66995977,  0.61219998,  0.91530942,  0.00388272]]])
sess = tf.Session()
sess.run(tf.reduce_mean(a,0))
Out[140]: 
array([[ 0.45212561,  0.43834012,  0.61063945,  0.53399655,  0.63556287],
       [ 0.54895615,  0.15082197,  0.77399555,  0.3896508 ,  0.72283294],
       [ 0.59090417,  0.31163959,  0.5397859 ,  0.49307339,  0.35629209],
       [ 0.3116457 ,  0.69002164,  0.38529681,  0.77899613,  0.3155786 ]])
sess.run(tf.reduce_mean(a,0)).shape
Out[141]: (4, 5)
sess.run(tf.reduce_mean(a,1)).shape
Out[142]: (3, 5)
In[...]: import tensorflow as tf
In[...]: import numpy as np
In[...]: a = np.random.rand(3,4,5)
In[...]: a
Out[138]: 
array([[[ 0.67386161,  0.25891236,  0.15750355,  0.99356577,  0.62401749],
        [ 0.44104193,  0.14171052,  0.92457023,  0.3475105 ,  0.46261946],
        [ 0.74483249,  0.22724867,  0.61261517,  0.20140201,  0.11718528],
        [ 0.53023319,  0.65372313,  0.34679634,  0.7626164 ,  0.47658279]],
       [[ 0.22209199,  0.57104615,  0.94053357,  0.10663142,  0.96630193],
        [ 0.87147539,  0.29464845,  0.41552753,  0.05044025,  0.92632825],
        [ 0.78404338,  0.42560083,  0.91265402,  0.37281405,  0.25450812],
        [ 0.00306304,  0.74638202,  0.19689413,  0.65906257,  0.46627029]],
       [[ 0.46042323,  0.48506186,  0.73388123,  0.50179246,  0.3163692 ],
        [ 0.33435115,  0.01610695,  0.98188888,  0.77100164,  0.7795511 ],
        [ 0.24383665,  0.28206927,  0.09408851,  0.90500411,  0.69718288],
        [ 0.40164087,  0.66995977,  0.61219998,  0.91530942,  0.00388272]]])
In[...]: sess = tf.Session()
In[...]: sess.run(tf.reduce_mean(a,0))
Out[140]: 
array([[ 0.45212561,  0.43834012,  0.61063945,  0.53399655,  0.63556287],
       [ 0.54895615,  0.15082197,  0.77399555,  0.3896508 ,  0.72283294],
       [ 0.59090417,  0.31163959,  0.5397859 ,  0.49307339,  0.35629209],
       [ 0.3116457 ,  0.69002164,  0.38529681,  0.77899613,  0.3155786 ]])
In[...]: sess.run(tf.reduce_mean(a,0)).shape
Out[141]: (4, 5)
In[...]: sess.run(tf.reduce_mean(a,1)).shape
Out[142]: (3, 5)

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值