Calibration Attention: Instance-wise Temperature Scaling for Vision Transformers

Wenhao Liang, Wei Emma Zhang, Lin Yue, Miao Xu, Olaf Maennel, Weitong Chen · DOI

Introduction

Modern deep learning models, particularly Vision Transformers (ViTs), are frequently miscalibrated, often exhibiting high confidence in incorrect predictions. While standard techniques like Temperature Scaling (TS) address this by applying a global constant to output logits, they ignore the inherent heterogeneity of image data—some samples are naturally more ambiguous or difficult than others. This paper proposes Calibration Attention (CalAttn), a lightweight module that shifts calibration from a post-hoc fix to an integral part of the representation learning process through instance-wise temperature scaling.

The Calibration Attention Framework

CalAttn introduces a small, two-layer Multi-Layer Perceptron (MLP) attached to the transformer backbone. This module takes the final [CLS] token as input to predict a sample-specific temperature $s(z)$ for every individual image. The model's logits are then divided by this predicted scale before the final softmax layer.

Architecture of Calibration Attention showing the integration of the Calibration Head with the Transformer Encoder and its effect on easy vs. hard samples.

Crucially, CalAttn is trained jointly with the backbone using a composite objective function that includes the standard Cross-Entropy loss and a Brier Score term. The Brier Score serves as a differentiable surrogate for calibration error. Because the temperature is derived directly from the [CLS] token, gradients from the calibration loss backpropagate into the transformer, forcing the backbone to learn "reliability-aware" representations. This module is highly efficient, adding less than 0.1% additional parameters.

Key Insights: Decoding Uncertainty

A primary insight of this research is that the geometry of internal tokens, specifically the [CLS] token, already encodes signals related to sample difficulty. CalAttn provides the mechanism to decode these signals into actionable confidence modulation.

Scatter plots comparing CLS-norm vs. max-probability for Vanilla ViT and CalAttn-equipped models on CIFAR-10 and CIFAR-100.

By integrating calibration into training, the backbone reshapes its feature space so that uncertain samples become geometrically distinct. The authors observed a three-phase learning behavior: starting with global alignment, moving through a near-uniform phase, and finally developing "heteroscedastic modulation" where the model assigns diverse scales to different samples to sharpen reliability. Importantly, because CalAttn scales logits by a scalar, it preserves the rank of class predictions, maintaining or even slightly improving Top-1 accuracy.

Experimental Results

The researchers evaluated CalAttn across diverse benchmarks including MNIST, CIFAR, Tiny-ImageNet, and ImageNet-1K, using ViT, DeiT, and Swin Transformer architectures. Key findings include:

  • Significant ECE Reduction: CalAttn achieved relative reductions in Expected Calibration Error (ECE) between 3.7% and 77.7%.
  • Enhanced Safety: The method significantly reduced "High-Confidence False Positives," which are critical in safety-sensitive applications.
  • Robustness to Distribution Shift: On corrupted datasets like CIFAR-10-C, CalAttn-equipped models maintained superior calibration compared to standard temperature scaling.
  • Scalability: On ImageNet-1K with a Swin-S backbone, CalAttn reduced ECE from 9.95% to 1.25%, outperforming more complex state-of-the-art methods.

Limitations and Future Directions

While effective, CalAttn currently utilizes scalar scaling per sample. Future work could explore class-wise or matrix scaling to address more complex biases. Additionally, while the module is optimized for architectures with a [CLS] token, it can be adapted to other models via patch-mean pooling. Finally, the weighting of the Brier term ($\lambda$) may require some task-specific tuning for optimal performance.

Summary updated

BibTeX
@misc{liang2025calibration,
  author = {Wenhao Liang and Wei Emma Zhang and Lin Yue and Miao Xu and Olaf Maennel and Weitong Chen},
  title = {{Calibration Attention: Instance-wise Temperature Scaling for Vision Transformers}},
  year = {2025},
  eprint = {2508.08547},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.08547},
}
Figure