triplet_margin_loss
→Tensortriplet_margin_loss(anchor: Tensor, positive: Tensor, negative: Tensor, margin: float = 1.0, p: float = 2.0, eps: float = 1e-06, swap: bool = False, reduction: str = 'mean')Triplet margin loss for metric learning.
Pulls an "anchor" embedding toward a "positive" example (same
class / matching pair) and pushes it away from a "negative"
example by at least a fixed margin. This is the workhorse
objective for learning embeddings used in face verification
(FaceNet), image retrieval, and contrastive representation
learning.
Optionally applies the "anchor-swap" trick of Balntas et al.
2016 — when d(p, n) < d(a, n) the positive is closer to the
negative than the anchor is, so we swap roles and use the
harder of the two distances, focusing gradient on the more
informative triplet.
Parameters
anchorTensorpositiveTensornegativeTensormarginfloat= 1.01.0). Triplets satisfying the margin already
receive zero loss / zero gradient.pfloat= 2.02.0 —
Euclidean).epsfloat= 1e-061e-6).swapbool= FalseFalse).reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-triplet tensor of shape .
Notes
Per-triplet loss:
where is the pairwise
distance. Easy triplets (already separated by margin)
contribute exactly zero — only "semi-hard" / "hard" triplets
drive the update, which is why batch mining strategies matter
so much in practice.
Examples
>>> import lucid
>>> from lucid.nn.functional import triplet_margin_loss
>>> a = lucid.tensor([[1.0, 0.0]])
>>> p = lucid.tensor([[1.0, 0.1]])
>>> n = lucid.tensor([[0.0, 1.0]])
>>> triplet_margin_loss(a, p, n, margin=1.0)
Tensor(0.6862...)