code: def clip_gradients_by_norm(grads_and_vars, add_to_summary=False):
      if add_to_summary:
          for grad, var in grads_and_vars:
              if grad is not None:
                  variable_summaries(
                      grad, "grad/{}".format(var.name[:-2]), "full"
                  )
                  variable_summaries(
                      tf.abs(grad),
                      "grad/abs/{}".format(var.name[:-2]),
                      "full",
                  )
  
      # Clip by norm. Grad can be null when not training some modules.
      with tf.name_scope("clip_gradients_by_norm"):
          grads_and_vars = [
              (
                  tf.check_numerics(
                      tf.clip_by_norm(gv[0], 10.0), "Invalid gradient"
                  ),
                  gv[1],
              )
              if gv[0] is not None
              else gv
              for gv in grads_and_vars
          ]
  
      if add_to_summary:
          for grad, var in grads_and_vars:
              if grad is not None:
                  variable_summaries(
                      grad,
                      "clipped_grad/{}".format(var.name[:-2]),
                      "full",
                  )
                  variable_summaries(
                      tf.abs(grad),
                      "clipped_grad/{}".format(var.name[:-2]),
                      "full",
                  )
  
      return grads_and_vars
  
imports: import tensorflow
  from luminoth.utils.vars import variable_summaries
label_desc: Use image node_id 3 for calling a global function with example usage: clip_gradients_by_norm(grads_and_vars, add_to_summary) and returns: grads_and_vars