In SimCLR with a mini-batch of N images (giving 2N augmented views), write the NT-Xent (InfoNCE) loss for a positive pair (i,j) with cosine similarity si,j and temperature τ. Why is this a (2N−1)-way cross-entropy loss, and how does τ affect the gradients?

Deep learning, Self-supervised learning
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can write the NT-Xent objective precisely, link it to multi-class cross-entropy and explain the role of temperature and L2 normalisation.

Key idea

Treating the matching augmented view j as the single target class among all 2N−1 non-anchor views in the batch turns contrastive learning into (2N−1)-way softmax cross-entropy over temperature-scaled cosine similarities: ℓi,j=−log⁡exp⁡(si,j/τ)∑k=12N1[k≠i]exp⁡(si,k/τ).

Steps

  1. Define the inputs: each image xn produces two random augmentations, giving 2N views passed through encoder f and projection MLP g to get zm=g(f(x~m)). Let si,k=zi⊤zk∥zi∥∥zk∥ be the cosine similarity.
  2. Write the NT-Xent loss for anchor i and its partner j: ℓi,j=−log⁡exp⁡(si,j/τ)∑k≠iexp⁡(si,k/τ), averaged symmetrically over all 2N ordered positive pairs (i,j) and (j,i).
  3. Show equivalence to (2N−1)-way cross-entropy: fix anchor i and define logits uk=si,k/τ for the 2N−1 candidates k∈{1,…,2N}∖{i}, with one-hot label 1 on k=j and 0 on the 2N−2 negatives. Then ℓi,j=−∑k≠iyklog⁡softmax⁡(u)k, exact cross-entropy.
  4. Analyse temperature τ: softmax probability on negative k is pi,k∝exp⁡(si,k/τ), and the gradient with respect to si,k scales like pi,k/τ. Small τ (e.g. 0.07–0.1) concentrates weight on the hardest negatives (large si,k), pushing them apart strongly, but too small a τ causes numerical instability or penalises false negatives (same-class images in the batch). Large τ treats all negatives nearly equally and yields weak gradients.

Common mistakes

How long your answer should be

Half a page; 4 minutes.

Last checked on 4 December 2026.

Read this

Previous in Deep learning: For the minimax GAN objective min⁡Gmax⁡DV(D,G)=Ex∼pdata[log⁡D(x)]+Ez∼pz[log⁡(1−D(G(z)))], how do you derive the optimal discriminator DG∗(x) for a fixed generator G, and show that substituting DG∗ reduces the generator objective to 2DJS(pdata∥pg)−log⁡4?
Next in Deep learning: What is the general message-passing neural network (MPNN) update for node v, and how do you prove that relabelling nodes with any permutation matrix P makes a node-level GNN layer permutation-equivariant while global sum or mean pooling makes a graph-level readout permutation-invariant?