Integrated Gradients: How Axiomatic Attribution Solves the Gradients-at-Saturation Problem in Deep Neural Networks

Integrated Gradients: How Axiomatic Attribution Solves the Gradients-at-Saturation Problem in Deep Neural Networks Feature attribution methods in deep learning aim to answer a fundamental interpretability question: given an input vector and a trained neural network, how much did each input dimension contribute to the model's final output score? In natural language processing and computer vision, practitioners routinely need to identify which input tokens, pixels, or tabular variables drove a sp

10 min
Integrated Gradients: How Axiomatic Attribution Solves the Gradients-at-Saturation Problem in Deep Neural Networks

Integrated Gradients: How Axiomatic Attribution Solves the Gradients-at-Saturation Problem in Deep Neural Networks

Feature attribution methods in deep learning aim to answer a fundamental interpretability question: given an input vector and a trained neural network, how much did each input dimension contribute to the model's final output score? In natural language processing and computer vision, practitioners routinely need to identify which input tokens, pixels, or tabular variables drove a specific classification, ranking, or token prediction.

Early interpretability approaches relied directly on input gradients (xF(x)\nabla_x F(x)) or simple heuristics like gradient-times-input (xxF(x)x \cdot \nabla_x F(x)). However, these local gradient methods suffer from severe pathologies: gradient saturation, threshold artifacts, and violations of foundational conservation laws. When an activation function plateaus or reaches saturation, its local derivative drops to zero, masking the causal importance of features that drove the network into that saturated state.

To resolve these structural flaws, Mukund Sundararajan, Ankur Taly, and Qiqi Yan introduced Integrated Gradients in their seminal ICML 2017 paper, Axiomatic Attribution for Deep Networks. Grounded in cooperative game theory and path-integral calculus, Integrated Gradients provides a mathematically unique attribution framework that satisfies two core properties: Completeness and Implementation Invariance.

This guide covers the failure modes of raw gradient saliency, the axiomatic derivation of Integrated Gradients from Aumann-Shapley cost sharing, the numerical mechanics of path integration, and practical considerations when attributing transformer token representations in production.


1. The Pathology of Raw Gradients: Saturation and Broken Sensitivity

To understand why simple gradients fail as feature attribution metrics, consider how non-linear neural networks process input features.

The Threshold Toy Example

Consider a simple scalar threshold model defined by:

F(x)=ReLU(1ReLU(1x))F(x) = \text{ReLU}(1 - \text{ReLU}(1 - x))

For an input x[0,)x \in [0, \infty), the output behavior is straightforward:

  • When x=0x = 0, F(0)=0F(0) = 0.
  • When x=1x = 1, F(1)=1F(1) = 1.
  • For any x1x \ge 1, F(x)=1F(x) = 1.

Suppose we evaluate an input x=2x = 2 against a neutral baseline x=0x' = 0. The model produces an output of 11, transitioning from 00 to 11 because xx was increased from 00 to 22.

However, evaluating the local gradient at x=2x = 2 yields:

F(x)xx=2=0\left.\frac{\partial F(x)}{\partial x}\right|_{x=2} = 0

Because the function is completely flat at x=2x = 2, raw gradient saliency assigns an attribution score of 00 to xx. This directly violates the Sensitivity axiom: the input changed from the baseline, the function value changed from 00 to 11, yet the attribution method reports that the feature had zero influence on the prediction.

