Sub topic 13
Using Attribution to Inspect Single Errors
Assume you already know how to evaluate a classifier and collect misclassified examples. This chapter focuses on one question: which parts of an input influenced a particular prediction? Attribution is a way to estimate each token's contribution to a model output. It helps you inspect an error, but it does not prove that the model “understood” a word or used a causal rule.
What attribution can reveal
Suppose a sentiment model predicts positive for:
> The camera is beautiful, but the battery dies quickly.
A token-level attribution may show strong positive weight for beautiful and weaker negative weight for dies. This suggests a possible failure mode: the model notices praise but does not give enough weight to the complaint.
Common attribution methods include:
| Method | Basic idea | Main limitation |
|---|---|---|
| Gradient attribution | Use output gradients with respect to token representations | Can be noisy or unstable |
| Integrated gradients | Accumulate gradients from a baseline input to the real input | Requires a meaningful baseline |
| Occlusion | Remove or mask one token and measure the output change | More expensive and affected by unnatural text |
| Attention inspection | Read attention weights as signals of focus | Attention is not automatically an explanation |
Use attribution to generate a hypothesis. Then test that hypothesis with similar examples, perturbations, or a retrained model.
A simple occlusion walkthrough
For a text classifier, record the original probability for the predicted class. Then mask one token at a time and run the model again. If the probability drops, that token supported the prediction. If it rises, the token pushed against it.
def occlusion_attribution(tokens, predict_probability, mask="<MASK>"):
"""Return token contributions for the model's predicted class."""
original_text = " ".join(tokens)
original_probability = predict_probability(original_text)
results = []
for index, token in enumerate(tokens):
masked = tokens.copy()
masked[index] = mask
masked_probability = predict_probability(" ".join(masked))
contribution = original_probability - masked_probability
results.append((token, contribution))
return sorted(results, key=lambda row: row[1], reverse=True)
def toy_positive_probability(text):
positive = {"beautiful", "great", "excellent"}
negative = {"terrible", "dies", "broken"}
words = set(text.lower().split())
score = sum(word in positive for word in words)
score -= sum(word in negative for word in words)
return 1 / (1 + 2.71828 ** (-score))
tokens = ["the", "camera", "is", "beautiful", "but", "the", "battery", "dies", "quickly"]
for token, contribution in occlusion_attribution(tokens, toy_positive_probability):
print(f"{token:>8} {contribution:+.3f}")This toy model is intentionally simple. A production model may use subword tokens, so one visible word can have several attribution values. Aggregate subword values carefully, and keep the original text beside the tokenized view.
From one explanation to a diagnosis
For every inspected error, save the input, gold label, prediction, confidence, attribution method, and your interpretation. Label the interpretation with a testable category such as “overweighted keyword,” “negation ignored,” or “spurious formatting signal.”
A useful diagram is:
Compare the erroneous example with correctly classified examples from the same class. If “not” receives little influence across many failures, the issue may be a systematic negation problem. If only one example looks strange, the label or wording may be unusual instead.
Exercise
A model predicts positive for “The product looks nice, but it stopped working.” Occlusion shows that removing “nice” sharply lowers positive probability, while removing “stopped” barely changes it. What hypothesis should you test?
Show answer
Test whether the model overweights positive appearance words and underweights failure language. Create more examples with praise followed by complaints, then measure whether the model handles the complaint consistently.