Calibration: can you trust the model's confidence

A classifier gives an answer and a probability: "cat, 90%". Now collect only the predictions where it said 90%. Are nine out of ten of them really right? Draw the picture with 200 made-up predictions, then fix it with one number, the temperature.

Short answer. Calibration asks whether the confidence a model states matches how often it is actually right. If 90% of the predictions it calls 90% sure are right, it is well calibrated. Modern deep networks are often more confident than they should be. Dividing the logits by one number T (temperature scaling) fixes much of this, and it does not change accuracy. Accuracy and calibration are different things.

This is a small toy. Instead of a real model it uses 200 made-up predictions (each picks one of two answers) from a fixed-seed random generator. We set a "true" probability first, then blew up the model's logits 2.5 times to make it overconfident on purpose. In real work you pick T on validation data and measure ECE on a separate test set. Here both use the same 200 predictions. Blue bars are accuracy, green lines are average confidence, orange boxes are the gap.

Try this. ① At the start (T = 1), accuracy is 0.745, average confidence is 0.862, and ECE is 0.135. 106 predictions crowd into the last bin, above 0.9. Their average confidence is 0.972, but their accuracy is 0.858. ② Lower T to 0.5. Average confidence grows to 0.928 and ECE gets worse, 0.205. ③ Raise T and the bars move onto the diagonal. ECE is 0.072 at T = 1.5, 0.02 at 2, and 0.015 at 2.4, the smallest on the slider's 0.1 steps. NLL is also smallest near 2.4 (0.523). We made the data with logits 2.5 times too big, but with only 200 predictions some luck mixes in, so 2.4 comes out. ④ Raise T to 5. Average confidence 0.636 drops below accuracy 0.745 and the bars rise above the diagonal (underconfident, ECE 0.109). ⑤ Accuracy stays at 0.745 the whole time.

Definitions

The text below explains these with 200 made-up predictions. These are the textbook definitions. For each input, let Y^ be the answer the classifier picks, P^ the probability it gives that answer, and Y the true answer.

confidence
The probability P^ that a classifier gives the answer it picks, that is, the class with the largest probability.
calibration
The property that a model's confidence equals the share of its predictions that are really right. The model is perfectly calibrated if the following holds for every p.

P(Y^=Y∣P^=p)=p,p∈[0,1]

overconfidence, underconfidence
Confidence higher than the share that is really right is overconfidence. Confidence lower than that share is underconfidence.
reliability diagram
A chart that splits predictions into bins by confidence and draws, for each bin, the share that is right (the accuracy) as a bar. For a perfectly calibrated model the bars sit on the diagonal (confidence = accuracy).
expected calibration error (ECE)
Split n predictions into M bins B1,…,BM by confidence. ECE is the weighted mean, over bins, of the gap between the accuracy acc(Bm) (the share right in the bin) and the mean confidence conf(Bm), weighted by the share of predictions in each bin. Its value changes with how the bins are made.

ECE=∑m=1M|Bm|n|acc(Bm)−conf(Bm)|

temperature scaling
A calibration method that divides all logits 𝐳 of a trained model by one positive number T (the temperature) before the softmax. T is chosen to make the cross-entropy (NLL) smallest on the validation set. The order of the logits does not change, so the picked answer and the accuracy stay the same.

𝐩^=softmax(𝐳T),T>0

1. What calibration means: "of the ones called 90%, are 90% right?"

A classifier gives each answer a score (a logit), turns the scores into probabilities, and picks the biggest. The probability of the chosen answer is the confidence. A model is well calibrated when you can take that confidence at face value. Gather every prediction with confidence 0.8, and 80% of them should be right. As a formula: P(right | confidence = p) = p.

The picture for this is the reliability diagram. Split the predictions into bins by confidence, and for each bin draw a bar as tall as the share that was right. For a well calibrated model the tops of the bars sit on the diagonal (confidence = accuracy). A bar below the diagonal means it is right less often than it says (overconfident). Above means it is right more often than it says (underconfident).

