Interpretability & Explainability
Chapter: 6.3 Interpretability & Explainability Level: 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60
Instructions: Derive from first principles where asked. Write code from memory (minor syntax slips forgiven if logic is correct). "Explain out loud" answers are graded on conceptual precision.
Question 1 — Shapley values from first principles (12 marks)
(a) State the exact Shapley value formula for the contribution of feature given a value function over coalitions . (3)
(b) Prove that Shapley values satisfy efficiency: for the general case. Give the combinatorial argument. (5)
(c) A model has 3 features. The value function is: Compute exactly. (4)
Question 2 — LIME from memory (10 marks)
(a) Write pseudocode (from memory) for LIME explaining a single prediction of a black-box classifier on instance . Include: perturbation sampling, weighting kernel, and the weighted local surrogate fit. (6)
(b) State two concrete failure modes of LIME and explain why each occurs. (4)
Question 3 — Grad-CAM derivation (10 marks)
(a) Derive the Grad-CAM formula. Start from feature maps of a target conv layer and class score ; define the neuron importance weights and the final localization map . (6)
(b) Explain out loud: why does Grad-CAM apply a ReLU at the end, and what would you lose without it? (2)
(c) Give one reason vanilla saliency (input-gradient) maps are noisier than Grad-CAM. (2)
Question 4 — Superposition & sparse autoencoders (12 marks)
(a) Explain out loud what superposition is and why it lets a model represent more features than it has neurons. Reference the role of sparsity. (4)
(b) Write the loss function of a sparse autoencoder (SAE) used to extract monosemantic features from residual-stream activations . Define encoder, decoder, and the sparsity penalty. (5)
(c) A residual stream has dimension . An SAE uses an expansion factor of 16 with an L1 penalty. State the dictionary size and explain why the dictionary is over-complete and what problem that solves. (3)
Question 5 — Activation patching & probing (10 marks)
(a) Describe the activation patching (causal tracing) procedure step by step: clean run, corrupted run, patch, metric. What causal claim does a large recovery effect support? (5)
(b) A linear probe achieves 95% accuracy predicting part-of-speech from layer-6 activations. Explain out loud why this does not prove the model uses POS information for its task. Name the confound and one control (e.g. selectivity / control tasks). (5)
Question 6 — Counterfactual explanations (6 marks)
(a) Write the optimization objective for a counterfactual explanation of instance targeting output , with a proximity term. (3)
(b) State two desirable properties of good counterfactuals beyond validity, and why each matters for a real user. (3)
Answer keyMark scheme & solutions
Question 1 (12 marks)
(a) (3 marks)
- 1 mark: sum over subsets excluding .
- 1 mark: correct weight .
- 1 mark: marginal contribution term .
(b) (5 marks) The weight equals times the number of orderings of the features in which the members of precede and the rest follow. Equivalently, where is the set of predecessors of in permutation . (2 marks — permutation formulation.) Summing over : for a fixed permutation , the sum telescopes to . (2 marks — telescoping.) Averaging over all permutations of a constant gives . (1 mark.)
(c) (4 marks) For , weights: ; ; . Marginal contributions of feature 1:
- : , weight .
- : , weight .
- : , weight .
- : , weight .
(2 marks correct method, 2 marks correct value.)
Question 2 (10 marks)
(a) (6 marks)
def lime_explain(f, x, num_samples=5000):
Z = [] # perturbed samples (interpretable rep)
for _ in range(num_samples):
z = sample_perturbation(x) # e.g. mask features/superpixels
Z.append(z)
Zp = [to_original_space(z) for z in Z]
y = [f(zp) for zp in Zp] # black-box predictions
# proximity weight: closeness of z to x
w = [exp(-D(x, zp)**2 / sigma**2) for zp in Zp] # kernel
# fit sparse linear surrogate g minimizing weighted loss
g = weighted_lasso_fit(Z, y, weights=w) # local, interpretable
return g.coefficients # feature attributions
Marks: perturbation sampling (2), predictions on perturbations (1), exponential kernel weighting by proximity (1), weighted (sparse) linear fit (2).
(b) (4 marks — 2 each)
- Instability / non-determinism: random sampling + local fit ⇒ different runs give different explanations; the fit depends heavily on kernel width (an arbitrary hyperparameter that defines "locality").
- Local-linearity failure: LIME assumes the decision surface is locally linear; near sharp/curved boundaries the linear surrogate misrepresents , producing misleading or even sign-wrong attributions. (Also acceptable: perturbations produce off-manifold/unrealistic inputs where behaves undefined.)
Question 3 (10 marks)
(a) (6 marks) Global-average-pool the gradients of the class score w.r.t. each feature map to get importance weights: (2 marks: gradient of class score; 1 mark: global average pool over spatial dims, = #pixels.) Combine maps weighted by importance and rectify: (2 marks: weighted sum of feature maps; 1 mark: ReLU.)
(b) (2 marks) ReLU keeps only features with a positive influence on the target class; negative contributions belong to other classes. Without it the map would mix in evidence against the class and highlight regions unrelated to (or suppressing) class , blurring localization.
(c) (2 marks) Input-gradients are computed w.r.t. individual pixels through many nonlinear layers, so they are high-frequency and sensitive to tiny input changes (gradient saturation/shattering); Grad-CAM operates on coarse, spatially-smooth high-level feature maps, averaging out that noise.
Question 4 (12 marks)
(a) (4 marks) Superposition: a network stores more distinct features than it has neurons/dimensions by representing them as near-orthogonal directions in activation space rather than one-neuron-per-feature. (2) It works because features are sparse — few are active at once — so the interference (dot-product overlap) from simultaneously active features is rare and tolerable; the model trades small interference noise for representing many more features. (2)
(b) (5 marks) Encoder/decoder over-complete dictionary: Loss: Marks: encoder with ReLU (1), decoder reconstruction (1), reconstruction MSE term (1), L1 sparsity penalty on codes (1), tuning tradeoff / definitions of (1).
(c) (3 marks) Dictionary size features. (1) It is over-complete because the number of dictionary atoms (8192) exceeds the activation dimension (512). (1) This gives room to disentangle superposed features into separate, mostly monosemantic directions — solving polysemanticity so each learned feature is human-interpretable. (1)
Question 5 (10 marks)
(a) (5 marks)
- Clean run: run the model on a prompt that produces the correct/target behavior; cache activations. (1)
- Corrupted run: run a minimally-different prompt (e.g. altered subject) that breaks the behavior; record the (lower) metric. (1)
- Patch: re-run the corrupted prompt but overwrite a specific activation (a layer/position/head) with the cached clean value. (1)
- Metric: measure recovery of the target output (e.g. logit-diff restored). (1) A large recovery effect supports a causal claim: that component is sufficient (in this context) to carry the information driving the behavior — not merely correlated with it. (1)
(b) (5 marks) High probe accuracy shows POS is linearly decodable from the activations, but decodability ≠ usage: the information could be an incidental byproduct never read by downstream computation. (2) Confound: a sufficiently expressive probe can memorize / fit structure present in rich activations regardless of task relevance (probe capacity confound). (1) Control: use a control task (random-label version) and report selectivity = task accuracy − control accuracy; a high selectivity indicates the representation genuinely encodes POS rather than the probe just being powerful. (2) (Activation patching / causal intervention also accepted as a stronger control.)
Question 6 (6 marks)
(a) (3 marks) Marks: loss pushing to target (1), proximity/distance penalty (1), correct combination with weight (1).
(b) (3 marks — 1.5 each; any two)
- Sparsity / minimality: change few features — a user can act on "raise income" not on 40 simultaneous changes.
- Actionability / plausibility (on-manifold): don't recommend changing immutable/unrealistic features (e.g. age down, or off-distribution values); the advice must be feasible.
- (Also: robustness, diversity of multiple counterfactuals.)
[
{"claim":"Shapley phi_1 = 20 for given value function",
"code":"from itertools import combinations\nfrom math import factorial\nv={frozenset():0,frozenset({1}):10,frozenset({2}):20,frozenset({3}):30,frozenset({1,2}):40,frozenset({1,3}):50,frozenset({2,3}):70,frozenset({1,2,3}):100}\nn=3\nother=[2,3]\nphi=0\nfor k in range(len(other)+1):\n for S in combinations(other,k):\n S=frozenset(S)\n w=factorial(len(S))*factorial(n-len(S)-1)/factorial(n)\n phi+=w*(v[S|{1}]-v[S])\nresult=abs(phi-20)<1e-9"},
{"claim":"Efficiency: sum of Shapley values equals v(N)-v(empty)=100",
"code":"from itertools import combinations\nfrom math import factorial\nv={frozenset():0,frozenset({1}):10,frozenset({2}):20,frozenset({3}):30,frozenset({1,2}):40,frozenset({1,3}):50,frozenset({2,3}):70,frozenset({1,2,3}):100}\nn=3\ndef phi(i):\n others=[j for j in [1,2,3] if j!=i]\n tot=0\n for k in range(len(others)+1):\n for S in combinations(others,k):\n S=frozenset(S)\n w=factorial(len(S))*factorial(n-len(S)-1)/factorial(n)\n tot+=w*(v[S|{i}]-v[S])\n return tot\nresult=abs(sum(phi(i) for i in [1,2,3])-100)<1e-9"},
{"claim":"SAE dictionary size = 512*16 = 8192",
"code":"result=(512*16)==8192"}
]