[tensorflow] which dimension to reduce in tf.reduce_sum()
2017-10-20 16:53
555 查看
I began to learn tensorflow.
if applying
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 usea = 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)
相关文章推荐
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- tensorflow:tf.reduce_mean()和tf.reduce_sum()
- 对tf.reduce_sum tensorflow维度上的操作详解
- tf.reduce_sum tensorflow维度上的操作
- Tensorflow 函数说明tf.reduce_sum
- EPLAN API 入门系列- 提高篇(How to get all connections which are not in location box)
- 133 You want to enable automatic PGA memory management in your database. Which setting is required t
- TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法
- tf.sign和tf.reduce_sum
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- TensorFlow的reduce_sum()函数
- xcode 报错 malloc: *** error for object 0x6c3c5a4: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug------d
- Do you know how many combinations to select n numbers from 1 to m with sum of which is mysum?
- How to test the code which maybe distroyed in the future ?
- Which to Prefer: to Leave or Stay in the National Enterprises
- 30.In which situation may the UNDO_RETENTION parameter be ignored, even if it is set to a value?
- same operation in multiple if statement, how to reduce code redundancy
- TensorFlow学习笔记12----Creating Estimators in tf.contrib.learn