您的位置:首页 > 其它

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

2017-10-20 16:53 555 查看
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.7
9d49
3388123,  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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