- Introduced by
- Devlin et al. (BERT); explainability methods by Lundberg & Lee (SHAP), Ribeiro et al. (LIME), Sundararajan et al. (Integrated Gradients)
- Year
- 2019–2020
- Type
- Pre-trained transformer classifier with post-hoc or intrinsic explainability
- Family
- Deep learning / NLP / CV
What does it do?
Explainable BERT-based Classification pairs a fine-tuned BERT transformer for text classification with an explainability step, such as SHAP, LIME, integrated gradients or attention analysis, that indicates which tokens contributed to each prediction.
The classifier's predictions come from BERT's contextual representations, and the explanation is a separate analysis of those predictions. It is most useful where the predicted label must be justified to reviewers, auditors or domain experts.
The intuition behind the method
BERT encodes a text bidirectionally through many attention layers and then predicts a class label, and the internal computation is hard to read directly. An explainability method runs after the prediction and asks how much each input token pushed the output toward or away from the predicted class. The explanation is therefore an approximation of the model's behaviour, not a record of its internal reasoning. Its value lies in letting a domain expert check whether the model relied on words that make sense for the task, or on incidental cues such as formatting or annotation artifacts.
History
BERT was introduced by Devlin et al. and published in the NAACL-HLT 2019 proceedings. SHAP was introduced by Lundberg & Lee in 'A unified approach to interpreting model predictions', published at NeurIPS 2017.
When to use it
Choose this approach when the predicted label must be defended to people who need to inspect the reasoning, for example in clinical NLP, legal document review, misinformation screening or content analysis with codebooks. It is appropriate when a fine-tuned BERT classifier already performs well and the open question is whether its decisions rest on sound evidence. Do not use it when speed of prediction is the only requirement, or when labelled data per class is too limited for stable fine-tuning. Instability of this kind shows up as large variation in validation results across random seeds, and the explanations inherit the same instability. If a bag-of-words or logistic regression model already meets the accuracy requirement, the extra complexity of a transformer and its explanations is unlikely to be justified.
Limitations and considerations
- Post-hoc explanations are approximations. SHAP and LIME rely on sampling or perturbation assumptions, so their outputs can change with the sampling settings.
- Attention weights alone are a weak proxy for token importance and should not be the only explanation offered.
- Methods that perturb the input, such as LIME and SHAP, need many forward passes per explained text, which adds considerable cost compared with predicting alone.
- WordPiece tokenisation splits many words into fragments, so attributions are reported on subword units that may not map cleanly onto words a reader expects.
- Faithfulness metrics are not standardised, which makes explanation quality difficult to compare across studies.
- Standard BERT models accept a fixed maximum input length, so longer documents such as full legal or clinical records must be truncated or split into chunks. Predictions and attributions then cover only the retained tokens, and content outside that window cannot be explained.
- Each post-hoc method rests on its own assumptions. LIME approximates the model locally with a simple surrogate, so results depend on the perturbation scheme and the neighbourhood sampled. Integrated gradients depend on the chosen baseline input. SHAP and LIME depend on the number of sampled perturbations and on the weighting kernel. Results from different methods should therefore be compared rather than treated as interchangeable.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Attributions are computed over contextual BERT representations, so a token is scored in the context of the full sentence rather than as an isolated word.
- Post-hoc methods such as SHAP, LIME and integrated gradients can be applied to a trained model without changing its architecture or retraining it.
- Token-level attribution maps let domain experts check whether the model relies on task-relevant words.
- Attribution maps can expose dataset artifacts, for example when high-scoring tokens are formatting markers or annotation conventions rather than content.
Applications
- Clinical NLP: classifying patient records with token-level evidence shown to clinicians.
- Legal document review: labelling clauses with explanations that legal reviewers can inspect.
- Misinformation and hate-speech screening, where the highlighted cues are reviewed before action.
- Social science content analysis, where model decisions are checked against the codebook.
- Bias auditing: examining which tokens drive predictions for demographic-sensitive content.
How it works
How it works
A BERT model pre-trained on large text corpora is fine-tuned on the labelled classification dataset. A [CLS] token representation or a mean-pooled representation is passed to a softmax output layer, and the model is trained to minimise cross-entropy over the target classes.
At inference, each input text is split into subword tokens by the BERT WordPiece tokenizer and passed through the transformer stack, which produces contextualised token embeddings. The output layer converts these into class scores and probabilities.
A post-hoc attribution method is then applied to the fine-tuned model. SHAP estimates attributions over the input features using Shapley-value-based sampling, LIME fits a local surrogate model on perturbed inputs with tokens removed or kept, integrated gradients accumulate gradients along a path from a baseline to the input embedding, and attention-based analysis inspects the attention weights of the transformer layers. Each method answers a different question, so the choice affects the resulting explanation.
Each token receives a scalar attribution for the predicted class. Positive values indicate support for that class and negative values indicate opposition. Scores are typically normalised and displayed as a saliency map over the input text, usually at subword level.
Classification performance is reported with standard metrics such as accuracy, F1 and AUC-ROC. Explanation quality is assessed separately, using faithfulness tests such as sufficiency (whether the top-attributed tokens alone preserve the prediction) and comprehensiveness (whether removing them changes it), together with agreement checks against domain experts.
Common pitfalls
- Treating raw attention weights as the explanation without checking them against gradient-based or perturbation-based attributions.
- Reporting classification metrics only, without any faithfulness or human-agreement evaluation of the explanations.
- Fine-tuning on imbalanced data without class weighting, and then trusting attribution scores for the minority class.
- Presenting subword-level saliency maps to non-specialists as if each highlighted unit were a meaningful word.
- Fine-tuning a large BERT variant on a small dataset, which can overfit and make the attributions unstable across runs.
Frequently asked questions
Are attention weights a reliable explanation for BERT's predictions?
Not on their own. Attention weights are an indirect signal and may not agree with the importance measured by gradient-based or perturbation-based methods, so attention should be checked against those attributions before it is presented as an explanation.
Which explainability method should I use with BERT?
There is no single correct answer. Integrated gradients require gradient access to the model and depend on the chosen baseline. LIME is model-agnostic and simple to apply, but it can be slow because it needs many perturbed predictions. SHAP and LIME both depend on sampling settings, so their results should be checked for stability. Compare more than one method and evaluate them with faithfulness tests.
How much labelled data do I need?
Enough to fine-tune reliably on each target class. No universal minimum applies across tasks, so judge sufficiency from validation results: if performance and attributions vary widely across several random seeds, the labelled data for that class is probably too limited. In that case, consider a few-shot approach or a simpler model, because the explanations depend on how well the underlying classifier has been trained.
Can I use a domain-specific BERT variant instead of base BERT?
Often yes. Domain-adapted variants such as BioBERT (biomedical), LegalBERT or FinBERT are designed for specialised vocabularies. Whether they improve accuracy and the relevance of token attributions for your data should be tested rather than assumed.
How do I evaluate whether my explanations are good?
Use sufficiency and comprehensiveness tests. Sufficiency checks whether the top-attributed tokens alone reproduce the prediction; comprehensiveness checks whether removing them changes it. Add human-agreement studies in which domain experts judge whether the highlighted tokens match their expectations.
Related methods
Sources
Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of deep bidirectional transformers for language understanding. Proceedings of NAACL-HLT 2019, pp. 4171–4186.
Open DOI referenceLundberg, S. M., & Lee, S.-I. (2017). A unified approach to interpreting model predictions. Advances in Neural Information Processing Systems (NeurIPS), 30, 4765–4774.
Open publication reference