Output F(x)
 1.0 |           ┌──────────────────────── (Saturated regime: gradient = 0)
     |          /
     |         /
     |        /
 0.0 └───────┴─────────────────────────────
     0       1       2                     Input x
    (x')            (x)

Gradient Saturation in Deep Networks

This saturation phenomenon is ubiquitous across deep architectures:

  • Saturated Activations: Sigmoids, hyperbolic tangents, and ReLUs operate in flat regions where local derivatives vanish.
  • Softmax Output Logits: High-confidence classification heads squash large pre-activation differences into marginal probability changes, compressing gradients toward zero.
  • Layer Normalization and Attention Softmax: In transformers, sharp attention distributions and LayerNorm scale factors produce regions where token gradients under-represent the cumulative causal shift created by earlier layers.

Multiplying the gradient by the input (xiF(x)xix_i \cdot \frac{\partial F(x)}{\partial x_i}) fails to resolve the problem: multiplying a zero gradient by 22 still equals zero.


2. The Axiomatic Foundation of Attribution

Rather than proposing heuristic modifications to backpropagation, Sundararajan, Taly, and Yan approached attribution axiomatically. They defined a set of mathematical requirements that any desirable attribution method must satisfy.

Path Integration versus Saturated Gradients

Axiom 1: Completeness (Sum-to-Delta)

The attributions assigned to all input features must sum to the difference between the model's output at the target input xx and the model's output at a chosen baseline xx':

i=1dAttributioni(x,x)=F(x)F(x)\sum_{i=1}^d \text{Attribution}_i(x, x') = F(x) - F(x')

Completeness guarantees that attributions account for the entire score change without leakage or artificial inflation. If a fraud detection model outputs a probability of 0.850.85 on a transaction xx and 0.050.05 on a neutral baseline transaction xx', the sum of feature attributions must equal exactly 0.800.80.

Axiom 2: Implementation Invariance

Two neural networks are functionally equivalent if they produce identical outputs for all possible inputs, regardless of how they are parameterized or factored across layers.

Implementation Invariance dictates that if F(x)=G(x)F(x) = G(x) for all xx, then the feature attributions for FF and GG must be identical:

AttributioniF(x,x)=AttributioniG(x,x)\text{Attribution}_i^F(x, x') = \text{Attribution}_i^G(x, x')

Many backpropagation heuristics (such as Layer-wise Relevance Propagation (LRP) and standard DeepLIFT configurations) break implementation invariance by relying on intermediate layer representations and discrete layer factorizations.

Axiom 3: Sensitivity (Null Player and Causality)

Sensitivity has two complementary requirements:

  • Sensitivity(a): If an input xx and a baseline xx' differ in exactly one feature xix_i and produce different predictions (F(x)F(x)F(x) \neq F(x')), feature ii must receive non-zero attribution.
  • Sensitivity(b) (Dummy / Null Player): If the model's mathematical output does not depend on feature ii across any input combination, feature ii must receive an attribution of exactly zero.

Axiom 4: Linearity

If a model FF is a linear combination of two sub-networks F(x)=aF1(x)+bF2(x)F(x) = a \cdot F_1(x) + b \cdot F_2(x), the feature attributions must reflect that linear weighting:

AttributioniF(x,x)=aAttributioniF1(x,x)+bAttributioniF2(x,x)\text{Attribution}_i^F(x, x') = a \cdot \text{Attribution}_i^{F_1}(x, x') + b \cdot \text{Attribution}_i^{F_2}(x, x')

Axiom 5: Symmetry Preservation

If two input features xix_i and xjx_j play identical functional roles in the network (swapping their values does not alter the output), and they share the same input and baseline values (xi=xjx_i = x_j and xi=xjx'_i = x'_j), both features must receive identical attribution scores.

The Uniqueness Theorem

In cooperative game theory, attributing credit among continuous variables connecting a reference state to an active state is known as the Aumann-Shapley cost sharing method (Aumann and Shapley, 1974).

Friedman (2004) proved that path integration methods are the only cost-sharing mechanisms that satisfy Completeness, Implementation Invariance, and Linearity. Sundararajan et al. extended this result to deep learning, proving that Integrated Gradients along a straight-line path is the unique path method that also preserves Symmetry.


3. Mathematical Derivation of Integrated Gradients

Integrated Gradients computes feature attribution by integrating the gradient of the model along the straight-line trajectory connecting the baseline xx' to the input xx.

The Continuous Path Integral

Let γ(α)\gamma(\alpha) represent the straight-line path parameterized by α[0,1]\alpha \in [0, 1]:

γ(α)=x+α(xx)\gamma(\alpha) = x' + \alpha (x - x')

The Integrated Gradient for the ii-th feature dimension is defined as:

IGi(x,x)=(xixi)×01F(x+α(xx))xidα\text{IG}_i(x, x') = (x_i - x'_i) \times \int_{0}^{1} \frac{\partial F(x' + \alpha(x - x'))}{\partial x_i} \, d\alpha

Where F(z)xi\frac{\partial F(z)}{\partial x_i} is the partial derivative of model FF along the ii-th dimension evaluated at point zz.

Proof of the Completeness Axiom

The mathematical property of Integrated Gradients lies in how directly it satisfies Completeness via the multivariate Fundamental Theorem of Calculus:

