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?
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]
"Note that one way to choose the last axis in a tensor is to use negative indexing (axis=-1)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With