Ejemplo n.º 1
0
    def setup_method(self, method):
        # super().setup_method(method)
        self.train_data = pd.DataFrame(data=np.random.randn(64, 4))
        self.val_data = pd.DataFrame(data=np.random.randn(16, 4))
        self.test_data = pd.DataFrame(data=np.random.randn(16, 4))

        self.past_seq_len = 6
        self.future_seq_len_1 = 1
        self.future_seq_len_2 = 2

        # use roll method in time_sequence
        self.feat = TimeSequenceFeatureTransformer()

        self.config = {
            'batch_size': 32,
            'epochs': 1
        }

        self.model_1 = LSTMSeq2Seq(check_optional_config=False,
                                   future_seq_len=self.future_seq_len_1)
        self.model_2 = LSTMSeq2Seq(check_optional_config=False,
                                   future_seq_len=self.future_seq_len_2)

        self.fitted = False
        self.predict_1 = None
        self.predict_2 = None
Ejemplo n.º 2
0
    def test_fit_eval(self):
        past_seq_len = 6
        future_seq_len = 2
        input_dim = 5
        output_dim = 4
        x_train = np.random.rand(100, past_seq_len, input_dim)
        y_train = np.random.rand(100, future_seq_len, output_dim)
        x_test = np.random.rand(100, past_seq_len, input_dim)
        y_test = np.random.rand(100, future_seq_len, output_dim)
        model = LSTMSeq2Seq(check_optional_config=False,
                            future_seq_len=future_seq_len)
        model_config = {
            'batch_size': 32,
            'epochs': 1,
            'latent_dim': 128,
            'dropout': 0.2
        }
        model.fit_eval(x_train, y_train, **model_config)
        y_pred = model.predict(x_test)
        rmse, smape = model.evaluate(x=x_test, y=y_test, metric=["rmse", "smape"])
        assert rmse.shape == smape.shape
        assert rmse.shape == (future_seq_len, output_dim)

        assert model.past_seq_len == past_seq_len
        assert model.future_seq_len == future_seq_len
        assert model.feature_num == input_dim
        assert model.target_col_num == output_dim
        assert y_pred.shape == y_test.shape
Ejemplo n.º 3
0
    def test_predict_with_uncertainty(self, ):
        x_train_2, y_train_2 = self.feat._roll_train(
            self.train_data,
            past_seq_len=self.past_seq_len,
            future_seq_len=self.future_seq_len_2)
        x_test_2 = self.feat._roll_test(self.test_data,
                                        past_seq_len=self.past_seq_len)
        self.model_2.fit_eval((x_train_2, y_train_2), mc=True, **self.config)
        prediction, uncertainty = self.model_2.predict_with_uncertainty(
            x_test_2, n_iter=2)
        assert prediction.shape == (x_test_2.shape[0], self.future_seq_len_2)
        assert uncertainty.shape == (x_test_2.shape[0], self.future_seq_len_2)
        assert np.any(uncertainty)

        new_model_2 = LSTMSeq2Seq(check_optional_config=False)
        dirname = tempfile.mkdtemp(prefix="automl_test_feature")
        try:
            save(dirname, model=self.model_2)
            restore(dirname, model=new_model_2, config=self.config)
            prediction, uncertainty = new_model_2.predict_with_uncertainty(
                x_test_2, n_iter=2)
            assert prediction.shape == (x_test_2.shape[0],
                                        self.future_seq_len_2)
            assert uncertainty.shape == (x_test_2.shape[0],
                                         self.future_seq_len_2)
            assert np.any(uncertainty)
        finally:
            shutil.rmtree(dirname)
Ejemplo n.º 4
0
    def test_save_restore_2(self):
        x_train_2, y_train_2 = self.feat._roll_train(
            self.train_data,
            past_seq_len=self.past_seq_len,
            future_seq_len=self.future_seq_len_2)
        x_test_2 = self.feat._roll_test(self.test_data,
                                        past_seq_len=self.past_seq_len)
        self.model_2.fit_eval((x_train_2, y_train_2), **self.config)

        predict_2_before = self.model_2.predict(x_test_2)
        new_model_2 = LSTMSeq2Seq(check_optional_config=False)

        dirname = tempfile.mkdtemp(prefix="automl_test_feature")
        try:
            save(dirname, model=self.model_2)
            restore(dirname, model=new_model_2, config=self.config)
            predict_2_after = new_model_2.predict(x_test_2)
            assert_array_almost_equal(predict_2_before, predict_2_after, decimal=2), \
                "Prediction values are not the same after restore: " \
                "predict before is {}, and predict after is {}".format(predict_2_before,
                                                                       predict_2_after)
            new_config = {'epochs': 2}
            new_model_2.fit_eval((x_train_2, y_train_2), **new_config)
        finally:
            shutil.rmtree(dirname)