C4W4 The tripletLoss test might be mistaken (Face Recognition Assignment)

Hi, I could not pass the test for the TripletLoss. Examining the test code, it seems it has a mistake here:
y_pred_perfect = ( [[1., 0.], [1., 0.]], [[1., 0.], [1., 0.]], [[0., 1.], [0., 1.]] )
loss = triplet_loss(y_true, y_pred_perfect, 3)
assert loss == 2., “Wrong value. Are you applying tf.reduce_sum to get the loss?”

For these inputs, pos_dist=0; neg_dist=1^2+1^2+1^2+1^2=4;
with alpha=3, that should yield loss=max(0,0-4+3)=0, not 2 ?!

It worked fine for me. I think what you’re missing is that pos_dist and neg_dist are vectors with one element per sample, right? Then you take the difference and add alpha:

[0., 0.] - [2., 2.] + 3 = [1., 1.]

Then when you take the max with 0 (per sample) and sum the terms, you get 2.

1 Like

You are right, thank you!

This saved my day, thank you, Paul!