def test_distance_is_symmetric(self): x = self.get_point([[-0.62972, -0.28971]]) y = self.get_point([[0.37216, -0.38184]]) result_xy = hyperb.hyp_distance(x, y, self.c) result_yx = hyperb.hyp_distance(y, x, self.c) self.assertAllClose(result_xy, result_yx)
def test_distance_to_same_point_is_zero(self): a = self.get_point([[0.5, 0.25]]) result = hyperb.hyp_distance(a, a, self.c) expected = tf.convert_to_tensor([[0.]], dtype=tf.float64) self.assertAllClose(expected, result)
def test_distance_between_two_points(self): x = self.get_point([[-0.62972, -0.28971]]) y = self.get_point([[0.37216, -0.38184]]) result = hyperb.hyp_distance(x, y, self.c) expected = tf.convert_to_tensor([[2.55035745]], dtype=tf.float64) self.assertAllClose(expected, result)
def test_distance_is_non_negative(self): x = tf.clip_by_norm(tf.random.uniform((1000, 32), dtype=tf.float64), clip_norm=0.99999, axes=-1) y = tf.clip_by_norm(tf.random.uniform((1000, 32), dtype=tf.float64), clip_norm=0.99999, axes=-1) result = hyperb.hyp_distance(x, y, self.c) self.assertAllGreaterEqual(result, 0)
def similarity_score(self, lhs, rhs, all_items): """Score based on square hyperbolic distance""" if all_items: return -hmath.hyp_distance_batch_rhs(lhs, rhs, self.get_c()) ** 2 return -hmath.hyp_distance(lhs, rhs, self.get_c()) ** 2