class_docstr: Implements DeepLIFT algorithm based on the following paper:
  Learning Important Features Through Propagating Activation Differences,
  Avanti Shrikumar, et. al.
  https://arxiv.org/abs/1704.02685
  
  and the gradient formulation proposed in:
  Towards better understanding of gradient-based attribution methods for
  deep neural networks,  Marco Ancona, et.al.
  https://openreview.net/pdf?id=Sy21R9JAW
  
  This implementation supports only Rescale rule. RevealCancel rule will
  be supported in later releases.
  In addition to that, in order to keep the implementation cleaner, DeepLIFT
  for internal neurons and layers extends current implementation and is
  implemented separately in LayerDeepLift and NeuronDeepLift.
  Although DeepLIFT's(Rescale Rule) attribution quality is comparable with
  Integrated Gradients, it runs significantly faster than Integrated
  Gradients and is preferred for large datasets.
  
  Currently we only support a limited number of non-linear activations
  but the plan is to expand the list in the future.
  
  Note: As we know, currently we cannot access the building blocks,
  of PyTorch's built-in LSTM, RNNs and GRUs such as Tanh and Sigmoid.
  Nonetheless, it is possible to build custom LSTMs, RNNS and GRUs
  with performance similar to built-in ones using TorchScript.
  More details on how to build custom RNNs can be found here:
  https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/
class_docstr_tok: ["Implements","DeepLIFT","algorithm","based","on","the","following","paper",":","Learning","Important","Features","Through","Propagating","Activation","Differences",",","Avanti","Shrikumar",",","et",".","al",".","https",":","\/\/arxiv.org\/abs\/1704.02685","and","the","gradient","formulation","proposed","in",":","Towards","better","understanding","of","gradient-based","attribution","methods","for","deep","neural","networks",",","Marco","Ancona",",","et.al",".","https",":","\/\/openreview.net\/pdf","?","id=Sy21R9JAW","This","implementation","supports","only","Rescale","rule",".","RevealCancel","rule","will","be","supported","in","later","releases",".","In","addition","to","that",",","in","order","to","keep","the","implementation","cleaner",",","DeepLIFT","for","internal","neurons","and","layers","extends","current","implementation","and","is","implemented","separately","in","LayerDeepLift","and","NeuronDeepLift",".","Although","DeepLIFT","'s","(","Rescale","Rule",")","attribution","quality","is","comparable","with","Integrated","Gradients",",","it","runs","significantly","faster","than","Integrated","Gradients","and","is","preferred","for","large","datasets",".","Currently","we","only","support","a","limited","number","of","non-linear","activations","but","the","plan","is","to","expand","the","list","in","the","future",".","Note",":","As","we","know",",","currently","we","can","not","access","the","building","blocks",",","of","PyTorch","'s","built-in","LSTM",",","RNNs","and","GRUs","such","as","Tanh","and","Sigmoid",".","Nonetheless",",","it","is","possible","to","build","custom","LSTMs",",","RNNS","and","GRUs","with","performance","similar","to","built-in","ones","using","TorchScript",".","More","details","on","how","to","build","custom","RNNs","can","be","found","here",":","https",":","\/\/pytorch.org\/blog\/optimizing-cuda-rnn-with-torchscript\/"]
docstr: we need forward hook to access and detach the inputs and
  outputs of a neuron
docstr_tok: ["we","need","forward","hook","to","access","and","detach","the","inputs","and","outputs","of","a","neuron"]
code: def _forward_hook(
      self,
      module: Module,
      inputs: Union[Tensor, Tuple[Tensor, ...]],
      outputs: Union[Tensor, Tuple[Tensor, ...]],
  ) -> None:
      r"""
      we need forward hook to access and detach the inputs and
      outputs of a neuron
      """
      outputs = _format_tensor_into_tuples(outputs)
      module.output = outputs[0].clone().detach()
      if not _check_valid_module(module.input_grad_fns, outputs[0]):
          warnings.warn(
              """An invalid module {} is detected. Saved gradients will
                  be used as the gradients of the module's input tensor.
                  See MaxPool1d as an example.""".format(
                  module
              )
          )
          module.is_invalid = True  # type: ignore
          module.saved_grad = None  # type: ignore
          self.forward_handles.append(
              cast(RemovableHandle, module.input_hook)
          )
      else:
          module.is_invalid = False  # type: ignore
          # removing the hook if there is no failure case
          cast(RemovableHandle, module.input_hook).remove()
      del module.input_hook
      del module.input_grad_fns
  
imports: import typing
  import warnings
  from typing import Any, Callable, List, Tuple, Union, cast
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.hooks import RemovableHandle
  from captum.attr._utils.attribution import GradientAttribution
  from captum.attr._utils.common import ExpansionTypes, _call_custom_attribution_func, _compute_conv_delta_and_format_attrs, _expand_additional_forward_args, _expand_target, _format_additional_forward_args, _format_attributions, _format_baseline, _format_callable_baseline, _format_input, _format_tensor_into_tuples, _is_tuple, _run_forward, _tensorize_baseline, _validate_input
  from captum.attr._utils.gradient import apply_gradient_requirements, undo_gradient_requirements
  from captum.attr._utils.typing import BaselineType, Literal, TargetType, TensorOrTupleOfTensorsGeneric
label_desc: Use image node_id 9 for calling the DeepLift obj's underlying member method code with example usage: obj._forward_hook(module, inputs, outputs) without return types