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

        future_seq_len = 1
        past_seq_len = 6

        # use roll method in time_sequence
        tsft = TimeSequenceFeatureTransformer()
        self.x_train, self.y_train = tsft._roll_train(
            train_data,
            past_seq_len=past_seq_len,
            future_seq_len=future_seq_len)
        self.x_val, self.y_val = tsft._roll_train(
            val_data, past_seq_len=past_seq_len, future_seq_len=future_seq_len)
        self.x_test = tsft._roll_test(test_data, past_seq_len=past_seq_len)
        self.config = {
            'epochs': 1,
            "lr": 0.001,
            "lstm_1_units": 16,
            "dropout_1": 0.2,
            "lstm_2_units": 10,
            "dropout_2": 0.2,
            "batch_size": 32,
        }
        self.model = VanillaLSTM(check_optional_config=False,
                                 future_seq_len=future_seq_len)
 def test_predict_save_restore(self):
     config = {"batch_size": 128}
     self.model.fit_eval(self.train_data[0], self.train_data[1],
                         self.val_data, **config)
     pred = self.model.predict(self.test_data[0])
     assert pred.shape == self.test_data[1].shape
     with tempfile.TemporaryDirectory() as tmp_dir_name:
         ckpt_name = os.path.join(tmp_dir_name, "ckpt")
         self.model.save(ckpt_name)
         model_1 = VanillaLSTM()
         model_1.restore(ckpt_name)
         pred_1 = model_1.predict(self.test_data[0])
         assert np.allclose(pred, pred_1)
示例#3
0
    def test_save_restore(self):
        new_model = VanillaLSTM(check_optional_config=False)
        self.model.fit_eval(self.x_train, self.y_train, **self.config)
        predict_before = self.model.predict(self.x_test)

        dirname = tempfile.mkdtemp(prefix="automl_test_vanilla")
        try:
            save(dirname, model=self.model)
            restore(dirname, model=new_model, config=self.config)
            predict_after = new_model.predict(self.x_test)
            assert_array_almost_equal(predict_before, predict_after, decimal=2)
            new_config = {'epochs': 2}
            new_model.fit_eval(self.x_train, self.y_train, **new_config)

        finally:
            shutil.rmtree(dirname)
示例#4
0
 def _build(self):
     """
     Build LSTM Model in tf.keras
     """
     # build model with TF/Keras
     self.internal = LSTMKerasModel(
         check_optional_config=self.check_optional_config,
         future_seq_len=self.target_dim)
     return self.internal._build(mc=self.uncertainty, **self.model_config)
示例#5
0
class LSTMForecaster(TFParkForecaster):
    """
    Vanilla LSTM Forecaster
    """
    def __init__(self,
                 target_dim=1,
                 feature_dim=1,
                 lstm_1_units=16,
                 dropout_1=0.2,
                 lstm_2_units=8,
                 dropout_2=0.2,
                 metric="mean_squared_error",
                 lr=0.001,
                 loss="mse",
                 uncertainty: bool = False):
        """
        Build a LSTM Forecast Model.

        :param target_dim: dimension of model output
        :param feature_dim: dimension of input feature
        :param lstm_1_units: num of units for the 1st LSTM layer
        :param dropout_1: p for the 1st dropout layer
        :param lstm_2_units: num of units for the 2nd LSTM layer
        :param dropout_2: p for the 2nd dropout layer
        :param metric: the metric for validation and evaluation
        :param lr: learning rate
        :param uncertainty: whether to return uncertainty
        :param loss: the target function you want to optimize on
        """
        #
        self.target_dim = target_dim
        self.check_optional_config = False
        self.uncertainty = uncertainty

        self.model_config = {
            "lr": lr,
            "lstm_1_units": lstm_1_units,
            "dropout_1": dropout_1,
            "lstm_2_units": lstm_2_units,
            "dropout_2": dropout_2,
            "metric": metric,
            "feature_num": feature_dim,
            "loss": loss
        }
        self.internal = None

        super().__init__()

    def _build(self):
        """
        Build LSTM Model in tf.keras
        """
        # build model with TF/Keras
        self.internal = LSTMKerasModel(
            check_optional_config=self.check_optional_config,
            future_seq_len=self.target_dim)
        return self.internal._build(mc=self.uncertainty, **self.model_config)
