Cosine Embedding and Triplet Losses
Some models aren't trained to predict a label at all — they're trained to arrange examples in space, pulling similar ones together and pushing dissimilar ones apart, using pairs (cosine embedding loss) or anchor-positive-negative triples (triplet loss).
Prerequisites: Loss Functions for Regression
Most losses compare a prediction to a single known target value. But some problems have no fixed target at all — finding similar news articles, matching a trade description to a taxonomy, clustering related market events — and instead just need a notion of "these two things are alike" and "these two are not." Cosine embedding loss and triplet loss both train a network to place its outputs (embeddings — vectors in a learned space) so that similar inputs land near each other and dissimilar ones land far apart, without ever specifying a numeric target for any individual output.
The analogy: arranging desks by who works together
Imagine rearranging an open office so colleagues who collaborate daily sit close together and colleagues who never interact sit far apart — with no map giving "correct" coordinates for any one desk, only a list of who should be near whom. Cosine embedding loss looks at pairs ("these two should be close" or "far"), while triplet loss looks at triples ("closer to a teammate than to a stranger, by at least some margin").
The formulas
Cosine embedding loss takes a pair of embeddings with a label (similar or dissimilar), and a margin :
In words: cosine similarity measures how aligned two vectors' directions are, from (opposite) to (identical direction), ignoring their length. If the pair is meant to be similar, the loss is minus that similarity — pushed to zero only when the vectors point in exactly the same direction. If the pair is meant to be dissimilar, there's no penalty at all once similarity has already dropped below the margin — the loss stops caring once "far enough apart" is achieved.
Triplet loss takes an anchor , a positive example (same class as the anchor), and a negative example (different class), with distance function and margin :
In words: the anchor's distance to its positive should be smaller than its distance to its negative, by at least the margin ; the loss is zero once that's already true by a comfortable margin, and grows linearly the more that ordering is violated.
Worked example 1: cosine embedding loss on two pairs
Pair A, labeled similar (): embeddings with . Loss .
Pair B, labeled dissimilar (), margin : . Loss — still similar enough (0.5) to exceed the margin, so there's a real penalty pushing them further apart. If instead this pair's similarity were already (below the margin), loss would be — no further push needed, they're already far enough apart.
Worked example 2: triplet loss with real distances
Anchor-positive squared distance , anchor-negative squared distance , margin . Loss — the positive is already closer than the negative by more than the margin, so this triplet contributes nothing to training. Now suppose instead (a "hard" negative, nearly as close as the positive): loss — a real, nonzero gradient. This is why "hard negative mining" — deliberately choosing negatives that are currently confusingly close — is standard practice: easy triplets like the first contribute zero gradient and waste a training step.
Both losses train embeddings by relative structure rather than a fixed target: cosine embedding loss pulls labeled-similar pairs toward the same direction and pushes labeled-dissimilar pairs apart past a margin, while triplet loss enforces that an anchor sits closer to a same-class positive than to a different-class negative by at least a margin, with zero gradient once that's already satisfied.
What this means in practice
These losses underlie any "find similar X" system — semantic search over research notes, matching historical market regimes, deduplicating news feeds — where embeddings are later compared with cosine similarity or nearest-neighbor search at inference time, separate from whatever labels were used during training. Triplet loss is sensitive to how triplets are sampled: random triplets are mostly "easy" (zero gradient) and training stalls unless hard or semi-hard negatives are deliberately mined.
The common confusion is thinking a small loss value means the embedding space is well-organized overall. Both losses are computed per-pair or per-triplet and say nothing about the global geometry — a network can drive loss to near zero on every sampled triplet while still leaving some far-apart classes tangled together, simply because no triplet involving that particular pair was ever sampled during training. The sampling strategy is as much a part of "the loss" in practice as the formula itself.
Related concepts
Practice in interviews
Further reading
- Schroff, Kalenichenko & Philbin, FaceNet: A Unified Embedding for Face Recognition and Clustering (2015)