Esempio n. 1
0
    def test_get_incremental_score(self):
        test_cases = [
            # scores to be returned in order by mock of model.score
            np.array(
                [0, 0, 0.5, 0.3, 0.8, 0.6, 0.9, 0.7, 1, 0.8, 1, 0.9, 1, 1]),
            np.array([0, 144, 222, -0.29, 1, 1])
        ]
        for scores in test_cases:
            model = RNNModel(3, 8, num_epochs=int(len(scores) / 2))
            model.score = MagicMock()
            model.score.side_effect = scores
            model.spawn_clf = MagicMock()

            # mock tensorflow network attributes
            model.sess = MagicMock()
            model.sess.run = MagicMock()
            model.opt = MagicMock()
            model.x = MagicMock()
            model.y = MagicMock()
            model.sequence_lengths = MagicMock()

            preprocess.up_sample = MagicMock()

            # none of the inputs are actually used due to mocking so just give dummy inputs
            train_scores, test_scores = model.incremental_score(
                [], [], [], [],
                train_sequence_lengths=[],
                test_sequence_lengths=[])
            self.assertListEqual(
                train_scores,
                list(scores[[i for i in range(0, len(scores), 2)]]))
            self.assertListEqual(
                test_scores,
                list(scores[[i for i in range(1, len(scores), 2)]]))
Esempio n. 2
0
    def test_score(self):
        test_cases = [
            (
                # True values for y
                np.array([1, 0, 0, 0, 1]),
                # Predicted values for y (used as return value for mocked prediction method - tested separately)
                np.array([0, 1, 0, 0, 1]),
                # score
                0.6),
            (np.array([0, 0, 0, 1, 1, 1, 2, 2,
                       2]), np.array([0, 1, 2, 0, 1, 2, 0, 1, 2]), 3 / 9),
            (np.array([0, 0, 0, 1, 1, 1, 2, 2,
                       2]), np.array([1, 1, 1, 2, 2, 2, 0, 0, 0]), 0),
            (np.array([0, 0, 0, 1, 1, 1, 2, 2,
                       2]), np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]), 1),
        ]
        for y_true, y_pred, score in test_cases:
            model = RNNModel(3, 8)
            model.predict = MagicMock()
            model.predict.return_value = y_pred

            # no params except y_true are used so just input dummy params
            self.assertAlmostEqual(
                model.score([], y_true, sequence_lengths=[]), score)