class_docstr: Interprets the prediction using Integrated Gradients (https://arxiv.org/abs/1703.01365)
  
  Registered as a `SaliencyInterpreter` with name "integrated-gradient".
class_docstr_tok: ["Interprets","the","prediction","using","Integrated","Gradients","(","https",":","\/\/arxiv.org\/abs\/1703.01365",")","Registered","as","a","`","SaliencyInterpreter","`","with","name","``","integrated-gradient","''","."]
docstr: Returns integrated gradients for the given [`Instance`](../../data/instance.md)
docstr_tok: ["Returns","integrated","gradients","for","the","given","[","`","Instance","`","]","(","..","\/","..","\/data\/instance.md",")"]
code: def _integrate_gradients(
      self, instance: Instance
  ) -> Dict[str, numpy.ndarray]:
      """
      Returns integrated gradients for the given [`Instance`](../../data/instance.md)
      """
      ig_grads: Dict[str, Any] = {}
  
      # List of Embedding inputs
      embeddings_list: List[torch.Tensor] = []
      token_offsets: List[torch.Tensor] = []
  
      # Use 10 terms in the summation approximation of the integral in integrated grad
      steps = 10
  
      # Exclude the endpoint because we do a left point integral approximation
      for alpha in numpy.linspace(0, 1.0, num=steps, endpoint=False):
          handles = []
          # Hook for modifying embedding value
          handles = self._register_hooks(
              alpha, embeddings_list, token_offsets
          )
  
          try:
              grads = self.predictor.get_gradients([instance])[0]
          finally:
              for handle in handles:
                  handle.remove()
  
          # Running sum of gradients
          if ig_grads == {}:
              ig_grads = grads
          else:
              for key in grads.keys():
                  ig_grads[key] += grads[key]
  
      # Average of each gradient term
      for key in ig_grads.keys():
          ig_grads[key] /= steps
  
      # Gradients come back in the reverse order that they were sent into the network
      embeddings_list.reverse()
      token_offsets.reverse()
      embeddings_list = self._aggregate_token_embeddings(
          embeddings_list, token_offsets
      )
  
      # Element-wise multiply average gradient by the input
      for idx, input_embedding in enumerate(embeddings_list):
          key = "grad_input_" + str(idx + 1)
          ig_grads[key] *= input_embedding
  
      return ig_grads
  
file_path: allennlp/allennlp/interpret/saliency_interpreters/integrated_gradient.py
imports: import math
  from typing import List, Dict, Any
  import numpy
  import torch
  from allennlp.common.util import JsonDict, sanitize
  from allennlp.data import Instance
  from allennlp.interpret.saliency_interpreters.saliency_interpreter import SaliencyInterpreter
  from allennlp.nn import util
label_desc: Use image node_id 3 for calling the IntegratedGradient obj's underlying member method code with example usage: obj._integrate_gradients(instance) and returns: ig_grads