code: def _calc_gradient(ys, xs):
      # (K.variable, list) -> K.variable
      """
      Return the gradient of scalar ``ys`` with respect to each of list ``xs``,
      (must be singleton)
      and apply grad normalization.
      """
      # differentiate ys (scalar) with respect to each variable in xs
      grads = K.gradients(ys, xs)
  
      # grads gives a python list with a tensor (containing the derivatives) for each xs
      # to use grads with other operations and with K.function
      # we need to work with the actual tensors and not the python list
      (
          grads,
      ) = grads  # grads should be a singleton list (because xs is a singleton)
  
      # validate that the gradients were calculated successfully (no None's)
      # https://github.com/jacobgil/keras-grad-cam/issues/17#issuecomment-423057265
      # https://github.com/tensorflow/tensorflow/issues/783#issuecomment-175824168
      if grads is None:
          raise ValueError(
              "Gradient calculation resulted in None values. "
              "Check that the model is differentiable and try again. "
              "ys: {}. xs: {}. grads: {}".format(ys, xs, grads)
          )
  
      # this seems to make the heatmap less noisy
      grads = K.l2_normalize(grads)
      return grads
  
file_path: eli5/eli5/keras/gradcam.py
imports: from __future__ import absolute_import
  from typing import Union, Optional, Tuple, List
  import numpy
  import keras
  import keras.backend
  from keras.models import Model
  from keras.layers import Layer
label_desc: Use image node_id 3 for calling a global function with example usage: _calc_gradient(ys, xs) and returns: grads