Can we trust a model's confidence score?
This project investigates neural network calibration and applies temperature scaling to fix overconfidence — without retraining.
A model that outputs "95% confidence" should be correct 95% of the time when it says that. Most modern neural networks are not — they tend to be overconfident, outputting high confidence even when wrong.
Calibration measures how well a model's confidence scores match its actual accuracy.
Temperature scaling is the simplest post-hoc calibration fix. It adds a single scalar T to the model's output:
p = softmax(logits / T)
T > 1→ softens the distribution → less confidentT = 1→ no change (original model)T < 1→ sharpens the distribution → more confident
We find the best T by minimising the Negative Log-Likelihood on a held-out validation set.
| Metric | Description |
|---|---|
| ECE (Expected Calibration Error) | Average gap between confidence and accuracy across bins. Lower is better. |
| MCE (Maximum Calibration Error) | Worst-case gap across all confidence bins. |
model-calibration-study/
│
├── main.py # Entry point — runs the full study
│
├── models/
│ ├── __init__.py
│ └── model_loader.py # ResNet-50 adapted for CIFAR-10
│
├── utils/
│ ├── __init__.py
│ ├── data_loader.py # CIFAR-10 download + train/val/test split
│ ├── calibration_metrics.py # ECE, MCE, reliability data computation
│ ├── temperature_scaling.py # TemperatureScaler class
│ └── plotting.py # Reliability diagrams, histograms, bar charts
│
├── results/ # Auto-created — plots saved here
├── data/ # Auto-created — CIFAR-10 downloaded here
│
├── requirements.txt
└── README.md
git clone https://github.com/YOUR_USERNAME/model-calibration-study.git
cd model-calibration-study
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtpython main.pyThis will:
- Download CIFAR-10 (~170 MB, automatic)
- Load a pretrained ResNet-50
- Collect logits on val + test sets
- Compute ECE and MCE before calibration
- Fit temperature scaling on the validation set
- Compute ECE and MCE after calibration
- Save plots to
results/ - Print a summary table
After running, you will find three plots in results/:
| File | Description |
|---|---|
reliability_diagram.png |
Bar chart: accuracy per confidence bin, before vs after |
confidence_histogram.png |
Distribution of model confidence scores |
ece_comparison.png |
ECE and MCE before vs after, side by side |
Calibration is critical in high-stakes applications:
- Medical AI: "90% confidence it's benign" should mean something precise
- Autonomous vehicles: Uncertainty must be trustworthy
- Any system where humans act on model confidence
- Guo et al. (2017) — On Calibration of Modern Neural Networks (the foundational paper for this project)
- Niculescu-Mizil & Caruana (2005) — Predicting Good Probabilities with Supervised Learning
Python · PyTorch · torchvision · matplotlib · NumPy · SciPy