Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Paying for wrong beliefs
Suppose you design a shorthand for a language, giving short codes to words you believe are common. If your beliefs match how people actually talk, your messages are short. If not, you waste space on every message. Cross-entropy is the average message length when you use codes built for your beliefs on data that really follows .
To train a classifier, we make its beliefs (predicted probabilities) cost as little as possible on the real labels. Confident wrong answers cost a lot.
Every image classifier, terrain classifier, semantic-segmentation network and language model is trained by minimizing cross-entropy. Its value in bits per symbol (or its perplexity) is the standard measure of how well a model predicts.
- 1. Cross-entropy is small when the model gives the true class…
- 2. Being confidently wrong makes cross-entropy…
- 3. Cross-entropy is the most common loss for…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Definition
For a single labelled example with one-hot , this is , the negative log-probability of the correct class.
Softmax cross-entropy
A network outputs logits , and , . The loss is
Compute it with the log-sum-exp trick: subtract first to avoid overflow.
Perplexity
(with in bits) is the effective number of equally likely choices the model is torn between. A language model with 3 bits per token has perplexity 8.
- 1. For a one-hot label, cross-entropy equals…
- 2. Uniform guessing over 3 classes costs…
- 3. Perplexity at 3 bits/token is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.Decomposition
is fixed by the data, so minimizing cross-entropy over the model is the same as minimizing , and the same as maximizing likelihood. That’s why cross-entropy training is maximum-likelihood estimation.
Gradient
Prediction minus target: bounded, and large when confidently wrong. Compare squared error on probabilities, whose gradient gets multiplied by the softmax Jacobian and vanishes when the model saturates on the wrong answer.
Practical variants
- Label smoothing: target (Szegedy et al., 2016). This discourages infinite logits and improves calibration.
- Class weighting / focal loss: up-weight rare classes or hard examples (Lin et al., 2017, focal loss for dense detection).
- Binary cross-entropy for independent multi-label outputs (sigmoid per class).
- Calibration: models trained to near-zero loss become overconfident. Temperature scaling, with fitted on validation NLL, fixes this without changing predictions (Guo et al., 2017).
- 1. ∂ℓ/∂z for softmax cross-entropy is…
- 2. Minimizing cross-entropy over q is equivalent to minimizing…
- 3. Temperature scaling changes…
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Deriving the softmax CE gradient
(with ). Then and , so . ∎
Gibbs’ inequality: cross-entropy is minimized at q = p
, by Jensen: . Equality holds iff . ∎ So the expected loss is minimized by predicting the true conditional probabilities. Cross-entropy is a proper scoring rule.
Cross-entropy = negative log-likelihood
For data , is the average negative log-likelihood. Minimizing it is MLE (A5.1-4), consistent and asymptotically efficient for well-specified models.
Exercise 3.1: Cross-entropy of p = (½, ½) with q = p
bit, the minimum possible for this .
- 1. Cross-entropy is a proper scoring rule, meaning…
- 2. ∂/∂z_k of log Σ eᶻʲ is…
- 3. Minimizing average cross-entropy over a model is…
Level 4: Expert
★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.Calibrating an overconfident terrain classifier
A softmax classifier with 10 features (2 informative) is trained on only 60 labelled terrain patches until the training loss is nearly zero. On test data it’s overconfident: its stated confidence exceeds its accuracy, with an expected calibration error (ECE) of about 0.12. Fitting a single temperature on a validation set lowers the test NLL and cuts the ECE to about 0.05, with accuracy unchanged. A planner deciding whether to cross terrain needs calibrated probabilities, not just correct argmaxes.
Cross-entropy in AI, robotics and aerospace
- Perception: semantic segmentation for terrain classification on planetary rovers trains with per-pixel cross-entropy (Rothrock et al., 2016, SPOC for Mars).
- Language and multimodal models minimize next-token cross-entropy. Reported in bits per byte or perplexity, it doubles as a compression rate (A7-7).
- Calibrated uncertainty feeds risk-aware planning and decision thresholds (Guo et al., 2017; Ovadia et al., 2019).
- Imitation learning: behaviour cloning with discrete actions is cross-entropy on expert actions.
References
- T. Cover, J. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley. Ch. 2, 5.
- I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning. MIT Press. §5.5, 6.2.
- C. Guo, G. Pleiss, Y. Sun, K. Weinberger (2017). “On calibration of modern neural networks.” ICML.
- C. Szegedy et al. (2016). “Rethinking the Inception architecture for computer vision.” CVPR (label smoothing).
- T.-Y. Lin et al. (2017). “Focal loss for dense object detection.” ICCV.
- T. Gneiting, A. Raftery (2007). “Strictly proper scoring rules, prediction, and estimation.” JASA, 102(477).
- Y. Ovadia et al. (2019). “Can you trust your model’s uncertainty?” NeurIPS.
- B. Rothrock et al. (2016). “SPOC: deep learning-based terrain classification for Mars rover missions.” AIAA SPACE.
- R. Müller, S. Kornblith, G. Hinton (2019). “When does label smoothing help?” NeurIPS.
- G. Delétang et al. (2024). “Language modeling is compression.” ICLR.