Contrastive learning: learning by comparing pairs
10 questions on positive and negative pairs, anchors, collapse, instance discrimination, supervised contrastive learning and image-text models.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
What is unusual about the error function of contrastive learning compared with most other machine learning tasks?
- The error for an input is defined only in relation to other inputs, not through a label or target for that input
- It has no minimum
- It doesn't use any data
Sample 2
What measure is typically used to judge how close two representations are, when they are normalised to unit length?
- Their number of pixels
- Their cosine similarity
- Their file size
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
Teaching a model what a cat is usually needs thousands of images labelled «cat». But there is a cheaper way of teaching it what similar means. Show it two different photographs of the same scene, one rotated or recoloured, and ask it to place them close together in its internal description. Show it a photograph of something else, and ask it to place that far away. After millions of such comparisons, with no labels at all, the model has learned a representation in which similar things sit near each other, and that turns out to be an excellent starting point for classification and many other tasks. This approach is called contrastive learning, and it is behind some of the most successful image and image-text models.
This quiz explains the idea through its building blocks. You will see what a positive pair, a negative pair and an anchor are, and what is unusual about the error function: it has no label for each input and compares each input only with others. You will see how a loss that resembles ordinary classification, but with similarities between representations in the place of scores, rewards closeness for positive pairs and distance for negative ones. The quiz asks why the negative pairs are indispensable, and what goes wrong without them.
Then you will meet the main variations, which differ in how the pairs are chosen. In instance discrimination, the positive pair is two altered versions of one image. In supervised contrastive learning, class labels define the pairs. In models trained on images and captions, the positive pair is a picture and its description, mapped into a shared space by two separate networks. The quiz asks what each choice assumes and what it saves.
Common misunderstandings include thinking that contrastive learning needs labels for every example, that negative pairs are optional, and that the way the pairs are chosen doesn't matter. In fact, the choice of pairs is exactly how prior knowledge about what counts as similar is supplied to the model. The method has become one of the main ways of obtaining useful representations without labels, and many of the large models that combine text and images owe their abilities to it. Seeing how the pairs are chosen is the key to understanding what such a model knows. Because the labels come from the structure of the data instead of from people, contrastive learning scales to very large collections of unlabelled images and text, which is why it has become so popular for the first stage of training large models.
The quiz suits students and practitioners curious about how modern models learn representations. It needs no mathematics. There are 10 questions, each with an explanation shown after you answer, and the quiz takes about four minutes.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation appears after every answer.
›Do I need to know about representation learning first?
It helps, and the quiz on hierarchical and distributed representations covers the basics. But the questions here define the terms they use, and the key notion, that a model can place inputs in a space where similar ones are close, is explained along the way.
›What is contrastive learning?
It is a way of learning representations in which pairs of inputs that should count as similar are pulled together in the representation space, and pairs that should count as different are pushed apart. Once the representation has been learned, it is usually used by training a small classifier on top of it with a modest number of labels, which is how contrastive methods combine cheap unlabelled data with a little expensive labelled data.
›What are positive and negative pairs?
A positive pair is two inputs that should have similar representations, such as two altered copies of one image. A negative pair is two inputs that should be far apart, such as pictures of unrelated things. An anchor is the input around which a positive partner and several negative partners are chosen.
›Why are negative pairs necessary?
Without them, there is a trivial way to make all positive pairs close: map every input to the same point. Negative pairs penalise that, because they require different inputs to be far apart. They keep the representation informative. In practice, the quality of a contrastive model depends heavily on how hard the negative examples are and on how the alterations are chosen, since these define what the model learns to ignore and what it learns to notice.
›What is instance discrimination?
It is a form of contrastive learning for images in which the positive pair is two altered versions of the same image, using changes such as rotation, shifts or colour changes that keep the content the same, and other images in the data set provide the negatives. It needs no labels at all.
›What is supervised contrastive learning?
It uses class labels to choose the pairs: images of the same class form positive pairs and images of different classes form negative pairs. It avoids having to decide which alterations preserve meaning, and it avoids treating two similar images as a negative pair.
›How do image-and-text models use contrastive learning?
A picture and its caption form a positive pair, and a picture with a caption from another image forms a negative pair. Two separate networks, one for images and one for text, map their inputs into the same space. Because captioned images are easy to collect, this is called weakly supervised.