Contrastive learning is a representation-learning approach that trains an encoder by contrasting an anchor input against positive examples (same distribution as the anchor) and negative examples (different distribution). It is mostly used self-supervised, where positives are constructed by data augmentation and negatives are other items in the batch.

Intuition

The model is not asked to predict a label. It is asked to make the embedding of the anchor close to its positives and far from its negatives. Whatever feature lets the encoder do this consistently turns out to be a useful general-purpose representation, even if no class labels existed during training.

center

Suppose we want to train an image encoder. Given anchor — a crop of a dog photo — its positive is another crop of the same image, and its negative is a crop of a different image (say a cat). The objective minimizes distance between anchor and positive while maximizing distance between anchor and negative in the feature space.

Mechanics

The actual training signal comes from a contrastive objective. The classic form is the pairwise Contrastive Loss with a margin , which is what made Siamese-network face verification work. Modern self-supervised setups (SimCLR, MoCo, CLIP) use InfoNCE instead, treating the positive among candidates as a softmax classification target so each batch contributes many negatives at once.

In both cases the geometry is the same: pull positives together, push negatives apart. The difference is how negatives are sampled (explicit pairs vs. all in-batch items) and how the loss is normalized (margin vs. softmax).

Why It Works Self-Supervised

Augmentations encode prior knowledge about what should be invariant. Cropping says “different parts of the same image are about the same thing.” Color jitter says “the identity is not in the exact pixel values.” The encoder learns the features that survive all chosen augmentations, which tend to be the semantically meaningful ones.

Failure Modes

  • Augmentation collapse: too-weak augmentations let the encoder rely on shortcut features (e.g. color histograms); too-strong ones destroy the signal.
  • Hard-negative starvation: with random in-batch negatives, most are easy and contribute little gradient. Hard negative mining or large batches help.
  • Representation collapse: without negatives or careful asymmetry (BYOL-style), all inputs can map to the same point. Contrastive losses prevent this directly; non-contrastive methods rely on architectural tricks.

One-line Interview Answer

Contrastive learning trains an encoder to map an anchor close to its positives and far from its negatives, using either a margin-based pairwise Contrastive Loss or an InfoNCE softmax over many negatives in a batch.

References

Loss Functions

See Contrastive Learning - Loss Functions.