# Interpretability: Integrated Gradients is a decent attribution method

*A short post laying out our reasoning for using integrated gradients as attribution method. It is intended as a stand-alone post based on our LIB papers* [*\[1\]*](https://arxiv.org/abs/2405.10927)  [*\[2\]*](https://arxiv.org/abs/2405.10928)*. This work was produced at Apollo Research.*

Context
=======

Understanding circuits in neural networks requires understanding how features interact with other features. There's a lot of features and their interactions are generally non-linear. A good starting point for understanding the interactions might be to just figure out how *strongly* each pair of features in adjacent layers of the network interacts. But since the relationships are non-linear, how do we quantify their 'strength' in a principled manner that isn't vulnerable to common and simple counterexamples? In other words, how do we quantify how much the value of a feature in layer $l+1$ should be attributed to a feature in layer $l$?

This is a [well-known](https://en.wikipedia.org/wiki/Shapley_value) sort of problem originally investigated in cooperative game theory. A while ago it made its way into machine learning, where people were pretty interested in attributing neural network outputs to their inputs for a while. Lately it's made its way into interpretability in the context of attributing variables in one hidden layer of a neural network to another.

Generally, the way people go about this is setting up a series of 'common-sense' axioms that the attribution method should fulfil in order to be self-consistent and act like an attribution is supposed to act. Then they try to show that there is one unique method that satisfies these axioms. Except that (a) people disagree about what axioms are 'common-sense', and (b) the axioms people maybe agree most on don't quite single out a single method as unique, just a *class* of methods called path attributions. So no attribution method has really been generally accepted as the canonical 'winner' in the ml context yet. Though some methods are certainly more popular than others.

Integrated Gradients
====================

Integrated gradients is a computationally efficient attribution method (compared to activation patching / ablations) grounded in a series of axioms. It was originally proposed the context of economics ([Friedman 2004](https://www.econstor.eu/bitstream/10419/94328/1/1999-23.pdf)), and recently used to attribute neural networks outputs to their inputs([Sundararajan *et al.* 2017](https://arxiv.org/abs/1703.01365)). Even more recently, they started being used for internal feature attribution as well ([Marks *et al.* 2024](https://arxiv.org/abs/2403.19647), [Redwood Research (unpublished) 2022](https://github.com/redwoodresearch/interp/blob/261f52eb1433a3dcbd3ef4a8884f514de560b2b2/interp/tools/custom_jvp.py#L7)).

Properties of integrated gradients
----------------------------------

Suppose we want to explain to what extent the value of an activation $f^{{l_2}}_i$ in a layer $l_2$ of a neural network can be 'attributed to' the various components of  the activations $\mathbf{f}^{l_1} = [f^{l_1}_0, \dots, f_d^{l_1}]$ in layer $l_1$ upstream of $$$l_2$.[^9bxx0c5qdnv] For now, we do this for a single datapoint only. So we want to know how much $f^{l_2}_i(x)$ can be attributed to $f^{l_1}_j(x)$. We'll write this attribution as $A^{l_2,l_1}_{i,j}(x)$.$$$$

There is a list of four standard requirements of properties attribution methods should satisfy that single out *path attributions* as the only kind of attribution methods that can be used to answer this question. Integrated gradients, and other path attribution methods, fulfil all of these ([Sundararajan *et al.* 2017](https://arxiv.org/abs/1703.01365)).

1.  **Implementation Invariance:** If two different networks have activations $\mathbf{f}^{{l_2}}$, $\mathbf{g}^{{l_2}}$ such that $f^{l_2}_i(\mathbf{f}^{l_1})={g}^{{l_2}}_i(\mathbf{f}^{l_1})$ for all possible inputs $\mathbf{f}^{l_1}$, then the attributions for any $f^{l_1}_j$ in both networks is the same. 
2.  **Completeness**: The sum over all attributions equals the value of $f^{{l_2}}_i(x)$, that is $\sum_j A^{l_2,l_1}_{i,j}(x)= f^{l_2}_i(x)$.
3.  **Sensitivity**: If $f^{{l_2}}_i$ does not depend (mathematically) on $f^{l_1}_j$, the attribution of $f^{l_1}_j$ for $f^{{l_2}}_i$ is zero.
4.  **Linearity**: Let $g=a_1 f^{{l_2}}_{i_1} + a_2 f^{{l_2}}_{i_2}$. Then the attribution from $f^{l_1}_j$ to $g$ should equal the weighted sum of its attributions for $f^{{l_2}}_{i_1}$ and $f^{{l_2}}_{i_2}$.

If you add on a fifth requirement that the attribution method behaves sensibly under coordinate transformations, integrated gradients are the only attribution method that satisfies all five axioms:

1.  **Consistency under Coordinate Transformations**: If we transform layer $l_1$ into an alternate basis of orthonormal coordinates, where the activation vector is one-hot (  $\mathbf{f}^{l_1}(x) = [\vert\vert\mathbf{f}^{l_1}(x)\vert\vert, 0, \dots, 0]$)[^tvumfn9e7n] then  the first component $f^{l_1}_0(x)$ should receive the full attribution $f^{l_2}_i(x)$, and the other components should receive zero attribution.

In other words, all the attribution should go to the direction our activation vector $\mathbf{f}^{l}(x)$actually lies in. If we go into an alternate basis of coordinates such that one of our coordinate basis vectors $\mathbf{e}_1$ lies along $\mathbf{f}^{l_1}(x)$, $\mathbf{e}_1= \frac{\mathbf{f}^{l_1}(x)}{\vert\vert\mathbf{f}^{l_1}(x)\vert\vert}$, then the component along $\mathbf{e}_1$ should get all the attribution at data point $x$, because the other components aren't even active and thus obviously can't influence anything.

We think that this is a pretty important property for an attribution method to have in the context of interpreting neural network internals. The hidden layers of neural networks don't come with an obvious privileged basis. Their activations are vectors in a vector space, which we can view in any basis we please. So in a sense, any structure in the network internals that actually matters for the computation should be [coordinate independent](https://www.lesswrong.com/posts/sxhfSBej6gdAwcn7X/coordinate-free-interpretability-theory). If our attribution methods are not well-behaved under coordinate transformations, they can give all kinds of misleading results, for example by [taking the network out of the subspace the activations are usually located in](https://www.lesswrong.com/posts/RFtkRXHebkwxygDe2/an-interpretability-illusion-for-activation-patching-of). 

Property 4 already ensures that the attributions are well-behaved under linear coordinate transformations of the target layer $l_2$. This 5th axiom ensures they're also well-behaved under coordinate transforms in the starting layer $l_1$.

We will show below that adding the 5th requirement singles out integrated gradients as the canonical attribution method that satisfies all five requirements.

Integrated gradient formula
---------------------------

The general integrated gradient formula to attribute the influence of feature $f^{l_1}_j(x)$ in a layer $l_1$ on feature $f^{{l_2}}_i(x)$ in layer $l_2$ is given by an integral along a straight-line path $\mathcal{C}$ in layer $l_1$ activation space. To clarify notation, we introduce a function which maps activations from layer $l_1$ to $l_2$  $\mathbf{F}^{l_2,l_1}: \mathbb{R}^{d^{l_1}}\to\mathbb{R}^{d^{l_2}}$ . For example, in an MLP (bias folded in) we might have  $\mathbf{F}^{l_2,l_1}(\mathbf{f^l}) = \text{ReLU}(W^{l_1} \mathbf{f}^{l_1})$. Then we can write the attribution from $f^{l_1}_j(x)$ to $f^{{l_2}}_i(x)$ as

$$
\begin{aligned} A^{l_2,l_1}_{ij}(x)&:= \int_{\mathcal{C}} \text{d}z_j \left[\frac {\partial} {\partial z_j} \big(F^{l_2,l_1}_i(\mathbf{z})\big) \right]_{\mathbf{z}=\alpha\mathbf{f}^{l_1}(x)+(1-\alpha) \mathbf{b}^{l_1}}\ &= f^{l_1}_j(x)\int^1_{0} \text{d}\alpha \left[\frac{ \partial }{ \partial z_j } (F^{l_2,l_1}_i(\mathbf{z})) \right]_{\mathbf{z}=\alpha\mathbf{f}^{l_1}(x)+(1-\alpha) \mathbf{b}^{l_1}} \,.\ \end{aligned}
$$

where $\mathbf{z}$ is a point in the layer $l_1$ activation space, and the path $\mathcal{C}$ is parameterised by $\alpha \in [0,1]$, such that along the curve we have $\mathbf{z}(\alpha) = \alpha \mathbf{f}^{l_1}(x) + (1-\alpha)\mathbf{b}^{l_1} $.[^tvumfn9e7n]

Intuitively, this formula asks us to integrate the gradient of $f^{{l_2}}_i(x)$ with respect to $f^{l_1}_j(x)$ along a straight path from a baseline activation $\mathbf{b}^{l_1}$ to the actual activation vector $\mathbf f^{l_1}(x)$, and multiply the result with $f^{l_1}_j(x)$.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/0717ff843f4f749df102a94e4877c79300c636b5baf87342.png)

We illustrate the integrated gradient attribution with a two-dimensional example. The plot shows a feature $f^{l_2}_1$in layer $l_2$ that we want to attribute to the two features $f^{l_1}_1$ and $f^{l_1}_2$ in layer $l_1$. The attribution to $f^{l+1}_1$ (or $f^{l_1}_2$) is calculated by integrating the gradient of $f^{l_2}_1$ with respect to $f^{l_1}_1$ (or $f^{l_1}_2$) along a straight line from the baseline activation $\mathbf{b}^{l_1}$, here chosen to be $\mathbf{b}^{l_1}=(0,0)$, to the activation vector $(f^{l_1}_1(x), f^{l_1}_2(x))$ and multiplying the result by the activation $f^{l_1}_1(x)$ (or $f^{l_1}_2(x)$).

Proof sketch: Integrated Gradients are uniquely consistent under coordinate transformations
===========================================================================================

[Friedman 2004](https://link.springer.com/article/10.1007/s001820400173) showed that any attribution method satisfying the first four axioms must be a path attribution of the form

$$
\begin{equation} \begin{aligned} &A^{l_2,l_1}_{ij}(x):= \int_{\mathcal{C}} \text{d}z_{j} \left[\frac {\partial} {\partial z^{l_1}_{j}} \big(F^{l_2,l_1}_i(\mathbf{z}^{l_1})\big) \right]\ &\text{with}\,\,\mathbf{z}^{l_1}(\alpha): \mathbb{R} \rightarrow \mathbb{R}^{n_{l_1}},\, \mathbf{z}^{l_1}(0)=\mathbf{b}^{l_1},\,\mathbf{z}^{l_1}(1)=\mathbf{f}^{l_1}(x)\,, \end{aligned} \end{equation}
$$

or a convex combination (weighted average with weights $c_k$) of these

$$
\begin{equation} \begin{aligned} &A^{l_2,l_1}_{ij}(x):= \sum_{k} c_k\int_{\mathcal{C}_k} \text{d}z_{k,j} \left[\frac {\partial} {\partial z^{l_1}_{k,j}} \big(F^{l_2,l_1}_i(\mathbf{z}^{l_1}_k)\big) \right]\ &\text{with}\,\,\mathbf{z}^{l_1}_k(\alpha): \mathbb{R} \rightarrow \mathbb{R}^{n_{l_1}},\, \mathbf{z}^{l_1}_k(0)=\mathbf{b}^{l_1},\,\mathbf{z}^{l_1}_k(1)=\mathbf{f}^{l_1}(x)\,,\,\sum_k c_k=1\,,\,c_k\geq 0\,. \end{aligned} \end{equation}
$$

Each term is a line integral along a monotonous path $\mathcal{C}_k$ in the activation space of layer $l_1$ that starts at the baseline $\mathbf{b}^{l_1}$ and ends at the activation vector $\mathbf{f}^{l_1}(x)$.

**Claim:** The only attribution that also satisfies the fifth axiom is the straight line from $\mathbf{b}^{l_1}$ to $\mathbf{f}^{l_1}(x)$. That is, $c_k = 0$ for all the paths in the sum except for the path parametrised as

$$
\mathbf{z}^{l_1}_1(\alpha)= \mathbf{b}^{l_1} (1-\alpha) + \alpha\,\mathbf{f}^{l_1}(x).
$$

**Proof sketch:** Take $\mathbf{f}^{l_2}(\mathbf{f}^{l_1}(x))=\mathbf{b}^{l_1}+\sum_k U_{1,k} \left(f^{l_1}_k(x)-b^{l_1}_k\right) e^{-z \sum_{\{{i\vert\;i>1}\}} {\left(\sum_j U_{i,j} \left(f^{l_1}_j(x)-b^{l_1}_j\right)\right)}^2}$ as the mapping between layers $l_1$ and $l_2$, with $U\in \mathbb{R}$ an orthogonal matrix $U U^T=1$, and $U_{1,k}=\frac{f^{l_1}_k(x)-b^{l_1}_k}{\vert\vert \mathbf{f}^{l_1}(x)-\mathbf{b}^{l_1} \vert\vert}$. Then, for any monotonous paths $\mathcal{C}_k$ which are *not* the straight line $\mathbf{z}^{l_1}_1(\alpha)$, at least one direction $v$ in layer $l_1$ $$with $v \cdot f^{l_1}(x)= 0$ will be assigned an attribution $>0$. 

Since no monotonous paths lead to a negative attribution, the sum over all paths must then also yield an attribution $>0$ for those $v$, unless $c_k=0$ for every path in the sum except $\mathbf{z}^{l_1}_1(\alpha)= \mathbf{b}^{l_1} (1-\alpha) + \alpha\,\mathbf{f}^{l_1}(x)$.

The problem of choosing a baseline
==================================

The integrated gradient formula still has one free hyperparameter in it: The baseline $\mathbf{b}^l$. We're trying to attribute the activations in one layer to the activations in another layer. This requires specifying the coordinate origin relative to which the activations are defined. 

Zero might look like a natural choice here, but if we are folding the biases into the activations, do we want the baseline for the bias to be zero as well? Or maybe we want the origin to be the expectation value of the activation $\mathbf{E}(\mathbf{f}^l)$ over the training dataset? But then we'd have a bit of a consistency problem with axiom 2 across layers, because the expectation value of a layer $\mathbf{E}(\mathbf{f}^{l+1})$ often will not equal its activation at the expectation value $\mathbf{E}(\mathbf{f}^{l})$ of the previous layer, $\mathbf{E}(\mathbf{f}^{l+1}) \neq\mathbf{F}^{l+1,l}(\mathbf{E}(\mathbf{f}^{l}))$. So, with this baseline the attributions to the activations in a layer $l$ would not add up to the activations in layer $l+1$. In fact, for some activation functions, like sigmoids for example, $0 \neq\mathbf{F}^{l+1,l}(0)$, so baseline zero potentially has this consistency problem as well.  
  
We don't feel like we have a good framing for picking the baseline in a principled way yet.

Attributions over datasets
==========================

We now have a method for how to do attributions on single data points. But when we're searching for circuits, we're probably looking for variables that have strong attributions between each other on average, measured over many data points. But how do we average attributions for different data points into a single attribution over a data set in a principled way?  
  
We don't have a perfect answer to this question. We experimented with applying the integrated gradient definition to functionals, attributing measures of the size of the function $f^{l_2}_i: x \rightarrow f^{l_2}_i(x)$ to the functions $f^{l_1}_j: \rightarrow f^{l_2}_i(x)$  but found counter-examples to those (e.g. cancellation between negative and positive attribution). Thus we decided to simply take the RMS over attributions on single datapoints

$A^{l_2,l_1}_{i,j}(\mathcal{D}) = \sqrt{\sum_x {A^{l_2,l_1}_{i,j}(x)}^2}\,.$  
  
This averaged attribution does not itself fulfil axiom 2 (completeness), but it seems workable in practice. We have not found any counterexamples (situations where $A^{l_2,l_1}_{i,j}(\mathcal{D})=0$ even though $f^{l_1}_j$ is obviously important for $f^{l_2}_i$) for good choices of bases (such as LIB).

* * *

Acknowledgements
----------------

This work was done as part of the LIB interpretability project [\[1\]](https://arxiv.org/abs/2405.10927) [\[2\]](https://arxiv.org/abs/2405.10928) at Apollo Research where it benefitted from empirical feedback: the method was implemented by Dan Braun, Nix Goldowsky-Dill, and Stefan Heimersheim. Earlier experiments were conducted by Avery Griffin, Marius Hobbhahn, and Jörn Stöhler.

[^9bxx0c5qdnv]: The activation vectors here are defined relative to some baseline \(b\). This can be zero, but it could also be the mean value over some data set. 

[^tvumfn9e7n]: Integrated gradients still leaves us a free choice of baseline relative to which we measure activations. We chose 0 for most of this post for simplicity, but e.g. the dataset mean of the activations also works.