Skip to content

Contrastive Learning

Learning embeddings by pulling matching pairs together and pushing non-matching pairs apart, without needing class labels.

Also SimCLR · InfoNCE · contrastive loss · positive and negative pairs

Fig. / How it works
DOGCARTREECATIMAGECAPTIONMATCHING PAIRS PULL TOGETHER, OTHERS PUSH APART
Domain 10
Multimodal AI
Role · kind
TrainingTechnique
Level
Advanced

A matching game: here is a photo and five captions, find the right one. Playing millions of rounds teaches the model what goes with what.

Where it fits

+All relationships as a list

Kinds and parts

Uses / built on

See it on the full map

Try it

Matching pictures and words with CLIP
Loading interactive widget…
TakeawayCLIP puts images and captions in one embedding space, so it can label images and search them with any words, including mistaking written text for the thing it names.

How it works

Contrastive learning is a form of self-supervised learning. Instead of predicting a label, the model plays a matching game:

  1. Take a batch of pairs that belong together, for example images and their captions.
  2. Encode every image and every caption into an embedding.
  3. Train so that each image’s embedding is most similar to its own caption’s and less similar to all the other captions in the batch.

The other items in the batch act as free “wrong answers” (negatives), so no human labeling is needed beyond the natural pairing. For images alone, the pairs can be two random edits (crops, color changes) of the same photo: the model learns that both are “the same thing”.

The result is an embedding space where closeness means similarity in meaning, which is exactly what search, recommendation and zero-shot classification need. CLIP applied this to 400 million image-text pairs.

The math

For a batch of NN pairs with normalized embeddings uiu_i (images) and viv_i (texts) and temperature τ\tau, the image-to-text InfoNCE loss is

L=−1N∑i=1Nlog⁡exp⁡(ui⋅vi/τ)∑j=1Nexp⁡(ui⋅vj/τ)\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} \log \frac{\exp(u_i \cdot v_i / \tau)}{\sum_{j=1}^{N} \exp(u_i \cdot v_j / \tau)}

CLIP averages it with the symmetric text-to-image loss.

Everyday use cases

  • Case 01

    Image search by text

    Embeddings trained on image-caption pairs let you search photos with any phrase.

  • Case 02

    Pretraining with little labeled data

    Learn good image features from unlabeled photos, then fine-tune on a few labeled ones.

Strengths, weaknesses and when to use it

Strengths

  • Uses unlabeled or loosely paired data at huge scale.
  • Produces general-purpose embeddings.

Weaknesses

  • Needs many negatives (large batches) to work well.
  • Learns whatever correlations the pairs contain, including biases.

When to use itWhen you have lots of paired or unlabeled data and want reusable embeddings.

Compare with

Check yourself

  1. Q01

    In CLIP-style training, what is a "positive pair"?

    Show answer

    B: An image and its own caption

Go deeper

Learn first

Next topics