Foundations to Frontier
Tests
I. Probabilistic ML and Uncertainty Quantification › I4 Calibration and Conformal Prediction

Temperature scaling and its relatives

Modern neural networks are usually overconfident: their softmax probabilities are too close to 0 and 1. Temperature scaling (Guo et al., 2017) fixes this with one number. Divide every logit by a temperature T > 1, chosen to minimise the negative log-likelihood on held-out data. Because one scalar cannot reorder the logits, accuracy is unchanged, and because there is only one parameter, a few hundred examples are enough. This page covers why the NLL is convex in 1/T and how to fit it with Newton's method. It compares temperature scaling with its richer relatives, vector scaling, matrix scaling and Dirichlet calibration, and explains why a temperature fitted on clean data does not transfer to shifted data. It also covers temperature in LLMs and in ensembles. In the exercises you implement the fit and run all three calibrators on a simulated 10-class model with logits three times too large plus a class bias. With 1000 calibration examples temperature scaling cut ECE from 0.264 to 0.014 (T = 3.47). Vector scaling also fixed the class bias, lowering NLL to 1.013 from 1.054. With 100 examples, matrix scaling (110 parameters) overfit badly, reaching test NLL 4.21. On corrupted inputs the in-distribution temperature left ECE at 0.168.

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.

Calibration, conformal and shift lab
Test NLL (green) and ECE (red), each scaled to its maximum, against T from 0.5 to 5.4. At T = 1: NLL 2.818, ECE 0.374. The logits are three times too large, so the best T is near 3; accuracy does not change with T.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Temperature scaling divides the logits by…
  2. 2. Does temperature scaling change the predicted class?
  3. 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.
📝 Quick check (Level 1)
0/3 answered
  1. 1. T is fitted on…
  2. 2. Matrix scaling for K classes has…
  3. 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
🐍 Exercise 1: fit a temperature
Write a stable softmax with temperature, the NLL, a Newton fit of the temperature and the top-label ECE.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Optimising log T instead of T…
  2. 2. A good calibration set size for temperature scaling:
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The second derivative of the NLL in β is…
  2. 2. Temperature scaling cannot fix…
  3. 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.
🐍 Exercise 2 (research): when is one temperature enough, and when does a richer calibrator or a shift break it?
Fit temperature, vector and matrix scaling on 100 or 1000 calibration examples from a simulated overconfident 10-class model; compare test accuracy, NLL and ECE, including on corrupted inputs.

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

  1. C. Guo, G. Pleiss, Y. Sun and K. Q. Weinberger (2017). “On calibration of modern neural networks.” ICML.
  2. M. Kull et al. (2019). “Beyond temperature scaling: obtaining well-calibrated multiclass probabilities with Dirichlet calibration.” NeurIPS.
  3. Y. Ovadia et al. (2019). “Can you trust your model’s uncertainty? Evaluating predictive uncertainty under dataset shift.” NeurIPS.
  4. M. Minderer et al. (2021). “Revisiting the calibration of modern neural networks.” NeurIPS.
  5. S. Kadavath et al. (2022). “Language models (mostly) know what they know.” arXiv.