def test_reset_states(self): fn_obj = metrics.FalseNegatives() model = _get_simple_sequential_model([fn_obj]) x = np.zeros((100, 4)) y = np.ones((100, 1)) model.evaluate(x, y) self.assertEqual(self.evaluate(fn_obj.accumulator), 100.) model.evaluate(x, y) self.assertEqual(self.evaluate(fn_obj.accumulator), 100.)
def test_weighted(self): fn_obj = metrics.FalseNegatives() self.evaluate(variables.variables_initializer(fn_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 = fn_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAllClose([5.], self.evaluate(result))
def test_config(self): fn_obj = metrics.FalseNegatives(name='my_fn', thresholds=[0.4, 0.9]) self.assertEqual(fn_obj.name, 'my_fn') self.assertEqual(len(fn_obj.variables), 1) self.assertEqual(fn_obj.thresholds, [0.4, 0.9]) # Check save and restore config fn_obj2 = metrics.FalseNegatives.from_config(fn_obj.get_config()) self.assertEqual(fn_obj2.name, 'my_fn') self.assertEqual(len(fn_obj2.variables), 1) self.assertEqual(fn_obj2.thresholds, [0.4, 0.9])
def test_weighted_with_thresholds(self): fn_obj = metrics.FalseNegatives(thresholds=[0.15, 0.5, 0.85]) self.evaluate(variables.variables_initializer(fn_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 = ((3.0,), (5.0,), (7.0,), (4.0,)) result = fn_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAllClose([4., 16., 23.], self.evaluate(result))
def test_unweighted_with_thresholds(self): fn_obj = metrics.FalseNegatives(thresholds=[0.15, 0.5, 0.85]) self.evaluate(variables.variables_initializer(fn_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 = fn_obj.update_state(y_true, y_pred) self.evaluate(update_op) result = fn_obj.result() self.assertAllClose([1., 4., 6.], result)
def test_unweighted(self): fn_obj = metrics.FalseNegatives() self.evaluate(variables.variables_initializer(fn_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 = fn_obj.update_state(y_true, y_pred) self.evaluate(update_op) result = fn_obj.result() self.assertAllClose([3.], result)
def test_config(self): fn_obj = metrics.FalseNegatives(name='my_fn', thresholds=[0.4, 0.9]) self.assertEqual(fn_obj.name, 'my_fn') self.assertEqual(len(fn_obj.variables), 1) self.assertEqual(fn_obj.thresholds, [0.4, 0.9])