Temperature Scaling
Temperature scaling divides a model's logits by one fitted value before softmax so its confidence matches its accuracy, with predictions unchanged.
Temperature scaling is a post-hoc calibration method that divides a trained classifier's logits by a single positive number T before the softmax, with T chosen on held-out data so that the model's confidence matches how often it is actually right. A temperature above 1 softens overconfident probabilities, and a temperature below 1 sharpens underconfident ones. Dividing every logit by the same positive number keeps their order, so the predicted class never changes and accuracy stays exactly the same. In the study "On Calibration of Modern Neural Networks", Guo and colleagues found modern deep networks to be poorly calibrated and temperature scaling, a single-parameter variant of Platt scaling, to be surprisingly effective at calibrating them on most of the datasets they tested.
How Temperature Scaling Works#
The scaled softmax gives class i the probability exp(z_i / T) / Σ_j exp(z_j / T), where z are the logits. At T = 1 the model is unchanged, as T grows the probabilities approach a uniform distribution, and as T approaches 0 the top class's probability approaches 1. T is fitted after training by minimizing the negative log-likelihood (NLL), the same cross-entropy used as the training loss, on a held-out validation set with the network's weights frozen. With a single parameter, the fit takes seconds and leaves little room to overfit the validation set. The PyTorch script below fits T for a model whose logits are three times too large:
import torch
import torch.nn.functional as F
# Held-out logits for 5,000 images and 10 classes from an overconfident model:
# labels are drawn so that softmax(logits / 3) is perfectly calibrated
torch.manual_seed(0)
logits = 9 * torch.randn(5000, 10)
labels = torch.multinomial(F.softmax(logits / 3, dim=1), 1).squeeze(1)
# Fit the single temperature that minimizes negative log-likelihood (NLL) on the held-out set
temperatures = torch.linspace(0.5, 6.0, 551)
nll = torch.stack([F.cross_entropy(logits / t, labels) for t in temperatures])
T = temperatures[nll.argmin()]
# Accuracy depends only on the argmax, which dividing by T never changes
accuracy = (logits.argmax(1) == labels).float().mean()
before = F.softmax(logits, dim=1).max(1).values.mean()
after = F.softmax(logits / T, dim=1).max(1).values.mean()
print(f"T = {T:.2f}, accuracy {accuracy:.3f}")
print(f"Mean confidence: {before:.3f} before, {after:.3f} after")The fit recovers T = 2.98, close to the true factor of 3. Accuracy stays at 0.663, while mean confidence falls from 0.880 to 0.664, so after scaling the model's average confidence matches its accuracy.
Why Networks Become Overconfident#
Cross-entropy training keeps rewarding larger logits on training examples the network already classifies correctly, so confidence keeps rising after accuracy on new data has leveled off. Guo and colleagues identified depth, width, weight decay, and batch normalization as important factors in miscalibration. Calibration is measured with reliability diagrams, which plot accuracy against confidence in bins, and with the expected calibration error (ECE), the average gap between confidence and accuracy across those bins, weighted by how many predictions fall in each. NLL and the Brier score also reward calibration. Calibration matters whenever a score is read as a probability, such as a medical triage rule that refers every case above 30% risk, a system that defers uncertain predictions to a person, or an active learning loop that picks the least confident images for labeling.
Temperature Scaling for Object Detectors#
Detectors usually score each class with an independent sigmoid instead of a softmax, and a temperature applies the same way, as sigmoid(z / T). With T above 1, scores move toward 0.5: high scores fall and low scores rise. In YOLO26, each box's confidence is the sigmoid of a class logit, compared against the conf threshold of 0.25 at prediction time, so a temperature fitted on held-out images changes which boxes clear that threshold while keeping the ranking of all boxes, which ranking metrics such as mean average precision (mAP) depend on.
Temperature in Other Settings#
- Large language models (LLMs): Sampling temperature divides each next-token logit vector by T in the same way, and users set it by hand to trade diversity against determinism. As T approaches 0, sampling approaches greedy decoding, which always picks the most likely token.
- Knowledge distillation: The distillation method of Hinton and colleagues raises the temperature of both teacher and student softmax outputs so the student learns the teacher's relative probabilities for the wrong classes.
- Contrastive learning: Models such as CLIP divide image-text similarity scores by a learned temperature before the softmax, which controls how sharply the loss separates matching pairs from the rest.
Temperature Scaling vs. Related Methods#
- Platt scaling: Fits a slope and an intercept to a binary classifier's score before a sigmoid. Temperature scaling extends it to many classes with one shared slope, 1 / T, and no intercept.
- Vector and matrix scaling: Learn a separate weight and bias for each class, or a full matrix over the logits. They are more flexible, overfit more easily, and can change the predicted class.
- Isotonic regression: A non-parametric, monotonic mapping from score to probability, available with Platt scaling in scikit-learn's calibration tools.
- Label smoothing: Softens the training targets, such as 0.9 instead of 1.0 for the true class, to reduce overconfidence during training. Temperature scaling changes only the outputs of a finished model.
- Conformal prediction: Turns scores into prediction sets that contain the true label at a guaranteed rate, such as 90% of the time, on exchangeable data.










