Course 4, Week 4, Assignment 4 error

I am getting the following assertion error while calculating the triplet loss. But my function implementation is correct ! Here is my calculation :

For A = \begin{bmatrix} 1 & 0\\ 1 & 0\end{bmatrix}, P = \begin{bmatrix} 1 & 0\\ 1 & 0\end{bmatrix}, and N = \begin{bmatrix} 0 & 1\\ 0 & 1\end{bmatrix} we have {\cal L}(A,P) = 0 and {\cal L}(A,N) = \begin{bmatrix} 1 & -1\\ 1 & -1\end{bmatrix}_F^2 = 4. Thus the overall loss {\cal L}(A,N,P) = [0 - 4 + \alpha]_+ = 0 for \alpha = 3 . But the assert statement in the test code wants a value of 2. !!

Hello @ultimateabhi

I hope this will give you some ideas on what might have gone wrong.

image

Cheers,
Raymond

Hi @rmwkwok ,

Even in the formula you have written the L2 norm has to be taken before adding \alpha. So the answer should be ||\begin{bmatrix}0 \\ 0 \end{bmatrix}||_2^2 - ||\begin{bmatrix}2 \\ 2 \end{bmatrix}||_2^2 + 3 = 5

Also I don’t get how you construct the rank 1, length 2 vectors \begin{bmatrix} 0 \\ 0 \end{bmatrix} and \begin{bmatrix} 2 \\ 2 \end{bmatrix} from the 2x2 dimensional matrices f(A)-f(P) and f(A)-f(N).

Hello @ultimateabhi

The cost is a summation over m samples, right? Therefore, the norm is per sample. How many samples do you think are represented by these matrices?

image

The answer is 2. For each sample, you get one triplet loss value. For 2 samples, you get two triplet loss values.

Raymond

1 Like