Level 5 — MasteryInterpretability & Explainability

Interpretability & Explainability

90 minutes60 marksprintable — key stays hidden on paper

Level: 5 (Mastery — cross-domain: math + coding + proof/derivation) Time limit: 90 minutes Total marks: 60

Instructions: Answer all three questions. Show full derivations. Where code is requested, pseudocode or runnable NumPy/PyTorch is acceptable. Justify every modelling assumption.


Question 1 — Shapley Values: Axioms, Kernel, and Superposition (22 marks)

The SHAP framework attributes a prediction f(x)f(x) to features via Shapley values from cooperative game theory. For a value function v:2{1,,n}Rv:2^{\{1,\dots,n\}}\to\mathbb{R} with v()=0v(\emptyset)=0, the Shapley value of feature ii is

ϕi  =  SN{i}S!(nS1)!n![v(S{i})v(S)].\phi_i \;=\; \sum_{S\subseteq N\setminus\{i\}} \frac{|S|!\,(n-|S|-1)!}{n!}\,\bigl[v(S\cup\{i\}) - v(S)\bigr].

(a) Prove the efficiency (local accuracy) axiom: i=1nϕi=v(N)v()\sum_{i=1}^n \phi_i = v(N) - v(\emptyset). Structure your proof around counting, over all n!n! orderings, the marginal contributions. (6)

(b) Consider a purely linear model f(x)=b+jwjxjf(x) = b + \sum_{j} w_j x_j with value function v(S)=E[f(X)XS=xS]E[f(X)]v(S) = \mathbb{E}\big[f(X)\mid X_S = x_S\big] - \mathbb{E}[f(X)] under feature independence. Derive a closed form for ϕi\phi_i and interpret why SHAP reduces to the "input times weight-deviation" attribution here. (5)

(c) Superposition tension. Suppose an interpretability tool assumes each of nn latent neurons encodes exactly one human concept, but the network actually stores m>nm > n concepts in superposition as near-orthogonal directions {u1,,um}Rn\{u_1,\dots,u_m\}\subset\mathbb{R}^n with ua,ub\langle u_a,u_b\rangle small for aba\neq b. Using the Johnson–Lindenstrauss lemma, state (with the standard bound) the minimum dimension nn needed to embed mm vectors with pairwise distances preserved to distortion ε\varepsilon. Then argue quantitatively why single-neuron Shapley attribution can be misleading under superposition. (6)

(d) LIME fits a local surrogate by minimising L(g)=zπx(z)(f(z)g(z))2+Ω(g)\mathcal{L}(g)=\sum_z \pi_x(z)\,(f(z)-g(z))^2 + \Omega(g). Give one concrete failure mode where LIME and SHAP disagree on feature ranking, and identify which SHAP axiom LIME may violate. (5)


Question 2 — Grad-CAM Derivation and a Saliency Sanity Check (20 marks)

(a) For a CNN with final conv feature maps AkRH×WA^k \in \mathbb{R}^{H\times W} (channel kk) and class score ycy^c (pre-softmax logit), derive the Grad-CAM localization map

LGrad-CAMc=ReLU ⁣(kαkcAk),αkc=1ZijycAijk.L^c_{\text{Grad-CAM}} = \mathrm{ReLU}\!\Big(\sum_k \alpha_k^c A^k\Big), \qquad \alpha_k^c = \frac{1}{Z}\sum_{i}\sum_{j}\frac{\partial y^c}{\partial A^k_{ij}}.

Explain the role of ZZ, the global-average-pooling of gradients, and the final ReLU\mathrm{ReLU}. (8)

(b) Prove that for a network whose head is a single linear layer yc=kwkcGAP(Ak)y^c = \sum_k w^c_k \,\mathrm{GAP}(A^k) on top of the conv maps (i.e. GAP then linear), Grad-CAM's weights satisfy αkc=1ZwkcZ=wkc\alpha_k^c = \tfrac{1}{Z} w^c_k \cdot Z = w^c_k up to the pooling constant — i.e. Grad-CAM reduces to CAM. Show the gradient computation explicitly. (6)

