Attention Meets Post-hoc Interpretability: A Mathematical Perspective

Gianluigi Lopardo, Frederic Precioso, Damien Garreau

Introduction

In the field of Explainable AI (XAI), a persistent debate centers on whether internal attention weights in Transformer models serve as valid explanations for model predictions. While attention heatmaps are frequently used to visualize token importance, critics argue they can be misleading, as attention weights can often be permuted without altering the final output. This paper addresses this controversy by establishing a rigorous mathematical connection between attention weights and established "post-hoc" interpretability methods like LIME and Gradients.

Theoretical Framework: Analyzing Single-Layer Attention

The authors focus their analysis on a single-layer, multi-head attention network designed for binary classification tasks, such as sentiment analysis. By simplifying the architecture, they are able to derive explicit closed-form expressions that link internal model parameters to external explanations.

Architecture of the single-layer multi-head attention model analyzed in the study.

The model processes an input $x$ through an embedding layer (including positional encoding), followed by $K$ parallel attention heads. Each head computes Query ($q$), Key ($k$), and Value ($v$) vectors to produce scaled dot-product attention weights ($\alpha$). The final output $f(x)$ is an average over these $K$ heads, followed by a linear layer $W_\ell$.

Bridging the Gap: Theorems for Gradients and LIME

The core contribution of this work lies in two major theorems that provide a "white-box" view of popular interpretability methods:

  1. Theorem 4.1 (Gradient meets Attention): The authors prove that the gradient of the model's output with respect to input tokens is a linear function of the attention weights. Crucially, this gradient also incorporates the "Value" weights ($W_v$) and the final linear layer ($W_\ell$), which raw attention ignores.
  2. Theorem 5.1 (LIME meets Attention): They derive a closed-form approximation for LIME coefficients, showing they are essentially an affine transformation of attention weights, adjusted by the model's treatment of the "Unknown" ([UNK]) token.

Key Insights: Why Post-hoc Methods Excel

The mathematical derivation reveals several fundamental differences between raw attention and post-hoc explanations:

  • Sign-Blindness of Attention: Attention weights ($\alpha$) are inherently non-negative. While they can indicate that a token is "important," they cannot specify the direction of that importance. In contrast, post-hoc methods like LIME can produce negative coefficients, identifying tokens that actively push the model away from a specific class prediction.
  • The Role of the Value Vector: Attention only determines the "focus" on a token. The actual contribution to the final decision is mediated by the Value vector and the output weights. Post-hoc methods naturally account for these "downstream" components, whereas raw attention is purely "upstream."
  • LIME as "Corrected" Attention: The math suggests that LIME acts as a refined version of attention that has been re-scaled and shifted to better reflect the model's internal logic.

Empirical Validation and Results

The authors validated their theoretical findings using the IMDb sentiment analysis dataset. They compared their derived formulas against empirical implementations of LIME and Gradients, finding a near-perfect match.

Attention heatmaps for different heads showing varying focus on tokens in a sample sentence.

As shown in the attention maps above, different heads focus on different parts of the input. For instance, Head 6 shows strong attention on the word "popular." However, the authors' analysis demonstrates that while "popular" might receive high attention, a post-hoc method might correctly identify "questionable" as the primary driver for a negative sentiment prediction because it accounts for the negative weight assigned to that token in the final layers.

Even in deeper models (e.g., 6-layer Transformers), the authors observed a significant correlation ($\rho = 0.672$) between LIME coefficients and attention weights, suggesting that the insights from their single-layer analysis have broader applicability.

Conclusion and Limitations

This work provides a much-needed mathematical foundation for the "attention as explanation" debate. It demonstrates that while attention is related to model behavior, it is an incomplete explanation because it lacks sign information and ignores the transformation of information in the Value and output layers.

Future work aims to extend this analysis to more complex architectures involving non-linearities (like ReLU) and skip connections, as well as applying these mathematical frameworks to other domains like Computer Vision.

Summary updated

BibTeX
@inproceedings{lopardo2024attention,
  author = {Gianluigi Lopardo and Frederic Precioso and Damien Garreau},
  title = {{Attention Meets Post-hoc Interpretability: A Mathematical Perspective}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=wnkC5T11Z9},
}
Figure