code: def gradient(self, idx, inputs):
      self.model.zero_grad()
      X = [x.requires_grad_() for x in inputs]
      outputs = self.model(*X)
      selected = [val for val in outputs[:, idx]]
      if self.input_handle is not None:
          interim_inputs = self.layer.target_input
          grads = [
              torch.autograd.grad(
                  selected,
                  input,
                  retain_graph=True
                  if idx + 1 < len(interim_inputs)
                  else None,
              )[0]
              .cpu()
              .numpy()
              for idx, input in enumerate(interim_inputs)
          ]
          del self.layer.target_input
      else:
          grads = [
              torch.autograd.grad(
                  selected,
                  x,
                  retain_graph=True if idx + 1 < len(X) else None,
              )[0]
              .cpu()
              .numpy()
              for idx, x in enumerate(X)
          ]
      return grads
  
imports: import warnings
  import numpy
  import pandas
  from packaging import version
  from .._explanation import Explanation
  from ..explainers._explainer import Explainer
  from ..explainers.tf_utils import _get_graph, _get_model_inputs, _get_model_output, _get_session
label_desc: Use image node_id 2 for calling the _PyTorchGradient obj's underlying member method code with example usage: obj.gradient(idx, inputs) and returns: grads