I have a problem in completeing the test with the input:
y_pred_perfect = ([[1., 1.], [2., 0.]], [[0., 3.], [1., 1.]], [[1., 0.], [0., 1.,]])
I get the following error:
InvalidArgumentError: Invalid reduction dimension (-1 for input with 0 dimension(s) [Op:Sum]
Are you sure it’s that test case that fails? That’s the very last one and I don’t see anything significantly different about it than the others. Maybe it would help to see the actual exception trace you are getting.
The high level point here is that you need to be careful not to make fixed assumptions about the dimensions here. On the “reduce_sum” calls, use axis = -1
to say “the last axis” as opposed to assuming that the last axis is axis = 1
.
I added some print statements to show the types of the input values and the shapes of the intermediate pos_dist values and here’s what I see with that test case:
type(anchor) <class 'tensorflow.python.framework.ops.EagerTensor'>
type(positive) <class 'tensorflow.python.framework.ops.EagerTensor'>
pos_dist.shape (3,)
pos_dist [3122.1926 3377.6548 3365.9343]
loss = tf.Tensor(527.2598, shape=(), dtype=float32)
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (1,)
pos_dist [0.]
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (1,)
pos_dist [0.]
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (1,)
pos_dist [2.]
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (1,)
pos_dist [0.]
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (2,)
pos_dist [0. 0.]
type(anchor) <class 'list'>
type(positive) <class 'list'>
pos_dist.shape (2,)
pos_dist [5. 2.]
The main difference is that the inputs are tensors in the first test case, but lists in the other cases. But there are lots of list cases before the one that fails for you, if you are interpreting the symptoms correctly.
Thank you Paul. Your prints helped me figure out what was my mistake: I forgot to add the “axis=-1” to the reduce_sum in the pos_dist and neg_dist calculations.
Gilad