In SimCLR with a mini-batch of images (giving augmented views), write the NT-Xent (InfoNCE) loss for a positive pair with cosine similarity and temperature . Why is this a -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 normalisation.
Key idea
Treating the matching augmented view as the single target class among all non-anchor views in the batch turns contrastive learning into -way softmax cross-entropy over temperature-scaled cosine similarities: .
Steps
- Define the inputs: each image produces two random augmentations, giving views passed through encoder and projection MLP to get . Let be the cosine similarity.
- Write the NT-Xent loss for anchor and its partner : , averaged symmetrically over all ordered positive pairs and .
- Show equivalence to -way cross-entropy: fix anchor and define logits for the candidates , with one-hot label on and on the negatives. Then , exact cross-entropy.
- Analyse temperature : softmax probability on negative is , and the gradient with respect to scales like . Small (e.g. –) concentrates weight on the hardest negatives (large ), 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
- Including the anchor itself () in the denominator, which makes it -way instead of -way and biases the logits since .
- Forgetting why cosine similarity (-normalisation) is paired with : without normalisation, the network could inflate norms to shrink the effective temperature arbitrarily.
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 , how do you derive the optimal discriminator for a fixed generator , and show that substituting reduces the generator objective to ?
Next in Deep learning: What is the general message-passing neural network (MPNN) update for node , and how do you prove that relabelling nodes with any permutation matrix makes a node-level GNN layer permutation-equivariant while global sum or mean pooling makes a graph-level readout permutation-invariant?