AI Logic

Explainability (XAI)

DeepLIFT

Updated:

Diagram of DeepLIFT. The left side shows the flow: the input and the reference input are passed forward, their difference is taken, and multipliers are backpropagated to obtain contributions. The right side shows a computation example on a small saturating network (z = 1 − i1 − i2, h = ReLU(z), y = 1 − h): gradients and gradient × input give contributions of 0, while DeepLIFT assigns 0.5 to each input, summing to the output difference of 1

What is DeepLIFT?

DeepLIFT (Deep Learning Important FeaTures) is a method that rewrites the output of a neural network as a difference, "how much it changed from the output for a reference input", and splits that difference into contributions from each input feature. Shrikumar, Greenside, and Kundaje proposed it in 2017.

It compares the value of each neuron with its value when a reference input (for example, an all-black image) is fed in, and distributes that difference by backpropagating it from the output to the input. The contributions sum exactly to the difference in the output. Like ordinary backpropagation, the computation needs only one backward pass.

Methods that use gradients, such as the gradient saliency map, assign zero contribution even to important features wherever the output has leveled off (is saturated). Because DeepLIFT uses differences from the reference instead of gradients, it can assign contributions even where the gradient is 0.

The gradient expresses "how much the output moves when the input is moved by a tiny amount", whereas the DeepLIFT multiplier expresses "how much the output moved when the input was moved from the reference value to the actual value". As an analogy, the gradient measures the slope of a hill only underfoot, while the DeepLIFT multiplier divides the difference in height between the starting point and the current position by the distance traveled.

Background: two problems with gradient-based contributions

The paper lists the following two problems with earlier contribution methods.

  • Saturation: when the output levels off with respect to changes in the input, the gradient becomes 0. In the example on the right of the top figure, h=max⁡(0,1−i1−i2)h = \max(0, 1 - i_1 - i_2) and y=1−hy = 1 - h; for i1+i2>1i_1 + i_2 > 1, hh stays flat at 0, so the gradient of yy with respect to i1i_1 and i2i_2 is 0. Setting only one of i1i_1 or i2i_2 back to 0 does not change the output either, so methods that remove inputs one at a time and observe the change in the output also give zero contribution.
  • Discontinuity: for a ReLU with a bias of −10-10, the gradient jumps from 0 to 1 the moment the input exceeds 10. When the input is 10+ϵ10 + \epsilon, gradient × input assigns a large contribution of 10+ϵ10 + \epsilon to the input and −10-10 to the bias. Although the output is almost 0, the contributions suddenly become large.

Among gradient-based methods, Guided Backpropagation and DeconvNet discard negative values during backpropagation, so they also cannot show inputs that worked to lower the output.

Computation steps

The explanation follows the equations in Section 3 of Shrikumar et al. (2017), who proposed the method. Let tt be the output neuron of interest and x1,…,xnx_1, \dots, x_n the neurons of a preceding layer.

  1. Choose a reference input. It is chosen to suit the problem; for MNIST, the paper uses an all-zero image (the background color).
  2. Run the input and the reference input through the forward pass, and compute for every neuron the difference between its value and its reference value, Δx=x−x0\Delta x = x - x^0.
  3. Going from the output to the input, compute the multipliers mΔxΔtm_{\Delta x \Delta t} with per-layer rules and backpropagate them by connecting them with the chain rule (Eq. 3).
  4. For each input feature, multiply the multiplier by the difference to obtain the contribution CΔxiΔt=mΔxiΔt ΔxiC_{\Delta x_i \Delta t} = m_{\Delta x_i \Delta t}\, \Delta x_i (Eq. 2).

The contributions are defined so that they sum to the difference in the output. The paper calls this property summation-to-delta.

∑i=1nCΔxiΔt=Δt,Δt=t−t0(1)\sum_{i=1}^{n} C_{\Delta x_i \Delta t} = \Delta t, \qquad \Delta t = t - t^0 \qquad (1)

The multiplier is the contribution divided by the difference in the input. Whereas a partial derivative is "the ratio of infinitesimal changes", a multiplier is "the ratio of finite differences".

