Search

Search papers and lessons

Integrated Gradients

Level 4 · 15 min read

Integrated gradients explains a model's output by adding up how the output changes along a straight path from a neutral baseline to the actual input, giving each input feature a share that sums to the total change.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

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 F(x)=x2F(x) = x^2. Its input is x=3x = 3, and the baseline is x′=0x' = 0. The score at the input is F(3)=9F(3) = 9 and at the baseline F(0)=0F(0) = 0. 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.

  1. Pick a baseline. The baseline is the "no information" input, the value the model should give almost no score to. Here x′=0x' = 0, so F(x′)=0F(x') = 0. Check: F(0)=02=0F(0) = 0^2 = 0.
  2. Draw a straight path from the baseline to the input. Points on the path are written γ(α)=x′+α(x−x′)\gamma(\alpha) = x' + \alpha(x - x'), with α\alpha running from 0 to 1. For our numbers, γ(α)=3α\gamma(\alpha) = 3\alpha. Check: γ(0)=0\gamma(0) = 0 is the baseline and γ(1)=3\gamma(1) = 3 is the input.
  3. Compute the gradient at points on the path. The Gradient of FF at a point is its slope there, and for one input that slope is the ordinary partial derivative. For F(x)=x2F(x) = x^2 the slope is 2x2x. At the path point γ(α)=3α\gamma(\alpha) = 3\alpha the slope is 2⋅3α=6α2 \cdot 3\alpha = 6\alpha. Check: at α=0.5\alpha = 0.5 the point is x=1.5x = 1.5 and the slope is 2⋅1.5=3=6⋅0.52 \cdot 1.5 = 3 = 6 \cdot 0.5.
  4. 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 dαd\alpha, gives a definite integral: ∫016α dα\int_0^1 6\alpha \, d\alpha. Because the path is a straight segment, this is a special case of a line integral. Check: the integral of 6α6\alpha from 0 to 1 is 3α23\alpha^2 evaluated from 0 to 1, which is 3−0=33 - 0 = 3.
  5. Multiply by the distance the input moved. The input moved from 0 to 3, a distance of 3. So the attribution is 3×3=93 \times 3 = 9.
  6. Check that the attribution equals the score change. The attribution is 9, and the score change was F(3)−F(0)=9−0=9F(3) - F(0) = 9 - 0 = 9. 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.

00.20.40.60.810246α (position along the path)slope of F
  • slope of F along the path, 6α
The area under this line is 3. Multiplied by the input's distance of 3, it gives 9, which is exactly F(3) minus F(0).

Now a case with two features, where the shares matter. Let F(x1,x2)=x1x2F(x_1, x_2) = x_1 x_2, with baseline (0,0)(0, 0) and input (2,3)(2, 3). The score at the input is 66 and at the baseline 00, so the total change is 66. We want one share for x1x_1 and one for x2x_2.

  1. Path. γ(α)=(2α,3α)\gamma(\alpha) = (2\alpha, 3\alpha).
  2. Gradient at a path point. The partial derivatives of x1x2x_1 x_2 are ∂F/∂x1=x2\partial F/\partial x_1 = x_2 and ∂F/∂x2=x1\partial F/\partial x_2 = x_1. At the path point the gradient is (3α,2α)(3\alpha, 2\alpha).
  3. Average each coordinate over the path. Use two steps, at the midpoints α=0.25\alpha = 0.25 and α=0.75\alpha = 0.75. For the first coordinate, 3×0.25=0.753 \times 0.25 = 0.75 and 3×0.75=2.253 \times 0.75 = 2.25, whose average is (0.75+2.25)/2=1.5(0.75 + 2.25)/2 = 1.5. For the second coordinate, 2×0.25=0.52 \times 0.25 = 0.5 and 2×0.75=1.52 \times 0.75 = 1.5, whose average is 1.01.0. Check: the exact averages are ∫013α dα=1.5\int_0^1 3\alpha \, d\alpha = 1.5 and ∫012α dα=1.0\int_0^1 2\alpha \, d\alpha = 1.0, so two steps already give the exact answer here.
  4. Multiply each average by that feature's distance moved. Feature 1 moved 2−0=22 - 0 = 2, so its share is 2×1.5=32 \times 1.5 = 3. Feature 2 moved 3−0=33 - 0 = 3, so its share is 3×1.0=33 \times 1.0 = 3.
  5. Check the sum. 3+3=63 + 3 = 6, which equals F(2,3)−F(0,0)=6F(2, 3) - F(0, 0) = 6. 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

Definition: Integrated gradients

For a model F:Rn→RF : \mathbb{R}^n \to \mathbb{R}, an input xx and a baseline x′x', the integrated gradient for feature ii is

