How to read what a trained network computes, from the outside and from the inside. Attributions, probes, lenses, causal patching, circuits, superposition, sparse dictionaries, steering, and attribution graphs, each derived, implemented, and tested against what it can and cannot prove.
Chapter 23 · Section 23.1 · 85 min
Why look inside
This chapter opens the model up. Chapter 22 judged a model by its outputs alone, treating it as a black box (§22.1). That has a limit. A behavioral test sees only the inputs someone thought to try, so it cannot show that a behavior is absent or say why a model succeeds. This chapter reads the computation itself, in the Transformer the book has built. Its residual stream is a running sum that every attention head and MLP writes into (§5.1), its heads move information between positions (§4.5), and its output layer turns the final vector into next-token scores (§6.2). Each section recaps the piece it needs.
Interpretability is the study of how a trained network computes its outputs. We can read every weight of a network, run it on any input, and record every number it computes along the way, and still not know how it reaches its answer. A trained network is a program that nobody wrote. This section explains why it is worth knowing how that program works, what would count as an explanation, and how far we can get from the outside, by asking which parts of the input an output depended on. It ends where the outside view stops working, which is where the rest of the chapter begins.
Here is the problem in its sharpest form. Two teams train image classifiers to spot horses. Both score 95 percent on a test set drawn the same way as the training data. One model looks at the animal. The other looks at a small copyright tag in the corner of the photo, which happens to sit on many of the horse pictures. This is close to a real case. Lapuschkin et al. (2019) found it in a classifier trained on PASCAL VOC 2007, a standard benchmark of labeled photos. A source tag sat on about one fifth of its horse images, and the classifier had learned to look for it. Pasting the tag onto a photo of a car made it call the car a horse.
No test set drawn from the training distribution can tell these two models apart, because on that distribution they are equally accurate. Two other kinds of evidence can. You can collect new photos in which the tag and the horse come apart, if you know to look for the tag. Or you can look at how each model computes its answer and see which one reads the tag. This chapter is about the second kind of evidence.
The section runs in this order. We first build the horse problem in two dimensions, small enough to check every number. Then we say what interpretability is for, what counts as an explanation, and how strong different kinds of evidence are. Then come the tools that work from the outside, called attributions, which split the credit for one output among the inputs. We build three of them (gradients, integrated gradients, and Shapley values), prove what each guarantees, and show how each fails. We end with the tests that catch attributions that ignore the model, and with why even a perfect attribution cannot say what the model computed.
Two models that no test set can tell apart
Each example in our toy problem has a label y∈{0,1}, which we also write as a sign s=2y−1∈{−1,+1}. It has two features.
Feature x1 is the cause, with noise, x1=1.645s+ϵ with ϵ∼N(0,1). Guessing the label from the sign of x1 is right with probability Φ(1.645)=0.95. Here Φ is the standard normal CDF, the probability that a standard normal variable falls below its argument.
Feature x2 is the tag. It equals s with probability ρ and −s otherwise, plus a little noise, so x2=±s+0.3ϵ′. In training the tag agrees with the label 95 percent of the time, ρ=0.95.
We fit three logistic regressions. A logistic regression scores an input with a weighted sum w⋅x+b, called the logit, and predicts class 1 when the logit is positive. It is trained by minimizing the cross-entropy loss (§0.2). Model A sees only x1, model B only x2, and model C sees both. We score each one twice. The first test set is fresh data from the same distribution. The second is shifted, so that the tag now points the wrong way, ρ=0.05.
Python · cell 1
Here the mechanism is a weight vector, and reading it settles the question. Model A puts weight 0 on the tag and model B puts weight 0 on the cause, by construction. Model C puts 2.63 on the tag next to 3.18 on the cause. Nothing about C's test score warned us, but its weights did.
The rest of the chapter is about getting that knowledge when the mechanism is not two numbers but billions. In the widget, you set how often the tag agrees with the label in training and at test, and watch the three models. Its table also shows how much of each model's credit goes to the tag x2 on data like training, measured with integrated gradients, a method this section builds later.
Can a test set tell a real feature from a shortcut?
x1 causes the label. The tag x2 agrees with it as often as the sliders say. Dots are test points.
Loading interactive figure…
The horse classifier and model C are cases of shortcut learning. Geirhos et al. (2020) gave that name to decision rules that do well on the standard test set and fail on harder or real-world inputs, and their survey collects many cases. A classifier recognizes cows by the grass around them and fails on a cow at the beach. A pneumonia detector trained on chest X-rays from several hospitals learned to tell which hospital a scan came from, partly from a metal token that technicians place on the film (Zech et al. 2018). It then used each hospital's pneumonia rate as a shortcut, and did worse on hospitals it had not seen.
In practice, this changes how you select and ship models. Held-out accuracy can prefer the model with the shortcut, as it preferred C. An interviewer will ask how you would find a spurious feature without data from the shifted distribution. One good answer is to test behavior under changes that should not matter. Another is to measure what the model's output depends on. This section builds both.
What interpretability is for
The word interpretability means little until you say what you want from it, because papers ask it for many different things (Lipton 2016). This book uses it for three purposes, and each has a published case.
Debugging. A model can be right for the wrong reason, and the reason predicts where it will fail. In the same paper as the horse tag, Lapuschkin et al. studied a network trained by reinforcement learning to play the Atari game Pinball. It had learned to nudge the table, gently enough never to trigger the game's tilt lockout, so that the ball kept passing over a scoring switch. That is an exploit of the game's physics, not good play. It is a case of specification gaming, behavior that satisfies the letter of an objective and misses its intent (§19.1). The same section describes goal misgeneralization, in which a model stays capable on new inputs while pursuing a goal other than the intended one. You can see either failure coming only if you can see what the model is keying on.
Safety. A behavioral evaluation can show that a model does something. It can never show that a model never does it. An evaluation measures what the model does on the prompts someone thought to try, so it is a lower bound on what the model can do (§14.1). A backdoor is a hidden behavior that fires only when a specific trigger appears in the input. A model with a backdoor, or one that behaves differently when it suspects a test, passes every test that misses the trigger. Looking inside can catch what tests miss.
§19.6 describes models trained with deliberate backdoors that survived safety training. A linear probe scores the model's internal vector by its dot product with a fixed direction. One probe, whose direction came from a single pair of generic texts unrelated to the backdoor, separated prompts on which the model would defect from the rest with an AUROC of 99.3 percent. The AUROC is the probability that the probe scores a random defecting case above a random normal one, so 50 percent is chance (§0.11).
In a blind auditing game, Marks et al. (2025) trained a model with a hidden objective and gave it to four teams. The three teams given the weights and the training data found the objective. The team that could only send prompts and read answers through an API, the access an outside auditor would have, did not.
Science. A network that solves a problem has found some algorithm for it, and reading that algorithm can teach us something. Small networks trained on algorithmic tasks sometimes memorize their training set first and generalize only much later, a pattern Power et al. (2022) called grokking (§9.6). Nanda et al. (2023) reverse-engineered a small Transformer that had grokked addition modulo a prime. It represents each number as an angle on a circle, at a few frequencies, and adds numbers by adding angles. Knowing the algorithm let them build measures that track hidden progress long before the model generalizes, and §23.4 takes that algorithm apart.
All three uses need an explanation of how the model computes its output, sharp enough to make a prediction. Here is the definition the chapter works with. It uses the word activations, which means the vectors of numbers a network computes at its internal layers on a given input.
In plain words, an explanation is a hypothesis about the model's insides that you could test by surgery. "The model reads the tag" predicts that covering the tag changes the answer, and it does. A story that predicts nothing you could check is not yet an explanation.
Levels of explanation
A system that processes information can be described at three levels. The top level says what function it computes. The middle level says which variables it keeps and which steps transform them. The bottom level says how those steps are physically built. An answer at one level does not settle the others. The neuroscientist David Marr (1982) made this argument for vision, and his levels map onto a trained network like this.
Level
The question
In a trained network
Methods in this chapter
Computational
What function is computed, and why is it the right one?
In a Transformer, the attention heads move information between positions (§4.5), and the MLPs are the two-layer networks that transform each position on its own (§5.3). Two models can agree at the top level and differ completely below it, as models A and B did on the training distribution.
Behavioral interpretability stays in the top row and treats the model as a black box. Mechanistic interpretability tries to reverse-engineer the bottom two rows, the way one might turn a compiled binary back into source code. Olah et al. (2020) set out three working claims for that program, and the rest of this chapter tests them. Networks are built from meaningful features, internal variables that each stand for one thing. Features connected by weights form circuits. And similar features and circuits recur across different models, a claim they called universality.
The ladder of evidence
Methods also differ in how strong a conclusion they allow. The chapter climbs a ladder of four rungs, and every later section says which rung it stands on.
Rung
What it shows
Examples
Where
Association
Some quantity tracks the output or a concept, with no intervention on the internals
Input attributions, probes (classifiers trained on activations)
Input attributions sit on the first rung, and the reason is worth stating carefully. Some of them do intervene, but on the input. Covering part of an image and watching the score drop is a causal statement about which inputs matter. As evidence about the internal computation it is only association, because many different internal mechanisms produce the same attributions, as the end of this section shows. When you read an interpretability result, ask which rung its evidence reaches.
Behavioral tests come first
Before opening anything, test behavior systematically. Ribeiro et al. (2020) adapted software testing to language models, in a method they called CheckList, with three kinds of test.
A minimum functionality test checks simple cases the model must get right. "I didn't love it" is not positive.
An invariance test makes a change that should not matter, such as swapping one city name for another, and checks that the prediction stays put.
A directional expectation test makes a change that should move the prediction one way, such as adding "the service was awful", and checks that it does.
With these tests, the team that owned a heavily tested commercial sentiment model found new bugs it could act on. §22.6 uses the same three test types to build a product's eval suite. Behavioral tests are cheap, and they find real failures. An invariance test is the cheapest way to check for a shortcut you already suspect. What they cannot do is find a shortcut nobody thought of, or prove that one is absent.
Faithful, not merely plausible
An explanation can fail in a way that is easy to miss. It can look right to a person and still not describe what the model did.
A plausible but unfaithful explanation is worse than none, because it creates confidence. A model trained on human-written explanations that writes a fluent justification after the fact is plausible by construction, and nothing guarantees that it is faithful. Attention weights were long read as explanations because they look like one. §3.6 explains why they are not, on their own. Quite different attention patterns can give the same prediction, and information mixes across positions at every layer.
So how do you test faithfulness, if not by looking? Jacovi and Goldberg collected the tests in the literature and the assumptions behind them, and three tests carry most of the weight in practice.
Same predictions, same explanation. If two models make the same predictions everywhere but a method explains them differently, the method is unfaithful to at least one of them. Jacovi and Goldberg call the assumption behind this test the model assumption.
Destroyed model, different explanation. A model whose weights have been randomized must not get the same explanation as the trained one. The model-randomization test later in this section runs this check.
Intervention. Change what the explanation says matters, and check that the output moves as the explanation predicts. This is the strongest test, and it follows straight from the definition of an interpretability claim. Every section of this chapter ends up using it.
In an interview, "how would you know your explanation is right?" has the third test as its answer.
Attributing an output to its inputs
The simplest question about one prediction is which inputs it depended on. An attribution answers with one number per input, the credit that input gets for the output. Credit has to be measured against something. So we compare the input with a baseline, a reference input that stands for "the information removed".
Here is a tiny case first. Take a linear model f(x)=2x1−x2+0.5, the input x=(3,1), and the baseline x′=(0,0). The output goes from f(x′)=0.5 to f(x)=5.5, a change of 5. The natural split gives x1 the amount 2×3=6 and x2 the amount −1×1=−1. These add up to the change, 5. For a nonlinear model the split is no longer obvious, and the methods below are different answers to how to make it.
In general we explain a scalar output f:Rn→R. For a classifier we pick the logit of the predicted class, not its softmax probability (the pitfall box later in this section says why). We fix the input x∈Rn and a baseline x′∈Rn, such as a black image, the all-zero embedding, or the training mean.
Three methods come first, because they are cheap and nearly everyone uses them.
The gradient. The saliency map of Simonyan et al. (2013) is the gradient ∇f(x), how fast the output changes as each input moves. Backpropagation computes all n entries in one backward pass, which costs about as much as two forward passes, whatever n is (§0.3). The gradient answers "what if input i moved a little?" That is a different question from "how much did input i contribute?"
Gradient times input. The first-order Taylor expansion of f around x (§0.2) says f(x′)≈f(x)+∇f(x)⋅(x′−x). Rearranged,
f(x)−f(x′)≈i=1∑n∂xi∂f(x)(xi−xi′).
In plain words, each input's share of the change is its slope times how far it moved. The i-th term, ∂if(x)(xi−xi′), is the attribution. With the baseline x′=0 it is the familiar "gradient times input". It is exact when f is linear, as in the tiny case above, and only approximate otherwise.
Occlusion.Zeiler and Fergus (2014) slid a gray square over an image and recorded how much the class score dropped. For one input at a time, ai=f(x)−f(x[i←xi′]), where x[i←xi′] is x with entry i replaced by its baseline value. It costs n+1 forward passes. It asks a real counterfactual question, but only about one input at a time.
In plain words, the gradient is local. It reports the slope where you stand. A unit has saturated when its output has stopped responding to its input. Then the slope is zero however much that input mattered on the way there. ReLUs past their kink, sigmoid and tanh units near their flat tails, and softmax probabilities near 1 all saturate, and trained networks are full of them.
Python · cell 2
The last line is the fix, and it is worth doing by hand. Instead of the slope at x, average the slope over the whole straight path from the baseline to the input. On the path from 0 to 2 the slope is 1 for the first half and 0 for the second, so the average is 21. Then 21×(2−0)=1, exactly the change in the output. That average is the next method, integrated gradients.
Warm-up
Gradient times input versus occlusion
Implement two attributions for a scalar function f.
grad_times_input(f_grad, x, x0) receives f_grad, a function that returns the gradient of f at a point. Return ∇f(x)⊙(x−x′), the gradient at x times how far each input moved from the baseline x0.
occlusion(f, x, x0) treats f as a black box. Return the vector whose entry i is f(x)−f(x[i←xi′]), the drop in the output when input i alone is set to its baseline value.
The tests use a nonzero baseline, a saturated unit, and the product x1x2. On the product you will see that occlusion is not complete.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.
Integrated gradients
Sundararajan et al. (2017) turned the hand calculation above into a method and, more importantly, into a theory. Attribution methods had mostly been judged by how good their pictures looked, which is plausibility. They wrote down properties an attribution should have, and found a method that has them.
In plain words, walk from the baseline to the input in a straight line. Average the sensitivity to input i along the way, and multiply by how far input i moved. Gradient times input uses the sensitivity at the end of the walk only. Integrated gradients uses all of it. On the saturated ReLU it gives 2×21=1, the right answer.
The reason it gets the right answer is a theorem. The attributions always add up to the change in the output.
Proof. The function g has one variable. By the chain rule (§0.2), wherever f is differentiable at γ(α),
since γ′(α)=x−x′. Integrate both sides over α from 0 to 1, and swap the finite sum with the integral. The right side becomes ∑iIGi(x). The left side is ∫01g′(α)dα=g(1)−g(0)=f(x)−f(x′), by the fundamental theorem of calculus. That theorem applies on each of the finitely many pieces between kinks, where g is differentiable with a bounded derivative. The pieces add up because g is continuous at the kinks. (For a ReLU network, g is piecewise linear along the segment, so the integral is a sum of finitely many slopes times lengths.) ∎
This is the gradient theorem for line integrals, the multivariable form of the fundamental theorem of calculus. It buys the property the gradient lacked, which Sundararajan et al. call Sensitivity(a). Suppose x and x′ differ in one input only and f(x)=f(x′). Every other input gets zero, because its factor xj−xj′ is zero. So completeness forces the one input that moved to get the whole difference, which is not zero. The saturated ReLU is that situation with n=1.
The widget draws the function along the path, with its derivative below. Integrated gradients is the shaded area under the derivative between x′ and x, which equals the distance moved times the average derivative. The dashed box is gradient times input, the derivative at x times the distance moved. The widget estimates the area with K rectangles, each sampled at its midpoint, and its bottom panel shows how far the estimate is from the true change with midpoints and with left ends. Press Walk the path to watch the area pile up as a dot moves from x′ to x, and stop growing wherever f is flat.
Why do gradients miss saturated inputs, and how does a path fix it?
Top, f on the path from baseline x′ to input x. Bottom, its slope, with IG shaded.
Loading interactive figure…
The axioms
Completeness is one of six properties Sundararajan et al. asked for. Here they all are, for an attribution method a. Interviewers ask for them by name.
Completeness.∑iai=f(x)−f(x′).
Sensitivity(a). If x and x′ differ in one input and the outputs differ, that input gets nonzero credit.
Sensitivity(b), called dummy in the economics of cost sharing. If f does not depend on input i at all, ai=0.
Implementation invariance. Two networks that compute the same function get the same attributions, however differently they are built.
Linearity. The attributions for c1f1+c2f2 are c1 times those for f1 plus c2 times those for f2.
Symmetry preservation. If swapping inputs i and j leaves f unchanged, and x and x′ each have equal values in positions i and j, then ai=aj.
Integrated gradients has all six. The plain gradient has implementation invariance, because the chain rule gives the same derivative whatever intermediate variables a network uses. But it fails Sensitivity(a), as the ReLU showed.
Computing it, and checking that you did
The integral is estimated with a Riemann sum, an average of the integrand at K points on the path. With the midpoint rule the points are αk=(k+21)/K for k=0,…,K−1, and
The left rule uses αk=k/K and the right rule uses (k+1)/K, which is the form Sundararajan et al. wrote down. All K gradients come from one batched backward pass, since the points on the path are independent inputs.
Completeness now doubles as a convergence check. The completeness gap is ∑iIGi−(f(x)−f(x′)), where IG is the Riemann-sum estimate. It is zero for the exact integral, so whatever is left is discretization error. Sundararajan et al. found that 20 to 300 steps brought the sum within 5 percent of the difference, and recommended checking this every time. How fast the gap shrinks depends on the rule.
∂ The error of the left and midpoint rules
Split [0,1] into K intervals of width δ=1/K. On one interval [a,a+δ], Taylor's theorem gives
∫aa+δh(α)dα=δh(a)+2δ2h′(a)+6δ3h′′(a)+O(δ4).
So the left rule's error on this interval, δh(a)−∫aa+δh, is −2δ2h′(a)+O(δ3). Summed over the K intervals, ∑kδh′(ak) is itself a Riemann sum for ∫01h′=h(1)−h(0), so the total is −2δ(h(1)−h(0))+O(δ2).
For the midpoint m=a+δ/2, expand around m instead. The odd powers integrate to zero over the symmetric interval [m−δ/2,m+δ/2], which leaves
∫m−δ/2m+δ/2h=δh(m)+24δ3h′′(m)+O(δ5).
So the midpoint rule's error on the interval is −24δ3h′′(m)+O(δ5), and summing as before gives −24δ2(h′(1)−h′(0))+O(δ4). The completeness gap is exactly this error, because ∑iIGi is the Riemann sum of h and f(x)−f(x′)=∫01h. The first-order term cancels at the midpoint, which is why midpoints are worth using at the same cost.
Here is a small network where the difference between the methods is not subtle.
Python · cell 3
The code writes vectors as rows, as the rest of the book does. So a layer computes xW, and W1∈R2×2 maps inputs to hidden units. Now for the convergence rates. The next cell builds a random tanh network with eight inputs and 32 hidden units, and explains one input against the zero baseline with both rules.
Python · cell 4
Each fourfold increase in K cuts the left-rule gap by a factor of about 4, from 9.4×10−3 at K=16 to 2.4×10−3 at 64. It cuts the midpoint gap by about 16, from 9.2×10−5 to 5.8×10−6, as the fact box predicts. At K=64 the midpoint rule is about 400 times more accurate than the left rule, for the same 64 gradient evaluations. In practice, use midpoints and always report the gap.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.
Choosing the baseline
Every attribution in this section is relative to a baseline, and changing the baseline changes the answer. Sundararajan et al. recommend a baseline whose output is near zero and that carries no signal, so that the attributions describe the input rather than the baseline.
For images it is often a black image.
For text models it is often the all-zero embedding vector. That is not a real input, but it works because unimportant tokens tend to have small embeddings.
Other common choices are a blurred copy of the input, uniform noise, and the training mean. Expected gradients (Erion et al. 2021) averages integrated gradients over many baselines drawn from the training data.
None of these is neutral. A black baseline cannot credit black pixels for anything, since they do not move along the path. A dark object on a dark background gets no attribution however much it mattered. The baseline is part of the question being asked, "compared with what?" Choose it on purpose and report it.
Shapley values
Integrated gradients needs gradients, and a smooth way to move an input partway. Many inputs are not like that. A word is in a sentence or it is not, and a feature in a table is observed or missing. For these the natural tool comes from cooperative game theory. Lloyd Shapley (1953) asked how to split a team's payoff fairly among its players, and his answer is the Shapley value.
Here is the difficulty in one example. Take f=x1x2 at x=(2,3) with baseline (0,0), so the output goes from 0 to 6. Bring in x1 first, with x2 still at its baseline, and the output stays 0. Bring in x1 after x2, and the output jumps from 0 to 6. What x1 contributed depends on who was already there. Shapley's answer is to average over every order of arrival.
To say this in symbols, treat the n inputs as playersN={1,…,n}. A coalition is a subset S⊆N. A value functionv assigns a number v(S) to every coalition, the output when only the inputs in S are present, with v(∅) the output when none of them are. When player i joins coalition S, its marginal contribution is v(S∪{i})−v(S).
In plain words, assemble the team in a random order and credit each player with what the team gained when it arrived. Then average over orders, so that nobody is favored by arriving early or late. For f=x1x2 above, x2 arrives before x1 in half the orders, so ϕ1=21×6=3, and likewise ϕ2=3.
∂ Why the two forms agree
In the ordering form, the term for an order π depends only on the set Piπ of players before i. Count the orders in which that set equals a given S with ∣S∣=s. The players in S come first, in any of s! orders. Then comes i. Then the remaining n−s−1 players follow, in any of (n−s−1)! orders. So the coalition S appears s!(n−s−1)! times among the n! orders, which is exactly the weight in the subset form.
When the game is built from a model with a single baseline, as below, efficiency is completeness under another name, and dummy is Sensitivity(b).
Efficiency has a short reason. In any one order of arrival, the marginal contributions telescope to v(N)−v(∅), so their average over orders does too. The derivation below proves all four properties and shows that no other method has them.
∂ Proof of the axioms and of uniqueness
The four properties hold. Fix one order π=(π1,…,πn) and let Sk={π1,…,πk} be its first k players. The marginal contributions in this order form a telescoping sum, ∑k=1n[v(Sk)−v(Sk−1)]=v(N)−v(∅). Every order gives the same total, so their average over all n! orders, which is ∑iϕi, gives it too. Symmetry and dummy follow by reading the formula, and additivity holds because ϕi is linear in v.
Only the Shapley value has them. For a nonempty coalition T, define the unanimity gameuT(S)=1 if T⊆S and 0 otherwise. The team scores only when every member of T is present. Every game with v(∅)=0 is a unique combination v=∑T=∅cTuT, with coefficients given by inclusion and exclusion, cT=∑S⊆T(−1)∣T∣−∣S∣v(S). (There are 2n−1 unanimity games and 2n−1 free values of v. The triangular system v(S)=∑T⊆ScT can be solved coalition by coalition, from small to large.)
Take any method ψ with the four properties, and apply it to the game cuT for a real number c. A player outside T is a dummy, so it gets 0. Any two players in T are symmetric, so they get equal shares. Efficiency says the shares sum to cuT(N)−cuT(∅)=c. So the axioms force ψi(cuT)=c/∣T∣ for i∈T and 0 otherwise. Additivity then fixes ψ on every game, ψi(v)=∑T∋icT/∣T∣. The Shapley value has the four properties, so it is this function. (A game with v(∅)=0 is handled by subtracting the constant, of which every player gets zero.)
This also shows where the credit for an interaction goes. Each coefficient cT is the part of the payoff that needs all of T together, and the Shapley value splits it equally among the members of T.
From a model to a game
To use any of this, a model and an input have to be turned into a game. That is a modeling choice with real consequences, and there are three common choices.
The single-baseline game. A coalition S keeps its inputs at their values in x and sets the rest to the baseline, v(S)=f(xS,xSˉ′), where Sˉ is the complement of S.
The conditional game.Lundberg and Lee (2017) defined SHAP values as the Shapley values of v(S)=E[f(X)∣XS=xS]. This averages the model over plausible values of the missing inputs, given the present ones.
The interventional game. The conditional expectation is hard to estimate. So in practice Lundberg and Lee, and most libraries, assume the inputs are independent and use v(S)=EX′[f(xS,XSˉ′)], with X′ drawn from a background dataset. This is the single-baseline game averaged over many baselines.
The choice changes the answer. Under the conditional game, an input the model ignores can receive credit because it is correlated with one the model uses, since knowing it changes the expected output. Under the interventional game it cannot, because then the dummy axiom applies to the model itself. Sundararajan and Najmi (2020) showed that these and other versions give very different attributions. That takes much of the force out of the uniqueness theorem. Shapley values are the unique fair split of a given game, and the game is yours to choose. An interviewer who asks "what does SHAP assume?" wants this answer.
On one kind of model, every method in this section gives the same answer.
∂ Proof that every method agrees on a linear model
The gradient is w everywhere, so gradient times (x−x′) is wi(xi−xi′). Integrated gradients averages the constant wi along the path and gives the same thing. Occlusion changes only input i, so f(x)−f(x[i←xi′])=wi(xi−xi′). For the Shapley value, v(S)=b+∑j∈Swjxj+∑j∈/Swjxj′. So every marginal contribution of i is v(S∪{i})−v(S)=wi(xi−xi′), whatever S is. The Shapley weights over S⊆N∖{i} sum to 1, since they are the probabilities of each set of predecessors in a random order. So ϕi=wi(xi−xi′). In the interventional game, linearity of expectation replaces xj′ by E[Xj′] in the same calculation. ∎
In plain words, when the model has no interactions and no saturation, there is only one sensible answer, and every method finds it. Everything interesting about attribution happens in the nonlinear parts. This is also why the widget near the start of this section can compute the credit for its three logistic models as each weight times the input's distance from the training mean.
The cell checks this by enumerating every coalition.
Python · cell 5
The two methods agree on this function, but they are different methods. Sundararajan et al. gave the example that separates them.
Python · cell 6
The cost, and how to pay less
Exact Shapley values need v on all 2n coalitions. Each one is a model evaluation, or for the interventional game an average of several. For n=20 features that is 220≈106 evaluations per explained input. For the 50,176 pixels of a 224 by 224 image, or the tokens of a long prompt, it is out of the question. Two estimators make it practical.
Permutation sampling. Draw M random orders and average each player's marginal contribution over them. Each order costs n+1 evaluations and gives an unbiased estimate of every ϕi at once. By the central limit theorem the error falls like 1/M. The cell compares this with the exact values on a quadratic model of 10 inputs, where the exact computation needs 210=1,024 coalitions. It encodes coalitions as bitmasks, integers whose bit j is set when input j is present.
Python · cell 7
The root-mean-square error over 40 repetitions goes from 0.066 to 0.033 to 0.016. It halves with each fourfold increase in M, the 1/M rate. (The table of all coalition values is a shortcut for this demonstration. A real sampler evaluates the model only on the coalitions its orders visit, M(n+1) of them.)
Regression. A second estimator fits a weighted linear model to the outputs on sampled coalitions, and with the right weights its coefficients are the Shapley values. Lundberg and Lee found it more sample-efficient than permutation sampling, because one regression estimates all n values jointly. The box below gives the weights.
Interview
Exact Shapley values
Implement shapley(v, n), which returns the exact Shapley values of an n-player game as an array of shape (n,).
The value function v takes a coalition as a bitmask, an integer whose bit i is set when player i is present. So v(0) is the empty coalition and v((1 << n) - 1) is the full one.
Call v at most once per coalition (2n calls), and do O(2nn) further work.
The tests include a weighted voting game, a classic case with known answers. Each player has a weight, and a coalition wins (value 1) when its members' weights reach a quota.
import numpy as np
from math import comb
def shapley(v, n):
raise NotImplementedError
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.
Back to the shortcut, with a real network
The opening example was too easy, because a linear model shows its mechanism in its weights. A neural network does not. Its first-layer weights can be large for an input the network ignores, if the units reading that input sit saturated on every example and so pass on a constant. They can also be small for an input it relies on. This is where attributions earn their keep. In the next exercise you get two small tanh networks with equal accuracy on the training distribution, and one of them secretly reads the tag. You may not use shifted data.
Core
Find the shortcut without shifted data
The setup defines these pieces.
make_data, the two-feature data of the opening, with x1 the cause and x2 the tag.
Two networks, model_A and model_B, with nearly equal accuracy.
logit(model, X) and logit_grad(model, X), the output logit and its gradient for a batch X of shape (N, 2).
In-distribution data X_val.
Write uses_shortcut(model, X), which returns True if the model relies more on the tag x2 than on the cause x1. Measure reliance as the average absolute integrated gradient of each input over the rows of X, with the mean of X as the baseline. The tests also check three networks you have not seen.
import numpy as np
def uses_shortcut(model, X):
# placeholder that looks at the inputs, not at the model
return bool(np.abs(X[:, 1]).mean() > np.abs(X[:, 0]).mean())
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.
The held-out test is the point. The model you flag from in-distribution attributions alone is the one that falls apart when the tag flips, at about 10 percent accuracy against 92 percent for the other. The exercise checks that fact only after you have answered. In practice this is the workflow for a suspected shortcut when no shifted data exists. Attribute on the data you have, and see which inputs carry the decision.
Do attributions explain the model?
Integrated gradients is complete, and its axioms are reassuring. But they are properties of a formula, not evidence that a map tells you about the model. In vision, many saliency methods were judged by whether their maps looked like the object, which is plausibility again. Adebayo et al. (2018) designed two tests that check whether a map depends on the thing it claims to explain.
The model-parameter randomization test. Replace the trained network's weights with random values, either from the top layer down, one layer after another (cascading), or one layer at a time (independent). Recompute the attributions after each step and measure how similar they are to the original maps. A method that explains the model must change when the model is destroyed.
The data randomization test. Train the same architecture on randomly permuted labels, so that it memorizes noise. Compare its maps with those of the model trained on real labels. A method that explains what the model learned must differ between the two.
How you compare maps matters too. Adebayo et al. used the Spearman rank correlation, the ordinary correlation coefficient computed on the ranks of the values rather than on the values themselves. It asks whether two maps order the inputs the same way. They reported it on absolute values and on signed values, because the sign of a map flips easily under randomization while its ranking by magnitude may not.
The cell runs the cascading test on a small network trained on 16 inputs whose scales differ by a factor of 15, a stand-in for the bright and dark regions of an image. Only inputs 3, 7, and 11 determine the label. We randomize the top layer, then the top two, then all three, and compare each method's map with its original. The third method returns the input itself and uses no model, so it plays the part of the edge detector.
Python · cell 8
Passing this test is necessary, not sufficient. A method can depend on the weights and still describe them badly. In practice, run the randomization test before you trust any attribution method on a new model. An interviewer will ask what the test can rule out (methods that ignore the model) and what it cannot (methods that depend on the model but misdescribe it).
layers is a list of (W, b) pairs for a trained network.
attr_fn(layers, X) returns attributions of shape (N, n).
reinit(l) returns fresh random weights for layer l.
Randomize cumulatively from the top. Step 1 replaces the last layer, step 2 the last two, and so on until every layer is random. After each step, compute the Spearman rank correlation between the absolute attributions of the original network and of the randomized one, for every row of X, and record the mean over rows. Return the array of means, one per step.
The setup provides a trained network, three attribution methods (gradient, grad_x_input, and the model-free edge_detector), and row_spearman(A, B), which computes the Spearman correlation row by row without a loop.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.
Attributions at real scale
Integrated gradients and Shapley values are widely used in practice. Their best-known applications show both what they do well and where they stop.
A Transformer's input is a sequence of token IDs. These are discrete, so its attributions are computed with respect to the input embeddings instead, the vectors each token is mapped to before the first layer. Each token's attribution is the sum of the attributions of its embedding coordinates. The baseline is the zero embedding or the embedding of a padding token, and completeness holds for the whole sequence.
Here is that computation in PyTorch, for a tiny model that averages its token embeddings and scores the average with an MLP. The whole path goes through the model as one batch, so one backward pass returns all K gradients. Summing the outputs before calling backward is valid, because each path point's output depends only on its own input. (The model has no batch statistics.)
🔥 In PyTorchfor a machine with PyTorch
In production code you would use a library. Captum, PyTorch's attribution library (Kokhlikyan et al. 2020), provides IntegratedGradients for inputs and LayerIntegratedGradients for an internal layer such as the embedding. Its attribute method takes the baseline, the target class, the number of steps (default 50), the quadrature rule, and a flag that returns the completeness gap. Its default rule is Gauss-Legendre quadrature, which places the points unevenly and converges faster than the midpoint rule on smooth integrands.
For models with special structure, Shapley values can be exact and fast. For tree ensembles, an algorithm that walks the trees computes them in polynomial time (Lundberg et al. 2020). For linear models they have the closed form above.
One more method points toward the next section. Kim et al. (2018) asked not which pixels mattered but whether a human concept, such as "striped", mattered. Their method, called TCAV (testing with concept activation vectors), works in three steps.
Collect examples of the concept and random counterexamples, and record a layer's activations on both.
Train a linear classifier on those activations to separate them. The vector perpendicular to its decision boundary is the concept's direction in that layer.
The concept's importance for a class is the fraction of that class's inputs whose logit increases when the activation moves along that direction, the sign of a directional derivative.
The attribution is now to an internal direction rather than to an input. That only makes sense if the network represents the concept as a direction. Whether it does is the subject of §23.2.
The table collects the methods of this section. It is a good page to have in your head for an interview.
Method
Cost per explanation
Properties it has
How it misleads
Gradient (saliency)
1 backward pass
Implementation invariance, dummy, linearity, symmetry, but not Sensitivity(a) or completeness
Saturation gives zero credit to inputs that mattered; noisy maps
Gradient times (input minus baseline)
1 backward pass
Those of the gradient, and exact for linear models
Saturation; imprints the input's structure; depends on baseline and input shifts
Occlusion
n+1 forward passes
Every axiom except completeness
Not complete with interactions; occluded inputs are off-distribution
Integrated gradients
K backward passes, K = 20 to 300
All six axioms above
Baseline choice; the path leaves the data distribution
Exponential cost; the answer depends on how the game is defined
Permutation sampling, KernelSHAP
M(n+1) evaluations, or M regression samples
Efficiency exactly; converge to the Shapley values as M grows (permutation sampling is also unbiased)
Variance; the interventional game assumes independent inputs
LIME
sampled forward passes
None guaranteed
Heuristic weights; can violate efficiency
Guided backpropagation
1 modified backward pass
None of the above
Fails the model-randomization test; behaves like edge detection
Where the outside view stops
Suppose an attribution method passed every test above. It would still answer only the question it was built for, which inputs this output depended on. It would not say what the model computed with them. That is a different question, and here is the shortest demonstration.
An attribution at one point is a summary of a function, and many functions share a summary. Inside a network the gap is wider still, since the same function can be computed by many different arrangements of weights. Three further limits follow from the same fact.
An attribution explains one output on one input. The questions at the start of this section are about the computation across inputs. Will it fail when the tag flips? Does it have a backdoor? What algorithm did it learn?
Attributions are taken with respect to inputs, so they cannot name the intermediate variables a model computes, such as "this token is a repeated name" or "the subject of the sentence is a city in France".
They inherit the model's behavior off the training distribution, through baselines, paths, and occlusions.
So we have to look inside. The obvious first move is to record the activations of individual neurons, the single coordinates of a layer's output, and ask what each one responds to by finding the inputs that activate it most. Bolukbasi et al. (2021) showed how that goes wrong. They took neuron 221 in the last layer of BERT, a Transformer text encoder, and found its top-activating sentences in the Quora Question Pairs dataset of 164,246 questions. The top sentences were questions about the meaning of song titles, a clean concept. On a question-answering set drawn from Wikipedia (58,645 sentences), the same neuron's top sentences began with dates ("On 16 June 2006, ..."). On BookCorpus, a collection of novels, a third pattern appeared. Each dataset alone told a convincing story, and the stories disagreed.
That standard governs the rest of the chapter, and the later sections take it up in order. Probing asks what information the activations contain. The logit lens reads them in the model's own output vocabulary. Activation patching intervenes on them, and its fastest variant, attribution patching, is gradient times input applied to an activation instead of an input. Superposition explains why single neurons like neuron 221 so often mean several things at once. Whether a measurement measures what it claims is the same question of validity that §22.1 asked about benchmarks.
Read more
Sundararajan, Taly, Yan (2017). Axiomatic Attribution for Deep Networks. Integrated gradients, the axioms, the saturation example, the uniqueness argument, and practical advice on baselines and step counts.
Olah et al. (2020). Zoom In: An Introduction to Circuits. The case for mechanistic interpretability as a science, with the features, circuits, and universality claims.
Bolukbasi et al. (2021). An Interpretability Illusion for BERT. Why a neuron's top-activating examples on one dataset can tell the wrong story, and why every later method must be tested across distributions.
Watch next
🎬 What is interpretability? · AnthropicA short, plain-language case for looking inside models, from the team behind much of the work in this chapter. Good motivation for why behavior alone is not enough.🎬 An Introduction to Mechanistic Interpretability – Neel Nanda | IASEAI 2025 · International Association for Safe & Ethical AIA lead researcher's map of the field, behavioral versus mechanistic methods, what counts as evidence, and where the chapter is headed.🎬 Shapley Values : Data Science Concepts · ritvikmathA clear whiteboard walk-through of the Shapley formula and its fairness axioms. Pairs with the coalition-weight derivation in this section.🎬 Integrated Gradients Explained — Theory, Axioms & Python Implementation · A Data OdysseyCovers the IG axioms, the baseline, and a from-scratch implementation. A second view on the path integral and completeness.