diff --git a/pytorch_forecasting/models/deepar/_deepar.py b/pytorch_forecasting/models/deepar/_deepar.py index d6d8616ba..06b285952 100644 --- a/pytorch_forecasting/models/deepar/_deepar.py +++ b/pytorch_forecasting/models/deepar/_deepar.py @@ -35,7 +35,36 @@ class DeepAR(AutoRegressiveBaseModelWithCovariates): - """DeepAR: Probabilistic forecasting with autoregressive recurrent networks.""" + """DeepAR: Probabilistic forecasting with autoregressive recurrent networks. + + Examples + -------- + Create a dataset, train a model, and predict the validation horizon: + + >>> import lightning.pytorch as pl + >>> from pytorch_forecasting import DeepAR, TimeSeriesDataSet + >>> from pytorch_forecasting.data.examples import generate_ar_data + >>> data = generate_ar_data(seasonality=10.0, timesteps=120, n_series=4) + >>> cutoff = data["time_idx"].max() - 6 + >>> training = TimeSeriesDataSet( + ... data[lambda x: x.time_idx <= cutoff], + ... time_idx="time_idx", + ... target="value", + ... group_ids=["series"], + ... max_encoder_length=24, + ... max_prediction_length=6, + ... time_varying_unknown_reals=["value"], + ... ) + >>> validation = TimeSeriesDataSet.from_dataset( + ... training, data, min_prediction_idx=cutoff + 1 + ... ) + >>> model = DeepAR.from_dataset(training, hidden_size=16) + >>> trainer = pl.Trainer(max_epochs=1, logger=False, enable_checkpointing=False) + >>> trainer.fit(model, training.to_dataloader(train=True, batch_size=32)) + >>> predictions = model.predict( + ... validation.to_dataloader(train=False, batch_size=32) + ... ) + """ @classmethod def _pkg(cls): diff --git a/pytorch_forecasting/models/deepar/_deepar_pkg.py b/pytorch_forecasting/models/deepar/_deepar_pkg.py index a126985bd..68f0bdb44 100644 --- a/pytorch_forecasting/models/deepar/_deepar_pkg.py +++ b/pytorch_forecasting/models/deepar/_deepar_pkg.py @@ -4,7 +4,15 @@ class DeepAR_pkg(_BasePtForecaster): - """DeepAR package container.""" + """DeepAR package container. + + Examples + -------- + The package container resolves to the user-facing model class: + + >>> DeepAR_pkg.get_cls().__name__ + 'DeepAR' + """ _tags = { "info:name": "DeepAR", diff --git a/pytorch_forecasting/models/nbeats/_nbeats.py b/pytorch_forecasting/models/nbeats/_nbeats.py index 63e5102d7..7247eb94b 100644 --- a/pytorch_forecasting/models/nbeats/_nbeats.py +++ b/pytorch_forecasting/models/nbeats/_nbeats.py @@ -86,6 +86,34 @@ class NBeats(NBeatsAdapter): nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]). **kwargs Additional arguments forwarded to :py:class:`~BaseModel`. + + Examples + -------- + Create a dataset, train a model, and predict the validation horizon: + + >>> import lightning.pytorch as pl + >>> from pytorch_forecasting import NBeats, TimeSeriesDataSet + >>> from pytorch_forecasting.data.examples import generate_ar_data + >>> data = generate_ar_data(seasonality=10.0, timesteps=120, n_series=4) + >>> cutoff = data["time_idx"].max() - 6 + >>> training = TimeSeriesDataSet( + ... data[lambda x: x.time_idx <= cutoff], + ... time_idx="time_idx", + ... target="value", + ... group_ids=["series"], + ... max_encoder_length=24, + ... max_prediction_length=6, + ... time_varying_unknown_reals=["value"], + ... ) + >>> validation = TimeSeriesDataSet.from_dataset( + ... training, data, min_prediction_idx=cutoff + 1 + ... ) + >>> model = NBeats.from_dataset(training, context_length=24) + >>> trainer = pl.Trainer(max_epochs=1, logger=False, enable_checkpointing=False) + >>> trainer.fit(model, training.to_dataloader(train=True, batch_size=32)) + >>> predictions = model.predict( + ... validation.to_dataloader(train=False, batch_size=32) + ... ) """ # noqa: E501 @classmethod diff --git a/pytorch_forecasting/models/nbeats/_nbeats_pkg.py b/pytorch_forecasting/models/nbeats/_nbeats_pkg.py index daeab1c4e..34bf868d2 100644 --- a/pytorch_forecasting/models/nbeats/_nbeats_pkg.py +++ b/pytorch_forecasting/models/nbeats/_nbeats_pkg.py @@ -4,7 +4,15 @@ class NBeats_pkg(_BasePtForecaster): - """NBeats package container.""" + """NBeats package container. + + Examples + -------- + The package container resolves to the user-facing model class: + + >>> NBeats_pkg.get_cls().__name__ + 'NBeats' + """ _tags = { "info:name": "NBeats",