NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Post-hoc Calibration of Neural Networks

Amir Rahimi, Kartik Gupta, Thalaiyasingam Ajanthan, Thomas Mensink, Cristian Sminchisescu, Richard HartleyarXiv 2020

Abstract: Calibration of neural networks is a critical aspect to consider when incorporating machine learning models in real-world decision-making systems where the confidence of decisions are equally important as the decisions themselves. In recent years, there is a surge of research on neural network calibration and the majority of the works can be categorized into post-hoc calibration methods, defined as methods that learn an additional function to calibrate an already trained base network. In this work, we intend to understand the post-hoc calibration methods from a theoretical point of view. Especially, it is known that minimizing Negative Log-Likelihood (NLL) will lead to a calibrated network on the training set if the global optimum is attained (Bishop, 1994). Nevertheless, it is not clear learning an additional function in a post-hoc manner would lead to calibration in the theoretical sense. To this end, we prove that even though the base network ($f$) does not lead to the global optimum of NLL, by adding additional layers ($g$) and minimizing NLL by optimizing the parameters of $g$ one can obtain a calibrated network $g \circ f$. This not only provides a less stringent condition to obtain a calibrated network but also provides a theoretical justification of post-hoc calibration methods. Our experiments on various image classification benchmarks confirm the theory.

The Problem: Overconfident Neural Networks

Modern neural networks are notoriously "uncalibrated." While they might be highly accurate, their confidence scores are often misleading—usually being far too overconfident. In safety-critical fields like medicine or autonomous driving, a model that says it is 99% sure when it is actually only 70% likely to be right is dangerous.

A reliability diagram showing the gap between a model's predicted confidence (score) and its actual accuracy, illustrating the calibration problem.

Existing theory (Bishop, 1994) states that if you minimize Negative Log-Likelihood (NLL) over all possible functions, the resulting model will be perfectly calibrated. However, in the real world, we cannot search through all possible functions, and our models are limited by their architecture and training data. While "post-hoc" methods like Temperature Scaling (adjusting a single parameter at the end) work well in practice, they have historically lacked a rigorous theoretical justification.

Methods: Introducing g-Layers

The authors propose adding g-layers: a small multi-layer perceptron (MLP) consisting of a few dense layers placed between the original network's "logits" (the raw output) and the final softmax activation. This approach shifts the burden of calibration from the massive backbone to a much smaller, easier-to-optimize sub-network.

The transition from an uncalibrated model trained on a training set to a calibrated model using g-layers optimized on a separate calibration set.

The process works as follows:

  1. Train a standard backbone network (e.g., a ResNet) on the training data.
  2. Freeze the backbone to preserve its feature extraction capabilities.
  3. Attach the g-layers to the end of the backbone.
  4. Initialize the g-layers as "transparent layers" using an identity mapping so they initially change nothing.
  5. Train only the g-layers on a separate, unseen "calibration set" using NLL.

Theoretical Foundation

The core of the paper is Theorem 2, which proves that if these g-layers reach an optimum for the NLL loss on the calibration set, the entire composite network becomes calibrated. This theory relies on the activation function (like softmax) being a "submersion." This mathematical property allows the calibration property to be "pulled back" from the output space to the g-layers.

Furthermore, the authors emphasize that Transparent Initialization is crucial. If g-layers are initialized with random weights, the model's accuracy drops significantly because the calibration set is usually too small to "re-learn" the classification task. By starting with an identity mapping, the g-layers preserve the backbone's accuracy while fine-tuning the confidence scores.

Experimental Results

The approach was evaluated on standard image classification benchmarks including CIFAR-10/100, SVHN, and ImageNet across various architectures like ResNet-110 and DenseNet-40. Key findings include:

  • Superior Calibration: g-layers consistently outperformed or matched state-of-the-art methods like Temperature Scaling and Dirichlet-based methods (MS-ODIR). On CIFAR-100 with a ResNet-110, the Kolmogorov-Smirnov (KS) error dropped from 18.48% (uncalibrated) to 0.52%.
  • Preserved Accuracy: Unlike some calibration methods that trade off accuracy for better confidence scores, g-layers maintained the original top-1 accuracy within a negligible margin (± 0.1%).
  • Multi-class Calibration: While many methods only calibrate the "top-1" prediction, g-layers effectively calibrate the entire probability distribution across all classes.

Conclusion and Limitations

While g-layers provide a robust theoretical and practical framework for calibration, they are not immune to overfitting if the calibration set is too small or the g-layers are too complex. The authors recommend keeping the number of layers low (1–3) and using weight decay. Additionally, the theory assumes the calibration set and the final test set come from the same distribution; addressing distribution shift remains an area for future research.

Summary updated

BibTeX
@misc{rahimi2020post,
  author = {Amir Rahimi and Kartik Gupta and Thalaiyasingam Ajanthan and Thomas Mensink and Cristian Sminchisescu and Richard Hartley},
  title = {{Post-hoc Calibration of Neural Networks}},
  year = {2020},
  eprint = {2006.12807},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2006.12807},
}
Figure