mΔxΔt=CΔxΔtΔx(2)m_{\Delta x \Delta t} = \frac{C_{\Delta x \Delta t}}{\Delta x} \qquad (2)

With an input layer xx, a hidden layer yy, and an output tt, multipliers can be connected in the same form as the chain rule for partial derivatives. This allows them to be computed in a single pass, just like ordinary backpropagation.

mΔxiΔt=∑jmΔxiΔyj mΔyjΔt(3)m_{\Delta x_i \Delta t} = \sum_{j} m_{\Delta x_i \Delta y_j}\, m_{\Delta y_j \Delta t} \qquad (3)

There are three rules for determining the multipliers of each layer.

RuleLayers it applies toMultiplier
Linear ruleFully connected and convolutional layers (y=b+∑iwixiy = b + \sum_i w_i x_i)The weight wiw_i (when separating positive and negative contributions, they are assigned by the sign of wiΔxiw_i \Delta x_i)
Rescale ruleNonlinear functions with a single input (ReLU, tanh, sigmoid)Δy/Δx\Delta y / \Delta x; replaced by the gradient when Δx\Delta x is close to 0
RevealCancel ruleSame as aboveSeparates positive and negative differences, and averages the effect of each with and without the other

The right side of the top figure is an example of the Rescale rule. With i1=i2=1i_1 = i_2 = 1 and a reference of 0,00, 0, z=1−i1−i2z = 1 - i_1 - i_2 changes from 1 to −1-1 (Δz=−2\Delta z = -2), hh from 1 to 0 (Δh=−1\Delta h = -1), and yy from 0 to 1 (Δy=1\Delta y = 1). The ReLU multiplier is Δh/Δz=0.5\Delta h / \Delta z = 0.5, which is not 0 even though the gradient is 0. By the chain rule, m=(−1)×0.5×(−1)=0.5m = (-1) \times 0.5 \times (-1) = 0.5, so i1i_1 and i2i_2 each receive a contribution of 0.5, and the total matches Δy=1\Delta y = 1.

Separating positive and negative contributions: the RevealCancel rule

Why the RevealCancel rule is needed. The Rescale rule can skew contributions when positive and negative inputs cancel each other out. The paper's example is o=min⁡(i1,i2)o = \min(i_1, i_2) (with a reference of 0,00, 0).

  • Rescale rule: the entire contribution goes to the smaller input. It hides the fact that the output also depends on the other input. Gradients, gradient × input, Guided Backpropagation, and Integrated Gradients likewise assign everything to one of the two.
  • RevealCancel rule: assigns 0.5min⁡(i1,i2)0.5 \min(i_1, i_2) to each of the two inputs.

The RevealCancel rule averages the effect of the positive difference Δx+\Delta x^+ "without the negative difference" and "with the negative difference", and does the same for the negative difference Δx−\Delta x^-. The paper notes that this can be viewed as the Shapley values of the two players, the positive and negative differences.

Δy+=12(f(x0+Δx+)−f(x0))+12(f(x0+Δx−+Δx+)−f(x0+Δx−))\Delta y^+ = \tfrac{1}{2}\big(f(x^0 + \Delta x^+) - f(x^0)\big) + \tfrac{1}{2}\big(f(x^0 + \Delta x^- + \Delta x^+) - f(x^0 + \Delta x^-)\big) Δy−=12(f(x0+Δx−)−f(x0))+12(f(x0+Δx++Δx−)−f(x0+Δx+))\Delta y^- = \tfrac{1}{2}\big(f(x^0 + \Delta x^-) - f(x^0)\big) + \tfrac{1}{2}\big(f(x^0 + \Delta x^+ + \Delta x^-) - f(x^0 + \Delta x^+)\big)

However, the Rescale rule can be better suited for suppressing noise. When an input failing to reach a threshold simply signals noise, the Rescale rule gives it zero contribution, whereas the RevealCancel rule may assign it a nonzero contribution.

Experiments in the paper

The paper compared the method with others on two tasks: handwritten digits and DNA sequences. In both cases the results are shown as figures.

