Explainability Methods
LIME, SHAP, saliency and their pitfalls.
A model denies a loan and the applicant asks why. LIME fits a small linear model around the case, SHAP divides the prediction among the features by the only rule that satisfies four axioms, a saliency map highlights the inputs the output is most sensitive to, and a counterfactual says what would have had to change. Each answers a different question, each can be precisely wrong, and some popular saliency methods draw nearly the same picture for a trained network as for one with random weights.
What you’ll learn
- Distinguish local from global and intrinsic from post hoc explanations, and faithfulness from plausibility.
- Write LIME's objective and algorithm, and explain why its explanations can be unstable and manipulable.
- State the Shapley axioms, prove that they determine the Shapley value uniquely, and explain the value-function choices behind SHAP and KernelSHAP.
- Explain gradient-based saliency methods and the model- and data-randomization sanity checks.
- Derive minimal counterfactual explanations for linear models in any norm, connect them to adversarial examples, and explain actionable recourse.
What an explanation is for
An explanation answers a question about a model, and different methods answer different questions. Global explanations describe a model's overall behavior, such as which features matter on average; local ones explain a single prediction. Intrinsically interpretable models, such as sparse linear models, short rule lists or small trees, are their own explanation, while post hoc methods explain a black box from outside. Rudin (2019) argues that for high-stakes decisions one should prefer an interpretable model whenever one is about as accurate, because a post hoc explanation is a second model that can be wrong about the first. Two properties are easily confused: faithfulness, whether the explanation reflects what the model actually computes, and plausibility, whether it convinces a person. Only faithfulness is a property of the model.
| Method | Question it answers | Access needed | Characteristic failure |
|---|---|---|---|
| LIME | Which features would a simple model use to mimic this one near ? | queries | depends on the sampling kernel; unstable; queries leave the data manifold |
| SHAP | How should minus a baseline be divided among the features? | queries (exact for trees) | depends on the value function and baseline; exponential cost; correlated features |
| Gradients, integrated gradients, Grad-CAM | Which input directions is the output locally sensitive to? | gradients | saturation and noise; some variants ignore the model |
| Counterfactuals | What is the smallest change that would flip the decision? | queries or gradients | infeasible, implausible or non-unique changes |
LIME: local surrogate models
function LIME(f, x, N, K, σ):
samples ← empty
repeat N times:
z′ ← random subset of x's interpretable components # drop words, gray out superpixels
z ← the input built from z′
add (z′, f(z), weight = exp(−D(x, z)² / σ²)) to samples
S ← K components chosen on the samples (e.g. by a LASSO path)
g ← weighted least squares of f(z) on the components S of z′
return the K coefficients of g # the explanationThree weaknesses follow from the construction. The explanation depends on the kernel width and on the random sample, so two runs can rank the features differently. Perturbed samples, such as an image with gray patches, can be unlike anything in the data, so describes the model where it was never trained. And that can be exploited: Slack et al. (2020) built a classifier that detects such off-manifold queries and answers them with an innocuous model, while on real inputs it relied on race, and LIME and KernelSHAP explanations then hid the dependence.
Shapley values and SHAP
A cooperative game on players assigns each coalition a value . To explain at , the players are the features, and is the model's output when only the features in are "present". Conditionally, ; interventionally, , with the absent features drawn from a background distribution.
The formula satisfies the axioms. Add the players in a uniformly random order and credit each with its marginal contribution when it joins; is the probability that exactly the set precedes . In every order the contributions telescope, which gives efficiency, and symmetry, null player and additivity can be read off the formula:
Uniqueness. Subtract , so that . The unanimity games , , form a basis of all such games (Möbius inversion), and the coefficients are the Harsanyi dividends:
In the game , players outside are null and get 0, players inside are symmetric, and efficiency splits among them, so each gets . Additivity then fixes on every game: any rule satisfying the axioms gives each player an equal share of the dividend of every coalition it belongs to, which is the Shapley value.
Explain at , with absent features set to a baseline of 0. What is ?
- 0
- 0.5
- 1
- 2
Show the answer
C. 1
The dividends are 1 for and 2 for , and every other coalition's is 0. The interaction's 2 is split equally between the two features that create it, so , which sums to as efficiency requires.
SHAP (Lundberg & Lee, 2017) writes an explanation as an additive model and shows that local accuracy, missingness and a monotonicity property called consistency single out the Shapley values of . KernelSHAP estimates them by weighted least squares on sampled coalitions with the Shapley kernel , under which the regression solution is exactly the Shapley values; exact computation needs evaluations of . TreeSHAP computes them exactly for tree ensembles in polynomial time (Lundberg et al., 2020). For a linear model the interventional values are , which equal gradient times (input minus baseline) and integrated gradients from the mean as baseline, whose completeness is proved in Mechanistic Interpretability.
Saliency maps and sanity checks
For differentiable models, the gradient of the class score, , is the oldest saliency map (Simonyan, Vedaldi & Zisserman, 2014). Its variants trade noise for other problems: gradient × input; SmoothGrad, which averages gradients over noisy copies of (Smilkov et al., 2017); integrated gradients, which integrates along a path from a baseline (Mechanistic Interpretability); Grad-CAM, which weights the last convolutional layer's feature maps by their average gradients (Selvaraju et al., 2017); and guided backpropagation, which also zeroes negative gradients at every ReLU on the way back (Springenberg et al., 2015). For a linear model the gradient is for every input, an explanation of the model rather than of the case, while gradient × input, , varies with the input and sums to .
Think first How can a method that barely depends on the weights still produce a convincing saliency map?
Natural images carry most of their structure in edges and object boundaries. A method whose output is dominated by the input highlights the object's outline whatever the weights; Nie, Zhang & Patel (2018) showed that guided backpropagation essentially performs partial image recovery. The map looks plausible because it is a picture of the input, which is exactly why plausibility cannot substitute for faithfulness. Kindermans et al. (2019) found a related failure: shifting every input by a constant that the first layer's bias exactly cancels leaves the model's outputs unchanged, yet changes gradient × input and integrated gradients with a zero baseline.
Counterfactual explanations and recourse
A counterfactual explanation answers a different question: what is the smallest change to this input that would have changed the decision? Wachter, Mittelstadt & Russell (2018) define it as , with the desired output and an distance in which each feature is scaled by its median absolute deviation, which favors changing few features. It needs no access to the model's internals and says nothing about why the model works as it does; it says what would have been enough.
Let with decision , and suppose . The decision flips once , so the smallest change in a norm solves
By Hölder's inequality , where is the dual norm, with equality for a suitably aligned . So the distance to the decision boundary is :
The answer changes only the feature with the largest , which is why Wachter et al.'s weighted distance yields sparse counterfactuals. The answer moves every input by the same amount along : it is the fast gradient sign step of an adversarial example. A minimal counterfactual and a minimal adversarial perturbation are the same mathematical object.
The adversarial widget makes this concrete. Its model is logistic regression on images of 3s and 8s, and its FGSM step moves every pixel by along , clipped to valid pixel values. For a linear model, that is the largest possible change of the logit among valid images within distance , so the smallest that flips the decision is the distance to the nearest counterfactual image.
A counterfactual helps the person affected only if they can act on it. Recourse requires actionability (age and race cannot be changed, and some changes are costly), plausibility (the counterfactual should look like a real applicant, not like the image above) and causal consistency (raising one's income also changes one's debt-to-income ratio). Ustun, Spangher & Liu (2019) compute minimal-cost actionable changes for linear classifiers by integer programming, and Karimi, Schölkopf & Valera (2021) argue that recourse should be computed as interventions in a causal model rather than as independent feature edits. Wachter et al. proposed counterfactuals partly as explanations compatible with the EU's General Data Protection Regulation that do not require opening the black box. Whether that regulation creates a legal right to explanation at all is debated: Wachter, Mittelstadt & Floridi (2017) argue that it does not, and Selbst & Powles (2017) that it does.
Key takeaways
- Explanations answer different questions: local or global, intrinsic or post hoc. Faithfulness to the model, not plausibility to people, is what makes an explanation evidence.
- LIME fits a proximity-weighted sparse linear surrogate to perturbed queries; its answers depend on the kernel and the sample, and off-manifold queries make it manipulable.
- The Shapley value is the unique attribution satisfying efficiency, symmetry, null player and additivity: equal shares of every coalition's Harsanyi dividend. SHAP's answers depend on the value function, since interventional values leave the data manifold and conditional values credit correlated but unused features.
- Saliency methods must pass sanity checks: plain gradients and Grad-CAM change when the weights are randomized, while guided backpropagation largely does not.
- For a linear model the minimal counterfactual distance is in the dual norm, and in it is the FGSM step. Useful recourse also needs actionability, plausibility and causal structure.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
SHAP is computed with the conditional value function . Can a feature that the model never reads receive a nonzero Shapley value?
- No: the null-player axiom guarantees it gets zero
- Yes, if it is correlated with features the model does read, because conditioning on it changes ; the axiom applies to the game , not to the model
- Only if the model is nonlinear
- Only because of KernelSHAP's sampling error
Show the answer
B. Yes, if it is correlated with features the model does read, because conditioning on it changes ; the axiom applies to the game , not to the model
If is unused but correlated with a used feature , then changes when joins , because conditioning on shifts the distribution of . So is not a null player in and receives credit. With the interventional value function, an unused feature does get zero, at the price of evaluating the model off the data manifold.
Guided backpropagation produces nearly identical maps for a trained network and for the same network with randomized weights. What does that show?
- The network learned nothing
- The method mainly reflects the input, such as its edges, rather than what the model learned, so it fails the model-randomization test
- Randomized networks are as accurate as trained ones
- Guided backpropagation is the most faithful method
Show the answer
B. The method mainly reflects the input, such as its edges, rather than what the model learned, so it fails the model-randomization test
A faithful explanation must change when the model changes. If randomizing the weights leaves the map unchanged, the map is a property of the input, which is why it can look plausible while saying little about the model. Plain gradients and Grad-CAM passed this test in Adebayo et al.'s experiments.
A linear classifier with gives , a rejection. What is the smallest Euclidean change to that reaches the decision boundary?
- 0.4, in the direction of
- 2, in the direction of
- 0.5, changing only the second feature
- 0.4, in the direction of
Show the answer
A. 0.4, in the direction of
, so the distance is , along , the direction that increases . Changing only the second feature, the one with the largest , costs : that is the minimal change, not the minimal change.
Two LIME runs on the same input rank its top features differently. What is the most likely cause?
- The model changed between the runs
- LIME fits its surrogate to randomly sampled perturbations weighted by a kernel, so sampling noise and the kernel width change the fitted weights
- LIME is exact, so the input must have changed
- The interpretable representation was not binary
Show the answer
B. LIME fits its surrogate to randomly sampled perturbations weighted by a kernel, so sampling noise and the kernel width change the fitted weights
The surrogate is a weighted regression on a finite random sample of perturbations; different samples, especially with few of them, give different coefficients, and the kernel width decides how local the fit is. Fixing seeds hides the instability without removing it.
Further reading
- M. T. Ribeiro, S. Singh & C. Guestrin, "'Why should I trust you?' Explaining the predictions of any classifier," KDD (2016).
- S. M. Lundberg & S.-I. Lee, "A unified approach to interpreting model predictions," NeurIPS (2017).
- J. Adebayo, J. Gilmer, M. Muelly, I. Goodfellow, M. Hardt & B. Kim, "Sanity checks for saliency maps," NeurIPS (2018).
- S. Wachter, B. Mittelstadt & C. Russell, "Counterfactual explanations without opening the black box: Automated decisions and the GDPR," Harvard Journal of Law & Technology (2018).
- C. Molnar, Interpretable Machine Learning: A Guide for Making Black Box Models Explainable (2nd ed., 2022).