def cos_distance(t1, t2, epsilon=1e-12, name=None):
  """Cos distance between t1 and t2 and caps the gradient of the Square Root.

  Args:
    t1: A tensor
    t2: A tensor that can be multiplied by t1.
    epsilon: A lower bound value for the distance. The square root is used as
      the normalizer.
    name: Optional name for this op.
  Returns:
    The cos distance between t1 and t2.
  """
  with tf.name_scope(name, 'cos_distance', [t1, t2]) as scope:
    t1 = tf.convert_to_tensor(t1, name='t1')
    t2 = tf.convert_to_tensor(t2, name='t2')
    x_inv_norm = tf.rsqrt(tf.maximum(length_squared(t1) * length_squared(t2),
                                     epsilon))
    return tf.subtract(1.0, dot_product(t1, t2) * x_inv_norm, name=scope)