Metric learning trains a model to measure similarity. Instead of predicting a fixed label, it learns a space where distance carries meaning: two photos of the same person, or two sentences that say the same thing, should land close together, and unrelated items should land far apart.
Why not just use ordinary distance on the raw data? Because raw distance rarely matches what “similar” means for a task. Two face photos of different people under the same lighting can be closer, pixel for pixel, than two photos of one person under different lighting. A learned metric can ignore the nuisance variation and focus on what matters.
From linear metrics to deep embeddings
Early methods learned a Mahalanobis distance, which is equivalent to a linear transformation of the input followed by ordinary Euclidean distance. Weinberger and Saul’s large-margin nearest neighbor method learned such a metric to improve k-nearest-neighbor classification.
Deep metric learning replaces the linear map with a neural network that outputs an embedding. FaceNet is the classic example: a CNN maps each face to a compact vector where distance corresponds directly to face similarity, so verification, recognition and clustering reduce to comparing vectors.
How it is trained
The loss function works on pairs or groups rather than single examples. Contrastive loss pulls matching pairs together and pushes non-matching pairs beyond a margin. Triplet loss, used by FaceNet, asks that an anchor sit closer to a positive than to a negative by a margin. Choosing informative hard examples matters a great deal, which is why FaceNet mined its triplets online during training.
Learned embeddings power face recognition, image and product search, and the similarity lookups behind a vector database.