Exemplo n.º 1
0
 def test_axis(self):
     self.setup(axis=1)
     cosine_obj = metrics.CosineProximity(axis=1)
     self.evaluate(variables.variables_initializer(cosine_obj.variables))
     loss = cosine_obj(self.y_true, self.y_pred)
     expected_loss = np.mean(self.expected_loss)
     self.assertAlmostEqual(self.evaluate(loss), expected_loss, 3)
Exemplo n.º 2
0
 def test_weighted(self):
   cosine_obj = metrics.CosineProximity()
   self.evaluate(variables.variables_initializer(cosine_obj.variables))
   y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),
                                  (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))
   y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),
                                  (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))
   sample_weight = constant_op.constant((1., 1.5, 2., 2.5))
   result = cosine_obj(y_true, y_pred, sample_weight=sample_weight)
   self.assertAllClose(-0.59916, self.evaluate(result), atol=1e-5)
Exemplo n.º 3
0
  def test_config(self):
    cosine_obj = metrics.CosineProximity(
        axis=2, name='my_cos', dtype=dtypes.int32)
    self.assertEqual(cosine_obj.name, 'my_cos')
    self.assertEqual(cosine_obj._dtype, dtypes.int32)

    # Check save and restore config
    cosine_obj2 = metrics.CosineProximity.from_config(cosine_obj.get_config())
    self.assertEqual(cosine_obj2.name, 'my_cos')
    self.assertEqual(cosine_obj2._dtype, dtypes.int32)
Exemplo n.º 4
0
 def test_weighted(self):
     self.setup()
     cosine_obj = metrics.CosineProximity()
     self.evaluate(variables.variables_initializer(cosine_obj.variables))
     sample_weight = np.asarray([1.2, 3.4])
     loss = cosine_obj(self.y_true,
                       self.y_pred,
                       sample_weight=constant_op.constant(sample_weight))
     expected_loss = np.sum(
         self.expected_loss * sample_weight) / np.sum(sample_weight)
     self.assertAlmostEqual(self.evaluate(loss), expected_loss, 3)
Exemplo n.º 5
0
  def test_unweighted(self):
    cosine_obj = metrics.CosineProximity()
    self.evaluate(variables.variables_initializer(cosine_obj.variables))

    y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),
                                   (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))
    y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),
                                   (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))

    update_op = cosine_obj.update_state(y_true, y_pred)
    self.evaluate(update_op)
    result = cosine_obj.result()
    self.assertAllClose(-0.60723, result, atol=1e-5)
Exemplo n.º 6
0
 def test_config(self):
   cosine_obj = metrics.CosineProximity(name='my_cos', dtype=dtypes.int32)
   self.assertEqual(cosine_obj.name, 'my_cos')
   self.assertEqual(cosine_obj._dtype, dtypes.int32)