code: def gradcam_backend(
      model,  # type: Model
      doc,  # type: np.ndarray
      targets,  # type: Optional[List[int]]
      activation_layer,  # type: Layer
  ):
      # type: (...) -> Tuple[np.ndarray, np.ndarray, np.ndarray, int, float]
      """
      Compute the terms and by-products required by the Grad-CAM formula.
  
      Parameters
      ----------
      model : keras.models.Model
          Differentiable network.
  
      doc : numpy.ndarray
          Input to the network.
  
      targets : list, optional
          Index into the network's output,
          indicating the output node that will be
          used as the "loss" during differentiation.
  
      activation_layer : keras.layers.Layer
          Keras layer instance to differentiate with respect to.
  
  
      See :func:`eli5.keras.explain_prediction` for description of the
      ``model``, ``doc``, ``targets`` parameters.
  
      Returns
      -------
      (weights, activations, gradients, predicted_idx, predicted_val) : (numpy.ndarray, ..., int, float)
          Values of variables.
      """
      # score for class in targets
      predicted_idx = _get_target_prediction(targets, model)
      predicted_val = K.gather(
          model.output[0, :], predicted_idx
      )  # access value by index
  
      # output of target activation layer, i.e. activation maps of a convolutional layer
      activation_output = activation_layer.output
  
      # score for class w.r.p.t. activation layer
      grads = _calc_gradient(predicted_val, [activation_output])
  
      # Global Average Pooling of gradients to get the weights
      # note that axes are in range [-rank(x), rank(x)) (we start from 1, not 0)
      # TODO: decide whether this should go in gradcam_backend() or gradcam()
      weights = K.mean(grads, axis=(1, 2))
  
      evaluate = K.function(
          [model.input],
          [
              weights,
              activation_output,
              grads,
              predicted_val,
              predicted_idx,
          ],
      )
      # evaluate the graph / do actual computations
      (
          weights,
          activations,
          grads,
          predicted_val,
          predicted_idx,
      ) = evaluate([doc])
  
      # put into suitable form
      weights = weights[0]
      predicted_val = predicted_val[0]
      predicted_idx = predicted_idx[0]
      activations = activations[0, ...]
      grads = grads[0, ...]
      return weights, activations, grads, predicted_idx, predicted_val
  
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 2 for calling a global function with example usage: gradcam_backend(model, doc, targets, activation_layer) and returns: weights,  activations,  grads,  predicted_idx,  predicted_val