C4 Week 4 - Face Recognition - triplet_loss

I’m having a little difficulty getting the expected output for the triplet_loss function.

I’m using tf functions for every operation. So this handles the test cases when tensors and lists are used without error exception.

So I tried to find the issue by printing the values at each step.

pos_dist
tf.Tensor([3122.1926 3377.6548 3365.9343], shape=(3,), dtype=float32)

neg_dist
tf.Tensor([3905.9553 2850.595 3434.0498], shape=(3,), dtype=float32)

basic_loss before tf.reduce.sum
tf.Tensor([-783.5627 527.2598 -67.91548], shape=(3,), dtype=float32)

basic_loss after tf.reduce.sum
tf.Tensor(-324.21832, shape=(), dtype=float32)

loss (of course it results in zero because loss was negative in the previous step)
loss = tf.Tensor(0.0, shape=(), dtype=float32)

I can see the expected output of 527.2598 in the basic_loss before tf.reduce.sum, but I don’t understand what I’m doing wrong.

1 Like

nvm, Had an issue with the the order I was applying tf.maximum and tf.reduce_sum when calculating “loss”. Corrected to find the max first and it’s working as expected now.

whew!

4 Likes

I had the same problem :grinning: . I should have used None as the axis in the last line. Before, I had used axis = 0, which was wrong. Although, don’t know why.