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
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
Is a kind of / part of
Kinds and parts
Siblings
Uses / built on
Try it
How it works
Contrastive learning is a form of self-supervised learning. Instead of predicting a label, the model plays a matching game:
- Take a batch of pairs that belong together, for example images and their captions.
- Encode every image and every caption into an embedding.
- 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 pairs with normalized embeddings (images) and (texts) and temperature , the image-to-text InfoNCE loss is
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
- Q01
In CLIP-style training, what is a "positive pair"?
Show answer
B: An image and its own caption