Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.One loss for many classes
With more than two classes, a network outputs one score per class, softmax turns the scores into probabilities (E1-12), and the loss is −log of the probability given to the correct class. That is categorical cross-entropy. If labels are stored as one-hot vectors it is called categorical; if they are stored as class numbers, sparse. The maths is identical.
Terrain classification, object recognition, fault-mode diagnosis and next-token prediction in language models are all trained with this loss. Whether the resulting probabilities can be trusted matters as much as accuracy when a downstream planner or operator acts on them, and the loss settings (label smoothing, class weights) directly change that.
- 1. Categorical cross-entropy charges…
- 2. Sparse cross-entropy differs from categorical in…
- 3. A uniform guess over 10 classes costs…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Definition: CE = −Σ_k t_k log p_k with p = softmax(z) and target row t. With one-hot t this is −log p_y.
- Example: z = (0, 0) and true class 1: CE = log 2 ≈ 0.693.
- Sparse form: CE = LSE(z) − z_y, computed directly from the integer label y.
- Gradient: ∂CE/∂z = p − t. For z = (0, 0) and y = 0: (−0.5, 0.5). Each row sums to 0.
- Label smoothing: t = (1 − ε)·one-hot + ε/K. With K = 4 and ε = 0.2, the true class gets 0.85 and the others 0.05.
- Class weights: weight each example’s loss by w_y. PyTorch divides by the sum of the weights used, not by the batch size.
- Perplexity: exp(CE), the effective number of equally likely choices. A uniform guess over 10 classes has perplexity 10.
- 1. The CE gradient with respect to the logits is…
- 2. Label smoothing with ε = 0.1 and K = 10 gives the true class…
- 3. Perplexity is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Forms, weights and calibration
Exercise 1.
- One-hot and sparse forms agree.
- The gradient (p − t)/n matches finite differences and sums to zero per row.
- Smoothed targets sum to 1.
- A uniform guess over 10 classes costs log 10, perplexity 10.
- Class weights are normalised by their sum.
Calibration. A model is calibrated if, among predictions made with confidence 0.8, 80% are right. Expected calibration error (ECE) bins the predictions by confidence and averages |accuracy − confidence|, weighted by bin size. Overconfident models have confidence above accuracy.
Practice.
- Pass logits and integer labels to the loss:
F.cross_entropy(z, y). - It accepts
label_smoothing=εandweight=w. - For soft targets (distillation, mixup), pass probability rows.
- After training, temperature scaling on validation data is the simplest calibration fix.
F.cross_entropy(logits, y, label_smoothing=0.05, weight=class_w)- 1. An overconfident model's confidence is…
- 2. The simplest post-hoc calibration is…
- 3. With class weights, PyTorch's mean divides by…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Maximum likelihood. For a categorical model P(y = k | x) = p_k, the negative log-likelihood of label y is −log p_y. Cross-entropy training is therefore maximum likelihood, and like BCE it is a proper scoring rule: the expected loss is minimised by the true class probabilities.
Why logits grow. If the training data are separable, the loss can always be reduced by scaling up the logits, so with no regularisation they grow without bound. The model becomes confident on training points, and that confidence carries over to test points it gets wrong. Large modern networks fit their training data, so this overconfidence is common (Guo et al., 2017).
Label smoothing. Training against t = (1 − ε)·one-hot + ε/K is equivalent to adding ε times the cross-entropy against the uniform distribution. The optimal logit gap between the true class and the others becomes finite, log((1 − ε + ε/K)/(ε/K)), so logits stop growing.
- It improved accuracy in Inception-v2 (Szegedy et al., 2016) and in transformers.
- It tends to improve calibration (Müller, Kornblith and Hinton, 2019), but it also changes representations, and it can hurt knowledge distillation.
- Too much smoothing makes models underconfident: their confidence can never exceed 1 − ε + ε/K.
- 1. Without regularisation on separable data, logits…
- 2. Label smoothing caps the optimal logit gap at…
- 3. Label smoothing was introduced in…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How much label smoothing?
History.
- Multinomial logistic regression and the softmax output (J. Bridle, 1990) made cross-entropy the multi-class default.
- C. Szegedy et al. (2016) introduced label smoothing.
- C. Guo et al. (2017) documented the overconfidence of modern networks and proposed temperature scaling.
- R. Müller, S. Kornblith and G. Hinton (2019) analysed when label smoothing helps.
Exercise 2. Softmax regression on 5 Gaussian classes in 20 dimensions, with only 200 training points, so the classes are nearly separable in training. It is trained by gradient descent for 2000 steps with label smoothing ε = 0, 0.05, 0.1 or 0.2, then evaluated on 5000 test points. The table gives means over three seeds.
| ε | accuracy | mean confidence | ECE | test NLL | mean logit spread |
|---|---|---|---|---|---|
| 0 | 0.774 | 0.955 | 0.181 | 1.561 | 33.8 |
| 0.05 | 0.805 | 0.825 | 0.040 | 0.540 | 8.2 |
| 0.1 | 0.815 | 0.761 | 0.060 | 0.529 | 5.7 |
| 0.2 | 0.821 | 0.669 | 0.153 | 0.595 | 3.9 |
- Without smoothing the model was badly overconfident. Its average confidence was 0.955 while it was right 77% of the time. Logits grew to spreads of 34, and test NLL was three times higher.
- ε = 0.05 nearly fixed calibration (ECE 0.04), with logits four times smaller.
- More smoothing overshot. At ε = 0.2 the model was underconfident (0.669 confidence at 0.821 accuracy), and its ECE was almost as bad as with no smoothing, in the other direction.
- Smoothing also regularised. Accuracy rose by about 3–5 points. Weight decay or early stopping would have a similar effect.
- Lesson: label smoothing is a strong regulariser whose best amount depends on the problem. Tune ε by validation NLL or ECE, and measure calibration again after training.
Cross-entropy in AI, robotics and aerospace
- Perception for autonomy: class probabilities from terrain or object classifiers feed planners and sensor fusion, so calibration matters as much as accuracy.
- Language models for mission assistants are trained with token-level cross-entropy; their quality is reported as perplexity.
- Fault diagnosis: multi-class fault classifiers use class weights for rare failure modes and temperature scaling so their probabilities can be thresholded for alarms.
References
- J. S. Bridle (1990). “Probabilistic interpretation of feedforward classification network outputs.” In Neurocomputing, Springer.
- C. Szegedy, V. Vanhoucke, S. Ioffe, J. Shlens, Z. Wojna (2016). “Rethinking the Inception architecture for computer vision.” Proc. CVPR.
- C. Guo, G. Pleiss, Y. Sun, K. Q. Weinberger (2017). “On calibration of modern neural networks.” Proc. ICML.
- R. Müller, S. Kornblith, G. Hinton (2019). “When does label smoothing help?” Advances in NeurIPS 32.