Center loss for PyTorch: when softmax isn't enough
It implements the 2016 center-loss paper to make your network learn features that actually cluster by class.

What it does\nThis repo is a minimal PyTorch port of the center-loss function from Wen et al. (ECCV 2016). You import center_loss.py, add it as a secondary penalty to your standard cross-entropy loss, and it pulls same-class feature vectors toward learned centroids. The bundled MNIST demo shows the network climbing from 10.3% accuracy to 98.5% in 30 epochs as the clusters tighten.\n\nThe interesting bit\nSoftmax only needs a decision boundary; it has no incentive to keep same-class features close. Center loss fixes the geometry by minimizing intra-class variance, which is why the included visualizations show tidy blobs instead of overlapping smears. The repo also surfaces the easy-to-miss gradient-scaling trick required to keep the center learning rate independent of the loss weight alpha.\n\nKey highlights\n- The whole implementation is one file: center_loss.py.\n- Runnable MNIST example with CNN, logging, and accuracy curves.\n- Side-by-side GIFs compare softmax-only embeddings against center-loss-augmented ones.\n- Adopted by the author’s deep-person-reid framework, so it has seen real-world use.\n- Correctly handles the center-update bookkeeping that most blog posts gloss over.\n\nCaveats\n- You must manually rescale gradients by 1/alpha (or lr_cent / (alpha * lr)) before each center update; miss this and the centroids wander.\n- The README presents two different optimizer setups, so check which gradient-scaling formula applies to your choice.\n- The use_gpu flag and manual GPU selection reflect an older PyTorch style.\n\nVerdict\nGrab this if you need a battle-tested, drop-in loss to tighten feature clusters for recognition or re-identification tasks. Pass if you are looking for a full metric-learning framework—this is a single-purpose paper implementation.
Frequently asked
- What is KaiyangZhou/pytorch-center-loss?
- It implements the 2016 center-loss paper to make your network learn features that actually cluster by class.
- Is pytorch-center-loss open source?
- Yes — KaiyangZhou/pytorch-center-loss is open source, released under the MIT license.
- What language is pytorch-center-loss written in?
- KaiyangZhou/pytorch-center-loss is primarily written in Python.
- How popular is pytorch-center-loss?
- KaiyangZhou/pytorch-center-loss has 992 stars on GitHub.
- Where can I find pytorch-center-loss?
- KaiyangZhou/pytorch-center-loss is on GitHub at https://github.com/KaiyangZhou/pytorch-center-loss.