Your contrastive loss is secretly just cross-entropy
Reference PyTorch implementation that improves CIFAR and ImageNet accuracy over standard cross-entropy by pulling same-class embeddings together, using one loss that becomes SimCLR when you withhold labels.

What it does
The repo is a reference implementation of Supervised Contrastive Learning and, incidentally, SimCLR. At its core is a single SupConLoss in losses.py that takes L2-normalized features and optional labels. Feed it labels and it performs supervised contrastive learning; omit them and it degenerates to SimCLR. The code uses CIFAR as the primary example and includes a pretrained ImageNet model that tops 79.1% top-1 accuracy using a momentum encoder trick.
The interesting bit The authors later point out—citing the StableRep paper—that the supervised contrastive loss is mathematically just cross-entropy in disguise. That meta-admission makes the whole exercise less about inventing a new loss and more about reframing how we think about pulling same-class embeddings together.
Key highlights
- One loss function toggles between supervised and unsupervised modes by checking for
labels. - CIFAR-10 accuracy climbs from 95.0% (cross-entropy) to 96.0%; CIFAR-100 goes from 75.3% to 76.5%.
- Includes an ImageNet checkpoint hitting 79.1% top-1 with a MoCo-style momentum encoder.
- t-SNE visualizations compare embedding clusters across standard cross-entropy, SupContrast, and SimCLR.
Caveats
- The
--syncBNflag currently does nothing because the code relies onDataParallelrather thanDistributedDataParallel. - The README itself points to a cleaner reimplementation of the loss in the StableRep repository if you find this one hard to parse.
Verdict Worth a look if you are researching contrastive learning or need a baseline that bridges supervised and self-supervised training. Skip it if you want production-ready infrastructure—this is reference code, not a framework.
Frequently asked
- What is HobbitLong/SupContrast?
- Reference PyTorch implementation that improves CIFAR and ImageNet accuracy over standard cross-entropy by pulling same-class embeddings together, using one loss that becomes SimCLR when you withhold labels.
- Is SupContrast open source?
- Yes — HobbitLong/SupContrast is open source, released under the BSD-2-Clause license.
- What language is SupContrast written in?
- HobbitLong/SupContrast is primarily written in Python.
- How popular is SupContrast?
- HobbitLong/SupContrast has 3.4k stars on GitHub.
- Where can I find SupContrast?
- HobbitLong/SupContrast is on GitHub at https://github.com/HobbitLong/SupContrast.