Ejemplo n.º 1
0
    def test_transformer(self):
        model_name = "transformer"
        model = Model(
            model_name,
            model_parameters={
                "activated": True,
                "kwargs": {
                    "model_dim": 16
                }
            },
            frequency="D",
            prediction_length=self.prediction_length,
            epoch=1,
            use_external_features=True,
            batch_size=32,
            num_batches_per_epoch=50,
        )
        metrics, identifiers_columns, forecasts_df = model.train_evaluate(
            self.train_list_dataset,
            self.test_list_dataset,
            make_forecasts=True)

        TestModel.metrics_assertions(metrics, model_name)
        TestModel.forecasts_assertions(
            forecasts_df, model_name, prediction_length=self.prediction_length)
def test_minute_frequency():
    prediction_length = 1
    timeseries = {
        TIMESERIES_KEYS.START: "2021-01-15 12:40:00",
        TIMESERIES_KEYS.TARGET: np.array([12, 13]),
        TIMESERIES_KEYS.TARGET_NAME: "target",
        TIMESERIES_KEYS.TIME_COLUMN_NAME: "date",
    }
    frequency = "20min"
    gluon_dataset = ListDataset([timeseries], freq=frequency)
    model = Model(
        "simplefeedforward",
        model_parameters={"activated": True, "kwargs": {}},
        frequency=frequency,
        prediction_length=prediction_length,
        epoch=1,
        batch_size=8,
        num_batches_per_epoch=5,
    )
    evaluation_forecasts_df = model.train_evaluate(gluon_dataset, gluon_dataset, make_forecasts=True, retrain=True)[2]
    assert evaluation_forecasts_df["index"].iloc[0] == pd.Timestamp("2021-01-15 13:00:00")

    trained_model = TrainedModel(
        predictor=model.predictor,
        gluon_dataset=gluon_dataset,
        prediction_length=prediction_length,
        quantiles=[0.5],
        include_history=True,
    )
    trained_model.predict()
    forecasts_df = trained_model.get_forecasts_df(session="2021-01-01", model_label="TEST")
    assert forecasts_df["date"].iloc[0] == pd.Timestamp("2021-01-15 13:20:00")
 def test_mqcnn(self):
     model_name = "mqcnn"
     model = Model(
         model_name,
         model_parameters={"activated": True, "kwargs": {}},
         frequency="D",
         prediction_length=self.prediction_length,
         epoch=1,
         use_external_features=False,
         batch_size=32,
         num_batches_per_epoch=50,
     )
     model.train(self.test_list_dataset)
     assert model.predictor is not None
    def setup_class(self):
        df = pd.DataFrame(
            {
                "date": ["2018-01-06", "2018-01-07", "2018-01-08", "2018-01-09", "2018-01-08", "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12"],
                "target": [2, 4, 2, 2, 5, 2, 3, 2, 3],
                "key": [1, 1, 1, 1, 2, 2, 2, 2, 2],
                "ext_feat": [0, 0, 0, 0, 0, 1, 0, 1, 1],
            }
        )
        df["date"] = pd.to_datetime(df["date"]).dt.tz_localize(tz=None)

        self.frequency = "D"

        gluon_dataset = GluonDataset(
            dataframe=df,
            time_column_name="date",
            frequency=self.frequency,
            target_columns_names=["target"],
            timeseries_identifiers_names=["key"],
            external_features_columns_names=["ext_feat"],
            min_length=2,
        )

        self.prediction_length = 2
        gluon_list_datasets = gluon_dataset.create_list_datasets(cut_lengths=[self.prediction_length, 0])
        self.train_list_dataset = gluon_list_datasets[0]
        self.test_list_dataset = gluon_list_datasets[1]

        self.model_name = "simplefeedforward"
        self.model = Model(
            self.model_name,
            model_parameters={"activated": True, "kwargs": {}},
            frequency=self.frequency,
            prediction_length=self.prediction_length,
            epoch=1,
            use_external_features=True,
            batch_size=32,
            num_batches_per_epoch=50,
        )
Ejemplo n.º 5
0
 def instantiate_models(self):
     """Instantiate all the selected models. """
     for model_name, model_parameters in self.models_parameters.items():
         self.models.append(
             Model(
                 model_name,
                 model_parameters=model_parameters,
                 frequency=self.frequency,
                 prediction_length=self.prediction_length,
                 epoch=self.epoch,
                 use_external_features=self.use_external_features,
                 batch_size=self.batch_size,
                 num_batches_per_epoch=self.num_batches_per_epoch,
                 season_length=self.season_length,
                 mxnet_context=self.mxnet_context,
             ))
class TestExternalFeaturesSimpleFeedForward:
    """Test simple feed forward with external features on multiple timeseries of different lengths"""

    def setup_class(self):
        df = pd.DataFrame(
            {
                "date": ["2018-01-06", "2018-01-07", "2018-01-08", "2018-01-09", "2018-01-08", "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12"],
                "target": [2, 4, 2, 2, 5, 2, 3, 2, 3],
                "key": [1, 1, 1, 1, 2, 2, 2, 2, 2],
                "ext_feat": [0, 0, 0, 0, 0, 1, 0, 1, 1],
            }
        )
        df["date"] = pd.to_datetime(df["date"]).dt.tz_localize(tz=None)

        self.frequency = "D"

        gluon_dataset = GluonDataset(
            dataframe=df,
            time_column_name="date",
            frequency=self.frequency,
            target_columns_names=["target"],
            timeseries_identifiers_names=["key"],
            external_features_columns_names=["ext_feat"],
            min_length=2,
        )

        self.prediction_length = 2
        gluon_list_datasets = gluon_dataset.create_list_datasets(cut_lengths=[self.prediction_length, 0])
        self.train_list_dataset = gluon_list_datasets[0]
        self.test_list_dataset = gluon_list_datasets[1]

        self.model_name = "simplefeedforward"
        self.model = Model(
            self.model_name,
            model_parameters={"activated": True, "kwargs": {}},
            frequency=self.frequency,
            prediction_length=self.prediction_length,
            epoch=1,
            use_external_features=True,
            batch_size=32,
            num_batches_per_epoch=50,
        )

    def test_simplefeedforward_external_features_training(self):
        metrics, identifiers_columns, forecasts_df = self.model.train_evaluate(
            self.train_list_dataset, self.test_list_dataset, make_forecasts=True, retrain=True
        )
        assert len(forecasts_df.index) == 4

    def test_simplefeedforward_external_features_predictions(self):
        external_features_future_df = pd.DataFrame(
            {
                "date": ["2018-01-10", "2018-01-11", "2018-01-13", "2018-01-14"],
                "key": [1, 1, 2, 2],
                "ext_feat": [0, 0, 0, 1],
            }
        )
        external_features_future_df["date"] = pd.to_datetime(external_features_future_df["date"]).dt.tz_localize(tz=None)

        gluon_dataset = add_future_external_features(self.test_list_dataset, external_features_future_df, self.prediction_length, self.frequency)

        trained_model = TrainedModel(
            model_name=self.model_name,
            predictor=self.model.predictor,
            gluon_dataset=gluon_dataset,
            prediction_length=self.prediction_length,
            quantiles=[0.1, 0.5, 0.9],
            include_history=True,
        )
        trained_model.predict()
        forecasts_df = trained_model.get_forecasts_df(session="1234")
        assert len(forecasts_df.index) == 13