code: def get_gradients_for_keras(optimizer, loss, params):
      from tensorflow.python.util import nest
      from tensorflow.python.keras import backend
      from tensorflow.python.ops import gradients
      from tensorflow.python.ops import clip_ops
      from tensorflow.python.keras.optimizers import TFOptimizer
  
      params = nest.flatten(params)
      if isinstance(optimizer, TFOptimizer):
          scope_name = optimizer.optimizer._name
      else:
          scope_name = optimizer._name
  
      with backend.get_graph().as_default(), backend.name_scope(
          scope_name + "/gradients"
      ):
          grads = gradients.gradients(loss, params)
  
          all_reduced_grads = []
          for grad, param in zip(grads, params):
              if grad is None:
                  invalidInputError(
                      False,
                      "Variable {} has `None` for gradient. "
                      "Please make sure that all of your ops have a "
                      "gradient defined (i.e. are differentiable). "
                      "Common ops without gradient: "
                      "K.argmax, K.round, K.eval.".format(param),
                  )
              grad = process_grad(grad)
  
              with tf.control_dependencies([param]):
                  grad_i = tf.identity(
                      grad, name="zoo_identity_op_for_grad"
                  )
  
              all_reduced_grads.append(grad_i)
  
          grads = all_reduced_grads
  
          if hasattr(optimizer, "clipnorm"):
              grads = [
                  clip_ops.clip_by_norm(g, optimizer.clipnorm)
                  for g in grads
              ]
          if hasattr(optimizer, "clipvalue"):
              grads = [
                  clip_ops.clip_by_value(
                      g, -optimizer.clipvalue, optimizer.clipvalue
                  )
                  for g in grads
              ]
      return grads
  
file_path: BigDL/python/orca/src/bigdl/orca/tfpark/zoo_optimizer.py
imports: from bigdl.dllib.optim.optimizer import OptimMethod
  from bigdl.dllib.utils.tf import process_grad
  from bigdl.dllib.utils.log4Error import invalidInputError
  import tensorflow
label_desc: Use image node_id 1 for calling a global function with example usage: get_gradients_for_keras(optimizer, loss, params) and returns: grads