[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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具