Definition
Triplet loss is a loss on an anchor , a positive of the same class and a negative of a different class: , where is a distance between embeddings and a margin. It is zero once the negative is at least farther from the anchor than the positive is.
Contrastive loss asks every similar pair to be near 0 and every dissimilar pair beyond a fixed . That is a strong demand: some identities are naturally spread out (many poses, lighting), others are tight. The triplet loss only asks for an ordering: for each anchor, its own class should come before other classes, with a gap. That is exactly what nearest-neighbour retrieval and verification need, and it leaves the network free to choose the absolute scale of each class.
Relative-comparison constraints appear in metric learning before deep nets: Schultz and Joachims (2003) learned a metric from "A is closer to B than to C" statements, and the large-margin nearest neighbour method of Weinberger, Blitzer and Saul (2005) used the same hinge on triplets for a Mahalanobis metric. The deep version became famous with FaceNet (Schroff, Kalenichenko and Philbin, CVPR 2015). FaceNet mapped faces to 128-dimensional L2-normalised embeddings with squared distances and margin , reached 99.63% accuracy on LFW, and showed that how triplets are chosen matters as much as the loss itself.
Formula and gradient
- embeddings from one shared network; has the anchor's label, does not
- the margin ( in FaceNet): the gap required between the negative and positive distances
- Euclidean distance, or its square; the margin's scale depends on which
- gradient descent moves toward along a unit vector
- moves away from along a unit vector
- moves the anchor toward and away from at once
With plain distances, every active triplet applies unit-strength forces, regardless of how badly it is violated. With FaceNet's squared distances the forces are and , so they scale with distance. In both cases a satisfied triplet, , gives exactly zero gradient.
At a glance
- Input
- triplets (anchor, positive, negative), usually formed online inside a batch
- Constrains
- relative distances , not absolute ones
- Range
- ; a collapsed embedding ( const) has loss exactly
- Hard negative
- : the negative is closer than the positive
- Semi-hard
- : correctly ordered but inside the margin
- Easy
- : zero loss, zero gradient
- PyTorch
nn.TripletMarginLoss(margin=1.0, p=2.0, swap=False), unsquared ;nn.TripletMarginWithDistanceLossfor a custom
Mining: which negatives to use
The number of possible triplets grows cubically with the dataset, and after a little training almost all of them are easy and contribute nothing. Averaging over all of them dilutes the signal (the figure's mean of 0.2697 over 8 triplets, only 5 nonzero). So triplet training is mostly about mining: picking triplets that still violate the margin. Doing it online, within each mini-batch using the current embeddings, is the standard approach, because offline mining over the whole set goes stale as the network changes.
Common mining strategies (within a batch of P identities × K images)
- 1Batch-all: every valid triplet in the batch, averaged over the ones with only, so easy triplets do not dilute the mean.
- 2Hardest (batch-hard): for each anchor, the farthest positive and the closest negative (Hermans, Beyer and Leibe, 2017). Strong signal, but sensitive to label noise and outliers.
- 3Semi-hard (FaceNet): for each anchor–positive pair, a negative with , i.e. one that is correctly ordered but still inside the margin. FaceNet used batches of about 1,800 faces with around 40 per identity so that such negatives exist.
- 4Distance-weighted or random among violators: sample negatives in proportion to how informative they are rather than always taking the extreme one.
Worked example: three negatives, two distances
Anchor , positive so . Negatives , , at distances , and .
| negative | ‖a−n‖ | type (plain, m=0.5) | L plain | ‖a−n‖² | L squared (α=0.2) |
|---|---|---|---|---|---|
| 0.5 | hard | 0.25 | |||
| 1.2 | semi-hard | 1.44 | |||
| 2.0 | easy | 0 | 4.00 | 0 |
Gradient of the semi-hard triplet (a, p, n₂), plain distances
- 1Unit vectors: and .
- 2, so descent moves by : straight toward .
- 3, so descent moves along : straight away from .
- 4: the anchor moves toward and away from .
- 5Averaged with reduction='mean' over all three triplets the loss is . Note how is semi-hard under one distance and easy under the other: a margin is only meaningful for a specific and embedding scale.
1import torch, torch.nn as nn, torch.nn.functional as F23a = torch.tensor([[0.0, 0.0]] * 3, requires_grad=True)4p = torch.tensor([[0.6, 0.8]] * 3)5n = torch.tensor([[0.5, 0.0], [1.2, 0.0], [0.0, -2.0]])67loss_fn = nn.TripletMarginLoss(margin=0.5, p=2.0) # max(0, ||a-p|| - ||a-n|| + m), mean over batch8print(loss_fn(a, p, n)) # ~0.4333 (1.0 + 0.3 + 0) / 3; eps=1e-6 shifts it slightly910# Batch-hard mining (Hermans et al., 2017): batch of P identities x K images11def batch_hard_triplet(emb, labels, margin=0.2):12 emb = F.normalize(emb, dim=1) # put embeddings on the unit sphere13 d = torch.cdist(emb, emb) # (B, B) pairwise distances14 same = labels[:, None] == labels[None, :]15 eye = torch.eye(len(labels), dtype=torch.bool)16 hardest_pos = d.masked_fill(~same | eye, 0).amax(dim=1) # farthest positive17 hardest_neg = d.masked_fill(same, float("inf")).amin(dim=1) # closest negative18 return F.relu(hardest_pos - hardest_neg + margin).mean()1920emb = torch.randn(32, 128, requires_grad=True) # e.g. 8 identities x 4 images21labels = torch.arange(8).repeat_interleave(4)22batch_hard_triplet(emb, labels).backward()Use when
- +Only relative order matters: classes may have different spreads, which suits faces and products
- +Directly optimises what nearest-neighbour retrieval and verification measure
- +Works with open sets of identities; new classes need no retraining
- +Simple loss with clear geometry: unit-strength pull on the positive, push on the negative
Avoid when
- −Most triplets are easy and give zero gradient; needs online mining and carefully built batches (P × K)
- −Hardest-negative mining is unstable and can collapse the embedding
- −Margin depends on the distance (plain vs squared) and the embedding scale
- −One negative per term: slower to converge than many-negative losses like InfoNCE
Check yourself
With squared distances and α = 0.2, d(a,p)² = 1.0. Which negative is semi-hard?
Check yourself
Every embedding collapses to the same point. What is the triplet loss, and why is this a trap?
Remember
- ✓: the negative must be farther than the positive (FaceNet, 2015).
- ✓It constrains only relative distances, which is what retrieval and verification need.
- ✓Hard: d(a,n) < d(a,p). Semi-hard: inside the margin but correctly ordered. Easy: zero loss.
- ✓Mining decides success: online, within P × K batches; semi-hard or batch-hard; avoid averaging in all the zeros.
- ✓Normalise embeddings and remember that
nn.TripletMarginLossuses unsquared distances while FaceNet used squared ones.