Standard 9 stops to get here

Metric Learning

Learning a distance function, usually via an embedding, so that items that should count as similar end up close together and dissimilar items end up far apart.

Your route here

9 stops · basics first
  1. Dataset ✓ understood

    A collection of data examples used for training, validating, or testing machine learning models.

  2. Training ✓ understood

    The process of fitting a model to data by repeatedly measuring how wrong its outputs are and adjusting its parameters to reduce that error.

  3. Loss Function ✓ understood

    A function that scores how wrong a model's prediction is as a single number, which training then works to make as small as possible.

  4. Feature ✓ understood

    A single measurable property of an example, such as a house's floor area or how many links an email contains, used as an input to a model.

  5. Machine Learning ✓ understood

    Building systems that learn patterns from data instead of following hand-written rules, getting better at a task as they see more examples.

  6. Neural Network ✓ understood

    A model built from layers of simple units, each taking a weighted sum of its inputs and applying a nonlinearity; training adjusts the weights so the whole stack maps inputs to useful outputs.

  7. Deep Learning ✓ understood

    A subset of machine learning that uses neural networks with multiple layers (deep neural networks) to learn hierarchical representations of data.

  8. Representation Learning ✓ understood

    Letting a model learn its own internal description of the data, such as embeddings or layered visual features, instead of relying on features designed by hand.

  9. Embedding ✓ understood

    A list of numbers (a vector) that represents a word, sentence, image or other item, learned so that similar items end up close together.

  10. Metric Learning · you are here ✓ understood

Picture it

Fixed distance

  • Plain Euclidean distance on raw inputs
  • Every feature counts equally
  • Similar-looking can mean unrelated

Learned distance

  • Model maps inputs to an embedding
  • Same-class items pulled close
  • Different-class items pushed apart
Notice that the model is trained on the distances themselves, so closeness in the learned space means similar for the task you care about.

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.

Where it sits

Metric Learning

Leads to

Nothing yet: a destination in its own right.

Explore nearby

Sources

  1. Weinberger and Saul, "Distance Metric Learning for Large Margin Nearest Neighbor Classification" . Journal of Machine Learning Research, 2009
  2. Schroff, Kalenichenko and Philbin, "FaceNet: A Unified Embedding for Face Recognition and Clustering" . CVPR 2015