C3W4 Problem with Triple Loss

Half my tests are failing.

Wrong output.
Expected 0.5.
Got 0.8008769154548645
Wrong output.
Expected 1.4382333.
Got 1.9704445600509644
Wrong output. Take a look to the use of the margin variable.
Expected 2.1715667.
Got 1.237111210823059

I am looking at the Evaluating a Siamese Model lab. There are differences between how various variables are computed. My lab id xcbszllt

Cheers,
Drew

P.S I am not sure what happens if I don’t finish by the end-of-the-day

Please click my name and message your notebook as an attachment.

Once again thanks! I am trying to understand why the difference between the example lab and the assignment? I found the hints confusing so I’ve been trying various things.

[code removed - moderator].

Cheers,
Drew

@drew_Frances
Here are some hints:

  1. Computation of mean_negative is incorrect. You have computed the sum. Remember that there are n - 1 negative instances and 1 positive instance.
  2. You should replace None with the actual index when calculating sum and max
  3. Implementations of triplet_loss1 and triplet_loss2 are incorrect. Please see the equations in the writeup.

Hi @balaji.ambresh:

Thanks for the advice! I didn’t notice the hints and expand it - silly me. I also find the comments confusing. I have made the changes. I use the keepdims=True. Now I get:

Wrong output.
Expected 0.5.
Got 0.5464922785758972
Wrong output.
Expected 1.4382333.
Got 1.6667333841323853
Wrong output. Take a look to the use of the margin variable.
Expected 2.1715667.
Got 2.766733407974243
3 Tests passed
3 Tests failed

Again, I am looking at the Modified Triplet Loss lecture notes. I don’t understand how the mask is used.

Cheers,
Drew

1 Like

Please read the markdown Exercise 03 for computing mask.

The loss functions are there in section 2.2 Hard Negative Mining.

Hi @balaji.ambresh :

I read the sections. I do believe I’ve implemented them. I get confused over the equation and the comments. Example

(looking at the equation)
triplet_loss2 = fastnp.max(-score + closest_negative + margin, 0)*

(if I follow the comments)
triplet_loss2 = fastnp.max(margin - positive + closest_negative, 0.)

Equation with the ‘*’ provides the closest answer. The problem is I am now not sure what to look at.

Again, I really appreciate the help!

Cheers,
Drew

Please look at one term at a time:

  1. \alpha = margin
  2. cos(A, P) is the distance between the anchor and positive. You have calculated this quantity using the diagonal operation on the scores variable.
  3. Negative distance is already calculated which is one of closest negative or mean negative, depending on the triplet loss you’re calculating.

Don’t forget to use fastnp.maximum

@drew_Frances

Here are some hints:

  1. remove keepdims=True when computing mean_negative and closest_negative.
  2. When calculating triplet losses, don’t use scores. We want only the distances between the anchor and positive. You have already created a variable that grabs the diagonal entries from scores.

Hi @balaji.ambresh :

It is now working. Thanks so much for the help!!!

Cheers,
Drew