Contrastive Learning

Contrastive learning trains an embedding space by comparing examples. A positive pair should land close together; negative examples should land farther away. In self-supervised learning, positives are often two augmentations of the same item. In multimodal learning, positives can be paired image-text examples. The learned vectors are useful for dense retrieval, clustering, transfer learning, and downstream classifiers.

Contrastive learning embedding geometry

The important design choice is not only the loss. The positive-pair construction defines the invariances the model learns. If two crops of the same image are treated as positive, the representation is encouraged to ignore crop location. If an image and its caption are treated as positive, the representation is encouraged to align visual and textual semantics.

The contrastive objective

Let and be normalized embeddings of two positive views. Similarity is often cosine similarity,

For an anchor with positive , the NT-Xent loss is

This is a cross-entropy classification problem over the batch: given anchor , classify which other embedding is its positive. The numerator rewards high anchor-positive similarity. The denominator makes every other item in the batch compete for probability mass.

The temperature controls sharpness. Smaller makes the softmax focus strongly on the most similar negatives; larger spreads gradient across more negatives.

Worked Calculation

Suppose an anchor has cosine similarities to its positive and , , and to three negatives. With , the positive probability is

The loss is

Even though the positive is the most similar item, the loss is not zero because the negatives still take about of the probability mass. Training increases the positive similarity, decreases hard-negative similarity, or both.

Positive and Negative Construction

SettingPositive pairNegative candidatesWhat the model is pushed to learn
SimCLR-style visiontwo augmentations of the same imageother images in the batchinvariance to crop, color, blur, and augmentation choices
CLIP-style multimodal trainingmatched image and textother images or texts in the batchcross-modal semantic alignment
Retrieval fine-tuningquery and relevant documentirrelevant or less relevant documentstask-specific ranking geometry
Instance discriminationtwo views of the same instanceother instancesinstance-level separation

False negatives are the central risk. Two different images of the same class, or two documents that answer the same query, may be treated as negatives if the batch labels do not know they are semantically related. The loss will then push useful neighbors apart.

Why Batch Composition Matters

Contrastive objectives use the batch as the classification universe. Larger batches or memory queues provide more negatives, which can make the task harder and the representation sharper. But more negatives are not automatically better: easy negatives contribute little gradient, while false negatives actively harm the embedding geometry.

Hard-negative mining has to be handled carefully. A hard negative should be genuinely different from the anchor while still being close enough to teach a boundary. If the negative is mislabeled or ambiguous, the model learns the wrong separation.

Caveats

Contrastive learning does not discover “semantic similarity” in the abstract. It learns the similarity implied by the view construction, augmentations, batch sampling, and temperature. If augmentations remove information needed by the downstream task, the representation can become invariant to the wrong features. If the deployment task needs calibrated probabilities rather than neighbor structure, contrastive pretraining usually needs supervised adaptation.

Connections

References