Topic 61 of 62 · Wider ML toolkit
Calibration and temperature scaling
MediumWhat does it mean for a model to be calibrated, how do you measure it, and how do you fix it?
1 · Start with the intuition
A calibrated model’s confidence matches its hit rate: of all predictions made with 80% confidence, about 80% are correct. Accuracy says how often you are right; calibration says whether your stated confidence can be trusted.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Bin by confidence
Group predictions by their top-class probability.
Next, we will look at: Compare with accuracy. Move on when this operation makes sense.
Visualizing the behavior
Reliability diagram for the worked example
Derived- perfect calibration
- model
3 · The governing equation
ECE = Σ_b (n_b / N) · |acc(b) − conf(b)| Temperature scaling: p = softmax(z / T), T fit on validation NLL
Notation: n_b is the number of predictions in bin b. ECE depends on the number and placement of bins, so report the binning scheme.
Guo et al. found modern deep networks are often overconfident and that temperature scaling, a single parameter, fixes much of it. Because dividing logits by a positive T preserves their order, accuracy is unchanged. Calibration can shift under distribution change, so recalibrate on data resembling deployment. For LLMs, post-training such as RLHF has been reported to worsen the calibration of answer probabilities compared with the pretrained model.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
“Higher accuracy means better-calibrated probabilities.” The two are separate: a highly accurate model can be badly overconfident, and temperature scaling fixes calibration without changing accuracy.
In practice · implementation & engineering rule of thumb
Fit a single scalar temperature T > 0 on a held-out validation set using L-BFGS on cross-entropy loss—it improves calibration in seconds without changing argmax accuracy.
log_T = nn.Parameter(torch.zeros(1, device=val_logits.device))
opt = torch.optim.LBFGS([log_T], lr=0.1, max_iter=50)
def closure():
opt.zero_grad()
loss = F.cross_entropy(val_logits / log_T.exp(), val_labels)
loss.backward()
return loss
opt.step(closure)
calibrated_probs = F.softmax(test_logits / log_T.exp(), dim=-1)Go one level deeper
Follow-ups that often come after a correct first answer.
- Proper scoring rules
- Log loss and the Brier score are minimized in expectation only by the true probabilities, so they cannot be gamed by hedging. Use them alongside ECE, which is not a proper scoring rule.
- Calibration for selective prediction
- Calibrated confidence lets you abstain below a threshold and trace a risk–coverage curve: error rate against the fraction of inputs answered. This is often more decision-relevant than a single ECE number.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy is NLL a better model-selection metric than ECE alone?
Show follow-up reasoning
NLL is a proper scoring rule: it is minimized only by the true probabilities, so it rewards both accuracy and calibration. ECE can be near zero for a useless model that always predicts the base rate.