Course contentsShow
← Machine Learning and Deep Learning
Lesson 3248 of 3,53871. Interpretability: Neural Network MethodsFree lesson

Riemann Approximation in Practice

Implementing IG with discrete steps: choosing the number of interpolation steps and computing the sum.

Riemann Approximation in Practice

What you'll learn: How to actually compute Integrated Gradients by turning the continuous integral into a discrete sum of steps.

From Theory to Implementation

Integrated Gradients gives you a beautiful integral formula, but computers can't compute continuous integrals directly—they work with discrete steps. This is where Riemann approximation comes in: you break the smooth path from baseline to input into a finite number of stops, compute the gradient at each stop, and sum them up.

Think of it like calculating distance traveled: instead of tracking every infinitesimal moment, you sample your speedometer at regular intervals (say, every 5 minutes) and multiply each reading by the time interval.

Choosing the Number of Steps

The key practical decision is how many interpolation steps (m) to use. Too few steps and your approximation is crude—you miss important gradient changes. Too many and you waste computation without meaningful accuracy gains.

Common practice: Start with 20-50 steps for quick experiments, use 100-300 for production interpretations. For neural networks, 50 steps often provides a good balance.

m = 50  # number of interpolation steps
alphas = [i / m for i in range(m + 1)]  # α = 0, 1/50, 2/50, ..., 1

Computing the Sum

For each α, you create an interpolated input x' = baseline + α * (input - baseline), compute its gradient, and accumulate. The final formula becomes:

IG ≈ (input - baseline) × (1/m) × Σ[gradient at step i]

The (1/m) factor is your Riemann rectangle width—the "dx" from calculus.

Key Takeaway: Integrated Gradients requires discretizing the path into m steps; choosing m balances accuracy versus computation, with 50-300 being typical in practice.