示例#6
0
    def test_predict_with_uncertainty(self, ):
        self.model.fit_eval(self.x_train, self.y_train, mc=True, **self.config)
        prediction, uncertainty = self.model.predict_with_uncertainty(
            self.x_test, n_iter=10)
        assert prediction.shape == (self.x_test.shape[0], 1)
        assert uncertainty.shape == (self.x_test.shape[0], 1)
        assert np.any(uncertainty)

        new_model = VanillaLSTM(check_optional_config=False)
        dirname = tempfile.mkdtemp(prefix="automl_test_feature")
        try:
            save(dirname, model=self.model)
            restore(dirname, model=new_model, config=self.config)
            prediction, uncertainty = new_model.predict_with_uncertainty(
                self.x_test, n_iter=2)
            assert prediction.shape == (self.x_test.shape[0], 1)
            assert uncertainty.shape == (self.x_test.shape[0], 1)
            assert np.any(uncertainty)
        finally:
            shutil.rmtree(dirname)
示例#7
0
class LSTMForecaster(Forecaster):
    """
    Vanilla LSTM Forecaster
    """
    def __init__(self,
                 horizon=1,
                 feature_dim=1,
                 lstm_1_units=16,
                 dropout_1=0.2,
                 lstm_2_units=8,
                 dropout_2=0.2,
                 metric="mean_squared_error",
                 lr=0.001,
                 uncertainty: bool = False):
        """
        Build a LSTM Forecast Model.

        @param horizon: steps to look forward
        @param feature_dim: dimension of input feature
        @param lstm_1_units: num of units for the 1st LSTM layer
        @param dropout_1: p for the 1st dropout layer
        @param lstm_2_units: num of units for the 2nd LSTM layer
        @param dropout_2: p for the 2nd dropout layer
        @param metric: the metric for validation and evaluation
        @param lr: learning rate
        @param uncertainty: whether to return uncertainty
        """
        #
        self.horizon = horizon
        self.check_optional_config = False
        self.uncertainty = uncertainty

        self.model_config = {
            "lr": lr,
            "lstm_1_units": lstm_1_units,
            "dropout_1": dropout_1,
            "lstm_2_units": lstm_2_units,
            "dropout_2": dropout_2,
            "metric": metric,
            "feature_num": feature_dim
        }
        self.internal = None

        super().__init__()

    def _build(self):
        """
        Build LSTM Model in tf.keras
        """
        # build model with TF/Keras
        self.internal = LSTMKerasModel(
            check_optional_config=self.check_optional_config,
            future_seq_len=self.horizon)
        return self.internal._build(mc=self.uncertainty, **self.model_config)
class TestVanillaLSTM(TestCase):
    train_data, val_data, test_data = create_data()
    model = VanillaLSTM()

    def test_fit_evaluate(self):
        config = {"batch_size": 128}
        self.model.fit_eval(self.train_data[0], self.train_data[1],
                            self.val_data, **config)
        mse, smape = self.model.evaluate(self.val_data[0],
                                         self.val_data[1],
                                         metrics=["mse", "smape"])
        assert len(mse) == self.val_data[1].shape[-1]
        assert len(smape) == self.val_data[1].shape[-1]

    def test_config(self):
        config = {"lstm_units": [128] * 2, "dropouts": [0.2] * 2}
        self.model.fit_eval(self.train_data[0], self.train_data[1],
                            self.val_data, **config)

        config = {"lstm_units": 128, "dropouts": 0.2}
        self.model.fit_eval(self.train_data[0], self.train_data[1],
                            self.val_data, **config)

        with pytest.raises(ValueError):
            config = {"lstm_units": 0.1, "dropouts": 0.2}
            self.model.fit_eval(self.train_data[0], self.train_data[1],
                                self.val_data, **config)

        with pytest.raises(ValueError):
            config = {"lstm_units": [128] * 2, "dropouts": [0.2] * 3}
            self.model.fit_eval(self.train_data[0], self.train_data[1],
                                self.val_data, **config)

        with pytest.raises(ValueError):
            config = {"lstm_units": 128, "dropouts": [0.2] * 2}
            self.model.fit_eval(self.train_data[0], self.train_data[1],
                                self.val_data, **config)

    def test_predict_save_restore(self):
        model = VanillaLSTM()
        config = {
            "lstm_units": [128] * 2,
            "dropouts": [0.2] * 2,
            "batch_size": 128
        }
        model.fit_eval(self.train_data[0], self.train_data[1], self.val_data,
                       **config)
        pred = model.predict(self.test_data[0])
        assert pred.shape == self.test_data[1].shape
        with tempfile.TemporaryDirectory() as tmp_dir_name:
            ckpt_name = os.path.join(tmp_dir_name, "ckpt")
            model.save(ckpt_name)
            model_1 = VanillaLSTM()
            model_1.restore(ckpt_name)
            pred_1 = model_1.predict(self.test_data[0])
            assert np.allclose(pred, pred_1)

    def test_predict_with_uncertainty(self):
        config = {"batch_size": 128}
        self.model.fit_eval(self.train_data[0], self.train_data[1],
                            self.val_data, **config)
        prediction, uncertainty = self.model.predict_with_uncertainty(
            self.test_data[0], n_iter=100)
        assert prediction.shape == self.test_data[1].shape
        assert uncertainty.shape == self.test_data[1].shape
        assert np.any(uncertainty)
