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.
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 understand at all.
x
has a shape of (2, 3)
(two rows and three columns):
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]
.
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]
.
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).