Skip to content

Guided Backpropagation

View colab tutorial | View source | 📰 Paper

Guided-backprop is one of the first attribution method and was proposed in 2014. Its operation is similar to Saliency: it consists in backpropagating the output score with respect to the input, however, at each non-linearity (the ReLUs), only the positive gradient of positive activations are backpropagated. We can see this as a filter on the backprop.

More precisely, with \(f\) our classifier and \(f_l(x)\) the activation at layer \(l\), we usually have:

\[ \frac{\partial f(x)}{\partial f_{l}(x)} = \frac{\partial f(x)}{\partial \text{ReLU}(f_{l}(x))} \frac{\partial \text{ReLU}(f_l(x))}{\partial f_{l}(x)} = \frac{\partial f(x)}{\partial \text{ReLU}(f_{l}(x))} \odot \mathbb{1}(f_{l}(x)) \]

with \(\mathbb{1}(.)\) the indicator function. With Guided-backprop, the backpropagation is modified such that :

\[ \frac{\partial f(x)}{\partial f_{l}(x)} = \frac{\partial f(x)}{\partial \text{ReLU}(f_{l}(x))} \odot \mathbb{1}(f_{l}(x)) \odot \mathbb{1}(\frac{\partial f(x)}{\partial \text{ReLU}(f_{l}(x))}) \]

Example

from xplique.attributions import GuidedBackprop

# load images, labels and model
# ...

method = GuidedBackprop(model)
explanations = method.explain(images, labels)

Notebooks

GuidedBackprop

Used to compute the Guided Backpropagation, which modifies the classic Saliency procedure on ReLU's non linearities, allowing only the positive gradients from positive activations to pass through.

__init__(self,
         model: keras.src.engine.training.Model,
         output_layer: Union[str, int, None] = None,
         batch_size: Optional[int] = 32,
         operator: Union[xplique.commons.operators.Tasks, str,
         Callable[[keras.src.engine.training.Model, tf.Tensor, tf.Tensor], float], None] = None)

Parameters

  • model : keras.src.engine.training.Model

    • The model from which we want to obtain explanations

  • output_layer : Union[str, int, None] = None

    • Layer to target for the outputs (e.g logits or after softmax).

      If an int is provided it will be interpreted as a layer index.

      If a string is provided it will look for the layer name.

      Default to the last layer.

      It is recommended to use the layer before Softmax.

  • batch_size : Optional[int] = 32

    • Number of inputs to explain at once, if None compute all at once.

  • operator : Union[xplique.commons.operators.Tasks, str, Callable[[keras.src.engine.training.Model, tf.Tensor, tf.Tensor], float], None] = None

    • Function g to explain, g take 3 parameters (f, x, y) and should return a scalar, with f the model, x the inputs and y the targets. If None, use the standard operator g(f, x, y) = f(x)[y].

explain(self,
        inputs: Union[tf.Dataset, tf.Tensor, numpy.ndarray],
        targets: Union[tf.Tensor, numpy.ndarray, None] = None) -> tf.Tensor

Compute Guided Backpropagation for a batch of samples.

Parameters

  • inputs : Union[tf.Dataset, tf.Tensor, numpy.ndarray]

    • Dataset, Tensor or Array. Input samples to be explained.

      If Dataset, targets should not be provided (included in Dataset).

      Expected shape among (N, W), (N, T, W), (N, H, W, C).

      More information in the documentation.

  • targets : Union[tf.Tensor, numpy.ndarray, None] = None

    • Tensor or Array. One-hot encoding of the model's output from which an explanation is desired. One encoding per input and only one output at a time. Therefore, the expected shape is (N, output_size).

      More information in the documentation.

Return

  • explanations : tf.Tensor

    • Guided Backpropagation maps.