ExperimentSetupResult
Handwritten digits (MNIST)A CNN with 2 convolutional layers and fully connected layers (test accuracy 99.2%). To turn a digit 8 into a 3 or a 6, up to 157 pixels (20% of the image) are erased in order of contribution, and the change in the log-odds between the two classes is measuredDeepLIFT with the RevealCancel rule selected pixels that moved the image closer to the target class better than gradient × input, Integrated Gradients, Guided Backpropagation, and others. Guided Backpropagation discards negative gradients, so it discriminated between classes poorly
DNA sequences (simulated data)A multi-task CNN that predicts which motifs are present in length-200 sequences embedded with short sequences (motifs) bound by 2 types of proteinsGradient × input and Guided Backpropagation × input gave excessive contributions to motifs that matched only weakly (the discontinuity problem). The combination of RevealCancel for fully connected layers and Rescale for convolutional layers revealed both motifs in the task that requires both, with little noise

On MNIST, Integrated Gradients and gradient × input gave similar results, and the paper concludes that "saturation and discontinuity problems do not arise much on this data".

Caveats in practice

DeepLIFT contributions depend strongly on the choice of reference input. In the paper, on CIFAR-10 images, using a blurred version of the original image as the reference highlighted object outlines, while using an all-zero image highlighted background pixels that were hard to interpret. Choose the reference by considering "what the difference should be measured against" for the problem, and in some cases compute with several references and average the results.

Keep the following points in mind as well.

  • Explain the values before the softmax or sigmoid: when the output saturates, the contributions shrink so that their sum matches the output difference, which makes them hard to compare across inputs. The paper recommends computing contributions for the value just before the last nonlinear function (the logit). For a softmax, normalize by subtracting the mean contribution across all classes (Eq. 5).
  • It depends on the implementation: the Integrated Gradients paper points out that DeepLIFT does not satisfy the property "if the outputs are the same, the contributions are the same" (implementation invariance). This is because the chain rule for multipliers does not hold exactly for finite differences, unlike for partial derivatives.
  • Handling of max operations is unresolved: the paper states that, for Maxout and max pooling, there is not yet a better way to propagate contributions than falling back to the gradient.
  • Per-layer rules must be implemented: the backward computation has to be replaced for each nonlinear function, such as ReLU.

Comparison with other attribution methods

The table compares DeepLIFT with attribution methods it is often compared with, from the same viewpoints.

MethodWhat it usesReference (baseline) inputContributions sum to the output differenceAssigns contributions in saturated regionsAmount of computation
Gradient saliency mapGradient at the inputNot usedNoNo1 backward pass
Gradient × inputProduct of the gradient at the input and the inputImplicitly all zerosNoNo1 backward pass
DeepLIFTDifference from the reference and multipliers obtained with per-layer rulesUsedYesYes1 backward pass
Integrated GradientsIntegral of gradients from the baseline to the inputUsedYesYes20 to 300 backward passes

According to the DeepLIFT paper, the LRP rules for ReLU networks are equivalent to a constant multiple of gradient × input, apart from tricks for numerical stability. Also, as Lundberg and Lee (2016) pointed out, DeepLIFT can be viewed as a fast approximation of Shapley values, which are contributions averaged over orderings of the inputs.

Rough guidelines for choosing:

  • Contributions from a single backward pass that are robust to saturation: use DeepLIFT.
  • Implementation invariance matters and the extra computation is acceptable: use Integrated Gradients.
  • Both of two cancelling inputs should be shown: use DeepLIFT's RevealCancel rule.

PyTorch example

Show code
python
import torch
import torch.nn as nn

'''
Compute DeepLIFT (Rescale rule) contributions on a small network of only Linear and ReLU layers
(Shrikumar et al. 2017: ReLU multiplier m = Δy/Δx, fully connected layer multiplier = weights, chain rule in the same form as backpropagation)
'''


