# Why does triplet loss allow to learn a ranking whereas contrastive loss only allows to learn similarity?

I am looking at this lecture, which states (link to exact time):

What the triplet loss allows us in contrast to the contrastive loss is that we can learn a ranking. So it's not only about similarity, being closer together or being further apart, but now we want to learn how much closer am I compared to another image.

The contrastive loss

$$L(A, B) = y|f(A) - f(B)| + (1-y)max(0, m-|f(A) - F(B)|)$$

would push similar samples together, and dissimilar samples apart.

The triplet loss

$$L(A, P, N) = max(0, |f(A) - f(P)| - |f(A) - f(N)| + m)$$

would push the positive close to the anchor, and the negative away from the anchor.

I fail to see why the quoted claim is or isn't true in either of these losses. To me, it looks like "same" samples are pushed together, and "different" samples are pushed apart by both.
Furthermore, with the contrastive loss, the distance in the embedding space would be, as I understand it, the ranking- which is claimed to only exist with the triplet loss.

Is there a clearer reference for this, or just a simple answer?

In the first you compare A and B. So if A is closer to B and y= 1 then it's better, that's it.

In the second, you compare A to P and A to N simultaneously.

So if A is closer to P it's good, but it's better if A is more distant to N. So with triplet you have a ranking.

I hope this was clear.

I think the presenter is referring to ranking in terms of the sample triplet. With the contrastive loss, we take a pair $$(A, B)$$ and simply measure how similar $$A$$ is to $$B$$. With triplet loss, we are measuring the similarity between an Anchor image $$A$$ and a positive sample $$P$$ and negative sample $$N$$, so intuitively we are comparing the similarities $$s(A, P)$$ with $$s(A, N)$$. In the lecture, she presents this math.

$$||f(A)-f(P)||^2<||f(A)-f(N)||^2$$

which is how we derive the formula for triplet loss. Triplet loss enforces this condition, i.e. that the distance between the embeddings for $$A$$ and $$P$$ are closer than the distance of the embeddings between $$A$$ and $$N$$. Think of rank as an ordering of the distances $$s(A, P)$$ and $$s(A, N)$$. We want to achieve the rank condition expressed in the above equation. Therefore, the triplet loss measurement will be in terms of rank because it is dependent on $$s(A, P)$$ compared to $$s(A, N)$$. To summarize, contrastive loss is learning similarity by pushing apart dissimilar embeddings and pushing towards each other similar embeddings based purely on the distance between the embeddings. Triplet loss is pushing together similar embeddings and pushing apart dissimilar embeddings concurrently by enforcing a ranking condition where $$s(A, P) < s(A, N)$$.

• Please don't upload math equations and text as images, it isn't searchable. There are tools available to convert PDFs to text and MathJax. Thanks.
– Rob
Commented Oct 19, 2022 at 21:27