Triplet lossMedium

Triplet loss

Background

Triplet loss trains embeddings so that an anchor is closer to a "positive" (same class) than to a "negative" (different class) by at least a margin. It is the workhorse of metric learning — face recognition, image retrieval, and contrastive representation learning all use it to shape an embedding space where distance means dissimilarity.

Problem statement

Implement triplet_loss(anchor, positive, negative, margin=1.0) using squared-Euclidean distances:

L=1Nimax ⁣(0,  aipi2aini2+margin)\mathcal{L} = \frac{1}{N}\sum_i \max\!\Big(0,\; \lVert a_i - p_i\rVert^2 - \lVert a_i - n_i\rVert^2 + \text{margin}\Big)

Input

  • anchor, positive, negativenp.ndarray (N, D): the three embedding sets.
  • marginfloat: the required separation.

Output

Returns a float: the mean triplet loss over the batch (0\ge 0).

Examples

Example 1

Input:  anchor = [[0, 0]], positive = [[2, 0]], negative = [[1, 0]], margin = 1.0
Output: 4.0

Explanation: dap=4d_{ap} = 4 and dan=1d_{an} = 1; the positive is farther than the negative, so the loss is max(0,41+1)=4\max(0, 4 - 1 + 1) = 4.

Constraints

  • Use the squared Euclidean distance per embedding pair.
  • The hinge max(0,)\max(0, \cdot) zeroes out triplets that already satisfy the margin.
  • Average over the batch; the result is 0\ge 0.

Notes

  • A loss of 0 means every triplet already respects the margin (anchor closer to its positive than its negative by at least margin) — the "easy" triplets that contribute no gradient.
  • Mining hard / semi-hard triplets (those with positive loss) is what makes training efficient, since most random triplets are already easy.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example: 4.0
  • Easy triplet (positive near, negative far) -> 0
  • Equal distances -> loss equals the margin
  • Averages over the batch