class RescaleReLU(torch.autograd.Function):
  # The forward pass is an ordinary ReLU. The backward pass propagates the multiplier Δy/Δx instead of the gradient
  @staticmethod
  def forward(ctx, x, x_ref):
    ctx.save_for_backward(x, x_ref)
    return x.clamp(min=0)

  @staticmethod
  def backward(ctx, grad_out):
    x, x_ref = ctx.saved_tensors
    dx = x - x_ref
    dy = x.clamp(min=0) - x_ref.clamp(min=0)
    grad = (x > 0).to(x.dtype)                     # fall back to the gradient when Δx is close to 0
    safe = dx.abs() > 1e-7
    m = torch.where(safe, dy / torch.where(safe, dx, torch.ones_like(dx)), grad)
    return grad_out * m, None


def deeplift(layers, x, x_ref, target):
  # x, x_ref: [1, input dim]. Use the output values before the softmax (logits)
  x = x.clone().requires_grad_(True)
  h, h_ref = x, x_ref
  for layer in layers:
    if isinstance(layer, nn.ReLU):
      h, h_ref = RescaleReLU.apply(h, h_ref.detach()), h_ref.clamp(min=0)
    else:                                          # fully connected layer: linear rule (multiplier = weights)
      h, h_ref = layer(h), layer(h_ref)
  delta_t = (h - h_ref)[0, target]                 # Δt = t - t^0
  # obtain the multipliers m_{Δx Δt} with a single backward pass (Eq. 3)
  m = torch.autograd.grad(delta_t, x)[0]
  contrib = m * (x - x_ref)                        # Eq. 2: C = m × Δx
  return contrib.detach(), delta_t.item()


torch.manual_seed(0)
layers = nn.Sequential(
  nn.Linear(784, 128), nn.ReLU(),
  nn.Linear(128, 64), nn.ReLU(),
  nn.Linear(64, 10),
)
layers.eval()
x = torch.rand(1, 784)            # example: an MNIST image flattened into a row
x_ref = torch.zeros(1, 784)       # reference input: all zeros (the MNIST background)
target = layers(x).argmax(dim=1).item()
contrib, delta_t = deeplift(layers, x, x_ref, target)
# Eq. 1: the contributions sum to the output difference Δt
print(f'sum of contributions {contrib.sum().item():.4f}, Δt {delta_t:.4f}')

FAQ

Q. What should be used as the reference input?
A.

It depends on "what the difference should be measured against". The paper uses an all-zero image matching the background for MNIST, an input with the average frequencies of the bases A, C, G, and T for DNA sequences (or the average over several shuffled versions of the original sequence), and a blurred version of the original image for CIFAR-10. When it is hard to settle on one, compute with several references and average the results.

Q. Which should be used, the Rescale rule or the RevealCancel rule?
A.

RevealCancel suits cases where both of two cancelling inputs should be shown, and Rescale suits cases where noise should be suppressed. In the paper's DNA experiment, the combination of RevealCancel for fully connected layers and Rescale for convolutional layers produced less noise than using RevealCancel for all layers. On MNIST, using Rescale instead of RevealCancel made the results worse.

Q. How does it differ from gradient × input?
A.

Gradient × input implicitly uses an all-zero reference and approximates the difference with the gradient at the input. DeepLIFT uses ratios of differences from the reference (multipliers) instead of gradients, so it can assign contributions that sum to the output difference even in saturated regions where the gradient is 0 and at discontinuities where the gradient jumps. In addition, DeepLIFT does not assign contributions to biases.

References

  1. A. Shrikumar, P. Greenside, and A. Kundaje, "Learning Important Features Through Propagating Activation Differences," Proceedings of the 34th International Conference on Machine Learning (ICML), PMLR 70, pp. 3145–3153, 2017(arXiv:1704.02685).
  2. M. Sundararajan, A. Taly, and Q. Yan, "Axiomatic Attribution for Deep Networks," Proceedings of the 34th International Conference on Machine Learning (ICML), PMLR 70, 2017(arXiv:1703.01365).
  3. S. Lundberg and S.-I. Lee, "An unexpected unity among methods for interpreting model predictions," arXiv:1611.07478, 2016.