i=1dIGi(x,x)=i=1d(xixi)01F(x+α(xx))xidα\sum_{i=1}^d \text{IG}_i(x, x') = \sum_{i=1}^d (x_i - x'_i) \int_0^1 \frac{\partial F(x' + \alpha(x - x'))}{\partial x_i} \, d\alpha

By swapping the summation and the integral:

i=1dIGi(x,x)=01i=1dF(γ(α))γi(α)dγi(α)dαdα\sum_{i=1}^d \text{IG}_i(x, x') = \int_0^1 \sum_{i=1}^d \frac{\partial F(\gamma(\alpha))}{\partial \gamma_i(\alpha)} \frac{d\gamma_i(\alpha)}{d\alpha} \, d\alpha

Applying the chain rule, the integrand is the total derivative of F(γ(α))F(\gamma(\alpha)) with respect to α\alpha:

i=1dIGi(x,x)=01dF(γ(α))dαdα=F(γ(1))F(γ(0))=F(x)F(x)\sum_{i=1}^d \text{IG}_i(x, x') = \int_0^1 \frac{d F(\gamma(\alpha))}{d\alpha} \, d\alpha = F(\gamma(1)) - F(\gamma(0)) = F(x) - F(x')

Because the line integral integrates through the entire transition from baseline to input, it captures gradients before, during, and after saturation points.


4. Numerical Approximation and Computational Architecture

Because neural networks are complex non-linear compositions, the integral cannot be evaluated in closed form. Instead, it is approximated numerically using discrete step summations.

Riemann Summation

The standard numerical approximation samples mm points uniformly along the straight line using Riemann summation:

IGiapprox(x,x)=(xixi)×1mk=1mF(x+km(xx))xi\text{IG}_i^{\text{approx}}(x, x') = (x_i - x'_i) \times \frac{1}{m} \sum_{k=1}^m \frac{\partial F\left(x' + \frac{k}{m}(x - x')\right)}{\partial x_i}

Alternatively, Gauss-Legendre Quadrature can be used to select optimal evaluation nodes and weights, achieving higher integration accuracy with fewer function evaluations.

