C4 W4 P1 (UNQ_C1) triplet_loss() - Calculation of Loss & Cost

Hello there!

I am working on the triplet_loss() function in C4 W4 P1 UNQ_C1, and I have a conceptual question on the following test case:

y_true = (None, None, None)
y_pred_perfect = (
    [1., 1.], #Anchor
    [1., 1.], #Positive
    [0., 0.,] #Negative
loss = triplet_loss(y_true, y_pred_perfect, 3) # alpha = 3
assert loss == 1., "Wrong value. Check that pos_dist = 0 and neg_dist = 2 in this example"
  • We have a perfect match between y_pred_perfect[0] and y_pred_perfect[1]
  • We have a perfect non-match between y_pred_perfect[0] and y_pred_perfect[2]
  • We have two samples of each (m = 2)

Thus, when indeed pos_dist = 0 and neg_dist = 2, the result of loss is 0 instead of 1:


basic_loss (loss) is -1.0 = 0.0 - 4.0 + 3 #diff of the squares plus alpha
loss (∑ of m losses) should be 1. an it is 0. #grader wants 1 but max(-1., 0.) = 0.

Why does the grader expects a loss of +1 (assert loss == 1) when the max of the loss in this test case is 0, is it not?

Thanks for any feedback!

Hi @Jonathanlugo,

The neg_dist is 2. The pos_dist is 0. The loss is max(pos_dist - neg_dist + alpha,0) = max(0-2+3,0) = max(1,0). Thus the loss is 1.

Hope this helps!

Thank you @thearkamitra,

but aren’t the squares missing in the formula? :thinking:

Never mind, in order for the formula to work 0 and 2 are already the squares of the differences. Thanks!

The distance can be anything you want; obviously some things distances should follow are:

  1. D(x,y)>=0
  2. D(x,y) = D(y,x)
  3. D(x,y)+ D(y,z)>= D(x,z).
    Where D is the dist function.

Got you.

Also, I actually fixed the issue for all cases once I understood the idea. I was squaring the difference AFTER the SUM, which was incorrect. Now I do it immediately after the subtraction. That worked.

But I think I got the concept :sunglasses:


Right! The point is that the loss is computed “per sample” and then summed. That’s why the “order of operations” is that way. And of course the sum of the squares is not the same as the square of the sum.