(self, grads_and_vars, clip_ratio)
return grad_and_var_clipped
Clip the gradients.
Clip the gradients.
Clip the gradients .
def clip_gradients(self, grads_and_vars, clip_ratio):
    """Clip the gradients."""
    is_zip_obj = False
    if isinstance(grads_and_vars, zip):
      grads_and_vars = list(grads_and_vars)
      is_zip_obj = True

    with tf.variable_scope('grad'):
      for grad, var in grads_and_vars:
        if grad is not None:
          tf.summary.histogram(var.name[:-2], grad)
        else:
          logging.debug('%s gradient is None' % (var.name))

    # not clip
    if not clip_ratio:
      if is_zip_obj:
        grads, variables = zip(*grads_and_vars)
        grads_and_vars = zip(grads, variables)
      return grads_and_vars

    gradients, variables = zip(*grads_and_vars)
    clipped, global_norm = tf.clip_by_global_norm(gradients, clip_ratio)
    grad_and_var_clipped = zip(clipped, variables)

    tf.summary.scalar('gradient/global_norm', global_norm)
    return grad_and_var_clipped
def clip_gradients ( self , grads_and_vars , clip_ratio ) : is_zip_obj = False if isinstance ( grads_and_vars , zip ) : grads_and_vars = list ( grads_and_vars ) is_zip_obj = True with tf . variable_scope ( 'grad' ) : for grad , var in grads_and_vars : if grad is not None : tf . summary . histogram ( var . name [ : - 2 ] , grad ) else : logging . debug ( '%s gradient is None' % ( var . name ) ) # not clip if not clip_ratio : if is_zip_obj : grads , variables = zip ( * grads_and_vars ) grads_and_vars = zip ( grads , variables ) return grads_and_vars gradients , variables = zip ( * grads_and_vars ) clipped , global_norm = tf . clip_by_global_norm ( gradients , clip_ratio ) grad_and_var_clipped = zip ( clipped , variables ) tf . summary . scalar ( 'gradient/global_norm' , global_norm ) return grad_and_var_clipped