Guided Backprop¶

class
captum.attr.
GuidedBackprop
(model)[source]¶ Computes attribution using guided backpropagation. Guided backpropagation computes the gradient of the target output with respect to the input, but gradients of ReLU functions are overridden so that only nonnegative gradients are backpropagated.
More details regarding the guided backpropagation algorithm can be found in the original paper here: https://arxiv.org/abs/1412.6806
Warning: Ensure that all ReLU operations in the forward function of the given model are performed using a module (nn.module.ReLU). If nn.functional.ReLU is used, gradients are not overridden appropriately.
 Parameters
model (nn.Module) – The reference to PyTorch model instance.

attribute
(inputs, target=None, additional_forward_args=None)[source]¶  Parameters
inputs (tensor or tuple of tensors) – Input for which attributions are computed. If forward_func takes a single tensor as input, a single input tensor should be provided. If forward_func takes multiple tensors as input, a tuple of the input tensors should be provided. It is assumed that for all given input tensors, dimension 0 corresponds to the number of examples (aka batch size), and if multiple input tensors are provided, the examples must be aligned appropriately.
target (int, tuple, tensor or list, optional) –
Output indices for which gradients are computed (for classification cases, this is usually the target class). If the network returns a scalar value per example, no target index is necessary. For general 2D outputs, targets can be either:
a single integer or a tensor containing a single integer, which is applied to all input examples
a list of integers or a 1D tensor, with length matching the number of examples in inputs (dim 0). Each integer is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
A single tuple, which contains #output_dims  1 elements. This target index is applied to all examples.
A list of tuples with length equal to the number of examples in inputs (dim 0), and each tuple containing #output_dims  1 elements. Each tuple is applied as the target for the corresponding example.
Default: None
additional_forward_args (any, optional) – If the forward function requires additional arguments other than the inputs for which attributions should not be computed, this argument can be provided. It must be either a single additional argument of a Tensor or arbitrary (nontuple) type or a tuple containing multiple additional arguments including tensors or any arbitrary python types. These arguments are provided to forward_func in order, following the arguments in inputs. Note that attributions are not computed with respect to these arguments. Default: None
 Returns
 attributions (tensor or tuple of tensors):
The guided backprop gradients with respect to each input feature. Attributions will always be the same size as the provided inputs, with each value providing the attribution of the corresponding input index. If a single tensor is provided as inputs, a single tensor is returned. If a tuple is provided for inputs, a tuple of corresponding sized tensors is returned.
 Return type
tensor or tuple of tensors of attributions
Examples:
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32, >>> # and returns an Nx10 tensor of class probabilities. >>> net = ImageClassifier() >>> gbp = GuidedBackprop(net) >>> input = torch.randn(2, 3, 32, 32, requires_grad=True) >>> # Computes Guided Backprop attribution scores for class 3. >>> attribution = gbp.attribute(input, target=3)