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 be the answer the classifier picks, the probability it gives that answer, and the true answer.
- confidence
- The probability 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.
- 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 by confidence. ECE is the weighted mean, over bins, of the gap between the accuracy (the share right in the bin) and the mean confidence , weighted by the share of predictions in each bin. Its value changes with how the bins are made.
- 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.
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 acc | average confidence conf | gap | share |Bₘ| ÷ n | share × gap |
|---|
- 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". - 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. - 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. - 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.
- T = 1 (as is).
e^(−3) ≈ 0.0498,1 ÷ 1.0498 ≈ 0.953. Confidence 0.953. - T = 2. Divide first:
3 ÷ 2 = 1.5.e^(−1.5) ≈ 0.223,1 ÷ 1.223 ≈ 0.818. Confidence dropped to 0.818. - Same answer. If z is positive,
z ÷ Tstays 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
| Property | In plain words | In the experiment |
|---|---|---|
| Accuracy and calibration are different | Whether the answer is right and whether its probability is right are measured separately | Accuracy stays 0.745 for every T. ECE ranges from 0.015 to 0.205 |
| Temperature scaling keeps the answers and their order | Every 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 again | Try this ⑤ |
| Deep networks are often overconfident | The bars sag below the diagonal | The chart at the start (T = 1) |
| ECE depends on how you split the bins | Change the number of bins and the value changes. A bin with few predictions jumps around by luck | The second bin has only 16, so its accuracy of 0.375 swings a lot |
| ECE of 0 is not the whole story | Inside a bin only the average counts. It does not tell you whether each single prediction has the right probability | Step ② 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
- Temperature scaling. Divide the logits of a trained model by one number T. Pick T so the NLL on validation data is smallest (about 2.4 in the experiment). Answers and order stay the same.
- Platt scaling. Fit
p = 1 ÷ (1 + e^(−(a·s + b)))on top of the model's score s. It is a logistic regression with two numbers, a and b. Set a = 1/T and b = 0 and you get temperature scaling, so temperature scaling is Platt without the shift b. - Isotonic regression. Fit a staircase function to the validation data that only promises "a higher score never gets a lower probability". It assumes no shape, but it needs a lot of data.
- Label smoothing. While training, give the target as "right answer 1 − ε, the rest share ε" instead of 1 and 0 (label formats). The model stops pushing confidence all the way, so there is less overconfidence from training itself.
7. When to use what
| Option | Good | Bad | Use it when |
|---|---|---|---|
| Leave it alone | No work | You cannot trust the confidence numbers | You only pick the top answer or only use the order (ranking, AUC) |
| Temperature scaling | One number, so little validation data is enough. Answers and order stay the same. No retraining | It can only stretch or shrink all confidences by one rate. It cannot fix bins that are off in different directions | The first thing to try on a neural network classifier |
| Platt scaling | Two numbers, so it also fixes a shift. Works for models that only give scores | Assumes the error has an S shape. Because of b, the answer at the 0.5 cutoff can change | Two answers, and a model whose scores are not probabilities (support vector machines and similar) |
| Isotonic regression | Assumes no shape, so it fits any kind of error | Overfits when validation data is small. Being a staircase, it lumps many scores into the same probability and loses some of the order | You have a lot of validation data |
| Label smoothing | Confidence grows less during training. No separate fitting step | Needs retraining. You must pick ε. There are reports that it hurts when the model is used as a teacher for knowledge distillation | Training 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
- Deciding what goes to a person. When only low-confidence predictions are sent to a human, the confidence has to be right before you can predict how many get sent.
- Costs from probabilities. Decisions that multiply "how much a mistake costs" by a probability only make sense if the probability matches reality.
- Combining models. If one model is overconfident, the combined result gets pulled toward it.
- Showing a probability on screen. People read "90%" literally.
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 here | Textbook term |
|---|---|
| How well confidence matches reality | calibration |
| Probability of the chosen answer | confidence |
| Chart with an accuracy bar per bin | reliability diagram |
| Per-bin gaps added up by bin share | expected calibration error (ECE) |
| A group split by confidence | bin |
| Right less often / more often than it says | overconfidence / underconfidence |
| The score before it becomes a probability | logit |
| Average −log of the probability given to the real answer | negative log-likelihood (NLL), cross-entropy |
| Dividing the logits by one number | temperature scaling |
| Fitting a logistic regression on the scores | Platt scaling |
| Fitting a staircase that never goes down | isotonic regression |
| Giving the right answer 1 − ε instead of 1 | label smoothing |
| Data kept out of training and used for fitting this | validation set |
Sources
- Chuan Guo, Geoff Pleiss, Yu Sun, Kilian Q. Weinberger, On Calibration of Modern Neural Networks, arXiv:1706.04599. Reliability diagrams, the ECE formula, overconfidence in deep networks and what drives it, and temperature scaling compared with Platt and isotonic.
- scikit-learn docs: Probability calibration. Calibration curves, sigmoid (Platt) and isotonic in
CalibratedClassifierCV, and isotonic overfitting on small data. - Rafael Müller, Simon Kornblith, Geoffrey Hinton, When Does Label Smoothing Help?, arXiv:1906.02629. Label smoothing improves calibration and hurts knowledge distillation.