示例#9
0
class TestVanillaLSTM(ZooTestCase):
    def setup_method(self, method):
        # super().setup_method(method)
        train_data = pd.DataFrame(data=np.random.randn(64, 4))
        val_data = pd.DataFrame(data=np.random.randn(16, 4))
        test_data = pd.DataFrame(data=np.random.randn(16, 4))

        future_seq_len = 1
        past_seq_len = 6

        # use roll method in time_sequence
        tsft = TimeSequenceFeatureTransformer()
        self.x_train, self.y_train = tsft._roll_train(
            train_data,
            past_seq_len=past_seq_len,
            future_seq_len=future_seq_len)
        self.x_val, self.y_val = tsft._roll_train(
            val_data, past_seq_len=past_seq_len, future_seq_len=future_seq_len)
        self.x_test = tsft._roll_test(test_data, past_seq_len=past_seq_len)
        self.config = {
            'epochs': 1,
            "lr": 0.001,
            "lstm_1_units": 16,
            "dropout_1": 0.2,
            "lstm_2_units": 10,
            "dropout_2": 0.2,
            "batch_size": 32,
        }
        self.model = VanillaLSTM(check_optional_config=False,
                                 future_seq_len=future_seq_len)

    def teardown_method(self, method):
        pass

    def test_fit_eval(self):
        print("fit_eval:",
              self.model.fit_eval(self.x_train, self.y_train, **self.config))

    def test_fit_eval_mc(self):
        print(
            "fit_eval:",
            self.model.fit_eval(self.x_train,
                                self.y_train,
                                mc=True,
                                **self.config))

    def test_evaluate(self):
        self.model.fit_eval(self.x_train, self.y_train, **self.config)
        mse, rs = self.model.evaluate(self.x_val,
                                      self.y_val,
                                      metric=['mse', 'r2'])
        print("Mean squared error is:", mse)
        print("R square is:", rs)

    def test_predict(self):
        self.model.fit_eval(self.x_train, self.y_train, **self.config)
        self.y_pred = self.model.predict(self.x_test)
        assert self.y_pred.shape == (self.x_test.shape[0], 1)

    def test_save_restore(self):
        new_model = VanillaLSTM(check_optional_config=False)
        self.model.fit_eval(self.x_train, self.y_train, **self.config)
        predict_before = self.model.predict(self.x_test)

        dirname = tempfile.mkdtemp(prefix="automl_test_vanilla")
        try:
            save(dirname, model=self.model)
            restore(dirname, model=new_model, config=self.config)
            predict_after = new_model.predict(self.x_test)
            assert_array_almost_equal(predict_before, predict_after, decimal=2)
            new_config = {'epochs': 2}
            new_model.fit_eval(self.x_train, self.y_train, **new_config)

        finally:
            shutil.rmtree(dirname)

    def test_predict_with_uncertainty(self, ):
        self.model.fit_eval(self.x_train, self.y_train, mc=True, **self.config)
        prediction, uncertainty = self.model.predict_with_uncertainty(
            self.x_test, n_iter=10)
        assert prediction.shape == (self.x_test.shape[0], 1)
        assert uncertainty.shape == (self.x_test.shape[0], 1)
        assert np.any(uncertainty)

        new_model = VanillaLSTM(check_optional_config=False)
        dirname = tempfile.mkdtemp(prefix="automl_test_feature")
        try:
            save(dirname, model=self.model)
            restore(dirname, model=new_model, config=self.config)
            prediction, uncertainty = new_model.predict_with_uncertainty(
                self.x_test, n_iter=2)
            assert prediction.shape == (self.x_test.shape[0], 1)
            assert uncertainty.shape == (self.x_test.shape[0], 1)
            assert np.any(uncertainty)
        finally:
            shutil.rmtree(dirname)