(c) Sanity-check coding task. Adebayo et al. showed some saliency methods fail a model-parameter randomization test. Write pseudocode for this test and state the pass/fail criterion. Explain, in probabilistic terms, why a method that passes gives more trustworthy explanations. (6)


Question 3 — Activation Patching, Probing, and Counterfactuals (18 marks)

(a) Activation patching (causal tracing). Define clean run xcleanx_{\text{clean}} with output logit clean\ell_{\text{clean}} and corrupted run xcorrx_{\text{corr}} with corr\ell_{\text{corr}}. Give the formal definition of the patching effect at layer LL, position tt (denominator-normalised), and explain why this is an interventional/causal metric rather than a correlational one. (6)

(b) Probing pitfall (theory). A linear probe achieves accuracy aa predicting concept cc from representation hh. Prove that high probe accuracy does not imply the model uses concept cc: construct a minimal counterexample where cc is linearly decodable from hh yet has zero causal effect on the output. Relate this to why activation patching is the stronger test. (6)

(c) Counterfactual explanation optimisation. A counterfactual for input xx w.r.t. classifier ff and target class yy' minimises

C(x)=λ(f(x)y)2+d(x,x).\mathcal{C}(x') = \lambda\,(f(x') - y')^2 + d(x,x').

Take d(x,x)=xx1d(x,x')=\|x-x'\|_1 and a logistic model f(x)=σ(wx+b)f(x)=\sigma(w^\top x + b). Derive the gradient xC\nabla_{x'}\mathcal{C}, and explain why the 1\ell_1 term produces sparse counterfactuals (few features changed). (6)


Answer keyMark scheme & solutions

Question 1

(a) Efficiency proof — 6 marks

Write the Shapley value in ordering form. Let Π\Pi be the set of all n!n! permutations of NN. For πΠ\pi\in\Pi, let Prei(π)\mathrm{Pre}_i(\pi) be the set of players preceding ii. Then

ϕi=1n!πΠ[v(Prei(π){i})v(Prei(π))].\phi_i = \frac{1}{n!}\sum_{\pi\in\Pi}\big[v(\mathrm{Pre}_i(\pi)\cup\{i\}) - v(\mathrm{Pre}_i(\pi))\big].

(2 marks — equivalence of weight S!(nS1)!n!\frac{|S|!(n-|S|-1)!}{n!} to counting orderings where the S|S| predecessors and nS1n-|S|-1 successors are permuted freely.)

Summing over ii and swapping order of summation:

iϕi=1n!πΠi[v(Prei(π){i})v(Prei(π))].\sum_i \phi_i = \frac{1}{n!}\sum_{\pi\in\Pi}\sum_i \big[v(\mathrm{Pre}_i(\pi)\cup\{i\}) - v(\mathrm{Pre}_i(\pi))\big].

(1 mark.)

For a fixed permutation π\pi, the inner sum telescopes: adding players one at a time from \emptyset to NN gives

i[v(Sk)v(Sk1)]=v(N)v().\sum_i \big[v(S_{k}) - v(S_{k-1})\big] = v(N) - v(\emptyset).

(2 marks — telescoping is the key step.)

Hence iϕi=1n!n!(v(N)v())=v(N)v()\sum_i\phi_i = \frac{1}{n!}\cdot n!\,\big(v(N)-v(\emptyset)\big) = v(N)-v(\emptyset). \blacksquare (1 mark.)

(b) Linear model closed form — 5 marks

Under independence, E[f(X)XS=xS]=b+jSwjxj+jSwjE[Xj]\mathbb{E}[f(X)\mid X_S=x_S] = b + \sum_{j\in S} w_j x_j + \sum_{j\notin S} w_j\,\mathbb{E}[X_j]. (2) So v(S)=jSwj(xjE[Xj])v(S) = \sum_{j\in S} w_j\,(x_j - \mathbb{E}[X_j]). (1) The marginal contribution of ii is v(S{i})v(S)=wi(xiE[Xi])v(S\cup\{i\})-v(S) = w_i(x_i-\mathbb{E}[X_i])independent of SS. (1) Since Shapley weights sum to 1,

ϕi=wi(xiE[Xi]).\boxed{\phi_i = w_i\,(x_i - \mathbb{E}[X_i]).}

Interpretation: attribution = weight × deviation of feature from its baseline mean — "input×(weight,centered)". (1)

(c) Superposition & JL — 6 marks

JL lemma: for mm points, distortion ε(0,1)\varepsilon\in(0,1), a random projection into

n=O ⁣(lnmε2)n = O\!\left(\frac{\ln m}{\varepsilon^2}\right)

dimensions preserves all pairwise distances up to factor (1±ε)(1\pm\varepsilon). (2 marks — statement + bound.)

Consequence: meε2nm \sim e^{\varepsilon^2 n} near-orthogonal directions fit in nn dims — exponentially more concepts than neurons. (1)

Misleading single-neuron Shapley: if concept uau_a is spread across many neurons (superposition), no single neuron ii "is" the concept; ϕi\phi_i splits credit across a distributed code, and a neuron carrying parts of several concepts yields an attribution that cannot be read as "concept aa's importance." The attribution is well-defined for the neuron but not for the human concept, because the concept→neuron map is many-to-many. (3 marks: distributed code + polysemantic neuron + interpretation gap.)

(d) LIME vs SHAP — 5 marks

Failure mode (any valid): with correlated/interacting features, LIME's locally-fit linear surrogate weights depend on the sampling kernel πx\pi_x and perturbation distribution; a strongly interacting feature can receive near-zero surrogate weight if perturbations rarely toggle it jointly, whereas SHAP's game-theoretic averaging assigns it non-zero credit. (3) LIME may violate local accuracy/efficiency (surrogate intercept + weights need not sum to f(x)E[f]f(x)-\mathbb{E}[f]) and/or consistency. (2)


Question 2

(a) Grad-CAM derivation — 8 marks

  • Neuron importance weight: αkc=1Zi,jycAijk\alpha_k^c = \frac{1}{Z}\sum_{i,j}\frac{\partial y^c}{\partial A^k_{ij}} where Z=HWZ=H\cdot W. This is global average pooling of the gradients: it measures how much the score ycy^c increases per unit increase averaged over the whole feature map, giving a single scalar "how important is channel kk." (3)
  • ZZ normalises so weights are comparable across maps of different spatial size (average, not sum). (1)
  • Linear combination kαkcAk\sum_k \alpha_k^c A^k forms a weighted sum of activation maps — a coarse heatmap at conv resolution, upsampled to input size. (2)
  • Final ReLU\mathrm{ReLU} keeps only features with positive influence on the class of interest (negative values correspond to evidence for other classes and would clutter localisation). (2)

(b) Reduction to CAM — 6 marks

Head: yc=kwkcGAP(Ak)=kwkc1Zi,jAijky^c = \sum_k w^c_k\,\mathrm{GAP}(A^k) = \sum_k w^c_k \cdot \frac{1}{Z}\sum_{i,j}A^k_{ij}. (2) Then

ycAijk=wkc1Z.\frac{\partial y^c}{\partial A^k_{ij}} = w^c_k\cdot\frac{1}{Z}.

(2) So

αkc=1Zi,jwkcZ=1ZZwkcZ=wkcZ  wkc.\alpha_k^c = \frac{1}{Z}\sum_{i,j}\frac{w^c_k}{Z} = \frac{1}{Z}\cdot Z\cdot\frac{w^c_k}{Z}=\frac{w^c_k}{Z}\ \propto\ w^c_k.

Up to the constant 1/Z1/Z, the Grad-CAM weight equals the classifier weight wkcw^c_k — exactly the CAM formulation. (2)

(c) Model-randomization sanity check — 6 marks

def parameter_randomization_test(model, x, saliency_fn):
    S0 = saliency_fn(model, x)          # explanation for trained model
    sims = []
    for layer in top_to_bottom(model.layers):
        randomize_weights(layer)        # cascading randomization
        S = saliency_fn(model, x)
        sims.append(similarity(S0, S))   # SSIM / rank correlation
    return sims

Pass criterion: as parameters are randomized, saliency should change substantially (similarity → low). If the map is invariant to randomized weights, it is essentially an edge/input detector, not model-explaining → fail. (3)

Probabilistic reasoning: a trustworthy explanation should be a function of the learned conditional pθ(yx)p_\theta(y\mid x); if it is invariant to θ\theta it carries no information about the model's decision function, i.e. mutual information I(saliency;θ)0I(\text{saliency};\theta)\approx 0. Passing (high sensitivity to θ\theta) is necessary for the map to reflect what the model actually computes. (3)


Question 3

(a) Activation patching definition — 6 marks

Run corrupted input but replace the activation hL,th_{L,t} with the clean value hL,tcleanh_{L,t}^{\text{clean}}; let resulting logit be patched\ell_{\text{patched}}. Normalised patching effect:

PE(L,t)=patchedcorrcleancorr.\mathrm{PE}(L,t) = \frac{\ell_{\text{patched}} - \ell_{\text{corr}}}{\ell_{\text{clean}} - \ell_{\text{corr}}}.

(3) PE=1\mathrm{PE}=1 means that activation alone restores the clean answer; 00 means it is irrelevant.

Causal, not correlational: we intervene (dodo-operation) on an internal variable and measure the change in output while holding everything else at the corrupted state. Unlike attention weights or probe accuracy (which only correlate with a concept), patching establishes that the activation causes the behaviour. (3)

(b) Probing pitfall proof — 6 marks

Counterexample: let h=(h1,h2)h=(h_1,h_2)^\top, output y=sign(h1)y = \mathrm{sign}(h_1). Suppose concept c=sign(h2)c = \mathrm{sign}(h_2) and h2h_2 is present in the representation (perhaps written by an earlier layer) but the readout weight on h2h_2 is 00. (2) A linear probe p(h)=sign(h2)p(h)=\mathrm{sign}(h_2) achieves accuracy a=1a=1 for cc. (2) Yet y/h2=0\partial y/\partial h_2 = 0: intervening on h2h_2 (activation patching along cc) leaves yy unchanged — zero causal effect. Thus decodability \neq usage. Patching is stronger because it tests the causal path, not mere presence of information. (2)

(c) Counterfactual gradient — 6 marks

f(x)=σ(z)f(x')=\sigma(z), z=wx+bz=w^\top x'+b, σ(z)=σ(z)(1σ(z))\sigma'(z)=\sigma(z)(1-\sigma(z)). (1)

x[λ(f(x)y)2]=2λ(f(x)y)σ(z)(1σ(z))w.\nabla_{x'}\Big[\lambda(f(x')-y')^2\Big] = 2\lambda\,(f(x')-y')\,\sigma(z)(1-\sigma(z))\,w.

(2) For xx1\|x-x'\|_1: xxx1=sign(xx)=sign(xx)\nabla_{x'}\|x-x'\|_1 = -\,\mathrm{sign}(x-x') = \mathrm{sign}(x'-x) (subgradient, componentwise; 00 at xj=xjx'_j=x_j). (1)

xC=2λ(f(x)y)σ(z)(1σ(z))w  +  sign(xx).\boxed{\nabla_{x'}\mathcal{C} = 2\lambda\,(f(x')-y')\,\sigma(z)(1-\sigma(z))\,w \;+\; \mathrm{sign}(x'-x).}

(1) Sparsity: the 1\ell_1 subgradient has constant magnitude 11 toward xjx_j regardless of distance, so a feature jj is only moved if the data-term gradient's magnitude on it exceeds the 1\ell_1 pull; features with small wj|w_j| stay at xj=xjx'_j=x_j. This is the same LASSO thresholding mechanism → few features change. (1)


[
  {"claim":"Shapley efficiency: for a 2-player game sum of phi equals v(N)-v(empty)",
   "code":