Metric Learning for Embeddings
Metric learning teaches networks to lay out a space where similarity means closeness, so new classes slot in without retraining any classifier.
Why Does This Exist?
Classifiers memorize a fixed guest list: add a class, retrain. But faces, products and pedestrians arrive continuously, and retraining on every arrival is absurd. Metric learning exists to replace the guest list with a seating rule. The network learns a space where closeness means sameness, and enrollment becomes storing one more vector. Face systems like FaceNet, re-identification trackers like DeepSORT, and image retrieval all run on this idea. Pair losses are in contrastive loss; triplets in triplet loss.
Think of It Like This
A library that shelves by meaning
A fixed catalogue assigns every book a number and breaks when new topics appear. A meaning-shelved library instead places similar books adjacently, so a new arrival slides onto the shelf beside its neighbours with no renumbering. Metric learning shelves data by meaning. The loss function is the librarian, pulling related books together and pushing unrelated sections apart, shelf by shelf, batch by batch. The analogy stops at the geometry: shelves are lines, embedding spaces are hyperspheres with hundreds of axes.
How It Actually Works
A backbone network maps each input to a normalized embedding. Training samples pairs or triplets with known same-or-different labels and penalizes violations of the desired geometry: same-class items must lie within a tight radius, different-class items beyond a margin. Mining selects the informative violations per batch, since random pairs are mostly already satisfied. At serving time the classifier head is gone; k-nearest neighbours or thresholded distance decides.
A worked pair
Take normalized toy embeddings where a same-class pair sits at Euclidean distance 0.3. Contrastive loss with no margin on positives gives 0.5 x 0.09 = 0.045, a gentle pull together. A different-class pair at distance 0.6 with margin 1.0 gives 0.5 x (1.0 - 0.6) squared = 0.5 x 0.16 = 0.08, a firm push apart. Both gradients reshape the same space, which is why pair-based training converges to clustered, separated classes.
Watch Out For
Collapsed embeddings that satisfy the loss trivially
Without normalization, the network can shrink all embeddings toward zero (or one point) so every distance looks small and positives seem solved. The symptom is near-zero training loss with garbage retrieval. Fix it by L2-normalizing embeddings onto the hypersphere so only directions, not magnitudes, carry identity.
The Quick Version
- Metric learning optimizes distances between embeddings instead of class logits.
- New classes enroll by storing vectors, with zero retraining.
- Mining hard pairs and triplets does the teaching; random sampling stalls.
- Embeddings live normalized on a hypersphere so distance means direction difference.
- Retrieval, verification and re-identification are all nearest-neighbour lookups in the learned space.