C3W3 Triplet Loss Function

Hello!

Currently working on exercise 2, and getting a triplet loss of 0.9999999903404633 instead of 0.70

I’m not sure where my code went wrong. I’ve crawled through most of the forum topics on this, but I can’t seem to find a solution. I also don’t want to post my code on the forum, so I’m just making a post about it and seeing if anyone can point me in the right direction!

Thank you for your help!

Hi @roses_r_red

try searching as Triplet loss function natural-language-processing:nlp-course-3

you will find almost 5 similar threads, although you don’t have same incorrect output as mentioned in other threads but you refer the response mentioned in how to recall the codes for the grade cell or hints

there is a thread by @arvyzukai which gives you an image with value to check with #print statement for each code line which will give you hint on where your codes might be going wrong!!!

other threads are by me mentioning how the codes needs to be recalled

let me know if you still didn’t get through!!

Regards
DP

1 Like

Hi @roses_r_red

As Deepti mentioned, there’s the post with all the intermediate values for this function. You can check where your values start to differ by printing them at any stage.

Cheers

Hello DP and arvyzukai,

I’ve been trying to print those values, but when I run print(mean_negative) the notebook returns mean_negative is not defined. I’m not sure how I can make the notebook print out the intermediate values better.

Do you have any ideas? As far as I can tell, I’ve been recalling all of my code correctly, but maybe there are some issues with my arguments?

Thank you!

This will depend on where you have placed the print state and if you run all the cells before cell in present addressing.

kindly share codes for the grade cell you are having issue via personal DM

regards
DP

Hi @roses_r_red

Your code requires correction for triplet_loss1 and triplet_loss2

While calculating the A and B
A = subtract positive from margin and add closest_negative
B = subtract positive from margin and add mean_negative

You have used correctly the codes but because you have missed tuppling in these codes caused these error.

Remember the instructions mentions first to subtract (margin-positive) and then add closest negative for A and mean_negative for B

Also while recalling triplet loss1 and triplet loss2 mentions you to compute tf.maximum among 0.0 and A for triplet loss1, 0.0 and B for triplet loss2, so here the tuple placement between the 0.0 and A calculation is required.

Regards
DP

1 Like

I SEE!!

Thank you for the reminder! I had completely forgot that we needed to use tuples.

Appreciate your pointers! Extremely helpful!

1 Like

Dear Mentor:

Regarding TripIetLoss unction, I got some diferent results from what the code suggested as well. The correct result is 0.7, I got something like 2.5. The interesting thing is when I put this similarity matrix in the previous lab to calculate, it also got something like 2.4.

One thing that got me quite confused is that the 2nd element of diagonal part of similarity matrix is -1, how could that be?

Thanks so much for your help.

2 Likes

Problem gets resolved by switching the order of v1 and v2 at the “tf.linalg.matmul” stage, but wondering in practice if it matters. Thanks.

2 Likes

Seems like the diagonal value is not exactly [1, 1] but really [1, -1]. The masking is done on columns not rows, so it will make a difference how you compute the score matrix