class_docstr: Computes GradCAM attribution for chosen layer. GradCAM is designed for
  convolutional neural networks, and is usually applied to the last
  convolutional layer.
  
  GradCAM computes the gradients of the target output with respect to
  the given layer, averages for each output channel (dimension 2 of
  output), and multiplies the average gradient for each channel by the
  layer activations. The results are summed over all channels.
  
  Note that in the original GradCAM algorithm described in the paper,
  ReLU is applied to the output, returning only non-negative attributions.
  For providing more flexibility to the user, we choose to not perform the
  ReLU internally by default and return the sign information. To match the
  original GradCAM algorithm, it is necessary to pass the parameter
  relu_attributions=True to apply ReLU on the final
  attributions or alternatively only visualize the positive attributions.
  
  Note: this procedure sums over the second dimension (# of channels),
  so the output of GradCAM attributions will have a second
  dimension of 1, but all other dimensions will match that of the layer
  output.
  
  GradCAM attributions are generally upsampled and can be viewed as a
  mask to the input, since a convolutional layer output generally
  matches the input image spatially. This upsampling can be performed
  using LayerAttribution.interpolate, as shown in the example below.
  
  More details regarding the GradCAM method can be found in the
  original paper here:
  https://arxiv.org/abs/1610.02391
class_docstr_tok: ["Computes","GradCAM","attribution","for","chosen","layer",".","GradCAM","is","designed","for","convolutional","neural","networks",",","and","is","usually","applied","to","the","last","convolutional","layer",".","GradCAM","computes","the","gradients","of","the","target","output","with","respect","to","the","given","layer",",","averages","for","each","output","channel","(","dimension","2","of","output",")",",","and","multiplies","the","average","gradient","for","each","channel","by","the","layer","activations",".","The","results","are","summed","over","all","channels",".","Note","that","in","the","original","GradCAM","algorithm","described","in","the","paper",",","ReLU","is","applied","to","the","output",",","returning","only","non-negative","attributions",".","For","providing","more","flexibility","to","the","user",",","we","choose","to","not","perform","the","ReLU","internally","by","default","and","return","the","sign","information",".","To","match","the","original","GradCAM","algorithm",",","it","is","necessary","to","pass","the","parameter","relu_attributions=True","to","apply","ReLU","on","the","final","attributions","or","alternatively","only","visualize","the","positive","attributions",".","Note",":","this","procedure","sums","over","the","second","dimension","(","#","of","channels",")",",","so","the","output","of","GradCAM","attributions","will","have","a","second","dimension","of","1",",","but","all","other","dimensions","will","match","that","of","the","layer","output",".","GradCAM","attributions","are","generally","upsampled","and","can","be","viewed","as","a","mask","to","the","input",",","since","a","convolutional","layer","output","generally","matches","the","input","image","spatially",".","This","upsampling","can","be","performed","using","LayerAttribution.interpolate",",","as","shown","in","the","example","below",".","More","details","regarding","the","GradCAM","method","can","be","found","in","the","original","paper","here",":","https",":","\/\/arxiv.org\/abs\/1610.02391"]
docstr: Args:
  
      forward_func (Callable): The forward function of the model or any
                    modification of it
      layer (torch.nn.Module): Layer for which attributions are computed.
                    Output size of attribute matches this layer's output
                    dimensions, except for dimension 2, which will be 1,
                    since GradCAM sums over channels.
      device_ids (list[int]): Device ID list, necessary only if forward_func
                    applies a DataParallel model. This allows reconstruction of
                    intermediate outputs from batched results across devices.
                    If forward_func is given as the DataParallel model itself,
                    then it is not necessary to provide this argument.
docstr_tok: ["Args",":","forward_func","(","Callable",")",":","The","forward","function","of","the","model","or","any","modification","of","it","layer","(","torch.nn.Module",")",":","Layer","for","which","attributions","are","computed",".","Output","size","of","attribute","matches","this","layer","'s","output","dimensions",",","except","for","dimension","2",",","which","will","be","1",",","since","GradCAM","sums","over","channels",".","device_ids","(","list","[","int","]",")",":","Device","ID","list",",","necessary","only","if","forward_func","applies","a","DataParallel","model",".","This","allows","reconstruction","of","intermediate","outputs","from","batched","results","across","devices",".","If","forward_func","is","given","as","the","DataParallel","model","itself",",","then","it","is","not","necessary","to","provide","this","argument","."]
code: def __init__(
      self,
      forward_func: Callable,
      layer: Module,
      device_ids: Union[None, List[int]] = None,
  ) -> None:
      r"""
      Args:
  
          forward_func (Callable): The forward function of the model or any
                        modification of it
          layer (torch.nn.Module): Layer for which attributions are computed.
                        Output size of attribute matches this layer's output
                        dimensions, except for dimension 2, which will be 1,
                        since GradCAM sums over channels.
          device_ids (list[int]): Device ID list, necessary only if forward_func
                        applies a DataParallel model. This allows reconstruction of
                        intermediate outputs from batched results across devices.
                        If forward_func is given as the DataParallel model itself,
                        then it is not necessary to provide this argument.
      """
      LayerAttribution.__init__(self, forward_func, layer, device_ids)
      GradientAttribution.__init__(self, forward_func)
  
file_path: captum/captum/attr/_core/layer/grad_cam.py
imports: from typing import Any, Callable, List, Tuple, Union
  import torch
  import torch.nn.functional
  from captum._utils.common import _format_additional_forward_args, _format_output, _format_tensor_into_tuples
  from captum._utils.gradient import compute_layer_gradients_and_eval
  from captum._utils.typing import TargetType
  from captum.attr._utils.attribution import GradientAttribution, LayerAttribution
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import Module
label_desc: Use image node_id 1 to create a new LayerGradCam object from inherited base classes: LayerAttribution, GradientAttribution with example: obj = LayerGradCam(forward_func, layer, device_ids)