Clips respective gradients of a given tensor.

  Acts as identity for the forward pass, but clips gradient tensor element-wise
  by value during the backward pass. Any gradient values less than
  `clip_value_min` or greater than `clip_values_max` are set to the respective
  limit values.

  Args:
    net: A `tf.Tensor`.
    clip_value_min: A 0-D Tensor or scalar. The minimum value to clip by.
    clip_value_max: A 0-D Tensor or scalar. The maximum value to clip by.
    name: A name for the operation (optional, default 'clip_gradient').

  Returns:
    A `tf.Tensor` with the same type as the input tensor.

  Raises:
    ValueError: If `net` dtype is non-float.

def clip_gradient(net, clip_value_min, clip_value_max, name=None):
  """Clips respective gradients of a given tensor.

  Acts as identity for the forward pass, but clips gradient tensor element-wise
  by value during the backward pass. Any gradient values less than
  `clip_value_min` or greater than `clip_values_max` are set to the respective
  limit values.

  Args:
    net: A `tf.Tensor`.
    clip_value_min: A 0-D Tensor or scalar. The minimum value to clip by.
    clip_value_max: A 0-D Tensor or scalar. The maximum value to clip by.
    name: A name for the operation (optional, default 'clip_gradient').

  Returns:
    A `tf.Tensor` with the same type as the input tensor.

  Raises:
    ValueError: If `net` dtype is non-float.
  """
  if not net.dtype.is_floating:
    raise ValueError("clip_gradient does not support non-float `net` inputs.")

  with tf.name_scope(name, "clip_gradient", values=[net]):
    dtype = net.dtype.base_dtype  # Convert ref dtypes to regular dtypes.
    min_tensor = tf.convert_to_tensor(clip_value_min, dtype=dtype)
    max_tensor = tf.convert_to_tensor(clip_value_max, dtype=dtype)

    clip_gradient_op = _clip_gradient_op(dtype)
    output = clip_gradient_op(net, min_tensor, max_tensor)
    output.set_shape(net.get_shape())

  return output