- Introduced by
- Vaswani et al. (Transformer); explainability extensions by Chefer et al. and the broader XAI community
- Year
- 2017–2021
- Type
- Interpretable deep learning model
- Family
- Deep learning / NLP / CV
What does it do?
An Explainable Transformer pairs a standard or pre-trained Transformer with post-hoc or built-in interpretability techniques, such as attention rollout, gradient-based attribution, or SHAP, to show which input tokens or image patches contributed to each prediction.
The aim is to keep the predictive capacity of Transformer models while making individual predictions open to inspection by domain experts.
The intuition behind the method
Raw attention weights show how strongly each token attends to others in a given layer, but they do not show how the final prediction was built up across many layers, where information is mixed by attention, skip connections, and other operations. Explainable Transformer methods therefore propagate relevance through the whole network, using gradients, rollout-style aggregation, or perturbation-based attribution, so that the final map reflects more of the computational path than a single attention snapshot. The result is a heat map over input tokens or image patches that a non-specialist can inspect.
History
The Transformer architecture was introduced by Vaswani et al. (2017) and became a common backbone for natural language processing and computer vision. Early explanations of Transformer predictions often visualized raw attention maps. Chefer et al. (2021) proposed a relevance propagation method for Transformers that handles attention layers and skip connections, and benchmarked it on visual Transformers and a text classification task.
When to use it
Choose an Explainable Transformer when the task needs a high-capacity sequence or vision model and the predictions must be justified to domain experts, reviewers, or end users, for example in clinical text analysis, legal document review, or image analysis where the highlighted regions must be checked. It is most useful when a fine-tuned Transformer already reaches acceptable task performance and the remaining problem is trust or review rather than accuracy. Post-hoc methods, which are applied to an existing trained model without changing it, are the lighter-weight option. Built-in approaches, such as sparse attention or explicit rationale generation, change the architecture or training objective, so they are a separate decision, and their effect on accuracy must be measured on the target task. Do not treat raw attention weights as explanations without a propagation or attribution step. Avoid the approach when the dataset is too small to support a Transformer, or when a simpler model performs as well, since interpretability methods cannot compensate for a poorly fitted model.
Limitations and considerations
- Gradient-based and perturbation-based methods add significant inference-time overhead compared to a plain Transformer forward pass.
- No single attribution method is universally superior; different methods can produce conflicting heat maps for the same input.
- Explanations reflect the model's internal reasoning, not ground-truth causality, and can mislead if the model has learned spurious correlations.
- Evaluating explanation quality is still an open research problem; metrics like faithfulness and plausibility often disagree.
- Large Transformer models require substantial memory to store the intermediate activations needed for gradient-based propagation.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Keeps the predictive capacity of the underlying Transformer while adding a per-prediction explanation step.
- Post-hoc methods can be applied to an existing pre-trained checkpoint without changing the training procedure.
- In the authors' own evaluation, relevance propagation through attention layers and skip connections was reported to show a clear advantage over existing attention-based and heuristic explainability methods. The benchmarks were visual Transformer networks and one text classification task, so results on other tasks and models must be checked for the target application.
- Token-level or patch-level heat maps are readable for domain experts without deep learning training.
- The same family of approaches has been applied to text classification and to vision Transformers, so it covers both token inputs and image-patch inputs.
- Stored per-prediction explanations can be logged and reviewed later, which can support audit documentation.
Applications
- Text classification with Transformer models, where highlighted tokens are reviewed against the expected evidence.
- Vision Transformers, where highlighted image patches are checked for the region the model relied on.
- Clinical text analysis, where experts check whether highlighted phrases in a note are the ones that support a prediction.
- Legal or compliance text review, where reviewers need to see the passages behind a classification.
How it works
How it works
A Transformer backbone (for example BERT, ViT, or GPT) is first trained or fine-tuned on the target task. For most post-hoc methods the parameters are then fixed before any explanation is computed, so no architectural change is needed and existing checkpoints can be explained.
A standard forward pass is run on the input of interest, producing multi-head self-attention maps and task-specific logits. The scaled dot-product attention formula weights values by query-key similarity. These intermediate tensors are kept in memory for the attribution step.
Formula
\text{Attention}(Q,K,V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)VAn attribution method identifies which input positions contributed most to the predicted output. Common options are (1) raw or averaged attention weights, (2) attention rollout, which multiplies attention maps across layers to trace information flow, (3) gradient-weighted attention in the style of GradCAM, and (4) SHAP or LIME applied to token-level perturbations.
For methods that go beyond raw attention, such as the relevance propagation proposed by Chefer et al. (2021), relevance is assigned locally using the Deep Taylor Decomposition principle and then propagated through the layers. The propagation is designed to handle attention layers and skip connections, and the formulation is reported to keep total relevancy consistent across layers. This step addresses the limitation of reading a single layer's attention as an explanation.
Attribution scores are normalized and mapped back onto input tokens or image patches as a colour-coded heat map. The explanation can be shown in a user interface next to the prediction, stored as metadata with the prediction, or used as the basis for human-written justifications in review workflows.
Common pitfalls
- Treating raw attention weights as faithful explanations without applying a propagation or gradient-based correction step.
- Reporting a single explanation method's output as the ground truth without cross-checking it with a second method.
- Using explainability to justify a model that performs poorly on subgroups or under distribution shift.
- Neglecting to validate explanations with domain experts who can judge whether highlighted tokens are substantively meaningful.
- Applying token-level explanations to aggregated or pooled representations (for example [CLS] token outputs) without accounting for the pooling operation.
Frequently asked questions
Are raw attention weights good explanations?
On their own, usually not. Raw attention shows how weights were distributed within a layer, not how the final prediction was built up across layers. Chefer et al. (2021) address this by propagating relevance through attention layers and skip connections.
Do I need to retrain the Transformer to make it explainable?
Not for most post-hoc methods. Attention rollout, SHAP, and gradient-based approaches can be applied to a pre-trained Transformer without modifying the training process. Some built-in approaches, such as sparse attention or explicit rationale generation, do require architectural or training changes.
Which explanation method should I choose?
There is no universal best method. Chefer et al. (2021) propose a relevance propagation method for both text and vision Transformers. SHAP gives Shapley-value attributions with a clear theoretical basis, but exact computation can be costly on long sequences. Starting with a propagation method and cross-checking a sample with SHAP is a reasonable strategy.
How do I know if my explanations are correct?
A common faithfulness check measures how performance changes when the top-ranked tokens are masked, and plausibility is often judged by having domain experts assess whether the highlighted spans are meaningful. No single metric is established as definitive, so both checks should be reported alongside the explanation method.
Does explainability hurt model accuracy?
Post-hoc methods do not change the model's predictions, because they are computed after the fact. Built-in constraints, such as forcing sparse attention, can reduce accuracy, and the size of that cost depends on the task and must be measured for the specific model.
Related methods
Sources
Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L., & Polosukhin, I. (2017). Attention is all you need. Advances in Neural Information Processing Systems, 30.
Open publication referenceChefer, H., Gur, S., & Wolf, L. (2021). Transformer interpretability beyond attention visualization. Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 782–791.
Open DOI reference