Paper commentary
Supervised Contrastive Learning
Supervised contrastive learning pulls same-class images together in embedding space
- Problem Cross-entropy is the default supervised loss, and alternatives rarely beat it on large datasets. Contrastive losses use one positive per anchor, so they cannot use several same-class samples.
- Method SupCon lets every sample sharing the anchor's class count as a positive, instead of only an augmented copy. It pulls those positives together and pushes other batch samples apart.
- Results On ResNet-200, SupCon reached 81.4% top-1 accuracy on ImageNet, which is 0.8% above the best reported for that architecture.
Why cross-entropy is hard to replace
Cross-entropy is the default loss for supervised classifiers, and it is still behind many state of the art ImageNet results. Its weak spots are well documented, including sensitivity to noisy labels and poor margins, but alternatives rarely beat it on large datasets. Contrastive learning takes a different route: it pulls a positive pair together in embedding space and pushes the anchor away from negatives.
Self-supervised contrastive losses use exactly one positive per anchor, and the standard form cannot take several same-class samples as positives.
Many same-class positives per anchor
The core idea is to let every sample that shares the anchor's class count as a positive, instead of only an augmented copy of the anchor. The negatives stay the same as in contrastive learning, namely the remaining samples of the batch, so the many-negatives design is kept. The authors also frame the loss as a generalization: the triplet loss uses one positive and one negative per anchor, and the N-pairs loss uses one positive with many negatives.
Generalizing to several positives can be done in more than one way, so the authors compared two forms. In one form the sum over positives sits outside the logarithm, and in the other it sits inside. Because the logarithm is concave, Jensen's inequality implies the inside form is at most the outside form. Their gradients differ mainly through a weight on the positives, and the outside form makes that weight uniform, which removes a bias that the inside form keeps.
How one training step runs
Each labeled batch of N images is augmented twice, which gives 2N views that form a multiviewed batch. Both views pass through the same encoder, which outputs a 2048-dimensional vector that is normalized to unit length. A projection network then maps each vector to a normalized 128-dimensional output, either through one hidden layer of size 2048 or as a single linear layer.
The outside-log loss is computed on these projections, and for each anchor i it reads \[ L^{\mathrm{sup}}_{\mathrm{out},i} = -\frac{1}{|P(i)|}\sum_{p\in P(i)}\log\frac{\exp(z_i\cdot z_p/\tau)}{\sum_{a\in A(i)}\exp(z_i\cdot z_a/\tau)} \] The temperature τ is set to 0.1 for all main runs, and it scales the inner products inside the loss. No separate hard-mining step is needed, because the gradient already weights hard positives and negatives more heavily.
After contrastive training the projection network is discarded, and a linear classifier is trained with cross-entropy on the frozen encoder. This second stage is optional for transfer or retrieval, and the authors say it needs as few as 10 epochs.
Why the outside-log form wins
On ResNet-50 at batch size 6144, the outside-log form reached 78.7% top-1 accuracy on ImageNet, while the inside-log form reached 67.4%. The authors trace the gap to the normalization factor, which sits outside the logarithm in the outside form and so rescales each positive term. Inside the logarithm the same factor only adds a constant and leaves the gradient unchanged, so the inside form stays more exposed to bias among the positives.
Setting each positive to the mean positive makes the inside weight equal the outside weight, so the authors use the outside form for the rest of the paper.
ImageNet accuracy against cross-entropy
With ResNet-200, SupCon reached 81.4% top-1 accuracy, which the authors report as 0.8% above the best figure reported for that architecture. That ResNet-200 run used Stacked RandAugment, which the authors found best for ResNet-200 with both losses.
Batch size is a confound, because SupCon uses two views per sample and so has an effective batch twice that of cross-entropy. Cross-entropy at batch size 12,288 reached only 77.5%, and training it for 1400 epochs lowered accuracy to 77.0%, so a larger batch alone doesn't close the gap.
A memory bank of 8192 vectors with batch size 256 gave SupCon 79.1% on ResNet-50, slightly above the 78.7% at batch size 6144 without memory. N-pairs in the same framework reached only 57.4%, which the authors attribute to missing multiple views, a temperature of 1 instead of 0.1, and fewer positives.
Robustness and transfer
On ImageNet-C with ResNet-50, SupCon scored a Mean Corruption Error of 67.2, against 68.6 for cross-entropy, and lower is better. Relative mCE gives the same ordering, 94.6 against 96.2 for ResNet-50. The ResNet-200 rows point the same way, with 50.6 for SupCon against 52.4 for cross-entropy on mCE.
Across changes of augmentation, optimizer and learning rate, the authors measured lower variance in SupCon's top-1 accuracy than in cross-entropy's. On 12 natural image datasets the transfer results are on par, with SupCon-50 averaging 84.27 against 84.67 for cross-entropy on ResNet-50. On ResNet-200 the averages are 85.67 for SupCon and 85.77 for cross-entropy.
Limits and open questions
The main ImageNet results use batch size 6144, although the authors note that 2048 is enough for most purposes. More heavily tuned models deteriorated in Expected Calibration Error, which the authors report as a cost of tuning.
All encoders are ResNet-50, ResNet-101 or ResNet-200, so other architectures are untested. The authors leave the choice of projection network architecture to future work. They also leave open how the training objective relates to transfer performance.
Paper info
- Authors
- Khosla, Prannay, Teterwak, Piotr, Wang, Chen, Sarna, Aaron, Tian, Yonglong, Isola, Phillip, Maschinot, Aaron, Liu, Ce, Krishnan, Dilip
- Venue
- arXiv · Preprint
- First published
- 2020-4-23
- Added
- 2026-11-12
- Updated
- 2021-3-10
- Version
- v5
- arXiv
- 2004.11362
- Category
- cs.LG · cs.CV · stat.ML
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Khosla, Prannay, Teterwak, Piotr, Wang, Chen, Sarna, Aaron, Tian, Yonglong, Isola, Phillip, Maschinot, Aaron, Liu, Ce, Krishnan, Dilip (2020). Supervised Contrastive Learning. arXiv:2004.11362v5. https://arxiv.org/abs/2004.11362v5
BibTeX
@misc{prannay2020supervised,
title = {{Supervised Contrastive Learning}},
author = {Khosla, Prannay and Teterwak, Piotr and Wang, Chen and Sarna, Aaron and Tian, Yonglong and Isola, Phillip and Maschinot, Aaron and Liu, Ce and Krishnan, Dilip},
year = {2020},
eprint = {2004.11362},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2004.11362v5}
}