code: def get_gradient_clip_fn(hparams=None):
      """Creates a gradient clipping function based on the hyperparameters.
  
      See the :attr:`gradient_clip` field in
      :meth:`~texar.tf.core.default_optimization_hparams` for all
      hyperparameters and default values.
  
      The gradient clipping function takes a list of `(gradients, variables)`
      tuples and returns a list of `(clipped_gradients, variables)` tuples.
      Typical examples include
      :tf_main:`tf.clip_by_global_norm <clip_by_global_norm>`,
      :tf_main:`tf.clip_by_value <clip_by_value>`,
      :tf_main:`tf.clip_by_norm <clip_by_norm>`,
      :tf_main:`tf.clip_by_average_norm <clip_by_average_norm>`, etc.
  
      Args:
          hparams (dict or HParams, optional): hyperparameters. Missing
              hyperparameters are set to default values automatically.
  
      Returns:
          function or `None`: If hparams["type"] is specified, returns
          the respective function. If hparams["type"] is empty,
          returns `None`.
      """
      if hparams is None or isinstance(hparams, dict):
          hparams = HParams(
              hparams, default_optimization_hparams()["gradient_clip"]
          )
      fn_type = hparams["type"]
      if fn_type is None or fn_type == "":
          return None
  
      fn_modules = ["tensorflow", "texar.tf.custom"]
      clip_fn = utils.get_function(fn_type, fn_modules)
      clip_fn_args = utils.get_args(clip_fn)
      fn_kwargs = hparams["kwargs"]
      if isinstance(fn_kwargs, HParams):
          fn_kwargs = fn_kwargs.todict()
  
      def grad_clip_fn(grads_and_vars):
          """Gradient clipping function.
  
          Args:
              grads_and_vars (list): A list of `(gradients, variables)` tuples.
  
          Returns:
              list: A list of `(clipped_gradients, variables)` tuples.
          """
          grads, vars_ = zip(*grads_and_vars)
          if clip_fn == tf.clip_by_global_norm:
              clipped_grads, _ = clip_fn(t_list=grads, **fn_kwargs)
          elif "t_list" in clip_fn_args:
              clipped_grads = clip_fn(t_list=grads, **fn_kwargs)
          elif "t" in clip_fn_args:  # e.g., tf.clip_by_value
              clipped_grads = [
                  clip_fn(t=grad, **fn_kwargs) for grad in grads
              ]
  
          return list(zip(clipped_grads, vars_))
  
      return grad_clip_fn
  
file_path: texar/texar/tf/core/optimization.py
imports: import re
  import tensorflow
  from texar.tf.hyperparams import HParams
  from texar.tf.utils import utils
label_desc: Use image node_id 4 for calling a global function with example usage: get_gradient_clip_fn(hparams) and returns: grad_clip_fn, None, list