예제 #1
0
 def test_reset_states(self):
   tp_obj = metrics.TruePositives()
   model = _get_simple_sequential_model([tp_obj])
   x = np.ones((100, 4))
   y = np.ones((100, 1))
   model.evaluate(x, y)
   self.assertEqual(self.evaluate(tp_obj.accumulator), 100.)
   model.evaluate(x, y)
   self.assertEqual(self.evaluate(tp_obj.accumulator), 100.)
예제 #2
0
 def test_weighted(self):
   tp_obj = metrics.TruePositives()
   self.evaluate(variables.variables_initializer(tp_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 = tp_obj(y_true, y_pred, sample_weight=sample_weight)
   self.assertAllClose([12.], self.evaluate(result))
예제 #3
0
  def test_weighted_with_thresholds(self):
    tp_obj = metrics.TruePositives(thresholds=[0.15, 0.5, 0.85])
    self.evaluate(variables.variables_initializer(tp_obj.variables))

    y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),
                                   (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))
    y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),
                                   (1, 1, 1, 1)))

    result = tp_obj(y_true, y_pred, sample_weight=37.)
    self.assertAllClose([222., 111., 37.], self.evaluate(result))
예제 #4
0
  def test_config(self):
    tp_obj = metrics.TruePositives(name='my_tp', thresholds=[0.4, 0.9])
    self.assertEqual(tp_obj.name, 'my_tp')
    self.assertEqual(len(tp_obj.variables), 1)
    self.assertEqual(tp_obj.thresholds, [0.4, 0.9])

    # Check save and restore config
    tp_obj2 = metrics.TruePositives.from_config(tp_obj.get_config())
    self.assertEqual(tp_obj2.name, 'my_tp')
    self.assertEqual(len(tp_obj2.variables), 1)
    self.assertEqual(tp_obj2.thresholds, [0.4, 0.9])
예제 #5
0
  def test_unweighted_with_thresholds(self):
    tp_obj = metrics.TruePositives(thresholds=[0.15, 0.5, 0.85])
    self.evaluate(variables.variables_initializer(tp_obj.variables))

    y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),
                                   (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))
    y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),
                                   (1, 1, 1, 1)))

    update_op = tp_obj.update_state(y_true, y_pred)
    self.evaluate(update_op)
    result = tp_obj.result()
    self.assertAllClose([6., 3., 1.], result)
예제 #6
0
  def test_unweighted(self):
    tp_obj = metrics.TruePositives()
    self.evaluate(variables.variables_initializer(tp_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 = tp_obj.update_state(y_true, y_pred)
    self.evaluate(update_op)
    result = tp_obj.result()
    self.assertAllClose([7.], result)
예제 #7
0
 def test_config(self):
   tp_obj = metrics.TruePositives(name='my_tp', thresholds=[0.4, 0.9])
   self.assertEqual(tp_obj.name, 'my_tp')
   self.assertEqual(len(tp_obj.variables), 1)
   self.assertEqual(tp_obj.thresholds, [0.4, 0.9])