Add per-class token and sentence attributions with interactive visualizer#250
Merged
tmills merged 4 commits intoApr 17, 2026
Merged
Conversation
091b5e7
into
Machine-Learning-for-Medical-Language:main
13 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lots of help from Claude on this one, but carefully steered and scrutinized by me.
rest_interpbranch, slightly modified to return signed per-class scores rather than a single unsigned saliency scoreGET /endpoint that serves an interactive HTML visualizer for exploring model predictions and attributionsToken attributions (
return_attributions=true)Uses the input × gradient method: one forward pass through the encoder, then one backward pass per class label (reusing the computation graph via
retain_graph=True). The signed dot product of gradient and embedding over the hidden dimension gives a per-token, per-class score in[-1, 1]:Each token in the response includes
token_id, character-levelstart/endoffsets into the original input string, and ascoresdict keyed by label. Special tokens ([CLS],[SEP]) are included withstart == end == 0. Only classification tasks are supported; tagging and relations tasks log a warning and are skipped.Sentence attributions (
return_sentence_attributions=true)Uses leave-one-out ablation: runs one batched forward pass over the full text and each ablated variant (one sentence removed), then for each class computes
p(class | full) − p(class | ablated). Scores are per-class and signed with the same polarity convention. Single-sentence inputs return a score of0.0for all classes.Visualizer (
GET /)A self-contained HTML page (no external dependencies) served at the root of each model's route prefix. Features:
/processquery parametersThe HTML file is bundled as package data via
importlib.resourcesand declared inpyproject.tomlso it is included correctly in installed wheels.