Baseline (x')                                                         Input (x)
  α = 0.0       α = 0.25        α = 0.50        α = 0.75        α = 1.0
    ○───────────────●───────────────●───────────────●───────────────●
  F(x')         ∇F(z_1)         ∇F(z_2)         ∇F(z_3)         F(x)

Convergence Monitoring and Step Calibration

To ensure the approximation is numerically sound, implementations compute the completeness error δ\delta:

δ=i=1dIGiapprox(x,x)(F(x)F(x))\delta = \left| \sum_{i=1}^d \text{IG}_i^{\text{approx}}(x, x') - (F(x) - F(x')) \right|

In production auditing systems, the step count mm is typically set between 5050 and 300300. A step count is considered converged when the relative error δF(x)F(x)\frac{\delta}{|F(x) - F(x')|} falls below 5%5\%.

Evaluating mm steps requires mm forward passes and mm backward passes per attribution run. When batching interpolation steps together ([m×d][m \times d] tensors), GPU memory bandwidth becomes the primary performance bottleneck.


5. Applying Integrated Gradients to Transformers and Large Language Models

Applying Integrated Gradients to transformer-based language models introduces architectural challenges distinct from vision or tabular models.

Input Tokens:      ["The", "contract", "is", "void"]
                     │          │        │       │
Embedding Vectors: [ E_1  ,    E_2  ,   E_3  ,  E_4  ]  ∈ ℝ^(L × d_model)
                     │          │        │       │
               Interpolation along α ∈ [0, 1] against Baseline (e.g., [PAD] / 0)
                     │
               Layer Integrated Gradients (Capture dF / dE_j)
                     │
Vector IG:         [ IG_1 ,    IG_2 ,   IG_3 ,  IG_4 ]  ∈ ℝ^(L × d_model)
                     │          │        │       │
L2 Norm / Sum:     [ 0.04 ,    0.72 ,   0.08 ,  0.89 ]  → Scalar Token Attributions

1. Attributing Token Embeddings

In transformers, discrete token IDs cannot be directly differentiated. Integrated Gradients is instead computed with respect to the continuous token embedding vectors ERL×dmodelE \in \mathbb{R}^{L \times d_{\text{model}}}, where LL is sequence length and dmodeld_{\text{model}} is hidden dimension.

For each token position jj and embedding dimension kk:

IGj,k(E,E)=(Ej,kEj,k)×1ms=1mF(E+sm(EE))Ej,k\text{IG}_{j,k}(E, E') = (E_{j,k} - E'_{j,k}) \times \frac{1}{m} \sum_{s=1}^m \frac{\partial F\left(E' + \frac{s}{m}(E - E')\right)}{\partial E_{j,k}}

To convert the resulting [L×dmodel][L \times d_{\text{model}}] attribution matrix into a single scalar importance score per token, practitioners either sum across the embedding dimension or compute the Euclidean norm (L2L_2 norm):

TokenScorej=k=1dmodelIGj,korTokenScorej=k=1dmodel(IGj,k)2\text{TokenScore}_j = \sum_{k=1}^{d_{\text{model}}} \text{IG}_{j,k} \quad \text{or} \quad \text{TokenScore}_j = \sqrt{\sum_{k=1}^{d_{\text{model}}} \left(\text{IG}_{j,k}\right)^2}

2. The NLP Baseline Selection Dilemma

The choice of baseline xx' represents the absence of information. In computer vision, a solid black or blurred image often serves as a natural baseline. In natural language, defining an empty text state is non-trivial, as explored by Sturmfels et al. (Distill 2020):

  • Zero Embedding (0\mathbf{0}): A vector of all zeros across the hidden dimension. Provides a simple, neutral mathematical reference point, but can produce out-of-distribution LayerNorm activations.
  • Padding Token ([PAD]): The embedding vector of the tokenizer pad token. Models are explicitly trained to ignore pad tokens, though pad embeddings still retain non-zero positional and semantic weights.
  • Mask Token ([MASK]): The embedding of the mask token in masked language models (BERT, RoBERTa). Represents unknown or hidden information, but is not natively present in causal autoregressive decoders.
  • Average Embedding (Eˉ\bar{E}): The mean embedding vector across the entire vocabulary. Represents the expected background language signal, though it blurs specific semantic contrasts.
  • Empty String or Random Uniform: Embeddings of whitespace or random uniform draws. Tests input presence versus absence, though random initialization can introduce high-frequency noise.

Because changing the baseline changes the counterfactual question being asked ("why this prediction compared to what reference?"), the baseline must be chosen deliberately and reported alongside the attributions.

3. Layer Integrated Gradients

Beyond input embeddings, Layer Integrated Gradients evaluates attributions with respect to intermediate activations within specific transformer layers:

  • Multi-Head Attention output projections
  • Feed-Forward Network (FFN) intermediate activations
  • Residual stream states at layer ll

This enables interpretability researchers to trace how token representations evolve through the transformer stack, identifying exactly which layer resolves semantic ambiguities or retrieves factual knowledge.


6. Implementation with PyTorch and Captum

The PyTorch Captum library provides native support for Integrated Gradients and Layer Integrated Gradients. Below is an end-to-end implementation for attributing prediction probabilities to input token embeddings in a transformer model:

import torch
from captum.attr import IntegratedGradients, LayerIntegratedGradients

class TransformerExplainer:
    def __init__(self, model, tokenizer):
        self.model = model
        self.tokenizer = tokenizer
        self.model.eval()
        
        # Initialize Layer Integrated Gradients targeting word embeddings
        self.lig = LayerIntegratedGradients(
            self._forward_wrapper, 
            self.model.transformer.wte
        )

    def _forward_wrapper(self, input_embeddings, attention_mask=None):
        # Forward pass consuming continuous embedding tensors
        outputs = self.model(inputs_embeds=input_embeddings, attention_mask=attention_mask)
        # Return target class logit or target token probability
        return outputs.logits[:, -1, :]

    def explain(self, text, target_token_id, n_steps=100):
        # 1. Encode text
        inputs = self.tokenizer(text, return_tensors="pt")
        input_ids = inputs["input_ids"]
        attention_mask = inputs["attention_mask"]

        # 2. Build baseline: all [PAD] tokens (or zero token IDs)
        pad_id = self.tokenizer.pad_token_id or 0
        baseline_ids = torch.full_like(input_ids, fill_value=pad_id)

        # 3. Compute Layer Integrated Gradients
        # Returns attributions with shape [batch, seq_len, hidden_dim]
        attributions, delta = self.lig.attribute(
            inputs=input_ids,
            baselines=baseline_ids,
            target=target_token_id,
            additional_forward_args=(attention_mask,),
            n_steps=n_steps,
            return_convergence_delta=True
        )

        # 4. Collapse hidden dimension into scalar token scores
        token_attributions = attributions.sum(dim=-1).squeeze(0)
        
        # Normalize scores for visualization
        token_attributions = token_attributions / torch.norm(token_attributions)
        tokens = self.tokenizer.convert_ids_to_tokens(input_ids.squeeze(0))

        return list(zip(tokens, token_attributions.detach().cpu().numpy())), delta.item()

7. Extensions, Manifold Paths, and Limitations

While Integrated Gradients is mathematically rigorous and uniquely axiomatic among straight-line path methods, practical deployments must account for several structural limitations:

Out-of-Distribution Interpolation

The straight line γ(α)=x+α(xx)\gamma(\alpha) = x' + \alpha(x - x') in high-dimensional embedding space frequently travels through non-manifold regions (combinations of token features that would never naturally occur in pre-training data). Gradients evaluated in these unrealistic spaces can introduce noise into the attribution sum.

To counter this, advanced path variants have been developed:

  • Guided Integrated Gradients (GIG): Adapts the integration path step-by-step to avoid high-gradient saturated ridges.
  • Blur Integrated Gradients (Blur IG): For image models, sweeps spatial frequencies from blurred baselines rather than linear pixel interpolation.
  • Expected Gradients (IG with Prior Distributions): Averages straight-line paths across an empirical distribution of baselines xDbaselinex' \sim \mathcal{D}_{\text{baseline}}, softening reliance on a single reference vector.

Computational Overhead

Unlike single-pass saliency or attention-weight heuristics, Integrated Gradients requires dozens to hundreds of backward passes per instance. In production RAG monitoring or real-time guardrail systems, running full IG passes on every request is computationally prohibitive. Teams typically run Integrated Gradients asynchronously for root-cause debugging, safety auditing, model evaluation, and offline red-teaming.


Summary

Integrated Gradients bridges continuous vector calculus and axiomatic game theory to solve the gradient saturation bottleneck in deep neural network interpretability. By integrating gradients along the straight-line trajectory between a neutral baseline and the target input:

  • It guarantees Completeness (F(x)F(x)=IGiF(x) - F(x') = \sum \text{IG}_i).
  • It preserves Implementation Invariance across functionally equivalent architectures.
  • It restores Sensitivity, capturing the cumulative contribution of features even when local derivatives have flattened to zero.

For transformer models and LLMs, Integrated Gradients provides a principled, ground-truth attribution mechanism for token embeddings, attention modules, and intermediate residual streams, moving model interpretability beyond superficial attention heatmaps into mathematically grounded causal attribution.


Sources

Written by

More to read

  • Alignment and Uniformity on the Hypersphere: How Geometric Losses Govern Contrastive Representation Learning

    Alignment and Uniformity on the Hypersphere: The Geometric Foundations of Contrastive Representation Learning Contrastive representation learning serves as the foundational objective behind modern neural embeddings, powering dense retrieval systems, visual-language models such as CLIP, and metric learning pipelines. While early literature justified contrastive learning through the InfoMax principle (maximizing mutual information between augmented views), theoretical and empirical analyses have

    1 min
  • Valor and Point72 Back General Intuition at B Valuation for Physical AI and Robotics

    New York-based foundation model startup General Intuition is in discussions to secure new funding at a $6 billion pre-money valuation, according to sources familiar with the matter. The financing round includes new backing from Valor Equity Partners, Point72 Ventures, and Seven Seven Six, alongside continued participation from existing investors Khosla Ventures and General Catalyst. The potential valuation represents a steep increase from the company's previous financing round, which raised $32

    1 min
  • SpaceXAI Deploys NVIDIA Vera CPUs for Gigawatt-Scale Agentic Infrastructure and Starmind Satellite

    SpaceXAI has selected NVIDIA's Vera central processing units to handle the CPU-bound orchestration and execution workloads powering its Grok models as its computing infrastructure expands toward gigawatts of capacity. The deployment spans both ground-based data centers and orbital systems, with SpaceXAI planning to base its first-generation Starmind AI satellite on an optimized Vera Rubin NVL72 rack architecture. While GPU clusters handle core model training and forward passes, agentic AI workf

    1 min