code: def _get_gradients(model, x, y, nodes):
      if model.optimizer is None:
          raise Exception(
              "Please compile the model first. The loss function is required to compute the gradients."
          )
  
      nodes_names = nodes.keys()
      nodes_values = nodes.values()
  
      try:
          if not hasattr(model, "total_loss"):
              raise Exception(
                  "Disable TF eager mode to use get_gradients.\n"
                  "Add this command at the beginning of your script:\n"
                  "tf.compat.v1.disable_eager_execution()"
              )
          grads = model.optimizer.get_gradients(
              model.total_loss, nodes_values
          )
      except ValueError as e:
          if "differentiable" in str(e):
              # Probably one of the gradients operations is not differentiable...
              grads = []
              differentiable_nodes = []
              for n in nodes_values:
                  try:
                      grads.extend(
                          model.optimizer.get_gradients(
                              model.total_loss, n
                          )
                      )
                      differentiable_nodes.append(n)
                  except ValueError:
                      pass
              # nodes_values = differentiable_nodes
          else:
              raise e
  
      gradients_values = _evaluate(model, grads, x, y)
  
      return OrderedDict(zip(nodes_names, gradients_values))
  
imports: import json
  import os
  from collections import OrderedDict
  import numpy
  import tensorflow
  import tensorflow.keras.backend
  from tensorflow.keras import Sequential
  from tensorflow.keras.models import Model
label_desc: Use image node_id 7 for calling a global function with example usage: _get_gradients(model, x, y, nodes) and returns: OrderedDict