The experiment has two possible answers, so the chosen answer always has probability 0.5 or more. Split 0 to 1 into ten bins of 0.1 and the lower five are always empty, leaving five bins from 0.5 to 1. With K answers, confidence starts at 1/K.

Accuracy and calibration are different things. Accuracy only counts whether the chosen answer was right. Calibration checks whether the number attached to it was right. A model that always says "heads, 50%" for a fair coin is only 50% accurate, yet perfectly calibrated. In the experiment, ECE moves from 0.205 to 0.015 as you change T, while accuracy stays at 0.745.

2. How the formula is built: expected calibration error (ECE)

The expected calibration error (ECE) squeezes a reliability diagram into one number. We use the same 200 predictions as above, at T = 1 (the model as it is). Press "Next" and the formula gains one piece at a time (orange background marks the new piece), while the chart and the table fill in.

bin (confidence)count |Bₘ|accuracy accaverage confidence confgapshare |Bₘ| ÷ nshare × gap

  1. Split by confidence: B₁, …, B₅. Put the n = 200 predictions into five bins by confidence. Bin Bₘ holds the predictions whose confidence falls in (0.5, 0.6], (0.6, 0.7], and so on. The counts are 17, 16, 25, 36 and 106. Why: almost no prediction has a confidence of exactly 0.8, so you group similar confidences together before you can count "what share of them was right".
  2. Accuracy and average confidence per bin: acc(Bₘ), conf(Bₘ). acc is the share that was right inside the bin, conf is the average confidence inside the bin. In the last bin, conf is 0.972 and acc is 0.858. Why: to compare "what it said" with "what happened", both have to be in the same unit (a share) for the same group.
  3. The gap: |acc(Bₘ) − conf(Bₘ)|. The five gaps are 0.107, 0.283, 0.109, 0.166 and 0.114. Why: dropping the sign stops a bin that is too sure and a bin that is not sure enough from canceling out. In the first bin accuracy (0.647) is higher than confidence (0.54). Keep the sign and that bin would eat into the error of the others.
  4. Weight by bin size and add: ECE = Σₘ (|Bₘ| ÷ n) × |acc(Bₘ) − conf(Bₘ)|. |Bₘ| is the number of predictions in the bin. The shares are 0.085, 0.08, 0.125, 0.18 and 0.53. Multiplied by the gaps they give 0.009, 0.023, 0.014, 0.03 and 0.06. The sum is ECE = 0.135 (the table shows rounded numbers, which add up to 0.136). Why: the error in a bin with 106 predictions happens to more predictions than the error in a bin with 16. With the shares, ECE becomes "pick any one prediction: on average, how far apart are its bin's confidence and accuracy".

This is the same formula as in the paper by Guo and others (see sources). Change the number of bins and the ECE changes too, so when you compare ECE values, check that the bins were split the same way.

3. By hand: lowering confidence with temperature

With two answers the probability is p = 1 ÷ (1 + e^(−z ÷ T)) (the sigmoid from logistic regression). z is the logit, T is the temperature. Take one prediction with logit z = 3.

  1. T = 1 (as is). e^(−3) ≈ 0.0498, 1 ÷ 1.0498 ≈ 0.953. Confidence 0.953.
  2. T = 2. Divide first: 3 ÷ 2 = 1.5. e^(−1.5) ≈ 0.223, 1 ÷ 1.223 ≈ 0.818. Confidence dropped to 0.818.
  3. Same answer. If z is positive, z ÷ T stays positive for any positive T, so the probability stays above 0.5. The chosen answer does not change.

T above 1 flattens the probabilities toward 0.5. T below 1 sharpens them toward 0 or 1. With many answers you divide every logit by T before the softmax, and the largest logit stays the largest. It is the same math as the temperature a language model uses when it picks the next word. There it sets how varied the picks are. Here it lines confidence up with reality.

4. Key properties

