# Triplet loss

> max(0, d(a,p) − d(a,n) + m): the anchor must be closer to its positive than to any negative by a margin. Only relative distances matter, most triplets are already satisfied, so which negatives you mine (hard, semi-hard) decides whether it learns.

Source: https://www.mlfoundry.in/learn/triplet-loss  
Section: Deep Learning › Loss Functions  
Level: core · 12 min read · updated 2026-10-11

**Triplet loss:** is a loss on an **anchor** $a$, a **positive** $p$ of the same class and a **negative** $n$ of a different class: $L=\max\big(0,\ d(a,p)-d(a,n)+m\big)$, where $d$ is a distance between embeddings and $m>0$ a margin. It is zero once the negative is at least $m$ 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 $m$. 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 $\alpha=0.2$, reached 99.63% accuracy on LFW, and showed that how triplets are chosen matters as much as the loss itself.

## Formula and gradient

$$
L(a,p,n)=\max\big(0,\ d(a,p)-d(a,n)+m\big),\qquad d(u,v)=\lVert u-v\rVert_2\ \ \text{(PyTorch)}\quad\text{or}\quad \lVert u-v\rVert_2^2\ \ \text{(FaceNet)}
$$

- $a,p,n$: embeddings $f(x_a),f(x_p),f(x_n)$ from one shared network; $p$ has the anchor's label, $n$ does not
- $m$: the margin ($\alpha$ in FaceNet): the gap required between the negative and positive distances
- $d$: Euclidean distance, or its square; the margin's scale depends on which

$$
\text{if }L>0:\quad \frac{\partial L}{\partial p}=-\frac{a-p}{\lVert a-p\rVert},\qquad \frac{\partial L}{\partial n}=\frac{a-n}{\lVert a-n\rVert},\qquad \frac{\partial L}{\partial a}=\frac{a-p}{\lVert a-p\rVert}-\frac{a-n}{\lVert a-n\rVert}
$$

- $-\partial L/\partial p$: gradient descent moves $p$ toward $a$ along a unit vector
- $-\partial L/\partial n$: moves $n$ away from $a$ along a unit vector
- $-\partial L/\partial a$: moves the anchor toward $p$ and away from $n$ 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 $2(a-p)$ and $2(a-n)$, so they scale with distance. In both cases a satisfied triplet, $d(a,n)\ge d(a,p)+m$, gives exactly zero gradient.

**At a glance**

- **Input:** triplets (anchor, positive, negative), usually formed online inside a batch
- **Constrains:** relative distances $d(a,p)+m\le d(a,n)$, not absolute ones
- **Range:** $[0,\infty)$; a collapsed embedding ($f\equiv$ const) has loss exactly $m$
- **Hard negative:** $d(a,n)<d(a,p)$: the negative is closer than the positive
- **Semi-hard:** $d(a,p)\le d(a,n)<d(a,p)+m$: correctly ordered but inside the margin
- **Easy:** $d(a,n)\ge d(a,p)+m$: zero loss, zero gradient
- **PyTorch:** `nn.TripletMarginLoss(margin=1.0, p=2.0, swap=False)`, unsquared $d$; `nn.TripletMarginWithDistanceLoss` for a custom $d$

> Figure: Anchor A, positive P and eight negatives. The rose disc has radius d(A,P) (hard negatives inside), the dashed orange circle has radius d(A,P) + m (semi-hard negatives in the band). At the start, with plain distances and m = 0.8, d(A,P) = 0.949: negative 1 is hard (loss 0.942), negatives 2, 3, 4 and 8 are semi-hard, and 5, 6, 7 are easy with zero loss. Averaging all 8 triplets, as reduction='mean' does, gives L = 0.2697 although only 5 triplets are active. The hardest negative alone gives 0.9425; FaceNet's semi-hard rule picks negative 2 (d = 1.082) and gives 0.667. Switch to squared distances: d(A,P) becomes 0.900 and only negatives 1 and 2 are still inside the margin.

## 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)**

