Explainable RoBERTa-based Classification

Machine Learning
Introduced by
Liu et al. (RoBERTa, 2019); Lundberg & Lee (SHAP, 2017); Ribeiro et al. (LIME, 2016)
Year
2019–2020
Type
Pre-trained transformer classifier with post-hoc XAI
Family
Deep learning / NLP / CV

What does it do?

Explainable RoBERTa-based classification fine-tunes a RoBERTa transformer on labeled text and then applies post-hoc interpretability methods, such as SHAP or attention analysis, to indicate which tokens or features contributed to each prediction.

The explanations are computed after training and describe the fitted model's behaviour; they do not change how the classifier is trained or make its predictions more accurate. Their usefulness therefore depends on whether the explanations are checked for faithfulness.

The intuition behind the method

RoBERTa is a pre-trained language model whose very large number of parameters makes its individual decisions hard to read directly. Post-hoc explanation methods work on the fitted model to recover a per-prediction account of which input words mattered. Perturbation-based methods such as SHAP need only the model's input-output behaviour, whereas inspecting attention weights requires access to the model's internal layers. Each explanation is a ranked list of tokens with signed scores that a practitioner can inspect, question, or report. The explanation describes what the model relied on, which is not necessarily the full reason for the label, so it should be validated before it is trusted.

History

RoBERTa (Robustly Optimized BERT Pretraining Approach) was introduced by Liu et al. in 2019 as a re-trained variant of BERT that trained longer on more data and dropped the next-sentence prediction objective. Post-hoc explanation methods were also applied to text classification; SHAP (Lundberg & Lee, 2017) is one such method, and other perturbation-based and gradient-based attribution methods have been applied to token-level text classification as well. Combining a fine-tuned RoBERTa with post-hoc attribution or attention visualization has been used in applied NLP work that needs both accuracy and interpretability.

When to use it

Choose this approach when a fine-tuned RoBERTa classifier is needed for accuracy and each prediction must be accompanied by inspectable evidence, for example in clinical, legal, financial, or content-moderation settings where a bare label is not enough. It needs a labeled dataset large enough to fine-tune a transformer reliably. Fine-tuning on very small labeled sets can be unstable, and the size needed depends on the task; check this by comparing against a simpler baseline and by examining whether the explanations stay stable across resampled training sets. Do not use it when labeled data are scarce, when compute is limited, or when a simpler model such as logistic regression on TF-IDF features already meets the accuracy requirement, since the explanation layer adds cost without improving the classifier. Explanations should serve as an audit tool alongside the performance evaluation, not as a replacement for it.

Limitations and considerations

  • Fine-tuning RoBERTa needs substantial GPU compute and a sizeable labeled dataset, and SHAP or other attribution methods add further inference cost on top of that.
  • Post-hoc explanations are approximations of the model, and their faithfulness is not guaranteed; attention weights are a weak basis for attribution.
  • Attribution methods can disagree on the same prediction, so the chosen method shapes the explanation that is reported.
  • SHAP typically requires many model evaluations per input, which can make it impractical for real-time or high-throughput use.
  • Readers may take highlighted tokens as the complete reason for a label, which creates a practical risk of over-trusting the explanation.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Pairs a pre-trained transformer's predictive power with token-level explanations that a domain expert can read directly.
  • Post-hoc methods such as SHAP can be applied to an already fine-tuned model without retraining it.
  • SHAP attributions rest on game-theoretic axioms (local accuracy, missingness, consistency) that the original SHAP work uses to unify several attribution methods, giving them a more defined basis than ad hoc heuristics.
  • Per-decision token attributions can support documentation for audit or regulatory review where reasons for individual outputs must be recorded.
  • Token-level outputs make it possible to check whether the model keys on task-relevant terms or on spurious artefacts in the data.

Applications

  • Clinical NLP: classifying patient records, discharge summaries, or clinical notes with auditable evidence for each label.
  • Legal and regulatory text: contract clause classification with highlighted relevant provisions.
  • Financial NLP: sentiment classification of earnings calls or news with attribution to key phrases.
  • Hate-speech and misinformation detection requiring explainability for content-moderation accountability.
  • Academic NLP research benchmarking interpretability methods against annotated rationale datasets.
  • Social-science text analysis where reviewers expect interpretable evidence alongside quantitative predictions.

How it works

How it works

1

