Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A mistake score
To teach a model, give it a loss: one number that says how wrong it is. Then ask, for each of the model’s predictions, “which way should I nudge this to be less wrong, and how hard?” That’s the gradient of the loss.
For many popular losses the answer is surprisingly simple: nudge by (prediction − target). If you guessed 7 and the answer was 4, push down by 3.
A robot learning to grasp gets a loss for every failed attempt, and a navigation model gets one for every wrong turn it predicts. The gradient of that loss is the only signal that tells millions of numbers inside the model how to change.
- 1. A loss is…
- 2. Predicted 7, target 4. The error (prediction − target) is…
- 3. To reduce that error, nudge the prediction…
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.Three losses, three gradients
| Loss | Formula (one example) | Gradient |
|---|---|---|
| mean squared error | ||
| binary cross-entropy on a logit | ||
| softmax cross-entropy on logits |
All three have the form prediction − target, measured at the right place (the value, the probability, or the probability vector).
Example. Logits give . If the true class is the third, the gradient is . Descent lowers the first two logits and raises the third.
- 1. MSE gradient for ŷ = 5, y = 2?
- 2. BCE gradient at z = 0 with y = 1?
- 3. Softmax-CE gradient for the true class with p = 0.8?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.More losses
| Loss | Gradient w.r.t. prediction | Behaviour |
|---|---|---|
| MSE | outliers dominate | |
| MAE | robust, but not smooth at 0 | |
| Huber () | if , else | best of both (Huber, 1964) |
| Gaussian NLL | , | learns its own uncertainty |
Over a whole dataset (vectorized)
- Linear regression: gives .
- Logistic regression: .
- Softmax regression: .
They share the same structure: the data matrix transposed, times prediction minus target (A3.3-2).
Implement them stably
Use logaddexp(0, z) - y·z for BCE, subtract the max inside softmax, and compute directly. Never take of a probability that may have rounded to 0 (A3.1-3).
- 1. The MAE gradient for ŷ = 1, y = 4?
- 2. The softmax-CE gradients over all logits sum to…
- 3. Logistic regression: ∇w = ?
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Deriving p − y
Softmax has Jacobian , i.e. . For :
∎ The same simplification holds for every canonical link in a generalized linear model. The gradient of the log-partition function is the mean, so the negative log-likelihood’s gradient is “mean − observation” (McCullagh and Nelder, 1989).
Why cross-entropy for classifiers
With MSE on a sigmoid, . The extra factor is tiny when is large, so a confidently wrong model barely learns. Cross-entropy cancels that factor exactly. It’s also the maximum-likelihood loss for a Bernoulli or categorical model.
Convexity
The logistic loss is convex in , with Hessian . So logistic and softmax regression have a single global optimum (A3.2-6). Deep networks lose this property through their hidden layers, not through the loss.
Exercise 3.1: Show the BCE-with-logits gradient is σ(z) − y
. Then .
- 1. The MSE-on-sigmoid gradient at z = 0, y = 1?
- 2. Cross-entropy is preferred for classification because…
- 3. The logistic-regression loss is…
Level 4: Expert
★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.Losses that model uncertainty
A robot policy or perception model can predict a mean and a variance, and train with the Gaussian negative log-likelihood (Kendall and Gal, 2017; Lakshminarayanan, Pritzel and Blundell, 2017). Its gradient automatically down-weights noisy examples () and grows where errors are large. Plain gradient descent on it is unstable, because steps on scale like . Fisher-preconditioned (natural-gradient) steps fix this, as do practical tricks like the “β-NLL” (Seitzer et al., 2022).
The same pattern everywhere
- Contrastive learning. InfoNCE (Oord, Li and Vinyals, 2018; CLIP, Radford et al., 2021) is softmax cross-entropy over similarities, with gradient . It pulls the positive pair together and pushes hard negatives away hardest (Practice Code 5).
- Policy gradients. For a softmax policy, , scaled by the advantage. It is the same “target − prediction” shape, which is why RL updates look like weighted classification.
- Focal loss (Lin et al., 2017) multiplies cross-entropy by to focus on hard examples, improving detectors with heavy class imbalance (like rare obstacles).
- Knowledge distillation uses a soft target vector, giving gradient (Hinton, Vinyals and Dean, 2015).
References
- P. Huber (1964). “Robust estimation of a location parameter.” Annals of Mathematical Statistics, 35(1).
- P. McCullagh, J. Nelder (1989). Generalized Linear Models, 2nd ed. Chapman & Hall.
- G. Hinton, O. Vinyals, J. Dean (2015). “Distilling the knowledge in a neural network.” NeurIPS Deep Learning Workshop.
- A. Kendall, Y. Gal (2017). “What uncertainties do we need in Bayesian deep learning for computer vision?” NeurIPS.
- B. Lakshminarayanan, A. Pritzel, C. Blundell (2017). “Simple and scalable predictive uncertainty estimation using deep ensembles.” NeurIPS.
- T.-Y. Lin, P. Goyal, R. Girshick, K. He, P. Dollár (2017). “Focal loss for dense object detection.” ICCV.
- A. van den Oord, Y. Li, O. Vinyals (2018). “Representation learning with contrastive predictive coding.” arXiv:1807.03748.
- A. Radford et al. (2021). “Learning transferable visual models from natural language supervision.” ICML.
- M. Seitzer, A. Tavakoli, D. Antic, G. Martius (2022). “On the pitfalls of heteroscedastic uncertainty estimation with probabilistic neural networks.” ICLR.