Contrastive learning

What Is Contrastive Learning?

Contrastive learning is a family of self-supervised representation learning methods that train a model by comparing examples rather than by predicting labels. The model is shown pairs it should treat as similar, called positives, and pairs it should treat as dissimilar, called negatives, and it learns an embedding in which positives are pulled together and negatives pushed apart. Because the positive pairs are constructed automatically from unlabeled data, usually by applying two different transformations to the same underlying item, the approach removes the dependence on human annotation that supervised training carries.

The idea has roots in metric learning, where siamese networks were used for signature matching in the 1990s and for face verification in the 2000s, first with contrastive losses and later with triplet losses. What changed in the late 2010s was the framing: instead of learning a similarity function for one narrow task, the objective became learning a general-purpose encoder whose representations transfer to many downstream tasks through a small classifier trained on top.

Instance Discrimination and the InfoNCE Objective

Most modern methods treat each training example as its own class, a pretext task known as instance discrimination. Two augmented views of the same image form the positive pair, and every other example in the batch supplies negatives. The loss is typically InfoNCE, a softmax over similarity scores scaled by a temperature parameter, introduced with contrastive predictive coding and derived as a lower bound on the mutual information between the two views. The temperature is not a minor hyperparameter: low values concentrate the gradient on the hardest negatives, while high values spread it evenly, and the choice materially changes the geometry of the learned space.

Views, Augmentation, and Negatives

The choice of transformation defines what the representation becomes invariant to, which makes augmentation design a modeling decision rather than a preprocessing detail. Systematic study in SimCLR showed that composing random cropping with color distortion is decisive for image representations, since cropping alone allows the network to solve the task by matching color histograms. That work also demonstrated the value of a projection head, a small network applied before the loss and discarded afterward, and the benefit of very large batches, which supply more negatives per step.

Supplying enough negatives without enormous batches motivated alternative designs. Momentum Contrast maintains a queue of encoded examples from recent batches and updates a second encoder as a slowly moving average of the primary one, keeping the stored representations consistent as training proceeds. Later methods including BYOL and Barlow Twins dispense with explicit negatives entirely, relying on architectural asymmetry or on decorrelating the components of the embedding to avoid the collapse into a constant output that a positives-only objective would otherwise permit.

Multimodal and Supervised Extensions

The same objective aligns representations across modalities when the positive pair is drawn from two different signals describing the same thing. Training on large collections of images paired with their captions produces a shared embedding space in which text and images can be compared directly, which supports retrieval and zero-shot classification by comparing an image against embedded class descriptions. Supervised contrastive learning applies the loss with labels available, treating all examples sharing a class as mutual positives, which often yields better results than cross-entropy on the same architecture.

Applications

Contrastive learning has applications in a range of fields, including:

  • Pretraining vision backbones for classification, detection, and segmentation
  • Text embedding models for semantic search and retrieval
  • Speech and audio representation learning
  • Medical imaging where annotation is scarce and expensive
  • Recommendation systems and user-item matching
  • Anomaly detection in industrial and network monitoring data
Loading…