From 8e00303434d444677de17b5499cdddb5a5f58fcd Mon Sep 17 00:00:00 2001 From: ahmadalguydi Date: Sun, 9 Aug 2026 13:16:51 +0300 Subject: [PATCH 1/3] docs: add DeepAR and NBeats examples --- pytorch_forecasting/models/deepar/_deepar.py | 10 +++++++++- pytorch_forecasting/models/deepar/_deepar_pkg.py | 10 +++++++++- pytorch_forecasting/models/nbeats/_nbeats.py | 9 +++++++++ pytorch_forecasting/models/nbeats/_nbeats_pkg.py | 10 +++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pytorch_forecasting/models/deepar/_deepar.py b/pytorch_forecasting/models/deepar/_deepar.py index d6d8616ba..4b63c4440 100644 --- a/pytorch_forecasting/models/deepar/_deepar.py +++ b/pytorch_forecasting/models/deepar/_deepar.py @@ -35,7 +35,15 @@ class DeepAR(AutoRegressiveBaseModelWithCovariates): - """DeepAR: Probabilistic forecasting with autoregressive recurrent networks.""" + """DeepAR: Probabilistic forecasting with autoregressive recurrent networks. + + Examples + -------- + Create a model from a configured :class:`TimeSeriesDataSet` (see the + :doc:`DeepAR tutorial ` for a complete example): + + >>> model = DeepAR.from_dataset(training_dataset, hidden_size=16) # doctest: +SKIP + """ @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..96e608a4e 100644 --- a/pytorch_forecasting/models/nbeats/_nbeats.py +++ b/pytorch_forecasting/models/nbeats/_nbeats.py @@ -86,6 +86,15 @@ class NBeats(NBeatsAdapter): nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]). **kwargs Additional arguments forwarded to :py:class:`~BaseModel`. + + Examples + -------- + Create a model from a configured :class:`TimeSeriesDataSet` (see the + `N-BEATS example + `_ + for a complete example): + + >>> model = NBeats.from_dataset(training_dataset, context_length=24) # doctest: +SKIP """ # 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", From 7a1ab5fa1d98e50247f4b9cf47612e05127c5966 Mon Sep 17 00:00:00 2001 From: ahmadalguydi Date: Mon, 10 Aug 2026 16:59:07 +0300 Subject: [PATCH 2/3] docs: show complete model example flows --- pytorch_forecasting/models/deepar/_deepar.py | 30 +++++++++++++++++--- pytorch_forecasting/models/nbeats/_nbeats.py | 30 ++++++++++++++++---- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/pytorch_forecasting/models/deepar/_deepar.py b/pytorch_forecasting/models/deepar/_deepar.py index 4b63c4440..7f357af00 100644 --- a/pytorch_forecasting/models/deepar/_deepar.py +++ b/pytorch_forecasting/models/deepar/_deepar.py @@ -39,10 +39,32 @@ class DeepAR(AutoRegressiveBaseModelWithCovariates): Examples -------- - Create a model from a configured :class:`TimeSeriesDataSet` (see the - :doc:`DeepAR tutorial ` for a complete example): - - >>> model = DeepAR.from_dataset(training_dataset, hidden_size=16) # doctest: +SKIP + Create a dataset, train a model, and predict the validation horizon: + + .. code-block:: python + + 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 diff --git a/pytorch_forecasting/models/nbeats/_nbeats.py b/pytorch_forecasting/models/nbeats/_nbeats.py index 96e608a4e..4ceaeb6a7 100644 --- a/pytorch_forecasting/models/nbeats/_nbeats.py +++ b/pytorch_forecasting/models/nbeats/_nbeats.py @@ -89,12 +89,32 @@ class NBeats(NBeatsAdapter): Examples -------- - Create a model from a configured :class:`TimeSeriesDataSet` (see the - `N-BEATS example - `_ - for a complete example): + Create a dataset, train a model, and predict the validation horizon: - >>> model = NBeats.from_dataset(training_dataset, context_length=24) # doctest: +SKIP + .. code-block:: python + + 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 From 906c0e71d88dd84be5f68844dedc40c38d667248 Mon Sep 17 00:00:00 2001 From: ahmadalguydi Date: Wed, 12 Aug 2026 18:41:40 +0300 Subject: [PATCH 3/3] docs: format model examples as doctests Signed-off-by: ahmadalguydi --- pytorch_forecasting/models/deepar/_deepar.py | 47 ++++++++++---------- pytorch_forecasting/models/nbeats/_nbeats.py | 47 ++++++++++---------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/pytorch_forecasting/models/deepar/_deepar.py b/pytorch_forecasting/models/deepar/_deepar.py index 7f357af00..06b285952 100644 --- a/pytorch_forecasting/models/deepar/_deepar.py +++ b/pytorch_forecasting/models/deepar/_deepar.py @@ -41,30 +41,29 @@ class DeepAR(AutoRegressiveBaseModelWithCovariates): -------- Create a dataset, train a model, and predict the validation horizon: - .. code-block:: python - - 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)) + >>> 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 diff --git a/pytorch_forecasting/models/nbeats/_nbeats.py b/pytorch_forecasting/models/nbeats/_nbeats.py index 4ceaeb6a7..7247eb94b 100644 --- a/pytorch_forecasting/models/nbeats/_nbeats.py +++ b/pytorch_forecasting/models/nbeats/_nbeats.py @@ -91,30 +91,29 @@ class NBeats(NBeatsAdapter): -------- Create a dataset, train a model, and predict the validation horizon: - .. code-block:: python - - 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)) + >>> 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