TL;DR
A trained network turns an input, such as a photo or a sentence, into a score. Integrated gradients answers the question "which input features pushed the score, and by how much?" It walks in a straight line from a neutral baseline input (for example, an all-black image) to the real input, adds up the gradient at every point on the way, and gives each feature a share of the change in score. The shares always sum to the total change, which is what makes the answer checkable.
Builds on: Backpropagation, Gradient · Leads to: interpretability tools for image and text models that need a score that adds up
Where it's used
Checking image classifiers. A network that reads a photo and outputs a class score can be asked which pixels drove the score up. Researchers use attribution maps like this to check whether a model is looking at the object or at an unrelated background pattern. The map is only as trustworthy as the method behind it, which is why the method's guarantees matter. Open-source libraries such as Captum, a PyTorch interpretability library, ship an implementation of the method.
Checking text classifiers for spurious words. A sentiment model that reads a movie review and outputs a score can be probed the same way. Each word gets a signed number: positive means the word pushed the review toward "positive", negative means it pushed the other way. Teams use these numbers to find words the model relies on that have no real connection to the label.
Auditing a model that sets a price. Suppose a model estimates an insurance premium from customer features. Here the score is a number someone will be billed, not just a label. A reviewer needs to know how the premium splits across the features, and that the splits add up to the premium minus a reference premium. The completeness property gives exactly this kind of accounting, which a plain gradient does not guarantee.
How it works
Take a toy model with one input and one output. The model is . Its input is , and the baseline is . The score at the input is and at the baseline . So the score rose by 9. The question is how much of that rise to assign to the single input feature. Here is the procedure.
- Pick a baseline. The baseline is the "no information" input, the value the model should give almost no score to. Here , so . Check: .
- Draw a straight path from the baseline to the input. Points on the path are written , with running from 0 to 1. For our numbers, . Check: is the baseline and is the input.
- Compute the gradient at points on the path. The Gradient of at a point is its slope there, and for one input that slope is the ordinary partial derivative. For the slope is . At the path point the slope is . Check: at the point is and the slope is .
- Add up the slopes along the path, weighted by the step size. Adding the slopes at every point on the path, each multiplied by a tiny width , gives a definite integral: . Because the path is a straight segment, this is a special case of a line integral. Check: the integral of from 0 to 1 is evaluated from 0 to 1, which is .
- Multiply by the distance the input moved. The input moved from 0 to 3, a distance of 3. So the attribution is .
- Check that the attribution equals the score change. The attribution is 9, and the score change was . They match. With one feature there is only one share, and it takes the whole change.
The picture of step 4 is the most useful one to keep in mind. The slope along the path is a line rising from 0 to 6, and the attribution is the area under that line, scaled by the distance moved.
- slope of F along the path, 6α
Now a case with two features, where the shares matter. Let , with baseline and input . The score at the input is and at the baseline , so the total change is . We want one share for and one for .
- Path. .
- Gradient at a path point. The partial derivatives of are and . At the path point the gradient is .
- Average each coordinate over the path. Use two steps, at the midpoints and . For the first coordinate, and , whose average is . For the second coordinate, and , whose average is . Check: the exact averages are and , so two steps already give the exact answer here.
- Multiply each average by that feature's distance moved. Feature 1 moved , so its share is . Feature 2 moved , so its share is .
- Check the sum. , which equals . Each feature gets half the change here. That is a coincidence of these particular numbers, not a general rule: the two inputs differ, and the shares depend on how the slopes along the path balance.
The math
For a model , an input and a baseline , the integrated gradient for feature is
Here is the partial derivative of with respect to feature , and is the position along the straight path from (at ) to (at ).
Step 2 in symbols
The straight path is . Its position at is and at is . Each coordinate moves linearly. In the toy example, and , so .
Step 3 in symbols
The chain rule tells us how a composed function changes, and it is the background for computing gradients layer by layer. Here the model is evaluated at , and the gradient at that point is what the integrand needs. For , , so the integrand at is .
Step 4 in symbols
The integral is . Multiplying by gives .
Step 6 in symbols: completeness
Summing over all features, the attributions always add to the total change in the score:
This is the fundamental theorem of calculus applied to the path. Differentiating along the path gives the integrand, and integrating that over from 0 to 1 recovers . The proof needs to be smooth enough for the integral to exist, which is true for networks built from common pieces such as ReLU units and softmax outputs, since they are continuous and differentiable almost everywhere.
Using a sum instead of an integral
A computer cannot integrate exactly, so the integral is approximated by averaging the gradient at evenly spaced points on the path, usually a few dozen to a few hundred. This is a Riemann sum with midpoints at for :
In the two-feature example, , , , and the average is exactly for feature 1 because the integrand is a straight line, and a midpoint sum is exact for straight lines. For curved integrands the approximation has an error that shrinks as grows.
Worked examples
Model , baseline , input .
- The score at the input is . The score at the baseline is . The total change is .
- The path is , which goes from 1 to 3.
- The slope at a path point is .
- The integral .
- Multiply by the distance moved, : .
- The attribution is 8, equal to the total change 8. Completeness holds.
Interpretation: with a baseline of 1 instead of 0, the single feature still receives the whole change, now 8, measured from the baseline's score.
Model , baseline , input .
- Scores: and . The total change is .
- Path: .
- Gradient at a path point: .
- Integrate the first coordinate: . The second coordinate: .
- Multiply by the distance moved. Feature 1 moved , so . Feature 2 moved , so .
- Sum: , equal to the total change. Completeness holds.
Interpretation: feature 2 gets twice the share of feature 1. The reason is that feature 2 moved from 0 to 2 while the other factor was already large, so its change mattered more. The shares are not just "how far each feature moved"; they track how the score actually responded.
Imagine a toy model that takes two pixel intensities and outputs a score , a product that stands in for a learned interaction between two pixels. A photo of a bird is the input , and the black image baseline is . Use two steps.
- Scores: and . The total change is .
- Path: . Midpoints are and .
- Gradient at : . At that is . At that is .
- Average over the two midpoints: first coordinate , second coordinate .
- Multiply by distance moved: pixel moved , so . Pixel moved , so .
- Sum: , equal to the total change. Completeness holds.
Interpretation: the two pixels happen to get equal shares here, because their distances moved and their average slopes balance for these numbers. A real attribution map does this for millions of pixels at once, with the gradients computed by Backpropagation at every path point. The same arithmetic applies, and the shares still add up to the score change.
Try it: Take , baseline , input . Use the exact integral. Find and , and confirm they sum to .
Answer: The total change is . The path is . The gradient is . The integral of the first coordinate is , and the second is . Multiply by the distances: and . Sum , which matches.
Common mistakes and tradeoffs
It is tempting to think that the gradient at the input already tells you the importance of each feature. In fact the plain gradient is a local slope, and it can be zero even when a feature matters a lot. Take at : the slope is 0, but moving the input from 0 to 3 changes the score by 9. Integrated gradients fixes this because it integrates the slope over the whole path from the baseline, not just at the input. This is why the baseline has to be chosen with care: a different baseline gives different attributions, so the choice is a modelling decision and should be reported with the result.
It is also tempting to think that the attributions are a measure of cause. In fact they describe how the model's score responds to changes along one particular path. Two different paths, or two different baselines, give different shares, and the shares describe the model's computation, not the world. A feature with a small share is not proven irrelevant, and a feature with a large share is not proven causal.
Integrated gradients is a good choice when you need shares that sum to the score change, when the model is differentiable, and when you can name a sensible baseline. It is a poor choice when the model has no meaningful neutral input, when the inputs are discrete tokens and the gradient must be taken with respect to an embedding instead, or when a quick approximate map is enough and cost matters, since it needs many gradient evaluations. Alternatives include plain gradient saliency (cheap, but it fails the completeness check), layer-wise relevance propagation, and Shapley-style methods that average over many feature subsets (more principled, but far more expensive).
History
Gradient-based saliency maps for image networks were popular in the mid-2010s. They were cheap and looked sensible, but they often failed basic checks, such as ignoring features that clearly changed the score. Other methods fixed some failures but broke a different property, so there was no shared standard for a good attribution.
In 2017, Mukund Sundararajan, Ankur Taly and Qiqi Yan proposed integrated gradients in the paper "Axiomatic Attribution for Deep Networks", which was presented at ICML that year. Their approach started from two stated properties that an attribution method should satisfy, sensitivity and implementation invariance, and showed that integrating gradients along a straight path from a baseline meets them. The completeness property, that the shares sum to the score change, follows from the fundamental theorem of calculus. The method caught on because it is simple to implement with the same backpropagation that trains the network, and because its properties can be checked rather than judged by eye.
Recap
- A model's score is a function of its inputs. To explain a score, we need shares for each input that say how much it contributed.
- The gradient gives a local slope, which can mislead when the score changes far from the input.
- Integrated gradients takes the straight path from a baseline to the input, integrates the gradient along it, and multiplies by the distance each feature moved.
- The shares sum to the total change in score (completeness), which is the reason the method is trusted for numeric scores.
- In practice the integral is a sum over a few dozen to a few hundred points on the path, computed with Backpropagation.
What to read next: the feedforward network lesson shows what the model being explained looks like, and Chain Rule explains why the gradients along the path can be computed layer by layer.
Check yourself
QuestionRecall: what does the baseline represent, and why does the method need one?Show answer
The baseline is a neutral reference input, one the model should give almost no score to, such as an all-black image or a zero embedding. Attributions measure change relative to it, because the shares are defined as the score change from the baseline to the input. Without a baseline there is nothing to measure the change against.
QuestionComputation: for with baseline and input , find the integrated gradient for the single feature. Give a number.Show answer
The total score change is . The path is , the slope along it is , and . Multiplying by the distance moved, , gives . It equals the score change, as completeness requires.
QuestionWhy: a classmate says that because the gradient at the input is zero for at , the feature must not matter. Why is that wrong for integrated gradients, and what does the method do instead?Show answer
The gradient at one point is a local slope. At the slope is zero, but the score changes a lot as the input moves away from 0. Integrated gradients integrates the slope over the whole straight path from a baseline to the input, so it captures how the score responded across that range rather than at one point.
QuestionApplied: a sentiment model's score for a review is 0.9 and for an empty review (the baseline) is 0.5. Two words get integrated-gradient shares of 0.3 and 0.1. Do the numbers make sense, and what should you check?Show answer
They do add up as the method promises: , which equals . So the shares are consistent with the score change. You should still check the baseline, since a different empty-text baseline would change the shares, and check that the approximation used enough path steps for the sum to be close to the exact integral.