Face Net Assignment : tf.reduce_sum()

I am passing tensor X into tf.reduce_sum():
tf.reduce_sum(X, axis=1)
The dimensions of the tensor are : (3,128)
My output should come out to be of shape : (3,1)
I am getting the correct output but it is accompanied with error message :

I got stuck on it for more than an hour and still unable to understand why this error is being shown.
However, if I pass axis=-1 :
tf.reduce_sum(X, axis=-1)
Everything works fine and all test cases pass
Please help

Since the tensor is a 2D tensor, axis=-1 and axis=1 must mean the same thing but I don’t see why error is being shown

-1 means ‘the last dimension, whatever that is’
1 means a specific dimension, starting the index count with 0 as customary with Python.

Suggest examine the shape of the input tensor X, does it have a ‘1’ indexed dimension?

What do you mean by 1 indexed dimension? In python(or any other language/framework), arrays are 0 indexed.
The dimension of X is (3, 128), I check the dimensions thoroughly

Sorry, but the evidence contradicts what you are claiming. I suggest you print the shape of X the line before the reduce_sum call that throws the exception. The evidence of the error message is that X is a 1D tensor.

For the record, not all programming languages use 0-based indexes

When using axis=integer the input must have integer+1 dimensions or the error you report will occur. Maybe try using an assertion to confirm your assumption.

1 Like

I have the same experience. I put a print statement that prints the shape of “X” which shows shape=(3, 128), dtype=float32). It doesn’t make sense to me that axis = -1 works but axis = 1 does not.

That said, the test calls the triplet_loss function several times and after the first call the shapes change to, for example, tf.Tensor([0. 0.], shape=(2,), dtype=float32). That could be why the explicit axis = 1 throws an exception.

Yes, if you examine the test cases, you’ll see that they come in at least 2 different “flavors” in terms of the input dimensions. Not sure why they do that, but it requires that you write the code in a more flexible way to handle that. The simplest and cleanest method is using -1 to say “the last axis whatever that happens to be”.