Contrastive learning

Contrastive learning is a way of training models to produce useful representations by pulling matching pairs of examples close together and pushing mismatched ones apart.

Contrastive learning teaches a model what makes things alike by showing it pairs. Each input is converted into a vector of numbers, its representation or embedding. Training rewards the model when two inputs that belong together, a positive pair, end up with similar vectors, and when inputs that do not belong together, negative pairs, end up far apart. Over millions of comparisons the model learns which features matter for telling things apart and which do not.

The appeal is that the pairs can often be made without human labels, which makes contrastive learning a leading form of self-supervised learning. In SimCLR, published in February 2020 by Ting Chen, Simon Kornblith, Mohammad Norouzi and Geoffrey Hinton, a positive pair is two randomly altered versions of the same photo, cropped, recoloured or blurred differently; the other images in the same training batch serve as negatives; with a batch of 8,192 photos, that gives 16,382 negatives for every positive pair. A simple classifier trained on top of SimCLR's representations reached 76.5 per cent top-1 accuracy on ImageNet, matching a fully supervised ResNet-50. The authors also found that contrastive learning benefits from larger batches, since bigger batches supply more negatives.

The best-known application pairs images with text. OpenAI's CLIP, released in January 2021, trained an image encoder and a text encoder together on 400 million image and caption pairs collected from the internet, with the task of predicting which caption belongs to which image. The result could recognise categories it was never explicitly trained on, matching the original ResNet-50's ImageNet accuracy without using any of ImageNet's 1.28 million labelled training images. Encoders trained this way have since been used in image search and text-to-image generation.

These systems built on earlier work such as Contrastive Predictive Coding, a 2018 method from Aaron van den Oord and colleagues whose probabilistic contrastive loss is known as InfoNCE. Not all self-supervised learning is contrastive; some methods use only positive pairs. But where a system has to judge whether two things match, such as a photo and a description, contrastive training is a natural fit.

Common questions

What is contrastive learning in simple terms?

It is a training method that teaches a model to recognise similarity. The model sees pairs of examples, learns to give matching pairs similar internal representations and mismatched pairs different ones, and in doing so learns features that are useful for many tasks.

How does CLIP use contrastive learning?

CLIP trains an image model and a text model side by side on image and caption pairs. Each matching image and caption is a positive pair; every other combination in the batch is a negative. The models learn to place matching images and captions close together in a shared space, which lets CLIP connect pictures and words.

Is contrastive learning supervised or unsupervised?

It is usually described as self-supervised. The training signal comes from how the pairs are constructed, for example two altered views of the same photo, rather than from human-written labels. Paired data such as image captions are sometimes described as natural supervision.

What is a negative pair?

A negative pair is two examples that should not match, such as two different photos, or an image and a caption written for a different image. Contrastive training pushes their representations apart, which stops the model from mapping everything to the same point.

Finding more

Search for "contrastive learning", "SimCLR", "CLIP contrastive language-image pre-training" or "InfoNCE loss" for the main methods. The original papers on arXiv and encyclopedia entries that cite them are the most reliable sources. Benchmark results in this area depend heavily on the evaluation protocol, so compare figures only when they were measured the same way.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.