class_docstr: Layer-wise relevance propagation is based on a backward propagation
  mechanism applied sequentially to all layers of the model. Here, the
  model output score represents the initial relevance which is decomposed
  into values for each neuron of the underlying layers. The decomposition
  is defined by rules that are chosen for each layer, involving its weights
  and activations. Details on the model can be found in the original paper
  [https://doi.org/10.1371/journal.pone.0130140]. The implementation is
  inspired by the tutorial of the same group
  [https://doi.org/10.1016/j.dsp.2017.10.011] and the publication by
  Ancona et al. [https://openreview.net/forum?id=Sy21R9JAW].
docstr: Args:
  
      inputs (Tensor or tuple[Tensor, ...]): Input for which relevance is
                  propagated. If model takes a single
                  tensor as input, a single input tensor should be provided.
                  If model 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, 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 (tuple, 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 (non-tuple) type or a tuple
              containing multiple additional arguments including tensors
              or any arbitrary python types. These arguments are provided to
              model in order, following the arguments in inputs.
              Note that attributions are not computed with respect
              to these arguments.
              Default: None
  
      return_convergence_delta (bool, optional): Indicates whether to return
              convergence delta or not. If `return_convergence_delta`
              is set to True convergence delta will be returned in
              a tuple following attributions.
              Default: False
  
      verbose (bool, optional): Indicates whether information on application
              of rules is printed during propagation.
  
  Returns:
      *Tensor* or *tuple[Tensor, ...]* of **attributions**
      or 2-element tuple of **attributions**, **delta**:
  
        - **attributions** (*Tensor* or *tuple[Tensor, ...]*):
                  The propagated relevance values with respect to each
                  input feature. The values are normalized by the output score
                  value (sum(relevance)=1). To obtain values comparable to other
                  methods or implementations these values need to be multiplied
                  by the output score. 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. The sum of attributions
                  is one and not corresponding to the prediction score as in other
                  implementations.
  
        - **delta** (*Tensor*, returned if return_convergence_delta=True):
                  Delta is calculated per example, meaning that the number of
                  elements in returned delta tensor is equal to the number of
                  of examples in the inputs.
  
  Examples::
  
          >>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
          >>> # and returns an Nx10 tensor of class probabilities. It has one
          >>> # Conv2D and a ReLU layer.
          >>> net = ImageClassifier()
          >>> lrp = LRP(net)
          >>> input = torch.randn(3, 3, 32, 32)
          >>> # Attribution size matches input size: 3x3x32x32
          >>> attribution = lrp.attribute(input, target=5)
code: def attribute(
      self,
      inputs: TensorOrTupleOfTensorsGeneric,
      target: TargetType = None,
      additional_forward_args: Any = None,
      return_convergence_delta: bool = False,
      verbose: bool = False,
  ) -> Union[
      TensorOrTupleOfTensorsGeneric,
      Tuple[TensorOrTupleOfTensorsGeneric, Tensor],
  ]:
      r"""
      Args:
  
          inputs (Tensor or tuple[Tensor, ...]): Input for which relevance is
                      propagated. If model takes a single
                      tensor as input, a single input tensor should be provided.
                      If model 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, 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 (tuple, 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 (non-tuple) type or a tuple
                  containing multiple additional arguments including tensors
                  or any arbitrary python types. These arguments are provided to
                  model in order, following the arguments in inputs.
                  Note that attributions are not computed with respect
                  to these arguments.
                  Default: None
  
          return_convergence_delta (bool, optional): Indicates whether to return
                  convergence delta or not. If `return_convergence_delta`
                  is set to True convergence delta will be returned in
                  a tuple following attributions.
                  Default: False
  
          verbose (bool, optional): Indicates whether information on application
                  of rules is printed during propagation.
  
      Returns:
          *Tensor* or *tuple[Tensor, ...]* of **attributions**
          or 2-element tuple of **attributions**, **delta**:
  
            - **attributions** (*Tensor* or *tuple[Tensor, ...]*):
                      The propagated relevance values with respect to each
                      input feature. The values are normalized by the output score
                      value (sum(relevance)=1). To obtain values comparable to other
                      methods or implementations these values need to be multiplied
                      by the output score. 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. The sum of attributions
                      is one and not corresponding to the prediction score as in other
                      implementations.
  
            - **delta** (*Tensor*, returned if return_convergence_delta=True):
                      Delta is calculated per example, meaning that the number of
                      elements in returned delta tensor is equal to the number of
                      of examples in the inputs.
  
      Examples::
  
              >>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
              >>> # and returns an Nx10 tensor of class probabilities. It has one
              >>> # Conv2D and a ReLU layer.
              >>> net = ImageClassifier()
              >>> lrp = LRP(net)
              >>> input = torch.randn(3, 3, 32, 32)
              >>> # Attribution size matches input size: 3x3x32x32
              >>> attribution = lrp.attribute(input, target=5)
  
      """
      self.verbose = verbose
      self._original_state_dict = self.model.state_dict()
      self.layers: List[Module] = []
      self._get_layers(self.model)
      self._check_and_attach_rules()
      self.backward_handles: List[RemovableHandle] = []
      self.forward_handles: List[RemovableHandle] = []
  
      is_inputs_tuple = _is_tuple(inputs)
      inputs = _format_tensor_into_tuples(inputs)
      gradient_mask = apply_gradient_requirements(inputs)
  
      try:
          # 1. Forward pass: Change weights of layers according to selected rules.
          output = self._compute_output_and_change_weights(
              inputs, target, additional_forward_args
          )
          # 2. Forward pass + backward pass: Register hooks to configure relevance
          # propagation and execute back-propagation.
          self._register_forward_hooks()
          normalized_relevances = self.gradient_func(
              self._forward_fn_wrapper,
              inputs,
              target,
              additional_forward_args,
          )
          relevances = tuple(
              normalized_relevance
              * output.reshape(
                  (-1,) + (1,) * (normalized_relevance.dim() - 1)
              )
              for normalized_relevance in normalized_relevances
          )
      finally:
          self._restore_model()
  
      undo_gradient_requirements(inputs, gradient_mask)
  
      if return_convergence_delta:
          return (
              _format_output(is_inputs_tuple, relevances),
              self.compute_convergence_delta(relevances, output),
          )
      else:
          return _format_output(is_inputs_tuple, relevances)
  
imports: import typing
  from collections import defaultdict
  from typing import Any, cast, List, Tuple, Union
  import torch.nn
  from captum._utils.common import _format_output, _format_tensor_into_tuples, _is_tuple, _register_backward_hook, _run_forward
  from captum._utils.gradient import apply_gradient_requirements, undo_gradient_requirements
  from captum._utils.typing import Literal, TargetType, TensorOrTupleOfTensorsGeneric
  from captum.attr._utils.attribution import GradientAttribution
  from captum.attr._utils.common import _sum_rows
  from captum.attr._utils.custom_modules import Addition_Module
  from captum.attr._utils.lrp_rules import EpsilonRule, PropagationRule
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.hooks import RemovableHandle
label_desc: Use image node_id 5 for calling the LRP obj's underlying member method code with example usage: obj.attribute(inputs, target, additional_forward_args, return_convergence_delta, verbose) and returns: _format_output,  self, _format_output