Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does tf.reduce_sum do with axis = -1?

I don't understand why the output of the following code is [7 56].

import tensorflow as tf

x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1)  # [ 9 18 36]

with tf.Session() as sess:
  output_a = sess.run(a)
  print(output_a)

I get that row-wise addition has been done. But can someone shed some light on why -1 in the reduce_sum function has been treated to sum all values in a row?

like image 701
ProgSnob Avatar asked Dec 08 '22 13:12

ProgSnob


2 Answers

-1 means the last axis; Since you have a rank 2 tensor, the last axis is the second axis, that is, along the rows; tf.reduce_sum with axis=-1 will thus reduce (sum) the second dimension.

like image 76
Psidom Avatar answered Dec 10 '22 18:12

Psidom


I run your code and actually gave me a different answer:

import tensorflow as tf
x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1)  
tf.print(a)

The answer is [7,56], which is added 1+2+4 =7, and 8+16+32=56.

axis: The dimensions to reduce. My understanding:

  1. '-1' means the last axis, or dimension.

tf.reduce_sum(x, -1) is equal to tf.reduce_sum(x, 1) here since only 2 dimensions.

  1. The dimension is 2*3 (2 rows*3 cols). Goal is to remove the last dimension '3' and the result should be 2 rows:

[[7] [56]]

Since no 'keepdims=True' here, [] will be removed and we get result [7,56]

  1. You can test this example. Note: '-1' and '1' are different here.

y = tf.constant([[[1, 2, 4], [1, 0, 3]],[[1,2,3],[2,2,1]]]) c = tf.reduce_sum(y, 1) # if (y,-1) will be [[7,4],[6,5]] tf.print(c) #[[2 2 7], [3 4 4]]

like image 39
sertine Avatar answered Dec 10 '22 17:12

sertine