Add SHAP tutorial for Transformers - #38
Conversation
…peline and markdown documentation
…gnostic interpretation
There was a problem hiding this comment.
🟡 Not ready to approve
The PR introduces shap usage in code, but the transformer requirements file does not include/pin shap==0.52.0 (and SHAP versions are inconsistent across tutorials), which blocks reproducible execution as described in the PR notes.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR extends the transformer tutorial utilities to support SHAP-based explainability workflows (model evaluation with balanced accuracy and helper plotting for SHAP token attributions), aligning the transformer track with existing SHAP coverage in other tutorials.
Changes:
- Added
evaluate_model()to compute balanced accuracy for transformer text classification pipelines. - Added
plot_shap_values()to compare mean SHAP token attributions between train/test splits. - Introduced new dependencies in the transformer utils module (
shap,balanced_accuracy_score).
File summaries
| File | Description |
|---|---|
| xai-for-transformer/utils.py | Adds SHAP evaluation/plotting helpers and related imports for the transformer SHAP tutorial workflow. |
Review details
- Files reviewed: 1/4 changed files
- Comments generated: 4
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
donatella-cea
left a comment
There was a problem hiding this comment.
Really nice and easy to follow notebook!
I have a few suggestions on the figure and some small clarifications.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ad of token_name; update tutorial notebook accordingly to reflect this change.
The new SHAP text tutorial needs shap, pinned to 0.52.0 (the version the notebook was written against).
|
Hi @lisa-sousa, I started on merger note #1. I pinned
The SHAP part itself works fine on 0.52.0 (PartitionExplainer, the text masker, and the plots all run), so the pin is the right version. I just wanted to agree on the Python-3.12 move and the token_type_ids fix before touching CNN/RF. Let me know how you'd like to proceed. |
…ebook utils.evaluate_model computes balanced accuracy, so the 0.9498/0.8776 figures are balanced accuracy, not plain accuracy. Relabel the prints accordingly and note in the text why balanced accuracy is used (imbalanced emotion classes). Verified on the test split: plain accuracy 0.9295, balanced accuracy 0.8776, weighted F1 0.929.
|
On #3 (accuracy check): it's just the metric, not a data/split mismatch. On the same test split I get:
So the 0.9498 / 0.8776 the notebook prints are balanced accuracy (that's what The notebook was labeling them just "accuracy", so I relabeled the prints to "balanced accuracy" and added a short note explaining why we use it (the emotion classes are imbalanced). Pushed to the branch (76711dc). |
|
@donatella-cea when you have a moment, could you publish the draft weights Release? Task #2 on this PR (automatic download of the fine-tuned DistilBERT weights) is ready in the notebook, but it is blocked because the "DistilBERT emotion weights" release (tag Publishing it also unblocks the "Load the fully fine-tuned model" cell in notebook 1, so one publish covers both notebooks. You can find it under the repo's Releases tab, named "DistilBERT emotion weights". Thanks! |
Hi @ema265 I published the model weights as a new release. I'm following our release versioning, hence the new release is v2.1.0: https://github.com/HelmholtzAI-Consultants-Munich/XAI-Tutorials/releases/tag/v2.1.0 |
Hi @ema265
|
The DistilBERT emotion weights are now published under release v2.1.0, so update the download URL from the old draft tag and drop the TODO/NOTE comments. Verified end-to-end: downloads (247 MB, HTTP 200), unzips, and the model loads with all six emotion labels.
…requirements) Bump transformers[torch] 4.51.3 -> 5.13.0 (the version the notebook was tested with; under 4.51.3 the pipeline crashed on token_type_ids with the hosted DistilBERT tokenizer, 5.13.0 handles it natively). Add datasets (the notebook loads dair-ai/emotion) plus requests and pandas, which the notebook imports directly, all pinned. Mark the file as requiring Python 3.12 (shap 0.52.0 needs >= 3.12). Verified on a fresh 3.12 env: the flow reproduces the notebook's TEST balanced accuracy of 0.8776 and the SHAP PartitionExplainer runs on real data.
shap==0.52.0 is now in requirements_xai-for-transformer.txt, so the notebook no longer needs the inline '!pip install shap==0.52.0' (removes the leftover TODO).
Per Lisa's request to use the same Python version across all tracks (RF, CNNs, Transformers). Their shap pins are left unchanged (CNN 0.47.2, RF 0.51.0); bringing those to 0.52.0 for cross-track consistency is handled when all notebooks are integrated into main. Verified both requirement sets resolve on Python 3.12.
|
Hi @lisa-sousa, status from the merger side. Thanks for the answers and for publishing the weights. #1 pin shap 0.52.0: done. I pinned shap==0.52.0, bumped transformers to 5.13.0 (that cleared the token_type_ids crash, so it was purely the version, thanks for the pointer), and added the missing deps (datasets, pandas, requests), all pinned. I also added a "Requires Python 3.12" marker to all three requirements files (transformer, CNN, RF) as you asked. I re-tested the SHAP notebook end-to-end on a clean Python 3.12 env: full run, no errors, and it reproduces the published accuracy (TRAIN 0.9498, TEST 0.8776). #2 weights auto-download: done. I pointed the download at the new v2.1.0 release and verified it from a clean state (the notebook downloads, unzips, and loads the model). #3 accuracy: done earlier. The prints are balanced accuracy (from utils.evaluate_model), relabeled with a short note on why we use it. Testing checklist:
Before I merge: Donatella left two review threads that are still open, and since they are both on content you authored, you might want to take a look before I merge:
It would be good to have these resolved first. The figure is best updated on your side, but I am happy to apply the small text fixes myself if you would prefer. |
|
@ema265 thanks for the addressing all those points. I still have to update the notebook content according to Donatellas comments and I also reworked the full video slides and reda the docs content, so I will tailor the notebook accordingly. I'll let you and Donatella know when I updated the notebook. |
…tants-Munich/XAI-Tutorials into shap_for_transformers
|
@donatella-cea could you please have a look at the updated transformer notebook? I’ve addressed your comments and aligned the content with the new Read the Docs structure for SHAP. In addition, could you briefly review the updated SHAP content in the Random Forest and CNN notebooks? These have also been revised to follow the new structure and now include additional explainers: KernelSHAP for the Random Forest and PartitionExplainer for the CNN. @ema265 once Donatella has approved the changes, this should be ready to merge. Before merging, could you please double-check that all three notebooks still run correctly with the updated requirements? Thanks :) |
Everything looks good to me! @ema265, as Lisa mentioned, you can go on with the merging after double-checking that the notebooks run with the updated requirements. Thanks! |
|
Regarding google collab section, could you try out this version: Like this the student should not be ofrced to share their google drive for the course purpose and with --depth 1 it should create a shallow copy of the branch such that less data needs to be downloaded (important for on-site courses where the bandwidth might be liited). Thank you! |
Summary
Adds a new SHAP-based explainability tutorial for Transformer models, extending the existing SHAP coverage (currently used for Random Forests and CNNs) to transformer architectures.
Changes
xai-for-transformer/demonstrating SHAP applied to a transformer modelPin
shapversion in requirementsrequirements_xai-for-transformer.txtneedsshap==0.52.0pinned. Please also check whetherrequirements_xai-for-cnn.txtandrequirements_xai-for-random-forest.txtcan be pinned to the same version — if so, update them too and re-run those notebooks end-to-end to confirm nothing breaks with the newer SHAP release before merging.Enable automatic model weights download
The transformer notebook currently expects model weights to be available locally/manually placed. Model weights will be published as part of a GitHub Release — once that release is live, switch on/uncomment the automatic download step (pointing at the release asset URL) so users don't have to source weights manually.
Double-check reported train/test accuracy for the loaded transformer model
When running the intro notebook, I get:
using balanced accuracy. Can you confirm whether the notebook's original reported numbers were computed with balanced accuracy or a different metric (plain accuracy, F1, etc.)? If the notebook text states different figures, we should either update the text to match, or check if there's a metric/data-split mismatch causing the discrepancy.
Testing checklist
xai-for-transformer/run end-to-end withshap==0.52.0xai-for-cnn/andxai-for-random-forest/re-tested with pinned SHAP version