DL0126 Contrastive Learning Pairs

What is contrastive learning, and what are positive and negative pairs?

Answer

Contrastive learning trains an encoder by comparison instead of by prediction of a label: it pulls the embeddings of things that should mean the same thing together and pushes everything else apart. A positive pair is two views of the same underlying content (two augmentations of one photo in SimCLR, an image and its caption in CLIP, two nearby audio segments in CPC), and a negative pair is an anchor paired with content assumed to be different (any other item in the batch). The standard objective, InfoNCE, turns this into a softmax classification problem: given an anchor, identify its single positive among one positive and K negatives, using scaled cosine similarity as the logit. Positives define what the representation should be invariant to, and negatives are what prevents the trivial solution where the encoder maps every input to the same vector, a failure called representational collapse. The whole design problem of a contrastive system is therefore the pair-construction policy: a positive that is too easy teaches nothing, and a negative that is secretly a positive actively teaches the wrong thing.

(1) Positives Encode The Invariance You Want: whatever transformation you apply to build a positive pair is exactly the factor the encoder learns to discard, so color jitter buys color invariance and destroys any task where color is the signal.
(2) Negatives Are The Anti-Collapse Term: without a repulsive term the constant map f(x) = c is a global minimum of the attraction loss, so negatives provide the uniformity pressure that spreads embeddings over the hypersphere.
(3) InfoNCE Is A K+1-Way Classification: one positive logit in the numerator, the positive plus all negatives in the denominator, which is why the loss is bounded below by \log(K+1) under a random encoder.
(4) Temperature Sets Hard-Negative Focus: small \tau (0.05 to 0.1 is typical) concentrates almost all repulsive gradient on the few most similar negatives, while large \tau treats all negatives nearly equally.
(5) Negative Count Is A Systems Problem: SimCLR needs batch sizes of 4096 or more to get enough in-batch negatives, MoCo decouples them with a momentum-encoder queue, and CLIP trained with a 32768 global batch across many GPUs.
(6) False Negatives Are The Main Bias: in-batch negatives assume every other sample is semantically different, which is false on class-imbalanced or deduplicated-poorly data, and the loss then pushes apart items that should be close.

Pairs do not have to come from augmentation. The general recipe is: find a cheap source of known agreement and treat everything else as disagreement. Augmentation gives agreement between two crops of one image; multimodal alignment gives agreement between an image and the alt-text scraped alongside it; temporal or spatial context gives agreement between adjacent frames, patches, or sentences; and labels give agreement between any two samples of the same class, which is what SupCon exploits to allow many positives per anchor. The negatives are almost always just the rest of the batch, because sampling them explicitly is expensive and in-batch negatives come for free with the forward pass already computed.

Two-panel diagram: left panel shows one source image producing two augmented views labeled anchor and positive while two other images in the batch produce negative views; right panel shows the same points on a unit circle with an attraction arrow from anchor to positive and dashed repulsion arrows pushing the negatives away

Figure 1: Pair construction and its geometric effect. Each anchor has exactly one positive (the other view of the same source) and 2N-2 negatives (all views of the other images in the batch); on the unit hypersphere the loss is a single attractive force toward the positive balanced against repulsive forces from every negative.

Mathematical Formulation:
s(u,v) = \frac{u^\top v}{\|u\|\,\|v\|}
\ell_i = -\log \frac{\exp(s_{i,i^+}/\tau)}{\sum_{k \neq i} \exp(s_{i,k}/\tau)}
\mathcal{L} = \frac{1}{2N}\sum_{i=1}^{2N} \ell_i
I(u;v) \geq \log K - \mathcal{L}_{\mathrm{InfoNCE}}

Where:

  • s(u,v) is the cosine similarity used as the logit, computed on L2-normalized embeddings u = f(v) produced by the encoder (and, in SimCLR, a discarded projection head).
  • \ell_i is the per-anchor NT-Xent loss and \mathcal{L} its average over all 2N views of an N-image batch.
  • i indexes the anchor view, i^{+} its unique positive, and k ranges over every other view, so the denominator holds 1 positive and 2N-2 negatives.
  • \tau > 0 is the temperature; the gradient weight on negative k is its softmax probability, so shrinking \tau sharpens that distribution onto the hardest negatives.
  • I(u;v) is the mutual information between the two views and K the number of negatives, giving the standard InfoNCE lower bound: the bound saturates at \log K, which is one reason large batches help.
Log-x line chart of the cumulative share of repulsive gradient carried by the hardest fraction of negatives, for temperature 0.05, 0.1 and 0.5 over 1024 negatives; the low-temperature curve rises almost vertically showing that the hardest one percent of negatives absorbs most of the gradient

Figure 2: Temperature decides which negatives matter. At \tau = 0.05 the hardest 1% of negatives absorbs most of the repulsive gradient, making training an implicit hard-negative miner that is also maximally sensitive to false negatives; at \tau = 0.5 the pressure is spread nearly uniformly and the embedding space stays smoother but less discriminative.

AspectAugmentation (SimCLR, MoCo)Multimodal (CLIP, ALIGN)Supervised (SupCon)
AnchorOne augmented view of an imageAn image embeddingA labeled sample
PositiveA second augmentation of the same image (exactly one)The paired caption text (exactly one, symmetric loss both directions)Every other sample sharing the label (many per anchor)
NegativesAll 2N-2 other views, or a momentum queue of 65k stale keysAll other captions in the global batch (32k in CLIP)Only samples with a different label, so false negatives vanish
Invariance learnedTo the augmentation family you chose (crop, color, blur)To modality and phrasing, giving a shared image-text spaceTo everything within a class, which can over-collapse fine detail
Main failure modeAugmentation removes task-relevant signal; shortcut solutions from crop statisticsNoisy or generic captions produce weak positives; batch size dominates costNeeds labels, so it is not self-supervised and inherits label noise

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *