Three inputs at once
The triplet loss trains an embedding using three items per step:
- An anchor.
- A positive that should be close to the anchor.
- A negative that should be far from the anchor.
It wants the anchor to be closer to the positive than to the negative by at least a margin.
The margin matters
Without a margin the model could satisfy the loss by collapsing all vectors together. The margin forces a real gap: the negative must be farther than the positive plus the margin before the loss reaches zero. This produces well separated clusters.
Triplet mining
Most random triplets are already easy and give no gradient. Performance depends on mining informative triplets:
- Hard negatives are negatives closer than the positive.
- Semi hard negatives are farther than the positive but still inside the margin.
Choosing semi hard triplets is a popular stable strategy made famous by FaceNet.
Key idea
Triplet loss pushes the anchor closer to its positive than to its negative by a fixed margin, and mining hard or semi hard triplets is what gives it a strong training signal.