Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Turning down the confidence
A network’s raw scores (logits) go through a softmax to become probabilities. If the scores are all too large, the probabilities come out too extreme: 99% when it should be 80%. Temperature scaling divides all the scores by one number T before the softmax. With T above 1 the probabilities soften, but the ranking of classes stays the same, so the predicted class never changes. T is chosen on held-out data so that the probabilities match reality as well as possible. Move the slider in the demo to see the loss fall and rise again around the best T.
A landing-site hazard classifier on a planetary lander is trained on simulated imagery. A temperature fitted on held-out simulated data makes its 0.9 confidences mean 90% there. Under real illumination it is overconfident again, so recalibrate on the target domain or monitor.
- 1. Temperature scaling divides the logits by…
- 2. Does temperature scaling change the predicted class?
- 3. An overconfident network needs…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Temperature scaling: p = softmax(z / T). T is fitted by minimising NLL on a held-out calibration set, never the training set (where the network is overfit and overconfident).
- Why one parameter works: overconfidence in modern networks comes largely from overfitting the NLL late in training (Guo et al., 2017), which inflates the logit scale; dividing by T undoes most of it.
- Richer maps: vector scaling z·w + b (2K parameters), matrix scaling zW + b (K² + K) and Dirichlet calibration (Kull et al., 2019; matrix scaling on log-probabilities with off-diagonal regularisation). These can fix class-specific bias but can change accuracy and overfit.
- Ensembles and test-time augmentation: averaging makes predictions less confident, sometimes underconfident; fit the temperature after averaging (Rahaman and Thiery, 2021).
- Training-time effects: label smoothing, mixup and focal loss also change calibration; temperature scaling on top is still standard.
- Under shift: Ovadia et al. (2019) found that post-hoc calibration on in-distribution data does not hold under corruption or domain shift; the needed T grows with the shift.
- LLMs: pretrained LLMs’ token probabilities are fairly well calibrated on multiple-choice questions (Kadavath et al., 2022), and RLHF tends to make them overconfident (GPT-4 technical report, 2023). Temperature scaling of answer probabilities is a common fix. The decoding temperature used for sampling is a separate setting.
- 1. T is fitted on…
- 2. Matrix scaling for K classes has…
- 3. After averaging an ensemble you should…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Fitting the temperature
Exercise 1.
- A numerically stable softmax with temperature.
- The NLL at a given temperature.
- Newton’s method on β = 1/T, using the derivative E_p[z] − z_y and the second derivative Var_p(z).
- Top-label ECE before and after.
Practice.
- Keep a calibration split (often the validation set) separate from the test set; 1000 to 5000 examples is plenty for one parameter.
- Optimise log T or β = 1/T with L-BFGS or Newton; the problem is one-dimensional and convex in β.
- Check that accuracy is unchanged and that NLL and ECE fall on the test set.
- Refit T for each deployment domain and after averaging an ensemble.
- Libraries: torch-uncertainty and netcal provide temperature, vector, matrix and Dirichlet scaling; the PyTorch snippet below is all that is needed.
import torch
logits_cal, y_cal = model_logits(cal_loader) # held-out calibration set, logits (N, K)
log_T = torch.zeros(1, requires_grad=True)
opt = torch.optim.LBFGS([log_T], lr=0.1, max_iter=200)
def closure():
opt.zero_grad()
loss = torch.nn.functional.cross_entropy(logits_cal / log_T.exp(), y_cal)
loss.backward()
return loss
opt.step(closure)
T = log_T.exp().item() # use softmax(logits / T) at test time- 1. Optimising log T instead of T…
- 2. A good calibration set size for temperature scaling:
- 3. After temperature scaling, accuracy should be…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Convexity in β = 1/T. NLL(β) = (1/N) Σ_i [log Σ_k exp(β z_ik) − β z_iy]. The log-sum-exp of a linear function of β is convex, and −β z_iy is linear, so NLL is convex in β. Its derivative is (1/N) Σ_i (E_p[z_i] − z_iy), the gap between the expected logit under the model and the true class’s logit. Its second derivative is (1/N) Σ_i Var_p(z_i) ≥ 0. Newton’s method converges in a handful of steps.
Accuracy is invariant. Dividing by T > 0 is strictly monotone, so the arg max and the full ranking of classes are unchanged. Temperature scaling changes the confidence, not the decision. Top-k accuracy and AUROC computed on the top-label confidence are unchanged too.
What one parameter cannot do. A constant shift of one class’s logit (a class prior bias) changes the ranking and cannot be represented by z/T. Vector scaling can represent it with one bias per class. Matrix scaling can represent any affine map, at the cost of K² + K parameters that need far more data, so regularise them (Dirichlet calibration uses off-diagonal and intercept penalties).
Calibration is relative to a distribution. The NLL-optimal T depends on how often the confident predictions are wrong. When inputs are corrupted, the model’s logits stay large while its accuracy falls, so the optimal T grows. A temperature fitted on clean data undercorrects.
- 1. The second derivative of the NLL in β is…
- 2. Temperature scaling cannot fix…
- 3. Under corruption the optimal temperature usually…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.When is one temperature enough, and when does a richer calibrator or a shift break it?
History.
- J. Platt (1999) fitted a sigmoid to SVM scores; temperature scaling is its multiclass, one-parameter descendant.
- G. Hinton, O. Vinyals and J. Dean (2015) used temperature in distillation; C. Guo et al. (2017) showed modern networks are overconfident and temperature scaling fixes most of it.
- M. Kull et al. (2019) Dirichlet calibration; Y. Ovadia et al. (2019) calibration under dataset shift; M. Minderer et al. (2021) revisited calibration of modern architectures.
Exercise 2. A simulated 10-class model.
- Truth: true logits g ~ N(0, 3²) per class; labels drawn from softmax(g).
- Model: logits 3g + N(0, 1.5²) noise + 4 on class 0. They are three times too large and biased towards class 0.
- Calibrators: temperature (Newton), vector and matrix scaling (gradient descent starting from the temperature solution). They are fitted on 100 or 1000 calibration examples and tested on 5000. Corrupted test set: logit noise N(0, 5²).
| 1000 calibration examples | accuracy | test NLL | test ECE |
|---|---|---|---|
| uncalibrated | 0.621 | 1.980 | 0.264 |
| temperature (T = 3.47) | 0.621 | 1.054 | 0.014 |
| vector scaling | 0.638 | 1.013 | 0.016 |
| matrix scaling | 0.624 | 1.050 | 0.031 |
| 100 calibration examples | accuracy | calibration-set NLL | test NLL | test ECE |
|---|---|---|---|---|
| temperature (T = 3.11) | 0.621 | 0.957 | 1.060 | 0.039 |
| vector scaling | 0.614 | 0.786 | 1.136 | 0.098 |
| matrix scaling | 0.453 | 0.231 | 4.210 | 0.430 |
| corrupted test set | NLL | ECE |
|---|---|---|
| uncalibrated | 3.796 | 0.406 |
| temperature from clean data (T = 3.47) | 1.528 | 0.168 |
| best temperature on corrupted data | T = 5.56 |
- One parameter did most of the work: ECE 0.264 → 0.014 and NLL 1.98 → 1.05 with accuracy unchanged. T = 3.47 is near the true inflation factor 3; the extra logit noise makes a larger T optimal.
- Vector scaling helped with enough data: its per-class bias removed the class-0 preference, gaining 1.7 points of accuracy and lowering NLL. With 100 examples it was worse than temperature scaling on all three seeds.
- Matrix scaling overfit: with 110 parameters and 100 examples its calibration-set NLL (0.231) fell far below the truth while test NLL rose to 4.21, worse than not calibrating. With 1000 examples it was about as good as temperature scaling.
- Shift broke the calibration: on corrupted inputs, ECE was still 0.168 after temperature scaling; the corrupted data wanted T = 5.56.
- Seeds 1 and 2: T = 3.43 and 3.46 with 1000 examples; vector-scaling NLL 1.010 and 1.026 against temperature 1.039 and 1.047; matrix scaling with 100 examples gave test NLL 3.137 and 3.656; corrupted ECE 0.163 and 0.162 with best T 5.49 and 5.41.
Temperature scaling in AI, robotics and aerospace
- Perception for autonomy: detection and segmentation networks are temperature-scaled per class or per output head before their scores enter trackers and occupancy grids.
- Ensembles in safety cases: deep ensembles plus a fitted temperature are a strong, cheap baseline for uncertainty in flight-software ML components.
- LLM assistants: answer probabilities are temperature-scaled on a held-out QA set before they drive abstention or escalation.
References
- C. Guo, G. Pleiss, Y. Sun and K. Q. Weinberger (2017). “On calibration of modern neural networks.” ICML.
- M. Kull et al. (2019). “Beyond temperature scaling: obtaining well-calibrated multiclass probabilities with Dirichlet calibration.” NeurIPS.
- Y. Ovadia et al. (2019). “Can you trust your model’s uncertainty? Evaluating predictive uncertainty under dataset shift.” NeurIPS.
- M. Minderer et al. (2021). “Revisiting the calibration of modern neural networks.” NeurIPS.
- S. Kadavath et al. (2022). “Language models (mostly) know what they know.” arXiv.