← All Posts
Machine Learning · Loss Functions · Triplet Loss · Part 1 of 4

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 targetModel outputTypical use
Cross-entropy classificationProbability over a fixed class listChoosing among known labels
Triplet lossEmbedding 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:

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:

The loss is relative. Triplet loss does not require one absolute target distance. It compares two distances that share the same anchor.

▶ 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:

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.

LossComparison unitMain difference
Contrastive lossOne labeled pairPulls matching pairs together and pushes nonmatching pairs beyond a margin
Triplet lossAnchor, positive, negativeEnforces a relative ordering around the anchor
N-pair or InfoNCE-style lossOne positive against many negativesUses several comparisons in one normalized objective
Proxy-based lossExample against learned class proxiesAvoids 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.