Triplet Loss Anchor Training
Triplet loss trains on triples of anchor, match and stranger, demanding the match beat the stranger by a margin every single time.
Why Does This Exist?
Contrastive loss judges pairs in isolation against one global margin, which struggles when some identities naturally vary more than others: a strict margin that suits passport photos crushes travel selfies of the same person. A relative rule works better: whatever the scale, the match must beat the stranger. Triplet loss is that relative rule, and it made FaceNet practical. Each training unit carries its own context, an anchor with both a friend and a stranger, so the gradient always knows what closer currently means.
This page covers the margin inequality, the easy to hard split, and mining. Pair losses live in contrastive loss, the embedding-space setup lives in metric learning, and the face pipeline that consumes the embeddings lives in face detection and recognition.
Think of It Like This
A judge at a lookalike contest
A judge holds the anchor photo, stands one contestant claiming to be the twin on the left and a stranger on the right, and demands the twin stand clearly closer. The judge never measures absolute distance to the stage: only the gap between the two contestants matters, and ties go to retraining. Triplets judge embeddings the same way.
The analogy stops at the margin: the judge's clearly is a number added to the inequality and enforced by gradient descent, usually on squared distances.
How It Actually Works
The margin inequality
For anchor , positive (same identity) and negative (a stranger), with the squared Euclidean distance on normalized embeddings and the margin, the loss is . The inequality demands : the stranger must trail the match by at least the margin, not merely lose. The gradient pulls toward and pushes from only while the inequality is violated; satisfied triplets contribute exactly zero.
Easy, semi-hard, hard
Easy triplets already trail by more than the margin and cost zero. Semi-hard triplets sit inside the margin but still order correctly () and teach steadily. Hard triplets currently order wrong () and teach fastest but noisily, since the hardest cases in a dirty dataset are mislabelled pairs. Online mining assembles each batch from the semi-hard and hard cases, because batches of easy triplets burn compute for zero gradient.
Worked triplet numbers
Use squared distances with margin . A semi-hard case has and : , so , a live gradient. An easy case with and gives , so and nothing is learned. A hard case with and gives , the strongest pull of the three. The diagram plots exactly these three: only the two violations move the anchor.
Code
The point of this snippet is the hinge: the same three number pairs from the worked example replayed as code, so the 0.10, 0.0, and 0.40 can be checked.
def triplet_loss(d_ap: float, d_an: float, margin: float = 0.2) -> float: return max(0.0, d_ap - d_an + margin)
print(round(triplet_loss(0.30, 0.40), 2))# -> 0.1print(round(triplet_loss(0.05, 0.40), 2))# -> 0.0print(round(triplet_loss(0.50, 0.30), 2))# -> 0.4The semi-hard pair pays , the easy pair pays , and the hard pair pays . Mining keeps the first and third kinds and discards the second before the backward pass.
Watch Out For
Hard-only mining that chases label noise
The hardest triplets in a noisy dataset are mislabelled pairs, and training on them exclusively pulls the space toward the noise. The symptom is oscillating loss with degrading validation retrieval. Fix it with semi-hard mining: train on negatives inside the margin but still farther than the positive, and clean the noisiest labels first.
Forgetting to normalize before measuring
The margin assumes distances share one scale, which holds only for unit-length embeddings. The symptom is a loss stuck near the margin value with retrieval that never improves, because raw dot products drift across batches. Fix it by L2-normalizing every embedding before computing , so means the same gap everywhere.
The Quick Version
- Triplet loss enforces per triplet, a relative order rather than an absolute distance.
- Distances and with margin pay loss ; distances and pay .
- Easy triplets cost zero while semi-hard and hard triplets supply the gradient.
- Online mining inside each batch is mandatory because random triplets are trivial.
- Hard-only mining overfits label noise, so prefer semi-hard sampling on dirty data.