Esempio n. 1
0
 def test_config(self):
   s_obj = metrics.SensitivityAtSpecificity(
       0.4, num_thresholds=100, name='sensitivity_at_specificity_1')
   self.assertEqual(s_obj.name, 'sensitivity_at_specificity_1')
   self.assertLen(s_obj.variables, 4)
   self.assertEqual(s_obj.value, 0.4)
   self.assertLen(s_obj.thresholds, 100)
Esempio n. 2
0
 def test_unweighted_all_correct(self):
   s_obj = metrics.SensitivityAtSpecificity(0.7)
   inputs = np.random.randint(0, 2, size=(100, 1))
   y_pred = constant_op.constant(inputs, dtype=dtypes.float32)
   y_true = constant_op.constant(inputs)
   self.evaluate(variables.variables_initializer(s_obj.variables))
   result = s_obj(y_true, y_pred)
   self.assertAlmostEqual(1, self.evaluate(result))
Esempio n. 3
0
  def test_unweighted_low_specificity(self):
    s_obj = metrics.SensitivityAtSpecificity(0.4)
    pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]
    label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

    y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)
    y_true = constant_op.constant(label_values)
    self.evaluate(variables.variables_initializer(s_obj.variables))
    result = s_obj(y_true, y_pred)
    self.assertAlmostEqual(0.6, self.evaluate(result))
Esempio n. 4
0
  def test_weighted(self, label_dtype):
    s_obj = metrics.SensitivityAtSpecificity(0.4)
    pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]
    label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
    weight_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)
    y_true = math_ops.cast(label_values, dtype=label_dtype)
    weights = constant_op.constant(weight_values)
    self.evaluate(variables.variables_initializer(s_obj.variables))
    result = s_obj(y_true, y_pred, sample_weight=weights)
    self.assertAlmostEqual(0.675, self.evaluate(result))
Esempio n. 5
0
    def test_reset_states_sensitivity_at_specificity(self):
        s_obj = metrics.SensitivityAtSpecificity(0.5, num_thresholds=1)
        model = _get_model([s_obj])
        x = np.concatenate((np.ones((25, 4)), np.zeros(
            (25, 4)), np.zeros((25, 4)), np.ones((25, 4))))
        y = np.concatenate((np.ones((25, 1)), np.zeros(
            (25, 1)), np.ones((25, 1)), np.zeros((25, 1))))

        for _ in range(2):
            model.evaluate(x, y)
            self.assertEqual(self.evaluate(s_obj.true_positives), 25.)
            self.assertEqual(self.evaluate(s_obj.false_positives), 25.)
            self.assertEqual(self.evaluate(s_obj.false_negatives), 25.)
            self.assertEqual(self.evaluate(s_obj.true_negatives), 25.)
Esempio n. 6
0
  def test_config(self):
    s_obj = metrics.SensitivityAtSpecificity(
        0.4, num_thresholds=100, name='sensitivity_at_specificity_1')
    self.assertEqual(s_obj.name, 'sensitivity_at_specificity_1')
    self.assertLen(s_obj.variables, 4)
    self.assertEqual(s_obj.specificity, 0.4)
    self.assertEqual(s_obj.num_thresholds, 100)

    # Check save and restore config
    s_obj2 = metrics.SensitivityAtSpecificity.from_config(s_obj.get_config())
    self.assertEqual(s_obj2.name, 'sensitivity_at_specificity_1')
    self.assertLen(s_obj2.variables, 4)
    self.assertEqual(s_obj2.specificity, 0.4)
    self.assertEqual(s_obj2.num_thresholds, 100)
Esempio n. 7
0
 def test_reset_states(self):
   s_obj = metrics.SensitivityAtSpecificity(0.5, num_thresholds=1)
   model = _get_simple_sequential_model([s_obj])
   x = np.concatenate((np.ones((25, 4)), np.zeros((25, 4)), np.zeros((25, 4)),
                       np.ones((25, 4))))
   y = np.concatenate((np.ones((25, 1)), np.zeros((25, 1)), np.ones((25, 1)),
                       np.zeros((25, 1))))
   model.evaluate(x, y)
   self.assertEqual(self.evaluate(s_obj.tp), 25.)
   self.assertEqual(self.evaluate(s_obj.fp), 25.)
   self.assertEqual(self.evaluate(s_obj.fn), 25.)
   self.assertEqual(self.evaluate(s_obj.tn), 25.)
   model.evaluate(x, y)
   self.assertEqual(self.evaluate(s_obj.tp), 25.)
   self.assertEqual(self.evaluate(s_obj.fp), 25.)
   self.assertEqual(self.evaluate(s_obj.fn), 25.)
   self.assertEqual(self.evaluate(s_obj.tn), 25.)
Esempio n. 8
0
  def test_value_is_idempotent(self):
    s_obj = metrics.SensitivityAtSpecificity(0.7)
    y_pred = random_ops.random_uniform((10, 3),
                                       maxval=1,
                                       dtype=dtypes.float32,
                                       seed=1)
    y_true = random_ops.random_uniform((10, 3),
                                       maxval=2,
                                       dtype=dtypes.int64,
                                       seed=1)
    update_op = s_obj.update_state(y_true, y_pred)
    self.evaluate(variables.variables_initializer(s_obj.variables))

    # Run several updates.
    for _ in range(10):
      self.evaluate(update_op)

    # Then verify idempotency.
    initial_sensitivity = self.evaluate(s_obj.result())
    for _ in range(10):
      self.assertAlmostEqual(initial_sensitivity, self.evaluate(s_obj.result()),
                             1e-3)
Esempio n. 9
0
 def test_invalid_num_thresholds(self):
   with self.assertRaisesRegexp(ValueError, '`num_thresholds` must be > 0.'):
     metrics.SensitivityAtSpecificity(0.4, num_thresholds=-1)
Esempio n. 10
0
 def test_invalid_specificity(self):
   with self.assertRaisesRegexp(
       ValueError, r'`specificity` must be in the range \[0, 1\].'):
     metrics.SensitivityAtSpecificity(-1)