Grad-SAM: Explaining Transformers via Gradient Self-Attention Maps

Oren Barkan, Edan Hauon, Avi Caciularu, Ori Katz, Itzik Malkiel, Omri Armstrong, Noam Koenigstein

TL;DR

This paper introduces Grad-SAM, a novel method for explaining predictions made by Transformer-based language models like BERT. By combining attention scores with their gradients, Grad-SAM effectively identifies which input tokens are most crucial for a given prediction, outperforming existing state-of-the-art explanation techniques.

What Problem Does This Paper Address?

Transformer models, such as BERT, have revolutionized Natural Language Processing (NLP), achieving state-of-the-art results across numerous tasks. However, their complex, multi-layered architecture, with millions of parameters, makes it challenging to understand why they make specific predictions. This "black box" nature hinders trust and debugging. While attention mechanisms within Transformers offer some insight into token relationships, it's not always clear if these attention scores directly correlate with the model's final prediction. Existing methods for explaining these models often rely on analyzing attention scores alone or using gradient-based approaches separately. This paper aims to bridge this gap by developing a more faithful and robust explanation method that leverages both attention and gradients.

Methods

The core of Grad-SAM lies in its novel approach to combining self-attention mechanisms with gradient information. The method works by:

  1. Calculating Attention Matrices: For each layer and attention head in the Transformer, an attention matrix ($A_{lm}$) is computed. Each entry in this matrix quantifies how much attention a token receives from another token.
  2. Computing Gradients: The gradients of the model's prediction score ($s_x$) with respect to these attention matrices are calculated. This tells us how a change in attention would affect the final prediction.
  3. Combining Attention and Gradients: Grad-SAM then computes a "heatmap" ($H_{lm}$) for each attention head by element-wise multiplying the attention matrix ($A_{lm}$) with the positive gradients. Crucially, negative gradients are zeroed out using a ReLU activation. This step is motivated by the observation that negative gradients can sometimes cancel out the important positive signals from attention activations.
  4. Aggregating Importance Scores: Finally, the importance of each input token ($r_{x_i}$) is calculated by summing up the values in the corresponding rows of these $H_{lm}$ matrices across all attention heads and layers. Tokens with higher $r_{x_i}$ scores are considered more influential for the prediction.

This method is designed to be generic and applicable to any Transformer-based architecture.

Datasets

The paper evaluates Grad-SAM on several standard NLP benchmarks:

  • Stanford Sentiment Treebank (SST): A sentiment analysis task (binary classification).
  • AgNews (AGN): A multiclass classification task for news articles.
  • IMDB: A sentiment analysis task for movie reviews (binary classification).
  • MultiRC (MRC): A reading comprehension task (binary classification).

The authors use a pre-trained BERT-base-uncased model, finetuned on each of these tasks.

Key Insights and Ideas

  • Attention is not enough: The paper reinforces the idea that raw attention scores alone don't always provide faithful explanations for model predictions.
  • Gradients are powerful, but need careful handling: While gradients offer valuable information about how predictions change, negative gradients can obscure important positive signals.
  • The power of positive gradients: Zeroing out negative gradients (using ReLU) before aggregation is crucial for Grad-SAM's effectiveness, preventing the cancellation of genuinely informative signals.
  • Combining attention and gradients yields superior explanations: Grad-SAM's success stems from its synergistic use of both attention mechanisms and their corresponding gradients, leading to more accurate identification of influential input tokens.
  • Task-agnostic explanation: Grad-SAM's ability to work without requiring task-specific auxiliary models (unlike some prior methods like FRESH) makes it a more versatile tool.

Key Results

  • Quantitative Performance: Grad-SAM significantly outperforms several state-of-the-art explanation methods, including "Gradient" and "[CLS] Att" from prior work, across all tested datasets and evaluation metrics (Macro F1 and Area Over the Perturbation Curve - AOPC). For instance, on the SST dataset, masking tokens identified by Grad-SAM led to a smaller drop in accuracy (19.5 points) compared to [CLS] Att (16.5 points), indicating more robust explanations.
  • Qualitative Results: In qualitative examples, Grad-SAM successfully identified key tokens that, when masked, did not alter the model's correct prediction. In contrast, masking tokens identified by a baseline method ([CLS] Att) sometimes led to incorrect predictions, demonstrating Grad-SAM's superior ability to pinpoint truly explanatory tokens.

Limitations and Future Work

  • Computational Cost: While Grad-SAM is a novel method, the paper doesn't explicitly detail its computational overhead compared to other methods, though gradient computation inherently adds to the cost.
  • Future Work: The authors suggest that Grad-SAM's effectiveness in identifying influential tokens could be further leveraged for tasks like adversarial attack detection or generating more robust models by understanding their decision-making process. The generic nature of the method also opens doors for its application to other Transformer-based architectures beyond BERT.

Summary updated

BibTeX
@inproceedings{barkan2021grad,
  author = {Oren Barkan and Edan Hauon and Avi Caciularu and Ori Katz and Itzik Malkiel and Omri Armstrong and Noam Koenigstein},
  title = {{Grad-SAM: Explaining Transformers via Gradient Self-Attention Maps}},
  booktitle = {ACM International Conference on Information and Knowledge Management},
  year = {2021},
  url = {https://openreview.net/forum?id=EXlt0LkWwa},
}
Figure