Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Asking a model why
When a model denies a loan or flags a scan, people want to know which inputs drove the decision, and developers want to catch models that rely on the wrong things. Attribution methods assign each input feature a share of the output. They are easy to compute and easy to over-trust: different methods give different answers, and an explanation can look convincing while being wrong.
Model: 3 tanh(2x1) + 2 x2 x3 + 1.5 max(x4 − 0.5, 0), with features 5 to 8 ignored. Each method explains 300 random inputs (baseline 0) and is compared with the exact Shapley values.
Bars: share of inputs where the method's top feature is truly the most important. Gradients miss saturated and thresholded effects; methods that integrate over the path from the baseline get them right, at more model calls.
In Exercise 2, integrated gradients named the most important feature correctly for every input, while the raw gradient did so for only 32.3%.
- 1. A feature attribution assigns…
- 2. Different attribution methods applied to the same prediction…
- 3. The gradient of the output with respect to an input measures…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Saliency (K. Simonyan, A. Vedaldi and A. Zisserman, 2014): the gradient of the output with respect to the input; local and blind to saturation.
- Gradient × input: gradient times (input − baseline); exact for linear models.
- Integrated gradients (M. Sundararajan, A. Taly and Q. Yan, 2017): (x − b) times the average gradient along the straight path from baseline b to x; satisfies completeness (attributions sum to f(x) − f(b)).
- LIME (topic D17-153): a weighted linear surrogate fitted to perturbed samples around the input.
- SHAP (topic D17-154): Shapley values of the game v(S) = model output with features in S present; unique under efficiency, symmetry, null player and additivity; exact computation needs 2^d evaluations, so it is sampled or approximated.
- Baselines: every method above explains relative to what “absent” means (zeros, a mean, a background sample), and the answer depends on that choice.
- Limits: attributions describe the model, not the world; correlated features, sanity checks and human evaluation matter.
- 1. Completeness of integrated gradients means the attributions…
- 2. Exact Shapley values for d features need about…
- 3. LIME explains a prediction by…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Testing attributions against the truth
Exercise 1.
- Implement the gradient by central differences, gradient × input and integrated gradients.
- Compute exact Shapley values by enumerating every subset of features.
- Fit LIME with random on/off masks and an exponential kernel.
- Check that all but the gradient agree on a linear model.
Practice.
- Validate explanation methods on models with known answers before trusting them on real ones.
- State the baseline and try more than one.
- Check stability: rerun sampled methods and compare top features.
- Treat correlated features with care: the model may use either, and equally good models may differ.
- Do not read an attribution as a cause in the world; it describes the model’s computation.
def integrated_gradients(f, x, b, steps=50):
g = np.mean([saliency(f, b + (k + 0.5) / steps * (x - b))
for k in range(steps)], axis=0)
return g * (x - b)
phi = shapley_exact(model, x, np.zeros(8)) # 256 subsets
print(np.abs(integrated_gradients(model, x, np.zeros(8)) - phi).sum())- 1. With 2 integration steps, integrated gradients missed completeness by about…
- 2. Two least-squares fits on bootstrap samples, with x2 a near copy of x1, gave weights on x1 of…
- 3. Sampled Shapley with 400 orders had a relative error of about…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Completeness. Along the path z(α) = b + α(x − b), the fundamental theorem of calculus gives f(x) − f(b) = ∫ ∇f(z(α)) · (x − b) dα, the sum of the integrated-gradient attributions. A midpoint rule with m steps has error falling like 1/m² for smooth functions, more slowly at kinks.
Additive models. If f is a sum of functions of single features, f = Σ gᵢ(xᵢ), then the exact Shapley value and integrated gradients both give gᵢ(xᵢ) − gᵢ(bᵢ). For a product 2x₂x₃ with baseline 0, both give x₂x₃ to each feature, splitting the output equally.
Where gradients fail. For 3 tanh(2x₁) the gradient is 6 sech²(2x₁), which vanishes as x₁ grows: at x₁ = 1 gradient × input gives 0.42 while the true contribution is 3 tanh(2) = 2.89. For 1.5 max(x₄ − 0.5, 0) gradient × input gives 1.5x₄ instead of 1.5(x₄ − 0.5), and for the product it gives 2x₂x₃ to each feature, counting the interaction twice.
Sampling. Monte Carlo Shapley with M random orders is unbiased, with error falling like one over the square root of M; each order costs d + 1 model calls.
- 1. For an additive model, Shapley values and integrated gradients give each feature…
- 2. Gradients understate saturated features because…
- 3. Gradient × input on a product of two features…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Which attribution methods recover a model’s true feature contributions, and at what cost?
History.
- K. Simonyan, A. Vedaldi and A. Zisserman (2014) introduced gradient saliency maps for image classifiers.
- M. Ribeiro, S. Singh and C. Guestrin (2016) proposed LIME; S. Lundberg and S.-I. Lee (2017) unified attribution methods under Shapley values as SHAP, building on L. Shapley’s 1953 value for cooperative games.
- M. Sundararajan, A. Taly and Q. Yan (2017) proposed integrated gradients from the axioms of sensitivity and implementation invariance.
- J. Adebayo and colleagues (2018) showed that some saliency methods barely change when a model’s weights are randomised, failing basic sanity checks; C. Rudin (2019) argued for inherently interpretable models in high-stakes decisions.
Exercise 2.
- Model f(x) = 3 tanh(2x₁) + 2x₂x₃ + 1.5 max(x₄ − 0.5, 0) on 8 standard-normal features; features 5 to 8 are ignored.
- Exact Shapley values with baseline 0 by enumerating all 256 subsets, at 300 random inputs.
- Each method is scored by its relative L1 error against the exact values, whether its top-ranked feature is truly a top feature, the share of attribution on ignored features, and its model calls per explanation.
Seed 0:
| method | error | top feature right | on ignored features | model calls |
|---|---|---|---|---|
| gradient | 2.343 | 32.3% | 0.0% | 16 |
| gradient × input | 0.743 | 46.0% | 0.0% | 16 |
| integrated gradients (50 steps) | 0.001 | 100.0% | 0.0% | 800 |
| LIME (500 samples) | 0.061 | 98.7% | 1.8% | 500 |
| sampled Shapley (25 orders) | 0.048 | 98.3% | 0.0% | 225 |
Seeds 1 and 2: gradient × input errors 0.744 and 0.763 with the top feature right 43.3% and 42.3% of the time; integrated gradients errors 0.0006 and 0.0005 with the top feature right every time; LIME 0.063 and 0.057; sampled Shapley 0.049 and 0.037. (The gradient’s error, 2.71 and 4.05, is not in output units and only its ranking is meaningful.)
Findings.
- Integrated gradients matched the exact Shapley values for this model, because each term depends on single features or is symmetric, at 800 model calls per explanation.
- LIME and sampled Shapley had errors of about 4% to 6%, with LIME placing about 2% of its attribution on ignored features through sampling noise.
- The gradient and gradient × input ranked the wrong feature first for most inputs, because the saturating term has a vanishing gradient where its contribution is largest.
- Cheap explanations can be confidently wrong; test methods on models with known answers.
Interpretability in practice
- Pick the question first: which features drove this decision, what would change it, or what the model learned overall.
- Use methods with guarantees where you can, such as integrated gradients or exact or well-sampled Shapley values, and report the baseline.
- Run sanity checks: randomise the model or labels and confirm that explanations change.
- Prefer interpretable models (sparse linear models, small trees, additive models) when stakes are high and accuracy allows.
- Document explanations with the model and their limits (topic P-5).
References
- K. Simonyan, A. Vedaldi and A. Zisserman (2014). “Deep inside convolutional networks: visualising image classification models and saliency maps.” ICLR workshop.
- M. T. Ribeiro, S. Singh and C. Guestrin (2016). “Why should I trust you? Explaining the predictions of any classifier.” KDD.
- S. M. Lundberg and S.-I. Lee (2017). “A unified approach to interpreting model predictions.” NeurIPS.
- M. Sundararajan, A. Taly and Q. Yan (2017). “Axiomatic attribution for deep networks.” ICML.
- J. Adebayo, J. Gilmer, M. Muelly, I. Goodfellow, M. Hardt and B. Kim (2018). “Sanity checks for saliency maps.” NeurIPS.