Computing Library › Machine Learning
Machine Learning

Metric Learning

Metric learning trains an embedding where distance reflects semantic similarity, enabling retrieval and verification.

Learning what close means

Metric learning builds a representation in which the distance between two points reflects how similar they are in a task-relevant sense: same-identity faces are near, different faces are far. Instead of predicting a fixed set of classes, it learns a geometry, which lets the model handle new classes at test time by comparison, essential for face verification, image retrieval, and one-shot recognition.

Contrastive and triplet losses

Kronos motion — lego machine

The contrastive loss pulls together pairs labeled similar and pushes apart pairs labeled dissimilar beyond a margin. The triplet loss uses an anchor, a positive (same class), and a negative (different class), and requires the anchor-positive distance to be smaller than the anchor-negative distance by at least a margin. These losses optimize the embedding directly rather than a classification boundary.

Mining hard examples

Most randomly chosen triplets are already correct and give no gradient, so training stalls without hard-negative mining: selecting negatives that are close to the anchor and thus violate the margin. Semi-hard mining, choosing negatives farther than the positive but still within the margin, stabilizes training by avoiding the noisiest hardest cases.

Relation to contrastive learning

Metric learning and contrastive learning share the same geometric objective; the difference is the source of pairs. Metric learning uses ground-truth labels to define similar and dissimilar, while self-supervised contrastive learning constructs pairs from augmentations. In both, the product is a reusable embedding evaluated by retrieval accuracy such as recall at k.