Key ideas
- Why explain: to trust and justify decisions (medicine, credit), to debug models that learned a shortcut, to check fairness, and to meet regulation — the GDPR's right to explanation concerns local explanations of individual decisions1. They matter most when what we want (fairness, safety) is not fully captured by the training objective, since a model can meet the objective with tricks its designers never intended1.
- Interpretability vs explainability: interpretability is understanding how the model works; explainability is understanding how it produced a given result1. A model can be interpretable by design (intrinsic: sparse linear model, short tree) or explained post-hoc after training2.
- Model-agnostic methods only query inputs and outputs; model-specific ones read internals (gradients, activations). Global methods describe average behaviour over a dataset; local methods explain one prediction2.
| Method | Scope | Agnostic? | Idea |
|---|---|---|---|
| Permutation importance | global | yes | score drop after shuffling a feature |
| PDP / ICE | global / per row | yes | prediction as one feature varies |
| LIME | local | yes | weighted local linear surrogate |
| Shapley / SHAP | local (aggregated: global) | Kernel SHAP yes | fair split of |
| Occlusion | local | yes | score drop when a patch is greyed out |
| Counterfactual | local | yes | smallest change that flips the decision |
| Gradient saliency, SmoothGrad | local | no | input gradient (averaged over noise) |
| Integrated gradients | local | no | gradients integrated from a baseline |
| Grad-CAM | local | no (CNNs) | gradient-weighted conv feature maps |
- PDP averages the prediction over the data with the feature of interest fixed; each row's curve is an ICE line, and the PDP is their mean3.
- Permutation importance: shuffle one column times on held-out data and average the score drop; it measures how much this model relies on the feature4.
- LIME samples perturbed inputs around , gets the black box's predictions, weights them by closeness to and fits a sparse linear model. For images its features are super-pixels switched on or greyed out2. Such explanations can expose shortcuts, e.g. a wolf-vs-husky classifier that relied on snow in the background2.
- Shapley values / SHAP average a feature's marginal contribution over all coalitions of the other features. They are the only additive attribution with local accuracy, missingness and consistency; they explain the gap between and the base value 5.
- Gradient saliency (vanilla gradient) differentiates the class score with respect to the pixels — a first-order Taylor view of the network2. For RGB images Simonyan et al. keep the largest absolute gradient over the three channels, and they work with the unnormalised score rather than the softmax probability, which can also rise when other classes' scores fall6. SmoothGrad averages such maps over noisy copies with noise level 10–20% of the pixel range2.
- Grad-CAM averages the gradients of the pre-softmax score over each feature map of (usually) the last convolutional layer, which still keeps the spatial layout, weights the maps with these averages, sums them and applies a ReLU to keep evidence for the class; the coarse map is upscaled and laid over the image2.
- Integrated gradients (IG) fixes the saturation problem of gradients: a flat region gives zero gradient even for an important input. It integrates gradients along the straight path from a baseline (black image, zero embedding) to and satisfies sensitivity, implementation invariance and completeness7. Check that the attributions sum to and raise the number of steps (20–300 is typical) if not7.
- Occlusion is perturbation-based: slide a grey patch over the image and record the drop of the class score. It is model-agnostic2, but needs one forward pass per patch position.
- Counterfactual explanations state the smallest change that would give the desired outcome ("with income 64k the loan would be approved"). Wachter et al. minimise a prediction loss plus an L1 distance in which each feature's change is divided by its median absolute deviation (MAD), which is robust to outliers and puts features on one scale2; the L1 form tends to change only a few features.
- Evaluating explanations: plausibility is how convincing an explanation looks to people, faithfulness is whether it reflects the model's actual reasoning8. Sanity checks randomise the weights or the labels: Guided Backprop and Guided Grad-CAM barely change, behaving like edge detectors, while vanilla gradients and Grad-CAM pass2. Attention weights are often read as a built-in explanation, but work on text classifiers has cast doubt on how faithful they are8.
Formulas
Permutation importance with reference score and shuffled scores 4:
Partial dependence (brute force over rows)3:
LIME: , where is an exponential proximity kernel such as whose width sets the neighbourhood2.
Shapley value of feature among features, and local accuracy5:
For SHAP, . Linear SHAP (independent features): 5.
Saliency of pixel : over channels 6. SmoothGrad: 2.
Grad-CAM2:
Integrated gradients and completeness7:
Counterfactual: 2.
Worked example
Shapley values of at , with absent features set to the baseline 0. Coalition values: , , , . With two features each coalition order has weight : and . They sum to : the interaction term is split equally.
Occlusion cost: a patch with stride on an image gives positions, e.g. forward passes.
In code
import torch
F = lambda z: z[..., 0] ** 2 * z[..., 1] # toy model F(x1, x2) = x1^2 * x2
x, x0 = torch.tensor([2.0, 3.0]), torch.zeros(2) # input and baseline
xg = x.clone().requires_grad_(True)
F(xg).backward()
print(xg.grad) # tensor([12., 4.]) vanilla gradient
m = 300 # Riemann steps
alphas = torch.arange(1, m + 1).view(-1, 1) / m
path = (x0 + alphas * (x - x0)).requires_grad_(True)
F(path).sum().backward()
ig = (x - x0) * path.grad.mean(dim=0) # integrated gradients
print(ig, ig.sum()) # tensor([8.0400, 4.0200]) tensor(12.0601)
The exact values are , summing to ; the gradient does not add up to the output7.
Common pitfalls
- A pretty map is not a faithful one: run sanity checks2, or erase the top-ranked inputs and check that the decision changes8. High test accuracy on data from the same collection does not rule out a Clever Hans shortcut such as a watermark, because the test set shares it1,2.
- Permutation importance shows reliance, not direction or causality; correlated features hide each other's importance, since the model still sees the copy4.
- A flat PDP can hide opposite effects in different rows — look at ICE lines; with correlated features PDPs evaluate impossible inputs3.
- SHAP values are measured from the base value , not from zero: a negative weight with a below-average input gives a positive 5.
- LIME's kernel width defines "local": with a very wide kernel, distant samples weigh as much as close ones, so the surrogate follows the global trend, which can even have the opposite sign to the local effect2.
- Plain gradients miss saturated features; IG fixes this, but any pixel equal to the baseline (black pixels with a black baseline) gets exactly zero7.
- Grad-CAM averages (not sums) the gradients, uses the pre-softmax score, keeps only positive evidence through the ReLU, and gives a map as coarse as the feature maps2.
- Attention weights are not explanations by default; check their faithfulness like any other attribution8.
References
- Wikipedia: Explainable artificial intelligence — Background; Goals; Explainability and interpretability techniques; Regulation
- Molnar, Interpretable Machine Learning (3rd ed., CC BY-NC-SA 4.0) — Ch. 3 Goals of Interpretability; Ch. 4 Methods Overview; Ch. 14 LIME; Ch. 15 Counterfactual Explanations (Method by Wachter et al.); Ch. 28 Saliency Maps (Vanilla Gradient, Grad-CAM, SmoothGrad, Limitations)
- scikit-learn User Guide — Sec. 5.1 Partial Dependence and ICE plots (5.1.2 ICE; 5.1.3 Mathematical Definition; 5.1.4 Computation methods)
- scikit-learn User Guide — Sec. 5.2 Permutation feature importance (5.2.1 Outline of the algorithm; 5.2.3 Misleading values on strongly correlated features)
- Lundberg & Lee (2017), A Unified Approach to Interpreting Model Predictions, arXiv:1705.07874 — Sec. 2.4 Classic Shapley Value Estimation (Eq. 4); Sec. 3 Properties 1–3 and Theorem 1; Sec. 4 SHAP values (Fig. 1); Sec. 4.2 Corollary 1 (Linear SHAP)
- Simonyan, Vedaldi & Zisserman (2014), Deep Inside Convolutional Networks, arXiv:1312.6034 — Sec. 2 Class Model Visualisation (scores vs posteriors); Sec. 3.1 Class Saliency Extraction
- Sundararajan, Taly & Yan (2017), Axiomatic Attribution for Deep Networks, arXiv:1703.01365 — Sec. 2 Two Fundamental Axioms; Sec. 3 Our Method (Completeness); Sec. 5 Applying Integrated Gradients
- Jacovi & Goldberg (2020), Towards Faithfully Interpretable NLP Systems: How should we define and evaluate faithfulness?, arXiv:2004.03685 — Sec. 2 Faithfulness vs. Plausibility; Sec. 3 Inherently Interpretable?; Sec. 6 Defining Faithfulness (erasure)