Example #1
0
  def testCosDistance(self):
    n1 = numpy.array([[1., 2., 3., 4.], [1., 1., 1., 1.]], dtype=numpy.float32)
    n2 = numpy.array([[5., 6., 7., -8.], [1., 1., 1., 1.]], dtype=numpy.float32)
    out = self.Run(functions.cos_distance(n1, n2))

    testing.assert_allclose(
        out[0],
        numpy.array([cosine(n1[0], n2[0]), cosine(n1[1], n2[1])]),
        rtol=TOLERANCE)
Example #2
0
  def test_cos_distance(self):
    n1 = numpy.array([[1., 2., 3., 4.], [1., 1., 1., 1.]], dtype=numpy.float32)
    n2 = numpy.array([[5., 6., 7., -8.], [1., 1., 1., 1.]], dtype=numpy.float32)
    out = self.eval_tensor(functions.cos_distance(n1, n2))

    testing.assert_allclose(
        out[0],
        numpy.array([cosine(n1[0], n2[0]), cosine(n1[1], n2[1])]),
        rtol=TOLERANCE, atol=TOLERANCE)
Example #3
0
 def testCosDistanceWithBroadcast(self):
   n1 = numpy.array([[[1., 2., 3., 4.], [1., 1., 1., 1.]], [[5., 6., 7., 8.],
                                                            [1., 1., 1., 2.]]],
                    dtype=numpy.float32)
   n2 = numpy.array([[5., 6., 7., -8.], [1., 1., 1., 1.]], dtype=numpy.float32)
   out = self.Run(functions.cos_distance(n1, n2))
   expected = numpy.array(
       [[cosine(n1[0, 0], n2[0]), cosine(n1[0, 1], n2[1])],
        [cosine(n1[1, 0], n2[0]), cosine(n1[1, 1], n2[1])]])
   testing.assert_allclose(expected, out[0], atol=TOLERANCE)
Example #4
0
 def test_cos_distance_with_broadcast(self):
   n1 = numpy.array([[[1., 2., 3., 4.], [1., 1., 1., 1.]], [[5., 6., 7., 8.],
                                                            [1., 1., 1., 2.]]],
                    dtype=numpy.float32)
   n2 = numpy.array([[5., 6., 7., -8.], [1., 1., 1., 1.]], dtype=numpy.float32)
   out = self.eval_tensor(functions.cos_distance(n1, n2))
   expected = numpy.array(
       [[cosine(n1[0, 0], n2[0]), cosine(n1[0, 1], n2[1])],
        [cosine(n1[1, 0], n2[0]), cosine(n1[1, 1], n2[1])]])
   testing.assert_allclose(expected, out[0], atol=TOLERANCE)