1. **Batch-all**: every valid triplet in the batch, averaged over the ones with $L>0$ only, so easy triplets do not dilute the mean.
2. **Hardest (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.
3. **Semi-hard** (FaceNet): for each anchor–positive pair, a negative with $d(a,p)<d(a,n)<d(a,p)+m$, 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.
4. **Distance-weighted or random among violators**: sample negatives in proportion to how informative they are rather than always taking the extreme one.

> **Hardest negatives can collapse training:** FaceNet reports that selecting the hardest negatives from the start can lead to bad local minima, specifically a collapsed model with $f(x)=0$. At collapse every distance is 0, so $L=m$ for every triplet; with squared distances the gradient there is exactly 0, a stationary point (checked in the figure's code: loss 0.2, all gradients 0 for $m=0.2$). Hard negatives early in training are often mislabelled or genuinely ambiguous, and pushing them hardest drags the whole embedding together. Semi-hard mining, warm-up with easier triplets, or a soft-margin softplus $\log(1+e^{d_{ap}-d_{an}})$ avoid this.

## Worked example: three negatives, two distances

Anchor $a=(0,0)$, positive $p=(0.6,0.8)$ so $\lVert a-p\rVert=1.0$. Negatives $n_1=(0.5,0)$, $n_2=(1.2,0)$, $n_3=(0,-2)$ at distances $0.5$, $1.2$ and $2.0$.

Per-triplet loss with plain distances (m = 0.5) and with FaceNet's squared distances (α = 0.2), checked in Node

| negative | ‖a−n‖ | type (plain, m=0.5) | L plain | ‖a−n‖² | L squared (α=0.2) |
| --- | --- | --- | --- | --- | --- |
| $n_1$ | 0.5 | hard | $1.0-0.5+0.5=1.0$ | 0.25 | $1-0.25+0.2=0.95$ |
| $n_2$ | 1.2 | semi-hard | $1.0-1.2+0.5=0.3$ | 1.44 | $\max(0,-0.24)=0$ |
| $n_3$ | 2.0 | easy | 0 | 4.00 | 0 |

**Gradient of the semi-hard triplet (a, p, n₂), plain distances**

1. Unit vectors: $(a-p)/\lVert a-p\rVert=(-0.6,-0.8)$ and $(a-n_2)/\lVert a-n_2\rVert=(-1,0)$.
2. $\partial L/\partial p=(0.6,0.8)$, so descent moves $p$ by $-\eta(0.6,0.8)$: straight toward $a$.
3. $\partial L/\partial n_2=(-1,0)$, so descent moves $n_2$ along $+x$: straight away from $a$.
4. $\partial L/\partial a=(-0.6,-0.8)-(-1,0)=(0.4,-0.8)$: the anchor moves toward $p$ and away from $n_2$.
5. Averaged with reduction='mean' over all three triplets the loss is $(1.0+0.3+0)/3=0.4333$. Note how $n_2$ is semi-hard under one distance and easy under the other: a margin is only meaningful for a specific $d$ and embedding scale.

```python
import torch, torch.nn as nn, torch.nn.functional as F

a = torch.tensor([[0.0, 0.0]] * 3, requires_grad=True)
p = torch.tensor([[0.6, 0.8]] * 3)
n = torch.tensor([[0.5, 0.0], [1.2, 0.0], [0.0, -2.0]])

loss_fn = nn.TripletMarginLoss(margin=0.5, p=2.0)    # max(0, ||a-p|| - ||a-n|| + m), mean over batch
print(loss_fn(a, p, n))                               # ~0.4333 (1.0 + 0.3 + 0) / 3; eps=1e-6 shifts it slightly

# Batch-hard mining (Hermans et al., 2017): batch of P identities x K images
def batch_hard_triplet(emb, labels, margin=0.2):
    emb = F.normalize(emb, dim=1)                     # put embeddings on the unit sphere
    d = torch.cdist(emb, emb)                         # (B, B) pairwise distances
    same = labels[:, None] == labels[None, :]
    eye = torch.eye(len(labels), dtype=torch.bool)
    hardest_pos = d.masked_fill(~same | eye, 0).amax(dim=1)          # farthest positive
    hardest_neg = d.masked_fill(same, float("inf")).amin(dim=1)      # closest negative
    return F.relu(hardest_pos - hardest_neg + margin).mean()

emb = torch.randn(32, 128, requires_grad=True)        # e.g. 8 identities x 4 images
labels = torch.arange(8).repeat_interleave(4)
batch_hard_triplet(emb, labels).backward()
```

nn.TripletMarginLoss, plus batch-hard online mining

> **Why normalise the embedding:** Without a constraint on scale, the network can satisfy any margin by blowing up all distances, or shrink everything toward the collapsed solution. FaceNet constrains $\lVert f(x)\rVert_2=1$, so squared distances lie in $[0,4]$ and $\alpha=0.2$ has a fixed meaning. Most modern implementations L2-normalise before the loss for the same reason.

> **Variants:** **Distance swap** (Balntas et al., 2016; `swap=True` in PyTorch) uses $\min(d(a,n),d(p,n))$ as the negative distance, so the positive's view of the negative also counts. **Soft margin** replaces the hinge with softplus. **N-pair** and **InfoNCE** losses compare one positive against many negatives at once, which is the step from triplets to modern contrastive learning.

**Strengths**

- 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

**Limitations**

- 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

**Question:** With squared distances and α = 0.2, d(a,p)² = 1.0. Which negative is semi-hard?

- A. d(a,n)² = 1.1
- B. d(a,n)² = 0.8
- C. d(a,n)² = 1.3
- D. d(a,n)² = 2.0

**Answer:** A. d(a,n)² = 1.1 — Semi-hard means $d(a,p)^2\le d(a,n)^2<d(a,p)^2+\alpha$, i.e. $1.0\le d^2<1.2$. $0.8$ is hard (closer than the positive), $1.3$ and $2.0$ are easy and give zero loss.

**Question:** Every embedding collapses to the same point. What is the triplet loss, and why is this a trap?

- A. L = m for every triplet, and with squared distances the gradient is zero there
- B. L = 0, so training has converged
- C. L is infinite, so training diverges
- D. L = m but the gradient is large, so it escapes immediately

**Answer:** A. L = m for every triplet, and with squared distances the gradient is zero there — All distances are 0, so $L=\max(0,0-0+m)=m$. With $d=\lVert\cdot\rVert^2$ the gradients $2(a-p)$ and $2(a-n)$ are both zero, so gradient descent cannot leave. This is the collapse FaceNet warns about when the hardest negatives are used too early.

**Key points**

- $L=\max(0,\,d(a,p)-d(a,n)+m)$: the negative must be $m$ 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.TripletMarginLoss` uses unsquared distances while FaceNet used squared ones.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
