Triplet Loss Foundations
Triplet loss trains an embedding space. Instead of predicting one fixed class directly, a model maps each input to a vector whose distances carry meaning. Similar inputs should land close together, while dissimilar inputs should land farther apart.
This objective is useful for face recognition, product retrieval, signature matching, speaker verification, image search, and other tasks where new identities can appear after training.
Classification Versus Metric Learning
A classifier learns boundaries between the classes seen during training. Metric learning learns a distance rule that can also compare unseen examples.
| Training target | Model output | Typical use |
|---|---|---|
| Cross-entropy classification | Probability over a fixed class list | Choosing among known labels |
| Triplet loss | Embedding vector $f_\theta(x)$ | Retrieval, matching, clustering, and verification |
The embedding dimension is chosen in advance. The network parameters $\theta$ learn where each input belongs inside that space.
Anchor, Positive, and Negative
One training triplet contains three inputs:
- Anchor $a$: the reference input.
- Positive $p$: an input that should match the anchor.
- Negative $n$: an input that should not match the anchor.
The network produces embeddings $z_a=f_\theta(a)$, $z_p=f_\theta(p)$, and $z_n=f_\theta(n)$. With distance function $d$, the desired ordering is
$$d(z_a,z_p)+\alpha\le d(z_a,z_n),$$where margin $\alpha>0$ asks the negative to be farther away by at least a fixed gap.
The Hinge Objective
The standard triplet loss is
$$L(a,p,n)=\left[d(z_a,z_p)-d(z_a,z_n)+\alpha\right]_+,$$with $[u]_+=\max(0,u)$. Two cases follow:
- If $d(z_a,z_n)\ge d(z_a,z_p)+\alpha$, the ordering already has enough margin and loss is zero.
- If the negative lies inside the required margin, loss is positive and the triplet contributes a gradient.
▶ Hard, Semi-Hard, and Easy Negatives
The anchor and positive stay fixed. Each step moves the negative and recomputes squared distances with margin $\alpha=0.5$.
Worked Distance Example
With squared Euclidean distance, $z_a=(0,0)$, $z_p=(1,0)$, and $\alpha=0.5$, the positive distance is
$$d_{ap}=\lVert z_a-z_p\rVert_2^2=1.$$| Negative | $d_{an}$ | Loss $[1-d_{an}+0.5]_+$ | Type |
|---|---|---|---|
| $(0.6,0.6)$ | $0.72$ | $0.78$ | Hard: closer than the positive |
| $(1.1,0.2)$ | $1.25$ | $0.25$ | Semi-hard: farther than the positive but inside the margin |
| $(1.5,0.4)$ | $2.41$ | $0$ | Easy: safely outside the margin |
Distance Choice
Triplet loss needs a distance definition. Common choices are:
- Squared Euclidean: $d(u,v)=\lVert u-v\rVert_2^2$. It has simple gradients and is used throughout this series.
- Euclidean: $d(u,v)=\lVert u-v\rVert_2$. Its gradient uses a unit direction and needs care when two embeddings coincide.
- Cosine distance: $d(u,v)=1-\frac{u^\top v}{\lVert u\rVert\lVert v\rVert}$. It compares direction rather than raw norm.
The margin has units determined by the distance. A margin of $0.2$ under cosine distance does not mean the same geometry as $0.2$ under squared Euclidean distance.
Normalized Embeddings
L2 normalization constrains each embedding to the unit sphere:
$$\bar z=\frac{z}{\lVert z\rVert_2}.$$For unit vectors, squared Euclidean distance and cosine similarity are directly related:
$$\lVert \bar u-\bar v\rVert_2^2=2-2\bar u^\top\bar v.$$The maximum squared distance is then $4$, so the margin must respect that bounded geometry. Normalization prevents the network from satisfying a margin merely by increasing every vector norm, but it also removes norm as a possible confidence signal.
Active Triplets and Learning Signal
Only positive-loss triplets are active. If almost every triplet is easy, average loss approaches zero and training receives little signal. If almost every triplet is hard, the model may be undertrained, the margin may be too large, or the labels may contain conflicts.
The active-triplet fraction is therefore an important diagnostic:
$$\text{active fraction}=\frac{\#\{(a,p,n):L(a,p,n)>0\}}{\#\text{ sampled triplets}}.$$Mining and batch construction control which triplets reach this calculation. They are not small implementation details; they define much of the effective training objective.
Relation to Other Metric-Learning Losses
| Loss | Comparison unit | Main difference |
|---|---|---|
| Contrastive loss | One labeled pair | Pulls matching pairs together and pushes nonmatching pairs beyond a margin |
| Triplet loss | Anchor, positive, negative | Enforces a relative ordering around the anchor |
| N-pair or InfoNCE-style loss | One positive against many negatives | Uses several comparisons in one normalized objective |
| Proxy-based loss | Example against learned class proxies | Avoids explicit example-to-example mining for every comparison |
Series Roadmap
Part 2: Geometry and Gradients
Exact active-triplet gradients, margin geometry, normalized spaces, and a stepwise embedding update.
Part 3: Mining and Batches
Easy, hard, and semi-hard mining, structured batches, pairwise distance matrices, and vectorized masks.
Part 4: Training and Evaluation
Retrieval metrics, collapse, false negatives, leakage, diagnostics, and production behavior.