ML Systems Lab Open interactive version →
Intermediate 45 min read contrastive lossNT-XentInfoNCEtemperaturehard negatives

Contrastive Loss Functions

NT-Xent, InfoNCE, temperature τ, negative mining, uniformity and alignment

In a batch of 512 images, each image has one positive pair—a second augmented view of itself—and 1,022 negatives: every other image in the batch. The NT-Xent loss for positive pair (i,j) is: -log[exp(sim(zᵢ,zⱼ)/τ) / Σ_{k≠i} exp(sim(zᵢ,zₖ)/τ)]. That formula looks mechanical until you understand what each piece does. The cosine similarities in the denominator are all the "wrong" answers the model must learn to reject. The temperature τ controls how much the model focuses on the hardest wrong answers versus treating all negatives equally.

Temperature is the hyperparameter practitioners misset most. Set τ = 0.07 (SimCLR's default) and the model concentrates gradient on the negatives closest to the anchor—useful, informative signal. Set τ = 0.01 and exp(sim/0.01) overflows: exp(1/0.01) ≈ 2.7×10^43, the softmax denominator saturates, gradients explode, training collapses to NaN within 100 steps. Set τ = 1.0 and every negative gets nearly equal weight, slowing convergence to a crawl because the signal cannot distinguish hard from easy negatives. The useful operating range is 0.07–0.2, and knowing why that range exists is more useful than memorizing it.

More negatives is not just empirically better—it is formally justified. NT-Xent is an instantiation of InfoNCE, a lower bound on mutual information I(X;C). The bound tightens as the number of negatives increases. At 512 images per batch, each anchor has 1,022 negatives; at MoCo's queue of 65,536, each anchor has 65,536 negatives. The mutual information lower bound is tighter, the discrimination task is harder, and the encoder must learn finer-grained features to identify the correct positive. This is the formal reason large batches help SimCLR and the formal reason MoCo's queue was designed to decouple negative count from batch size.

Hard negatives—items close to the anchor in embedding space but not actually the same image—carry far more gradient than easy negatives already pushed far apart. A model that has learned basic discrimination finds easy negatives uninteresting: they contribute near-zero gradient. Hard negatives force the model to learn fine-grained distinctions. But hard negatives increase false negative risk: two cat images in the same batch are treated as negatives even though they should cluster together. Debiased contrastive loss estimates and subtracts this same-class contribution from the denominator.

NOT this. The intuition "just lower τ to make training harder" is wrong and dangerous. τ < 0.05 causes numerical overflow before any useful learning occurs. The useful range of harder training is 0.07–0.12, not 0.01. Stabilization techniques (log-sum-exp, gradient clipping, warmup) can extend this slightly, but the fundamental constraint is the exponential—smaller τ raises every similarity to a power that overflows floating point.

Key points

Takeaway

Temperature τ is the most consequential contrastive hyperparameter: it controls whether gradients concentrate on the hardest negatives (low τ, overflow risk below 0.05) or spread uniformly over all negatives (high τ, slow convergence). More negatives tightens the InfoNCE mutual information lower bound—so methods like MoCo's 65,536-entry queue exist not as engineering convenience but as a formal improvement in what is being optimized.

Recap

Check your understanding

Q1. SimCLR with a batch size of 256 gives poor results. Increasing to 4096 dramatically improves performance. Explain the mechanism.

Q2. You set temperature τ = 0.01 (very low) and training collapses to NaN loss after 100 steps. What happened?

Q3. Explain why debiased contrastive loss is needed and sketch how it corrects for false negatives.

Q4. Select the two true statements about the relationship between uniformity and alignment in NT-Xent.

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →