Skip to content
ml-recall

Topic notes

Explainable AI

Explaining what trained models do — global vs local and post-hoc methods, permutation importance, PDP/ICE, LIME, SHAP, saliency, Grad-CAM, integrated gradients, and how to test explanations.

5-min read AI-checked

Practise 34 questions Take a test

Key ideas

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 f(x)−E[f(X)]f(x) - \mathbb{E}[f(X)]
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

Formulas

Permutation importance with reference score ss and shuffled scores sk,js_{k,j}4: ij=s−1K∑k=1Ksk,ji_j = s - \frac{1}{K}\sum_{k=1}^{K} s_{k,j}

Partial dependence (brute force over nn rows)3: pdXS(xS)≈1n∑i=1nf(xS,xC(i))\mathrm{pd}_{X_S}(x_S) \approx \frac{1}{n}\sum_{i=1}^{n} f(x_S, x_C^{(i)})

LIME: ξ(x)=arg min⁡g∈GL(f,g,πx)+Ω(g)\xi(x) = \operatorname*{arg\,min}_{g \in G} L(f, g, \pi_x) + \Omega(g), where πx\pi_x is an exponential proximity kernel such as exp⁡(−D(x,z)2/σ2)\exp(-D(x, z)^2/\sigma^2) whose width σ\sigma sets the neighbourhood2.

Shapley value of feature ii among MM features, and local accuracy5: ϕi=∑S⊆F∖{i}∣S∣! (M−∣S∣−1)!M! [v(S∪{i})−v(S)],∑i=1Mϕi=v(F)−v(∅)\phi_i = \sum_{S \subseteq F \setminus \{i\}} \frac{|S|!\,(M - |S| - 1)!}{M!}\,\bigl[v(S \cup \{i\}) - v(S)\bigr], \qquad \sum_{i=1}^{M} \phi_i = v(F) - v(\emptyset)

For SHAP, v(F)−v(∅)=f(x)−E[f(X)]v(F) - v(\emptyset) = f(x) - \mathbb{E}[f(X)]. Linear SHAP (independent features): ϕi=wi(xi−E[xi])\phi_i = w_i(x_i - \mathbb{E}[x_i])5.

Saliency of pixel (i,j)(i, j): Mij=max⁡k∣∂Sc/∂Iijk∣M_{ij} = \max_k \lvert \partial S_c / \partial I_{ijk} \rvert over channels kk6. SmoothGrad: M^c(x)=1n∑k=1nMc(x+N(0,σ2))\hat{M}_c(x) = \frac{1}{n}\sum_{k=1}^{n} M_c(x + \mathcal{N}(0, \sigma^2))2.

Grad-CAM2: αkc=1Z∑i∑j∂yc∂Aijk,Lc=ReLU(∑kαkcAk)\alpha_k^c = \frac{1}{Z}\sum_{i}\sum_{j}\frac{\partial y^c}{\partial A^k_{ij}}, \qquad L^c = \mathrm{ReLU}\Bigl(\sum_k \alpha_k^c A^k\Bigr)

Integrated gradients and completeness7: IGi(x)=(xi−xi′)∫01∂F(x′+α(x−x′))∂xi dα,∑iIGi(x)=F(x)−F(x′)\mathrm{IG}_i(x) = (x_i - x'_i)\int_0^1 \frac{\partial F(x' + \alpha(x - x'))}{\partial x_i}\,d\alpha, \qquad \sum_i \mathrm{IG}_i(x) = F(x) - F(x')

Counterfactual: min⁡x′λ (f(x′)−y′)2+∑k∣xk−xk′∣/MADk\min_{x'} \lambda\,(f(x') - y')^2 + \sum_k |x_k - x'_k| / \mathrm{MAD}_k2.

Worked example

Shapley values of f(x1,x2)=x1+2x2+x1x2f(x_1, x_2) = x_1 + 2x_2 + x_1x_2 at x=(1,2)x = (1, 2), with absent features set to the baseline 0. Coalition values: v(∅)=0v(\emptyset) = 0, v({1})=1v(\{1\}) = 1, v({2})=4v(\{2\}) = 4, v({1,2})=7v(\{1, 2\}) = 7. With two features each coalition order has weight 12\frac{1}{2}: ϕ1=12[(1−0)+(7−4)]=2\phi_1 = \frac{1}{2}[(1 - 0) + (7 - 4)] = 2 and ϕ2=12[(4−0)+(7−1)]=5\phi_2 = \frac{1}{2}[(4 - 0) + (7 - 1)] = 5. They sum to f(x)−f(0)=7f(x) - f(0) = 7: the interaction term x1x2=2x_1x_2 = 2 is split equally.

Occlusion cost: a k×kk \times k patch with stride ss on an H×HH \times H image gives ((H−k)/s+1)2((H - k)/s + 1)^2 positions, e.g. ((32−8)/4+1)2=49((32 - 8)/4 + 1)^2 = 49 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 IG=(8,4)\mathrm{IG} = (8, 4), summing to F(x)−F(x′)=12F(x) - F(x') = 12; the gradient (12,4)(12, 4) does not add up to the output7.

Common pitfalls

References

  1. Wikipedia: Explainable artificial intelligence — Background; Goals; Explainability and interpretability techniques; Regulation
  2. 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)
  3. 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)
  4. 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)
  5. 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)
  6. 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
  7. 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
  8. 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)