Statistical Test for Attention Maps in Vision Transformers

Tomohiro Shiraishi, Daiki Miwa, Teruyuki Katsuoka, Vo Nguyen Le Duy, Kouichi Taji, Ichiro Takeuchi

Introduction

Vision Transformers (ViTs) have become a standard architecture for computer vision, largely because their self-attention mechanisms allow them to weigh the importance of different image patches. In high-stakes fields like medical imaging or autonomous driving, these attention maps are often used as explanatory evidence to understand why a model made a certain prediction (e.g., "the model predicts a tumor because it is looking here").

However, researchers have identified a critical flaw in this practice: attention maps are prone to "hallucinating" importance in completely irrelevant regions. To address this, a new theoretically grounded statistical test has been developed to determine if the attention shown by a ViT is actually meaningful or just a statistical fluke. By applying a framework called Selective Inference, researchers can now calculate reliable p-values for attention maps, ensuring that the regions a model flags as "important" are statistically significant while strictly controlling the rate of false positives.

The Challenge with Attention Maps

When using attention maps as evidence, standard statistical tests (which yield naive p-values) fail due to selection bias. Because the model specifically searches for patches to attend to, testing those same patches for significance is effectively "double-dipping" into the data.

This leads to an incredibly high rate of false positives (Type I errors), where random noise is mistaken for a critical feature. Until now, there was no mathematically rigorous way to tell if a ViT’s focus was statistically significant or just an artifact of the model's architecture.

ViT attention pipeline for a positive (tumor) MRI scan.
ViT attention pipeline for a negative (healthy) MRI scan, showing scattered attention.

As seen in the contrast between positive and negative predictions, a ViT will still generate an attention map and highlight regions even when no actual anomaly (like a tumor) is present. Without a statistical test, these "hallucinated" regions in healthy scans could easily be misinterpreted as medical evidence.

Methodology: Selective Inference for ViTs

The authors bridge this reliability gap using Selective Inference (SI). The core idea of SI is to calculate a p-value that is explicitly conditioned on the fact that the model already selected a specific region.

Adapting SI to Vision Transformers required overcoming several key technical hurdles:

  • Defining the Test Statistic: The researchers defined a standardized test statistic based on the difference in average pixel intensity between the "attended" region and the rest of the image.
  • The Non-Linearity Challenge: Previous SI methods worked for Convolutional Neural Networks (CNNs) because those models are "piecewise linear." ViTs, however, use softmax-based self-attention, which is highly non-linear and much harder to analyze statistically.
  • Adaptive Grid Search: To compute the p-value, the authors had to characterize a "truncated" Gaussian distribution. Because they couldn't solve this analytically for the non-linear ViT, they developed an Adaptive Grid Search algorithm. This algorithm uses the Lipschitz constant (a measure of how fast a function can change) and smart heuristics to efficiently map out the "selection region" in the data space.
  • Forward-Mode Auto-Differentiation: To make the math work, they needed the derivatives of the attention maps. They implemented a modular version of forward-mode auto-differentiation in TensorFlow to handle this efficiently across any ViT architecture.

Experimental Validation and Key Results

The researchers evaluated their method using both synthetic datasets (images with Gaussian noise and injected signals) and a real-world Brain Tumor MRI Dataset containing 1,880 MRI scans with and without lower-grade gliomas.

  • Controlling False Positives: The paper demonstrates that "naive" p-values are almost always near zero, meaning a standard test would declare almost every attention map significant, even on pure noise. In experiments with 10,000 null images, the proposed adaptive SI method successfully controlled the Type I error rate at the target level (e.g., 5%), whereas naive and permutation tests failed spectacularly.
  • Higher Statistical Power: Compared to the Bonferroni correction (a standard but overly conservative way to handle multiple comparisons), the proposed method demonstrated significantly higher statistical power. It was much better at detecting actual tumors when they were present without being drowned out by corrections.
  • Real-World Utility: On the brain MRI dataset, the method correctly assigned high p-values (not significant) to attention maps in healthy brains, while assigning very low p-values (statistically significant) to the attention maps that correctly identified actual tumors.

Limitations and Future Work

While this statistical test provides a much-needed foundation for trusting ViT attention maps, it comes with limitations:

  • Computational Cost: The adaptive grid search, while more efficient than a fixed grid, remains computationally expensive. Applying this to very high-resolution images or "Huge" ViT variants is challenging.
  • Heuristic Dependency: The method relies on the assumption that the attention function is relatively smooth. While the authors provide heuristics to handle "micro-oscillations," extremely complex or "jittery" transformer architectures might require even more robust Lipschitz estimations.
  • Scaling to Large Models: The current implementation was tested on "Small" to "Huge" architectures. Applying this to the absolute largest foundation models will require further optimization of the computational pipeline.

Summary updated

BibTeX
@inproceedings{shiraishi2024statistical,
  author = {Tomohiro Shiraishi and Daiki Miwa and Teruyuki Katsuoka and Vo Nguyen Le Duy and Kouichi Taji and Ichiro Takeuchi},
  title = {{Statistical Test for Attention Maps in Vision Transformers}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=uLonuOfrwp},
}
Figure