Causal Interpretation of Self-Attention in Pre-Trained Transformers

Raanan Y. Rohekar, Yaniv Gurwicz, Shami Nisimov · DOI

Abstract

While Transformers have revolutionized natural language processing and recommendation systems, their internal decision-making processes remain opaque. Traditional attention mechanisms provide a glimpse into which tokens a model "focuses" on, but they fail to distinguish between mere correlation and true causality. This paper establishes a formal mathematical link between self-attention and Structural Causal Models (SCMs). By treating the attention matrix as a proxy for causal relationships, the authors introduce ABCD (Attention-Based Causal Discovery) and CLEANN (CausaL Explanations from Attention in Neural Networks). These methods enable zero-shot causal discovery and the identification of minimal sets of tokens responsible for model predictions, offering a more robust framework for explainable AI.

Introduction

The ubiquity of Transformer-based models has intensified the need for reliable Explainable AI (XAI) techniques. Most existing methods rely on attention weights to explain model behavior; however, high attention scores often capture marginal correlations rather than causal drivers. This distinction is critical: a model might attend to a common noun like "cinema" in a movie review, even if the actual sentiment is driven by a specific adjective like "disappointing."

Presented at NeurIPS 2023, "Causal Interpretation of Self-Attention in Pre-Trained Transformers" addresses this gap. The researchers from Intel Labs demonstrate that pre-trained Transformers inherently estimate the causal structure of their input sequences. By bridging deep learning with structural causal inference, they provide a way to extract dynamic, context-specific causal graphs directly from pre-trained, "black-box" models.

Bridging Attention and Structural Causal Models

The core contribution of this research is the mathematical proof that the self-attention mechanism in a Transformer can be interpreted as estimating a linear structural equation model (SEM).

Specifically, the authors show that the covariance of a Transformer’s output embeddings ($\mathbf{C}_Z$) is approximately proportional to $\mathbf{AA}^\top$, where $\mathbf{A}$ represents the attention matrix. This relationship allows the attention matrix to serve as a foundation for causal discovery. Unlike traditional causal discovery, which often assumes a static graph for an entire dataset, this approach allows for the recovery of a unique causal graph for every individual input sequence.

The ABCD and CLEANN Frameworks

Building on this theoretical bridge, the authors propose two primary algorithms:

1. Attention-Based Causal Discovery (ABCD)

ABCD utilizes the relationship between attention and covariance to perform conditional independence tests via partial correlations. These tests are then used within constraint-based algorithms to recover a Partial Ancestral Graph (PAG). The use of PAGs is significant because they can explicitly represent latent confounders—hidden variables that influence both the input and the output—which standard directed graphs often ignore.

2. CausaL Explanations from Attention in Neural Networks (CLEANN)

CLEANN is the practical application of the discovered causal graph. It identifies the "minimal explaining set"—the smallest subset of input tokens that, if modified or removed, would change the model's final prediction. This provides a much more precise explanation than raw attention maps, which tend to be "noisy" and highlight irrelevant tokens.

Experimental Evaluation

The researchers validated their framework across two distinct domains: sentiment analysis and recommender systems.

Sentiment Analysis (IMDB)

Using a BERT model fine-tuned on the IMDB movie review dataset, the authors compared CLEANN against standard attention-based explanation methods. The results showed that CLEANN was significantly more efficient. For a review of approximately 60 tokens, CLEANN required only ~3.5 tokens to explain a prediction, whereas baseline methods flagged over 13 tokens. Qualitatively, CLEANN ignored irrelevant nouns and focused on logical connectors (e.g., "but") and sentiment-laden adjectives (e.g., "bad") that truly drove the classification.

Recommender Systems (MovieLens 1M)

In the context of BERT4Rec, a Transformer-based recommender, CLEANN was used to identify which specific movies in a user's history "caused" a new recommendation. When these causal movies were removed, the model's alternative recommendations remained more consistent with the user's original preferences than when movies identified by baseline methods were removed. This suggests that the causal graph accurately captures the user's underlying intent.

Key Insights

  • Layer-Wise Specialization: The study found that the deepest attention layers are the most effective for causal discovery. While early layers appear to focus on general context estimation, the final layers represent refined structural and causal relationships.
  • Zero-Shot Capability: One of the most compelling aspects of this work is that it is entirely zero-shot. It requires no fine-tuning or architectural changes; it simply extracts causal information from the weights of existing, pre-trained models.
  • Robustness to Latents: By utilizing PAGs, the method acknowledges the presence of unobserved variables, making the resulting explanations more robust in real-world scenarios where not all data is captured.

Limitations and Future Directions

Despite its success, the method relies on a linear assumption regarding the relationship between attention and embeddings. While the results are strong, the non-linearities inherent in Transformers introduce some noise into the causal estimation. Additionally, the quality of the causal discovery is inherently tied to the quality of the underlying model; if the Transformer is poorly trained, the extracted causal graph will reflect those deficiencies.

Future research may explore the integration of "causal bottlenecks" directly into Transformer architectures to make causal discovery an intrinsic part of the model's forward pass, further improving the transparency of these powerful AI systems.

Conclusion

This research provides a rigorous foundation for interpreting Transformers through the lens of causality. By moving beyond simple attention weights and leveraging the principles of Structural Causal Models, ABCD and CLEANN offer a path toward more precise, minimal, and theoretically grounded explanations for AI behavior.

Reference: Rohekar, R. Y., Gurwicz, Y., & Nisimov, S. (2023). Causal Interpretation of Self-Attention in Pre-Trained Transformers. 37th Conference on Neural Information Processing Systems (NeurIPS 2023).

Summary updated

BibTeX
@inproceedings{rohekar2023causal,
  author = {Raanan Y. Rohekar and Yaniv Gurwicz and Shami Nisimov},
  title = {{Causal Interpretation of Self-Attention in Pre-Trained Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=DS4rKySlYC},
}
Figure