Notes

Temperature Scaling

Getting a classifier to be accurate is only half the story. When it says “90% confident,” you also want that to mean “it’s right about 9 times out of 10.” Temperature scaling is a simple way to fix overconfident or underconfident probability outputs without changing the model’s predicted class.

What it is (and what it changes)

Temperature scaling is a post-hoc calibration method for multi-class classifiers that output logits (the raw scores before softmax), such as neural networks. It introduces one learned scalar parameter T and rescales logits before converting them to probabilities:

p(y=k | x) = softmax(z_k / T)

If T > 1, the logits shrink and the predicted probabilities become less extreme (reducing overconfidence). If T < 1, probabilities become sharper (addressing underconfidence). Importantly, dividing all logits by the same T does not change which class has the largest probability, so accuracy stays the same; only the probability values change.

How it’s fit in practice

You fit T on a held-out validation set by minimizing negative log-likelihood (cross-entropy) using the model’s logits and true labels. This makes it lightweight and stable compared with more flexible calibrators. In PyTorch, it’s commonly implemented as a tiny wrapper module with one parameter optimized via LBFGS/Adam; conceptually it plays a similar role to scikit-learn’s probability calibration tools, but tailored to softmax logits.

Why it matters (real-world examples)

  • Medical diagnosis: a “95%” cancer risk should correspond to real observed frequencies for safe triage decisions.
  • Fraud detection: calibrated probabilities let you set thresholds based on expected cost, not gut feel.
  • Churn prediction: reliable probabilities improve ranking, budgeting, and intervention planning.

Without calibration, a model can look great on accuracy yet produce probabilities that mislead downstream decisions, risk estimates, and threshold tuning.

Temperature scaling is a post-hoc probability calibration method for classification that divides a model’s logits by a single learned positive scalar temperature before applying softmax, adjusting confidence without changing predicted classes. The temperature is fit on a validation set by minimizing negative log-likelihood. It matters because many supervised classifiers are miscalibrated; temperature scaling yields more reliable probabilities for thresholding, risk estimation, and decision-making.

Imagine a weather app that’s great at saying “rain” or “no rain,” but its confidence is off: it says “99% chance” way too often, even when it’s wrong. Temperature Scaling is a simple way to “turn the confidence dial” up or down so the percentages better match reality.

In supervised learning, many classifiers output probabilities like “this email is spam: 0.92.” If those numbers are too extreme (overconfident) or too timid (underconfident), decisions can suffer—especially in medicine or fraud detection. Temperature Scaling adjusts the model’s probability outputs after training, aiming to keep the same rankings while making the confidence more trustworthy.