docstr: Computes conductance using gradients along the path, applying
  riemann's method or gauss-legendre.
  The details of the approach can be found here:
  https://arxiv.org/abs/1805.12233
  
  Args
  
      inputs:     A single high dimensional input tensor, in which
                  dimension 0 corresponds to number of examples.
      baselines:   A single high dimensional baseline tensor,
                  which has the same shape as the input
      target:     Predicted class index. This is necessary only for
                  classification use cases
      n_steps:    The number of steps used by the approximation method
      method:     Method for integral approximation, one of `riemann_right`,
                  `riemann_middle`, `riemann_trapezoid` or `gausslegendre`
  
  Return
  
      attributions: Total conductance with respect to each neuron in
                    output of given layer
docstr_tok: ["Computes","conductance","using","gradients","along","the","path",",","applying","riemann","'s","method","or","gauss-legendre",".","The","details","of","the","approach","can","be","found","here",":","https",":","\/\/arxiv.org\/abs\/1805.12233","Args","inputs",":","A","single","high","dimensional","input","tensor",",","in","which","dimension","0","corresponds","to","number","of","examples",".","baselines",":","A","single","high","dimensional","baseline","tensor",",","which","has","the","same","shape","as","the","input","target",":","Predicted","class","index",".","This","is","necessary","only","for","classification","use","cases","n_steps",":","The","number","of","steps","used","by","the","approximation","method","method",":","Method","for","integral","approximation",",","one","of","`","riemann_right","`",",","`","riemann_middle","`",",","`","riemann_trapezoid","`","or","`","gausslegendre","`","Return","attributions",":","Total","conductance","with","respect","to","each","neuron","in","output","of","given","layer"]
code: def attribute(
      self,
      inputs,
      baselines=None,
      target=None,
      n_steps=500,
      method="riemann_trapezoid",
  ):
      r"""
      Computes conductance using gradients along the path, applying
      riemann's method or gauss-legendre.
      The details of the approach can be found here:
      https://arxiv.org/abs/1805.12233
  
      Args
  
          inputs:     A single high dimensional input tensor, in which
                      dimension 0 corresponds to number of examples.
          baselines:   A single high dimensional baseline tensor,
                      which has the same shape as the input
          target:     Predicted class index. This is necessary only for
                      classification use cases
          n_steps:    The number of steps used by the approximation method
          method:     Method for integral approximation, one of `riemann_right`,
                      `riemann_middle`, `riemann_trapezoid` or `gausslegendre`
  
      Return
  
          attributions: Total conductance with respect to each neuron in
                        output of given layer
      """
      if baselines is None:
          baselines = 0
      gradient_mask = apply_gradient_requirements((inputs,))
      # retrieve step size and scaling factor for specified approximation method
      step_sizes_func, alphas_func = approximation_parameters(method)
      step_sizes, alphas = step_sizes_func(n_steps), alphas_func(
          n_steps
      )
  
      # compute scaled inputs from baseline to final input.
      scaled_features = torch.cat(
          [
              baselines + alpha * (inputs - baselines)
              for alpha in alphas
          ],
          dim=0,
      )
  
      # Conductance Gradients - Returns gradient of output with respect to
      # hidden layer, gradient of hidden layer with respect to input,
      # and number of hidden units.
      (
          input_gradients,
          mid_layer_gradients,
          hidden_units,
      ) = self._conductance_grads(
          self.forward_func, scaled_features, target
      )
      # Multiply gradient of hidden layer with respect to input by input - baseline
      scaled_input_gradients = torch.repeat_interleave(
          inputs - baselines, hidden_units, dim=0
      )
      scaled_input_gradients = (
          input_gradients
          * scaled_input_gradients.repeat(
              *(
                  [len(alphas)]
                  + [1] * (len(scaled_input_gradients.shape) - 1)
              )
          )
      )
  
      # Sum gradients for each input neuron in order to have total
      # for each hidden unit and reshape to match hidden layer shape
      summed_input_grads = torch.sum(
          scaled_input_gradients,
          tuple(range(1, len(scaled_input_gradients.shape))),
      ).view_as(mid_layer_gradients)
  
      # Rescale gradients of hidden layer by by step size.
      scaled_grads = mid_layer_gradients.contiguous().view(
          n_steps, -1
      ) * torch.tensor(step_sizes).view(n_steps, 1).to(
          mid_layer_gradients.device
      )
  
      undo_gradient_requirements((inputs,), gradient_mask)
  
      # Element-wise mutliply gradient of output with respect to hidden layer
      # and summed gradients with respect to input (chain rule) and sum across
      # stepped inputs.
      return _reshape_and_sum(
          scaled_grads.view(mid_layer_gradients.shape)
          * summed_input_grads,
          n_steps,
          inputs.shape[0],
          mid_layer_gradients.shape[1:],
      )
  
file_path: captum/tests/attr/helpers/conductance_reference.py
imports: import numpy
  import torch
  from captum._utils.gradient import apply_gradient_requirements, undo_gradient_requirements
  from captum.attr._utils.approximation_methods import approximation_parameters
  from captum.attr._utils.attribution import LayerAttribution
  from captum.attr._utils.common import _reshape_and_sum
label_desc: Use image node_id 3 for calling the ConductanceReference obj's underlying member method code with example usage: obj.attribute(inputs, baselines, target, n_steps, method) and returns: _reshape_and_sum