PropertyIn plain wordsIn the experiment
Accuracy and calibration are differentWhether the answer is right and whether its probability is right are measured separatelyAccuracy stays 0.745 for every T. ECE ranges from 0.015 to 0.205
Temperature scaling keeps the answers and their orderEvery logit is divided by the same positive number, so big stays big. The ROC curve and AUC, and the precision-recall curve, stay the same. But a cutoff like "pass only above 0.8" has to be set againTry this ⑤
Deep networks are often overconfidentThe bars sag below the diagonalThe chart at the start (T = 1)
ECE depends on how you split the binsChange the number of bins and the value changes. A bin with few predictions jumps around by luckThe second bin has only 16, so its accuracy of 0.375 swings a lot
ECE of 0 is not the whole storyInside a bin only the average counts. It does not tell you whether each single prediction has the right probabilityStep ② in section 2

5. Why deep networks get overconfident

Networks usually train to lower the cross-entropy (NLL). This loss keeps shrinking as you push the confidence of a right answer closer to 1. So even after the model gets almost all the training data right, it keeps growing its logits to raise confidence, and that confidence does not hold on data it has not seen. Guo and others showed test error still going down while test NLL goes back up, and called this "overfitting to NLL" (overfitting). The same paper showed in experiments that calibration tends to get worse with deeper and wider networks, with batch normalization, and with less weight decay. Choices that help accuracy can work against calibration.

6. How to fix it

7. When to use what

OptionGoodBadUse it when
Leave it aloneNo workYou cannot trust the confidence numbersYou only pick the top answer or only use the order (ranking, AUC)
Temperature scalingOne number, so little validation data is enough. Answers and order stay the same. No retrainingIt can only stretch or shrink all confidences by one rate. It cannot fix bins that are off in different directionsThe first thing to try on a neural network classifier
Platt scalingTwo numbers, so it also fixes a shift. Works for models that only give scoresAssumes the error has an S shape. Because of b, the answer at the 0.5 cutoff can changeTwo answers, and a model whose scores are not probabilities (support vector machines and similar)
Isotonic regressionAssumes no shape, so it fits any kind of errorOverfits when validation data is small. Being a staircase, it lumps many scores into the same probability and loses some of the orderYou have a lot of validation data
Label smoothingConfidence grows less during training. No separate fitting stepNeeds retraining. You must pick ε. There are reports that it hurts when the model is used as a teacher for knowledge distillationTraining from scratch. You can still add temperature scaling after it

In scikit-learn, CalibratedClassifierCV lets you pick Platt (method="sigmoid") or isotonic (method="isotonic"), and its docs say isotonic tends to overfit when there is little data.

Where it is used

Questions people ask

If accuracy is high, isn't the model calibrated too?

No. Accuracy only counts whether the chosen answer was right. Calibration checks whether the probability attached to it was right. A model that always says 99% and is right 90% of the time has high accuracy but poor calibration. A model that always says "heads, 50%" for a fair coin is only 50% accurate, yet perfectly calibrated.

Does temperature scaling change accuracy?

No. Divide every logit by the same positive T and the largest logit stays the largest. The chosen answer is the same, so accuracy is the same. Only the size of the confidence, the NLL and the ECE change. In the experiment, accuracy stays at 0.745 wherever you put T.

Which data do you calibrate on?

Fit it on validation data the model never trained on, and measure ECE on a separate test set. On the training data the model gets almost everything right, so its high confidence looks correct and there is nothing to fix.

Words

Plain words used hereTextbook term
How well confidence matches realitycalibration
Probability of the chosen answerconfidence
Chart with an accuracy bar per binreliability diagram
Per-bin gaps added up by bin shareexpected calibration error (ECE)
A group split by confidencebin
Right less often / more often than it saysoverconfidence / underconfidence
The score before it becomes a probabilitylogit
Average −log of the probability given to the real answernegative log-likelihood (NLL), cross-entropy
Dividing the logits by one numbertemperature scaling
Fitting a logistic regression on the scoresPlatt scaling
Fitting a staircase that never goes downisotonic regression
Giving the right answer 1 − ε instead of 1label smoothing
Data kept out of training and used for fitting thisvalidation set

Sources

← Hyperparameter search: grid and randomQuantization →