IGi(x)=(xi−xi′)∫01∂F(x′+α(x−x′))∂xi dα\mathrm{IG}_i(x) = (x_i - x'_i) \int_{0}^{1} \frac{\partial F\big(x' + \alpha (x - x')\big)}{\partial x_i} \, d\alpha

Here ∂F/∂xi\partial F / \partial x_i is the partial derivative of FF with respect to feature ii, and α\alpha is the position along the straight path from x′x' (at α=0\alpha = 0) to xx (at α=1\alpha = 1).

Step 2 in symbols

The straight path is γ(α)=x′+α(x−x′)\gamma(\alpha) = x' + \alpha (x - x'). Its position at α=0\alpha = 0 is x′x' and at α=1\alpha = 1 is xx. Each coordinate moves linearly. In the toy example, x′=0x' = 0 and x=3x = 3, so γ(α)=0+α(3−0)=3α\gamma(\alpha) = 0 + \alpha(3 - 0) = 3\alpha.

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 γ(α)\gamma(\alpha), and the gradient at that point is what the integrand needs. For F(x)=x2F(x) = x^2, ∂F/∂x=2x\partial F / \partial x = 2x, so the integrand at α\alpha is 2⋅3α=6α2 \cdot 3\alpha = 6\alpha.

Step 4 in symbols

The integral is ∫016α dα=[3α2]01=3\int_0^1 6\alpha \, d\alpha = \left[ 3\alpha^2 \right]_0^1 = 3. Multiplying by x−x′=3x - x' = 3 gives IG=9\mathrm{IG} = 9.

Step 6 in symbols: completeness

Summing over all features, the attributions always add to the total change in the score:

∑i=1nIGi(x)=F(x)−F(x′)\sum_{i=1}^{n} \mathrm{IG}_i(x) = F(x) - F(x')

This is the fundamental theorem of calculus applied to the path. Differentiating FF along the path gives the integrand, and integrating that over α\alpha from 0 to 1 recovers F(γ(1))−F(γ(0))F(\gamma(1)) - F(\gamma(0)). The proof needs FF 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 mm evenly spaced points on the path, usually a few dozen to a few hundred. This is a Riemann sum with midpoints at αk=(k−0.5)/m\alpha_k = (k - 0.5)/m for k=1,…,mk = 1, \ldots, m:

IGi(x)≈(xi−xi′)⋅1m∑k=1m∂F(x′+αk(x−x′))∂xi\mathrm{IG}_i(x) \approx (x_i - x'_i) \cdot \frac{1}{m} \sum_{k=1}^{m} \frac{\partial F\big(x' + \alpha_k (x - x')\big)}{\partial x_i}

In the two-feature example, m=2m = 2, α1=0.25\alpha_1 = 0.25, α2=0.75\alpha_2 = 0.75, and the average is exactly 1.51.5 for feature 1 because the integrand 3α3\alpha is a straight line, and a midpoint sum is exact for straight lines. For curved integrands the approximation has an error that shrinks as mm grows.

Worked examples

Example
Example 1: the square, one feature (tiny)

Model F(x)=x2F(x) = x^2, baseline x′=1x' = 1, input x=3x = 3.

  1. The score at the input is F(3)=9F(3) = 9. The score at the baseline is F(1)=1F(1) = 1. The total change is 9−1=89 - 1 = 8.
  2. The path is γ(α)=1+2α\gamma(\alpha) = 1 + 2\alpha, which goes from 1 to 3.
  3. The slope at a path point is 2γ(α)=2+4α2\gamma(\alpha) = 2 + 4\alpha.
  4. The integral ∫01(2+4α) dα=[2α+2α2]01=2+2=4\int_0^1 (2 + 4\alpha) \, d\alpha = \left[ 2\alpha + 2\alpha^2 \right]_0^1 = 2 + 2 = 4.
  5. Multiply by the distance moved, x−x′=2x - x' = 2: IG=2×4=8\mathrm{IG} = 2 \times 4 = 8.
  6. 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.

Example
Example 2: the product, two features (standard)

Model F(x1,x2)=x1x2F(x_1, x_2) = x_1 x_2, baseline (1,0)(1, 0), input (3,2)(3, 2).

  1. Scores: F(3,2)=6F(3, 2) = 6 and F(1,0)=0F(1, 0) = 0. The total change is 66.
  2. Path: γ(α)=(1+2α,2α)\gamma(\alpha) = (1 + 2\alpha, 2\alpha).
  3. Gradient at a path point: (∂F/∂x1,∂F/∂x2)=(x2,x1)=(2α,1+2α)(\partial F/\partial x_1, \partial F/\partial x_2) = (x_2, x_1) = (2\alpha, 1 + 2\alpha).
  4. Integrate the first coordinate: ∫012α dα=1\int_0^1 2\alpha \, d\alpha = 1. The second coordinate: ∫01(1+2α) dα=1+1=2\int_0^1 (1 + 2\alpha) \, d\alpha = 1 + 1 = 2.
  5. Multiply by the distance moved. Feature 1 moved 3−1=23 - 1 = 2, so IG1=2×1=2\mathrm{IG}_1 = 2 \times 1 = 2. Feature 2 moved 2−0=22 - 0 = 2, so IG2=2×2=4\mathrm{IG}_2 = 2 \times 2 = 4.
  6. Sum: 2+4=62 + 4 = 6, 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.

Example
Example 3: a small image-style model (realistic)

Imagine a toy model that takes two pixel intensities (p,q)(p, q) and outputs a score F(p,q)=p⋅qF(p, q) = p \cdot q, a product that stands in for a learned interaction between two pixels. A photo of a bird is the input (p,q)=(0.8,0.6)(p, q) = (0.8, 0.6), and the black image baseline is (0,0)(0, 0). Use two steps.

  1. Scores: F(0.8,0.6)=0.48F(0.8, 0.6) = 0.48 and F(0,0)=0F(0, 0) = 0. The total change is 0.480.48.
  2. Path: γ(α)=(0.8α,0.6α)\gamma(\alpha) = (0.8\alpha, 0.6\alpha). Midpoints are α=0.25\alpha = 0.25 and α=0.75\alpha = 0.75.
  3. Gradient at α\alpha: (0.6α,0.8α)(0.6\alpha, 0.8\alpha). At α=0.25\alpha = 0.25 that is (0.15,0.20)(0.15, 0.20). At α=0.75\alpha = 0.75 that is (0.45,0.60)(0.45, 0.60).
  4. Average over the two midpoints: first coordinate (0.15+0.45)/2=0.30(0.15 + 0.45)/2 = 0.30, second coordinate (0.20+0.60)/2=0.40(0.20 + 0.60)/2 = 0.40.
  5. Multiply by distance moved: pixel pp moved 0.80.8, so IGp=0.8×0.30=0.24\mathrm{IG}_p = 0.8 \times 0.30 = 0.24. Pixel qq moved 0.60.6, so IGq=0.6×0.40=0.24\mathrm{IG}_q = 0.6 \times 0.40 = 0.24.
  6. Sum: 0.24+0.24=0.480.24 + 0.24 = 0.48, 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 F(x1,x2)=x12+x2F(x_1, x_2) = x_1^2 + x_2, baseline (0,0)(0, 0), input (2,4)(2, 4). Use the exact integral. Find IG1\mathrm{IG}_1 and IG2\mathrm{IG}_2, and confirm they sum to F(2,4)−F(0,0)F(2,4) - F(0,0).

Answer: The total change is (4+4)−0=8(4 + 4) - 0 = 8. The path is (2α,4α)(2\alpha, 4\alpha). The gradient is (2x1,1)=(4α,1)(2x_1, 1) = (4\alpha, 1). The integral of the first coordinate is ∫014α dα=2\int_0^1 4\alpha \, d\alpha = 2, and the second is ∫011 dα=1\int_0^1 1 \, d\alpha = 1. Multiply by the distances: IG1=2×2=4\mathrm{IG}_1 = 2 \times 2 = 4 and IG2=4×1=4\mathrm{IG}_2 = 4 \times 1 = 4. Sum =8= 8, 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 F(x)=x2F(x) = x^2 at x=0x = 0: 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 F(x)=x2F(x) = x^2 with baseline x′=0x' = 0 and input x=2x = 2, find the integrated gradient for the single feature. Give a number.Show answer

The total score change is F(2)−F(0)=4F(2) - F(0) = 4. The path is γ(α)=2α\gamma(\alpha) = 2\alpha, the slope along it is 2⋅2α=4α2 \cdot 2\alpha = 4\alpha, and ∫014α dα=2\int_0^1 4\alpha \, d\alpha = 2. Multiplying by the distance moved, 2−0=22 - 0 = 2, gives IG=4\mathrm{IG} = 4. It equals the score change, as completeness requires.

QuestionWhy: a classmate says that because the gradient at the input is zero for F(x)=x2F(x) = x^2 at x=0x = 0, 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 x=0x = 0 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: 0.3+0.1=0.40.3 + 0.1 = 0.4, which equals 0.9−0.50.9 - 0.5. 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.

Sources