Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does reduce_sum() work in tensorflow?

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. 
like image 374
Bhaskar Dhariyal Avatar asked Nov 07 '17 12:11

Bhaskar Dhariyal


People also ask

How do you find the sum of a tensor in Tensorflow?

The tf. sum() function is used to calculate sum of the elements of a specified Tensor across its dimension. It reduces the given input elements along the dimensions of axes. If the parameter “keepDims” is true, the reduced dimensions are retained with length 1 else the rank of Tensor is reduced by 1.

How do you transpose in Tensorflow?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .

What does the axis parameter of TF Expand_dims do?

expand_dims() is used to insert an addition dimension in input Tensor. Parameters: input: It is the input Tensor. axis: It defines the index at which dimension should be inserted.

What is TF stack?

tf.stack( values, axis=0, name='stack' ) Defined in tensorflow/python/ops/array_ops.py. Stacks a list of rank- R tensors into one rank- (R+1) Packs the list of tensors in values into a tensor with rank one higher than each tensor in values , by packing them along the dimension.


2 Answers

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).

like image 117
Dmitriy Danevskiy Avatar answered Sep 20 '22 21:09

Dmitriy Danevskiy


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) 
like image 30
Enrique Ortuño Avatar answered Sep 17 '22 21:09

Enrique Ortuño