class_docstr: Registered as a `SaliencyInterpreter` with name "simple-gradient".
docstr: Interprets the model's prediction for inputs.  Gets the gradients of the loss with respect
  to the input and returns those gradients normalized and sanitized.
docstr_tok: ["Interprets","the","model","'s","prediction","for","inputs",".","Gets","the","gradients","of","the","loss","with","respect","to","the","input","and","returns","those","gradients","normalized","and","sanitized","."]
code: def saliency_interpret_from_json(self, inputs: JsonDict) -> JsonDict:
      """
      Interprets the model's prediction for inputs.  Gets the gradients of the loss with respect
      to the input and returns those gradients normalized and sanitized.
      """
      labeled_instances = self.predictor.json_to_labeled_instances(
          inputs
      )
  
      instances_with_grads = dict()
      for idx, instance in enumerate(labeled_instances):
          # List of embedding inputs, used for multiplying gradient by the input for normalization
          embeddings_list: List[torch.Tensor] = []
          token_offsets: List[torch.Tensor] = []
  
          # Hook used for saving embeddings
          handles = self._register_hooks(embeddings_list, token_offsets)
          try:
              grads = self.predictor.get_gradients([instance])[0]
          finally:
              for handle in handles:
                  handle.remove()
  
          # 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
          )
  
          for key, grad in grads.items():
              # Get number at the end of every gradient key (they look like grad_input_[int],
              # we're getting this [int] part and subtracting 1 for zero-based indexing).
              # This is then used as an index into the reversed input array to match up the
              # gradient and its respective embedding.
              input_idx = int(key[-1]) - 1
              # The [0] here is undo-ing the batching that happens in get_gradients.
              emb_grad = numpy.sum(
                  grad[0] * embeddings_list[input_idx][0], axis=1
              )
              norm = numpy.linalg.norm(emb_grad, ord=1)
              normalized_grad = [math.fabs(e) / norm for e in emb_grad]
              grads[key] = normalized_grad
  
          instances_with_grads["instance_" + str(idx + 1)] = grads
      return sanitize(instances_with_grads)
  
file_path: allennlp/allennlp/interpret/saliency_interpreters/simple_gradient.py
imports: import math
  from typing import List
  import numpy
  import torch
  from allennlp.common.util import JsonDict, sanitize
  from allennlp.interpret.saliency_interpreters.saliency_interpreter import SaliencyInterpreter
  from allennlp.nn import util
label_desc: Use image node_id 1 for calling the SimpleGradient obj's underlying member method code with example usage: obj.saliency_interpret_from_json(inputs) and returns: sanitize