Example #1
0
 def test_weighted(self):
     mape_obj = metrics.MeanAbsolutePercentageError()
     self.evaluate(variables.variables_initializer(mape_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 = mape_obj(y_true, y_pred, sample_weight=sample_weight)
     self.assertAllClose(40e7, self.evaluate(result), atol=1e-5)
Example #2
0
    def test_config(self):
        mape_obj = metrics.MeanAbsolutePercentageError(name='my_mape',
                                                       dtype=dtypes.int32)
        self.assertEqual(mape_obj.name, 'my_mape')
        self.assertEqual(mape_obj._dtype, dtypes.int32)

        # Check save and restore config
        mape_obj2 = metrics.MeanAbsolutePercentageError.from_config(
            mape_obj.get_config())
        self.assertEqual(mape_obj2.name, 'my_mape')
        self.assertEqual(mape_obj2._dtype, dtypes.int32)
Example #3
0
    def test_unweighted(self):
        mape_obj = metrics.MeanAbsolutePercentageError()
        self.evaluate(variables.variables_initializer(mape_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 = mape_obj.update_state(y_true, y_pred)
        self.evaluate(update_op)
        result = mape_obj.result()
        self.assertAllClose(35e7, result, atol=1e-5)