# Error in the triplet\_loss function (based on expected output)

**URL:** <https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [December 12, 2023, 10:07pm UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335 "2023-12-12T22:07:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![parth\_upadhyay1](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@parth\_upadhyay1](https://community.deeplearning.ai/u/parth_upadhyay1)\
**Post date:** [December 12, 2023, 10:07pm UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/1 "2023-12-12T22:07:01Z")

</div>

Step-4 of the triplet\_loss function was supposed to be implemented like following based on the given theory:

_{moderator edit - solution code removed}_

But, with the following implementation, we get the expected output (which is wrong):

_{moderator edit - solution code removed}_

Please correct me if my analysis is incorrect!

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [December 12, 2023, 10:27pm UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/2 "2023-12-12T22:27:41Z")

</div>

Can you post some screen capture images that show your results?

---

<div class="post-metadata">

**Author:** ![parth\_upadhyay1](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@parth\_upadhyay1](https://community.deeplearning.ai/u/parth_upadhyay1)\
**Post date:** [December 12, 2023, 10:34pm UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/3 "2023-12-12T22:34:39Z")

</div>

This is the error, when I used loss equation, based on theory (my first loss function in the post)

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/8/d8c3972c483a0405a226617221314a70d880fe43.png)

Whereas, second loss equation, which is not correct as per theory gives me this output

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/5/d58efb20985c109187daa3319f8bd1f34504f393.png)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 13, 2023, 2:28am UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/4 "2023-12-13T02:28:14Z")

</div>

Sorry, but your analysis is incorrect. What the formula asks for is the sum over the max values, which is the second set of code. The first code is the max of the sums, which is not the same thing. The evidence that you get the correct answer with the second set of code is at least suggestive one would think. These courses were last updated in any major way in April of 2021. If the test code were incorrect, we would know about it by now.

---

<div class="post-metadata">

**Author:** ![parth\_upadhyay1](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@parth\_upadhyay1](https://community.deeplearning.ai/u/parth_upadhyay1)\
**Post date:** [December 13, 2023, 3:55am UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/5 "2023-12-13T03:55:14Z")

</div>

I am grateful for your reply. I just realized that I misinterpreted equation (3). I apologize for raising a naive concern without thoroughly investigating it.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 13, 2023, 4:48am UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/6 "2023-12-13T04:48:39Z")

</div>

Yes, the point is that the loss is always the sum of the losses for the individual training samples and it is only at the level of the values for the individual samples that we need to do the max to assure that the answer is non-negative. So it should be clear from looking at the formula that the sum is the “outer” operation.

Glad to hear that it makes sense now. If it’s any comfort, you are far from the first person to step on that landmine. 🤓

---

<div class="post-metadata">

**Author:** ![parth\_upadhyay1](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@parth\_upadhyay1](https://community.deeplearning.ai/u/parth_upadhyay1)\
**Post date:** [December 13, 2023, 5:20am UTC](https://community.deeplearning.ai/t/error-in-the-triplet-loss-function-based-on-expected-output/517335/7 "2023-12-13T05:20:00Z")

</div>

I agree with your explanation about the loss function. I overlooked an important detail by not checking it thoroughly. Thanks again for your input on my concern and for sharing your perspective in your last comment.
