Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Invalid reduction dimension 1 for input with 1 dimensions

The tf.reduce_mean() function sums the elements of an array in such a way that the index referred to in the axis argument.

In the following code:

import tensorflow as tf

x = tf.Variable([1, 2, 3])
init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

So for the line

print(sess.run(tf.reduce_sum(x)))

The output is: 6

In order to generate the same output, I need to sum all the elements in a way to reduce the number of columns. So I need to set axis = 1 right?

print(sess.run(tf.reduce_sum(x, 1)))

But I get an error:

ValueError: Invalid reduction dimension 1 for input with 1 dimensions

But if I set axis = 0, I get 6. Why is this?

like image 304
chaine09 Avatar asked Mar 10 '23 11:03

chaine09


2 Answers

The error you get is ValueError: Invalid reduction dimension 1 for input with 1 dimensions. This pretty much means that if you can't reduce the dimension of a 1-dimensional tensor.

For an N x M tensor, setting axis = 0 will return an 1xM tensor, and setting axis = 1 will return a Nx1 tensor. Consider the following example from the tensorflow documentation:

# 'x' is [[1, 1, 1]
#         [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
like image 50
lordingtar Avatar answered Apr 05 '23 22:04

lordingtar


"Note that one way to choose the last axis in a tensor is to use negative indexing (axis=-1)"

like image 36
What Love Avatar answered Apr 05 '23 22:04

What Love