Example #1
0
 def test_dot(self):
     # Four users, two topics, and slate size is two.
     similarity_model = affinities.TargetPointSimilarity((4, ), 2, 'dot')
     user_interests = ed.Deterministic(loc=np.array(
         [[-4, 3], [-4, -3], [4, 3], [4, -3]], dtype=np.float32))
     slate_docs = ed.Deterministic(
         loc=np.array([[[-8, 6], [0, 0]], [[-8, -6], [0, 0]],
                       [[8, 6], [0, 0]], [[8, -6], [0, 0]]],
                      dtype=np.float32))
     expected = ed.Deterministic(loc=np.array(
         [[50, 0.], [50, 0.], [50, 0.], [50, 0.]], dtype=np.float32))
     actual = similarity_model.affinities(user_interests, slate_docs)
     self.assertAllClose(self.evaluate(expected),
                         self.evaluate(actual.get('affinities')))
Example #2
0
 def test_negative_cosine(self):
     # Four users, two topics, and five documents.
     similarity_model = affinities.TargetPointSimilarity((4, ), 5,
                                                         'negative_cosine')
     user_interests = ed.Deterministic(loc=np.array(
         [[-4, 3], [-4, -3], [4, 3], [4, -3]], dtype=np.float32))
     documents = ed.Deterministic(loc=np.array(
         [[-8, 6], [-8, -6], [8, 6], [8, -6], [0, 0]], dtype=np.float32))
     expected = ed.Deterministic(loc=np.array(
         [[1., 0.28, -0.28, -1., 0.], [0.28, 1., -1., -0.28, 0.],
          [-0.28, -1., 1., 0.28, 0.], [-1., -0.28, 0.28, 1., 0.]],
         dtype=np.float32))
     actual = similarity_model.affinities(user_interests, documents)
     self.assertAllClose(self.evaluate(expected),
                         self.evaluate(actual.get('affinities')))
Example #3
0
 def test_single_peaked(self):
     similarity_model = affinities.TargetPointSimilarity((4, ), 2,
                                                         'single_peaked')
     user_interests = ed.Deterministic(loc=np.array(
         [[-4, 3], [-4, -3], [4, 3], [4, -3]], dtype=np.float32))
     slate_docs = ed.Deterministic(
         loc=np.array([[[-8, 6], [0, 0]], [[-8, -6], [0, 0]],
                       [[8, 6], [0, 0]], [[8, -6], [0, 0]]],
                      dtype=np.float32))
     expected = ed.Deterministic(loc=np.array(
         [[50, 0.], [14, 0.], [-14, 0.], [-50, 0.]], dtype=np.float32))
     actual = similarity_model.affinities(user_interests,
                                          slate_docs,
                                          affinity_peaks=tf.constant(
                                              [[32., 32.], [32., 0.],
                                               [0., 32.], [0., 0.]]))
     self.assertAllClose(self.evaluate(expected),
                         self.evaluate(actual.get('affinities')))
Example #4
0
 def test_inverse_euclidean(self):
     # Four users, two topics, and five documents.
     similarity_model = affinities.TargetPointSimilarity(
         (4, ), 5, 'inverse_euclidean')
     user_interests = ed.Deterministic(loc=np.array(
         [[-4, 3], [-4, -3], [4, 3], [4, -3]], dtype=np.float32))
     documents = ed.Deterministic(loc=np.array(
         [[-8, 6], [-8, -6], [8, 6], [8, -6], [0, 0]], dtype=np.float32))
     expected = ed.Deterministic(loc=np.array(
         [[.2, np.sqrt(1. / 97),
           np.sqrt(1. / 153), 1. /
           15, .2], [np.sqrt(1. / 97), .2, 1. / 15,
                     np.sqrt(1. / 153), .2],
          [np.sqrt(1. / 153), 1. /
           15, .2, np.sqrt(1. / 97), .2],
          [1. / 15, np.sqrt(1. / 153),
           np.sqrt(1. / 97), .2, .2]],
         dtype=np.float32))
     actual = similarity_model.affinities(user_interests, documents)
     self.assertAllClose(self.evaluate(expected),
                         self.evaluate(actual.get('affinities')))
Example #5
0
 def test_specs(self):
     similarity_model = affinities.TargetPointSimilarity((4, ), 2, 'dot')
     specs = similarity_model.specs()
     self.assertAllEqual(specs.get('affinities').space.shape, (4, 2))