def test_reset_states(self): tn_obj = metrics.TrueNegatives() model = _get_simple_sequential_model([tn_obj]) x = np.zeros((100, 4)) y = np.zeros((100, 1)) model.evaluate(x, y) self.assertEqual(self.evaluate(tn_obj.accumulator), 100.) model.evaluate(x, y) self.assertEqual(self.evaluate(tn_obj.accumulator), 100.)
def test_weighted(self): tn_obj = metrics.TrueNegatives() self.evaluate(variables.variables_initializer(tn_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 = tn_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAllClose([4.], self.evaluate(result))
def test_config(self): tn_obj = metrics.TrueNegatives(name='my_tn', thresholds=[0.4, 0.9]) self.assertEqual(tn_obj.name, 'my_tn') self.assertEqual(len(tn_obj.variables), 1) self.assertEqual(tn_obj.thresholds, [0.4, 0.9]) # Check save and restore config tn_obj2 = metrics.TrueNegatives.from_config(tn_obj.get_config()) self.assertEqual(tn_obj2.name, 'my_tn') self.assertEqual(len(tn_obj2.variables), 1) self.assertEqual(tn_obj2.thresholds, [0.4, 0.9])
def test_weighted_with_thresholds(self): tn_obj = metrics.TrueNegatives(thresholds=[0.15, 0.5, 0.85]) self.evaluate(variables.variables_initializer(tn_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))) sample_weight = ((0.0, 2.0, 3.0, 5.0),) result = tn_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAllClose([5., 15., 23.], self.evaluate(result))
def test_unweighted_with_thresholds(self): tn_obj = metrics.TrueNegatives(thresholds=[0.15, 0.5, 0.85]) self.evaluate(variables.variables_initializer(tn_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 = tn_obj.update_state(y_true, y_pred) self.evaluate(update_op) result = tn_obj.result() self.assertAllClose([2., 5., 7.], result)
def test_unweighted(self): tn_obj = metrics.TrueNegatives() self.evaluate(variables.variables_initializer(tn_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 = tn_obj.update_state(y_true, y_pred) self.evaluate(update_op) result = tn_obj.result() self.assertAllClose([3.], result)
def test_config(self): tn_obj = metrics.TrueNegatives(name='my_tn', thresholds=[0.4, 0.9]) self.assertEqual(tn_obj.name, 'my_tn') self.assertEqual(len(tn_obj.variables), 1) self.assertEqual(tn_obj.thresholds, [0.4, 0.9])