Input text is tokenized into subword units using RoBERTa's byte-pair encoding (BPE) vocabulary. The boundary tokens <s> and </s> are added at the start and end of each sequence; token IDs are padded or truncated to the maximum sequence length. The tokenized representation feeds into the model's embedding layer.

2

The pre-trained RoBERTa encoder weights are updated on the labeled classification dataset using a cross-entropy loss, with a classification head placed on top of the first-token (<s>) representation. A small learning rate with a warm-up phase is commonly used so that pre-trained knowledge is not overwritten too quickly; the exact schedule is a hyperparameter that should be tuned and reported.

3

After fine-tuning, the model produces class probabilities for each input via the softmax output of the classification head. Standard evaluation metrics — accuracy, F1, AUC, precision, recall — are computed on a held-out test set to confirm the model's predictive quality before explanation is applied.

4

Post-hoc methods are applied to the trained model. SHAP (SHapley Additive exPlanations) estimates each token's contribution to the predicted class by considering how the output changes across subsets of the input tokens. Other attribution methods, including perturbation-based and gradient-based ones, also assign a score to each token, but they differ in how the input is perturbed or which baseline input is used, and these choices shape the resulting scores. Each method yields a signed score per token; scores from different methods need not agree.

5

Explanations should be checked for faithfulness before they are reported. A common check is to remove or mask the top-attributed tokens and measure whether the prediction confidence drops; a larger drop supports the attribution, while a small drop is evidence against it, although one case is not conclusive. Agreement between attribution methods, such as SHAP and attention, can be compared, but disagreement does not by itself show which method is correct. Where annotated human judgements or rationale datasets exist for the task, they provide external reference points for comparing explanations with human-identified evidence.

Common pitfalls

  • Treating raw attention weights as faithful explanations. Attention reflects how information is weighted inside the network and does not by itself establish how much each token changed the output.
  • Reporting explanations without running a faithfulness check, such as masking the top-attributed tokens and measuring the change in prediction confidence.
  • Reporting explanations without naming the XAI method used and its known limitations, including baseline or perturbation settings that shape the result.
  • Fine-tuning on imbalanced classes without reweighting or resampling, then explaining the resulting biased classifier, so that the explanations describe a biased model.
  • Aggregating per-instance SHAP or other attributions into a global feature ranking without a stated aggregation rule. A global ranking is a separate analysis, and the averaging choices can change the ordering.

Frequently asked questions

Is attention the same as explanation in RoBERTa?

No. Attention weights show how the self-attention mechanism distributes information between tokens, but they may not reliably reflect which inputs caused a prediction. Use SHAP or another attribution method for attribution, and use attention visualization only as a complementary diagnostic.

How many labeled examples do I need to fine-tune RoBERTa?

No fixed number applies to every task, and no threshold is given here. Performance and explanation stability generally improve as the labeled set grows, and very small datasets make both less reliable. If data are scarce, compare against a simpler baseline and check whether the explanations are stable across resampled training sets.

Which XAI method should I use — SHAP or another attribution method?

SHAP offers attributions with a game-theoretic basis and consistent properties, but it is computationally expensive. Other perturbation-based and gradient-based methods can be cheaper, but their results depend on choices such as the perturbation scheme or the baseline input, which should be stated. A reasonable practice is to apply at least two methods and report where they agree and diverge.

Can I use this approach for multilingual classification?

Yes, by replacing RoBERTa with a multilingual variant such as XLM-RoBERTa and applying the same post-hoc explanation pipeline. The attribution scores remain token-level. Tokenization and explanation quality may differ across languages, so explanations should be validated separately on held-out data for each language before they are reported.

How do I validate that explanations are faithful and not misleading?

Run faithfulness tests: mask or remove the top-attributed tokens and measure how much the prediction confidence falls, comparing this with the drop from masking randomly chosen tokens. Measures of this kind, such as comprehensiveness and sufficiency, are more informative when computed over many examples. A single unchanged prediction does not by itself show that an explanation is unfaithful.

Related methods

Sources

  1. Liu, Y., Ott, M., Goyal, N., Du, J., Joshi, M., Chen, D., Levy, O., Lewis, M., Zettlemoyer, L., & Stoyanov, V. (2019). RoBERTa: A Robustly Optimized BERT Pretraining Approach. arXiv preprint arXiv:1907.11692.

    Open publication reference
  2. Lundberg, 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