Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP004 · Algorithm
Integrated Gradients
Allocate the change in a differentiable scalar output between a reference input and the input of interest by integrating gradients along a path.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.
- Gradient attributions for a saved model Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Runs in CodeSplash (Experimental)
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through explain.gradients. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Allocate the change in a differentiable scalar output between a reference input and the input of interest by integrating gradients along a path.
Question / hypotheses
How does the selected output change along this reference-to-input path, and how is that change distributed among input coordinates?
Architecture / computational structure
A gradient-capable predictor is evaluated at a sequence of interpolated inputs. Numerical quadrature integrates each input derivative; multiplying by the input-reference difference gives an attribution vector. The common straight path is part of the explanation specification.
Assumptions
The scalar output must be chosen and the function must be sufficiently regular along the path. Discrete tokens require an embedding-space interpretation. The baseline and intermediate inputs must be discussed even when they lie outside the data manifold.
Formula / computational definition
Parameters and notation
Predictor weights are fixed. Choices include baseline x0 or baseline distribution, target output/logit, path, quadrature rule, number of integration steps, and batching. Numerical completeness error estimates integration error; it is not an uncertainty interval for feature relevance. Here x is the query, x0 the reference, i a feature coordinate, alpha the interpolation fraction in [0,1], and f the selected scalar output.
Fitting, fine-tuning, and adaptation
Recompute after fine-tuning and preserve the baseline protocol for comparisons. For multi-output models specify the same output target or a justified contrast. Averaging over baselines changes the reference question and should be reported.
How to use and calibrate
Run finite-difference gradient checks on small inputs, increase quadrature steps, and report completeness residuals. Compare plausible baselines and examine parameter-randomization sensitivity. Separate signed attributions from absolute-magnitude visualizations.
Example application
Explain a nonlinear predictor with a known analytic gradient, then show how changing the reference changes the contributions while completeness still holds.
Interpretation / pitfalls
Watch out
A complete attribution can still be scientifically misleading. Saturation, input scale, path through implausible regions and baseline choice matter. Attribution to embeddings is not an intervention on words or people. The method does not make the model causal.
Python and R code examples
Scope: Complete trapezoidal IG calculation for a supplied smooth two-variable function. Python: NumPy; R: base R.
Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Complete trapezoidal IG calculation for a supplied smooth two-variable function. Python: NumPy; R: base R. · needs numpy
import numpy as np
x=np.array([1.,2.]); b=np.zeros(2);a=np.linspace(0,1,1001);z=b+a[:,None]*(x-b)
g=np.column_stack([2*z[:,0]+z[:,1],z[:,0]])
ig=(x-b)*np.trapezoid(g,a,axis=0)
assert np.allclose(ig.sum(),x[0]**2+x[0]*x[1]);print(ig)
Component demo, not a trained model — Complete trapezoidal IG calculation for a supplied smooth two-variable function. Python: NumPy; R: base R.
x<-c(1,2);b<-c(0,0);a<-seq(0,1,length.out=1001);z<-outer(a,x-b)+matrix(b,1001,2,byrow=TRUE)
g<-cbind(2*z[,1]+z[,2],z[,1]);ig<-(x-b)*colSums((g[-1,,drop=FALSE]+g[-nrow(g),,drop=FALSE])/2)*diff(a)[1]
stopifnot(abs(sum(ig)-(x[1]^2+x[1]*x[2]))<1e-10);print(ig)
Sources
- Source 1proceedings.mlr.press
- Source 2captum.ai
From the CodeSplash reference, entry EXP004.