# The function triplet\_loss ()

**URL:** <https://community.deeplearning.ai/t/the-function-triplet-loss/1801>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [April 26, 2021, 2:05pm UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801 "2021-04-26T14:05:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vuqpham](https://avatars.discourse-cdn.com/v4/letter/v/b77776/32.png) [@vuqpham](https://community.deeplearning.ai/u/vuqpham)\
**Post date:** [April 26, 2021, 2:05pm UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/1 "2021-04-26T14:05:01Z")

</div>

Assignment: Face Recognition

In the notebook cell for testing the function triplet\_loss, the first test uses Tensor for y\_pred[0], y\_pred[1], and y\_pred[2] , but all the other tests use list for these y\_pred’s. My implementation of triplet\_loss gives an exception because of incompatible types.

Any advice is greatly appreciated.

Vu

---

<div class="post-metadata">

**Author:** ![vuqpham](https://avatars.discourse-cdn.com/v4/letter/v/b77776/32.png) [@vuqpham](https://community.deeplearning.ai/u/vuqpham)\
**Post date:** [April 26, 2021, 4:26pm UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/2 "2021-04-26T16:26:26Z")

</div>

My temporary solution to pass the triplet\_loss cell tests is to check the types of anchor,positive, and negative. If they are not Tensor, then convert them to Tensor, reshape them to have two dimensions then continue to the calculation of equation (3) using Tensor. This solution passes both the cell tests and the grader.

Do the tests really want students to do the above conversion if the inputs are not of the type Tensor ? Or do I misunderstand something ?

Thanks,  
Vu

---

<div class="post-metadata">

**Author:** ![reinoudbosch](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@reinoudbosch](https://community.deeplearning.ai/u/reinoudbosch)\
**Post date:** [April 26, 2021, 4:30pm UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/3 "2021-04-26T16:30:17Z")

</div>

Hi Vu,

Using tensorflow methods with every calculation in triplet\_loss() may resolve this.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![vuqpham](https://avatars.discourse-cdn.com/v4/letter/v/b77776/32.png) [@vuqpham](https://community.deeplearning.ai/u/vuqpham)\
**Post date:** [April 26, 2021, 9:07pm UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/4 "2021-04-26T21:07:11Z")

</div>

Hi Reinoud,

Thanks for your reply.

I copied the cell test code and the triplet\_loss function to ipython to run each command manually, and found that tf.reduce\_sum complained when y\_pred = ([1., 1.], [1., 1.], [1., 1.,]), which is from the 2nd test. In this case, the data has only one dimension, and tf.reduce\_sum is called with axis=1 when calculating pos\_dist and neg\_dist.

The doc of the function does say the anchor, positive, and negative have two dimensions, but the test data has only one dimension. I am sure I miss something, but not sure what it is.

Thanks,  
Vu

---

<div class="post-metadata">

**Author:** ![reinoudbosch](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@reinoudbosch](https://community.deeplearning.ai/u/reinoudbosch)\
**Post date:** [April 27, 2021, 12:03am UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/5 "2021-04-27T00:03:30Z")

</div>

Hi Vu,

A way to think about it is this: Which axis value could you use that would not create this problem?

The provided hints may be helpful.

Good luck!

---

<div class="post-metadata">

**Author:** ![vuqpham](https://avatars.discourse-cdn.com/v4/letter/v/b77776/32.png) [@vuqpham](https://community.deeplearning.ai/u/vuqpham)\
**Post date:** [April 27, 2021, 12:26am UTC](https://community.deeplearning.ai/t/the-function-triplet-loss/1801/6 "2021-04-27T00:26:12Z")

</div>

Thanks a lot Renoud. " Which axis value could you use that would not create this problem?" is what I need 👍. The moment I read it I know what the problem is.

It works and the function looks clean.

Thanks,  
Vu
