Python reduce_sum() 在 tensorflow 中是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47157692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does reduce_sum() work in tensorflow?
提问by Bhaskar Dhariyal
I am learning tensorflow, I picked up the following code from the tensorflow website. According to my understanding, axis=0 is for rows and axis=1 is for columns.
我正在学习 tensorflow,我从 tensorflow 网站上获取了以下代码。根据我的理解,axis=0 表示行,axis=1 表示列。
How are they getting output mentioned in comments? I have mentioned output according to my thinking against ##.
他们如何获得评论中提到的输出?我已经根据我对## 的想法提到了输出。
import tensorflow as tf
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0) # [2, 2, 2] ## [3, 3]
tf.reduce_sum(x, 1) # [3, 3] ##[2, 2, 2]
tf.reduce_sum(x, [0, 1]) # 6 ## Didn't understood at all.
回答by Dmitriy Danevskiy
x
has a shape of (2, 3)
(two rows and three columns):
x
具有(2, 3)
(两行三列)的形状:
1 1 1
1 1 1
By doing tf.reduce_sum(x, 0)
the tensor is reduced along the first dimension (rows), so the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
.
通过这样做tf.reduce_sum(x, 0)
,张量沿着第一维(行)减少,所以结果是[1, 1, 1] + [1, 1, 1] = [2, 2, 2]
。
By doing tf.reduce_sum(x, 1)
the tensor is reduced along the second dimension (columns), so the result is [1, 1] + [1, 1] + [1, 1] = [3, 3]
.
通过这样做tf.reduce_sum(x, 1)
,张量沿着第二个维度(列)减少,所以结果是[1, 1] + [1, 1] + [1, 1] = [3, 3]
。
By doing tf.reduce_sum(x, [0, 1])
the tensor is reduced along BOTH dimensions (rows and columns), so the result is 1 + 1 + 1 + 1 + 1 + 1 = 6
or, equivalently, [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
, and then 2 + 2 + 2 = 6
(reduce along rows, then reduce the resulted array).
通过这样做tf.reduce_sum(x, [0, 1])
,张量沿两个维度(行和列)减少,因此结果是1 + 1 + 1 + 1 + 1 + 1 = 6
或等价地,[1, 1, 1] + [1, 1, 1] = [2, 2, 2]
,然后2 + 2 + 2 = 6
(沿行减少,然后减少结果数组)。
回答by Maxim
The input is a 2-D tensor:
输入是一个二维张量:
1 1 1
1 1 1
The 0 axis in tensorflow is the rows, 1 axis is the columns. The sum along the 0 axiswill produce a 1-D tensor of length 3
, each element is a per-column sum. The result is thus [2, 2, 2]
. Likewise for the rows.
tensorflow 中的 0 轴是行,1 轴是列。沿 0 轴的总和将产生一个长度为 的一维张量3
,每个元素是每列的总和。结果是这样的[2, 2, 2]
。行也是如此。
The sum along both axes is, in this case, the sum of all values in the tensor, which is 6
.
在这种情况下,沿两个轴的总和是张量中所有值的总和,即6
。
Comparison to numpy:
与numpy 的比较:
a = np.array([[1, 1, 1], [1, 1, 1]])
np.sum(a, axis=0) # [2 2 2]
np.sum(a, axis=1) # [3 3]
np.sum(a, axis=(0, 1)) # 6
As you can see, the output is the same.
如您所见,输出是相同的。
回答by Enrique Ortu?o
In order to understand better what is going on I will change the values, and the results are self explanatory
为了更好地了解发生了什么,我将更改值,结果不言自明
import tensorflow as tf
x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, 0) # [ 9 18 36]
b = tf.reduce_sum(x, 1) # [ 7 56]
c = tf.reduce_sum(x, [0, 1]) # 63
with tf.Session() as sess:
output_a = sess.run(a)
print(output_a)
output_b = sess.run(b)
print(output_b)
output_c = sess.run(c)
print(output_c)
回答by asakryukin
Think it like that, the axis indicates the dimension which will be eliminated. So for the first case axis 0
, so if you go through this dimension (2 entries) they will all collapse into 1. Thus it will be as following:
这样想,轴表示将被淘汰的维度。所以对于第一个 case axis 0
,所以如果你通过这个维度(2 个条目),它们将全部折叠为 1。因此它将如下所示:
result = [[1,1,1] + [1,1,1]] = [2,2,2]
So you removed dimension 0
.
所以你删除了维度0
。
Now, for the second case, you will collapse axis 1
(or columns), so:
现在,对于第二种情况,您将折叠轴1
(或列),因此:
result = [[1,1] + [1,1] + [1,1]] = [3,3]
And the last case is you keep collapsing in order indicated in the brackets. In other words, first you eliminate the rows and then the columns:
最后一种情况是您继续按括号中指示的顺序折叠。换句话说,首先消除行,然后消除列:
result1 = [2,2,2]
result_final = 2 + 2 + 2 = 6
Hope this helps!
希望这可以帮助!
回答by Ashiq Imran
tf.reduce_sum(x, [0, 1])
commands will calculate sum across axis = 0 (row-wise) first, then will calculate sum across axis = 1 (column-wise)
命令将首先计算跨轴的总和 = 0(按行),然后计算跨轴的总和 = 1(按列)
For example,
例如,
x = tf.constant([[1, 1, 1], [1, 1, 1]])
You are summing into [2,2,2] after calculating sum across axis = 0. You are summing 2 + 2 + 2 after calculating sum across axis = 1.
在计算跨轴总和 = 0 后求和为 [2,2,2]。在计算跨轴总和 = 1 后求和 2 + 2 + 2。
Finally, getting 6 as output.
最后,得到 6 作为输出。