PyTorch Forecasting
Use this skill after forecasting-data-prep. PyTorch Forecasting is best for neural, multi-horizon forecasting on pandas data when you need TimeSeriesDataSet, covariates, multiple related series, Lightning-based training, probabilistic/quantile outputs, TensorBoard logging, and model interpretation.
Do not use it as a first choice for one very short series or simple statistical baselines; the official docs warn that deep learning needs enough history and related series. Prefer simpler baselines first.
Minimum Install
pip install pytorch-forecasting --extra-index-url https://download.pytorch.org/whl/cpu
pip install lightning
For Windows or GPU, install the matching PyTorch build first from the official PyTorch instructions. Conda install is documented as conda install pytorch-forecasting pytorch>=2.0.0 -c pytorch -c conda-forge. Use pip install pytorch-forecasting[mqf2] only when using MQF2DistributionLoss.
Data Contract
- Start from a
forecasting-data-prep contract: sorted rows, explicit target(s), integer time index, panel IDs, frequency, horizon, covariate availability, temporal cutoffs, and leakage notes.
- Use
TimeSeriesDataSet with a pandas DataFrame. Required roles are time_idx, target, and group_ids; for a single series, create a constant group ID column.
time_idx must be integer-like and increase by 1 for regular observations. If timestamps are real datetimes, map them to a documented integer index and keep the original timestamp for reporting.
- Use
target="y" for one target or target=[...] for multiple targets. Multiple targets require compatible model and loss choices.
- Put calendar and planned regressors in
time_varying_known_*; put target and observed-only signals in time_varying_unknown_*; put static attributes in static_*.
- Use
max_encoder_length for lookback and max_prediction_length for forecast horizon.
- Fit encoders, scalers, target normalizers, and categorical mappings on the training
TimeSeriesDataSet; create validation/test/inference datasets with TimeSeriesDataSet.from_dataset(..., stop_randomization=True).
allow_missing_timesteps=True handles missing integer time steps, not NA values. Fill or exclude NAs before creating the dataset.
- The official
TimeSeriesDataSet is in-memory; no built-in large-data rotation exists.
Read references/pytorch-forecasting-data-validation.md before using custom panels, missing timesteps, lags, multiple targets, future covariates, or backtests.
Model Selection
Use public model names exactly as documented. The main documented models are Baseline, DeepAR, DecoderMLP, NBeats, NBeatsKAN, NHiTS, RecurrentNetwork, TemporalFusionTransformer, TiDEModel, TimeXer, and xLSTMTime.
Use TemporalFusionTransformer for rich covariates, multiple related series, quantile forecasts, and interpretation. Use DeepAR for autoregressive probabilistic regression and DeepVAR-style multivariate distribution loss. Use NBeats only for single-target regression without covariates. Use NHiTS for long horizons and covariates. Use TimeXer for exogenous-variable forecasting. Use Baseline as a last-value benchmark.
The API index also lists v2/source classes such as DLinear, Samformer, TFT, and package-wrapper classes. Do not treat them as top-level stable imports unless the installed version and import path are verified. Read references/pytorch-forecasting-model-map.md before claiming support.
Training Pattern
import lightning.pytorch as pl
from lightning.pytorch.callbacks import EarlyStopping, LearningRateMonitor
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer
from pytorch_forecasting.metrics import QuantileLoss
training = TimeSeriesDataSet(
train_df,
time_idx="time_idx",
target="y",
group_ids=["series_id"],
max_encoder_length=36,
max_prediction_length=6,
static_categoricals=["series_id"],
time_varying_known_reals=["time_idx"],
time_varying_unknown_reals=["y"],
)
validation = TimeSeriesDataSet.from_dataset(
training, full_df, min_prediction_idx=validation_start_idx, stop_randomization=True
)
train_loader = training.to_dataloader(train=True, batch_size=128, num_workers=2)
val_loader = validation.to_dataloader(train=False, batch_size=128, num_workers=2)
model = TemporalFusionTransformer.from_dataset(
training, loss=QuantileLoss(), learning_rate=0.03, hidden_size=32
)
trainer = pl.Trainer(
max_epochs=100,
accelerator="auto",
callbacks=[EarlyStopping(monitor="val_loss", mode="min"), LearningRateMonitor()],
)
trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=val_loader)
Use Tuner(trainer).lr_find(...) when tuning the learning rate, then recreate or update the model with the chosen rate.
Prediction, Evaluation, and Plotting
- Load checkpoints with the model class'
load_from_checkpoint(...) when needed.
- Predict with
model.predict(data_or_loader, mode="prediction"|"quantiles"|"raw", return_index=True, return_x=True) as needed; verify mode support for the installed version/model.
- Evaluate on temporal validation/test windows only. Use a final test once; use rolling-origin or expanding-window backtests for model selection.
- Recommended metrics:
MAE, RMSE, MASE, SMAPE, MAPE only when actuals are safely away from zero, QuantileLoss for quantiles, and distribution losses for probabilistic models.
- Use
plot_prediction() for actual-vs-predicted plots. Use predict_dependency() and model-specific interpretation only on validation/test or documented sensitivity scenarios.
- For residual diagnostics, compute held-out residuals from predictions and actuals. PyTorch Forecasting does not document one universal residual diagnostics API.
Anti-Leakage Rules
- Never random split rows or series windows across time. Split by temporal cutoffs, then construct datasets.
- Fit
TimeSeriesDataSet, normalizers, scalers, categorical encoders, target transformations, hyperparameter tuning, and feature selection on train only or inside each backtest fold.
- Use
from_dataset() for validation/test so train-fitted transformations are reused forward.
- Use
lags= or manual groupby(...).shift(lag) only with past values; never rolling windows that include the forecast timestamp or future.
- Put future covariates in
time_varying_known_* only if their values are known for every decoder step at prediction time.
- Keep
max_encoder_length, max_prediction_length, min_prediction_idx, gap, frequency, and panel cutoffs aligned with the data-prep contract.
- Disable validation/test randomization with
stop_randomization=True.
Common Errors
- Passing datetimes directly as
time_idx instead of an integer index.
- Building validation with a new
TimeSeriesDataSet(...) and refitting scalers/encoders on validation/test.
- Putting observed future target drivers in
time_varying_known_*.
- Using
NBeats for covariate or multi-target tasks it does not document.
- Expecting neural models to work well on one short series without enough history.
- Treating
allow_missing_timesteps=True as NA imputation.
- Ignoring version/import differences for v2 or API-indexed classes.
References
- Read
references/pytorch-forecasting-model-map.md for supported model names, model capabilities, and API caveats.
- Read
references/pytorch-forecasting-data-validation.md for data formats, validation, covariates, lags, probabilistic forecasts, plotting, and diagnostics.
- Read
references/official-sources.md for official sources consulted.
Ready Checklist
forecasting-data-prep contract is complete and anti-leakage risks are resolved or documented.
time_idx, target, group_ids, covariate roles, horizon, and cutoffs are explicit.
- Train dataset owns fitted encoders/scalers/normalizers; validation/test are built with
from_dataset().
- Model choice supports the required covariates, multiple targets, probabilistic output, and horizon.
- Validation uses temporal cutoffs/backtesting; no random split.
- Metrics, plots, quantiles/intervals, and residual diagnostics are computed on held-out periods only.
1---2name: pytorch-forecasting3description: Use sktime/PyTorch Forecasting for neural time-series forecasting with pandas DataFrames, TimeSeriesDataSet, Lightning Trainer, TemporalFusionTransformer, DeepAR/DeepVAR, N-BEATS, N-HiTS, TiDE, TimeXer, xLSTMTime, RecurrentNetwork, DecoderMLP, Baseline, probabilistic or quantile losses, covariates, multiple series via group_ids, multiple targets, temporal validation, plotting, interpretation, and anti-leakage safeguards after applying forecasting-data-prep.4---56# PyTorch Forecasting78Use this skill after `forecasting-data-prep`. PyTorch Forecasting is best for neural, multi-horizon forecasting on pandas data when you need `TimeSeriesDataSet`, covariates, multiple related series, Lightning-based training, probabilistic/quantile outputs, TensorBoard logging, and model interpretation.910Do not use it as a first choice for one very short series or simple statistical baselines; the official docs warn that deep learning needs enough history and related series. Prefer simpler baselines first.1112## Minimum Install1314```bash15pip install pytorch-forecasting --extra-index-url https://download.pytorch.org/whl/cpu16pip install lightning17```1819For Windows or GPU, install the matching PyTorch build first from the official PyTorch instructions. Conda install is documented as `conda install pytorch-forecasting pytorch>=2.0.0 -c pytorch -c conda-forge`. Use `pip install pytorch-forecasting[mqf2]` only when using `MQF2DistributionLoss`.2021## Data Contract2223- Start from a `forecasting-data-prep` contract: sorted rows, explicit target(s), integer time index, panel IDs, frequency, horizon, covariate availability, temporal cutoffs, and leakage notes.24- Use `TimeSeriesDataSet` with a pandas `DataFrame`. Required roles are `time_idx`, `target`, and `group_ids`; for a single series, create a constant group ID column.25- `time_idx` must be integer-like and increase by 1 for regular observations. If timestamps are real datetimes, map them to a documented integer index and keep the original timestamp for reporting.26- Use `target="y"` for one target or `target=[...]` for multiple targets. Multiple targets require compatible model and loss choices.27- Put calendar and planned regressors in `time_varying_known_*`; put target and observed-only signals in `time_varying_unknown_*`; put static attributes in `static_*`.28- Use `max_encoder_length` for lookback and `max_prediction_length` for forecast horizon.29- Fit encoders, scalers, target normalizers, and categorical mappings on the training `TimeSeriesDataSet`; create validation/test/inference datasets with `TimeSeriesDataSet.from_dataset(..., stop_randomization=True)`.30- `allow_missing_timesteps=True` handles missing integer time steps, not `NA` values. Fill or exclude `NA`s before creating the dataset.31- The official `TimeSeriesDataSet` is in-memory; no built-in large-data rotation exists.3233Read `references/pytorch-forecasting-data-validation.md` before using custom panels, missing timesteps, lags, multiple targets, future covariates, or backtests.3435## Model Selection3637Use public model names exactly as documented. The main documented models are `Baseline`, `DeepAR`, `DecoderMLP`, `NBeats`, `NBeatsKAN`, `NHiTS`, `RecurrentNetwork`, `TemporalFusionTransformer`, `TiDEModel`, `TimeXer`, and `xLSTMTime`.3839Use `TemporalFusionTransformer` for rich covariates, multiple related series, quantile forecasts, and interpretation. Use `DeepAR` for autoregressive probabilistic regression and DeepVAR-style multivariate distribution loss. Use `NBeats` only for single-target regression without covariates. Use `NHiTS` for long horizons and covariates. Use `TimeXer` for exogenous-variable forecasting. Use `Baseline` as a last-value benchmark.4041The API index also lists v2/source classes such as `DLinear`, `Samformer`, `TFT`, and package-wrapper classes. Do not treat them as top-level stable imports unless the installed version and import path are verified. Read `references/pytorch-forecasting-model-map.md` before claiming support.4243## Training Pattern4445```python46import lightning.pytorch as pl47from lightning.pytorch.callbacks import EarlyStopping, LearningRateMonitor48from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer49from pytorch_forecasting.metrics import QuantileLoss5051training = TimeSeriesDataSet(52 train_df,53 time_idx="time_idx",54 target="y",55 group_ids=["series_id"],56 max_encoder_length=36,57 max_prediction_length=6,58 static_categoricals=["series_id"],59 time_varying_known_reals=["time_idx"],60 time_varying_unknown_reals=["y"],61)62validation = TimeSeriesDataSet.from_dataset(63 training, full_df, min_prediction_idx=validation_start_idx, stop_randomization=True64)6566train_loader = training.to_dataloader(train=True, batch_size=128, num_workers=2)67val_loader = validation.to_dataloader(train=False, batch_size=128, num_workers=2)6869model = TemporalFusionTransformer.from_dataset(70 training, loss=QuantileLoss(), learning_rate=0.03, hidden_size=3271)72trainer = pl.Trainer(73 max_epochs=100,74 accelerator="auto",75 callbacks=[EarlyStopping(monitor="val_loss", mode="min"), LearningRateMonitor()],76)77trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=val_loader)78```7980Use `Tuner(trainer).lr_find(...)` when tuning the learning rate, then recreate or update the model with the chosen rate.8182## Prediction, Evaluation, and Plotting8384- Load checkpoints with the model class' `load_from_checkpoint(...)` when needed.85- Predict with `model.predict(data_or_loader, mode="prediction"|"quantiles"|"raw", return_index=True, return_x=True)` as needed; verify mode support for the installed version/model.86- Evaluate on temporal validation/test windows only. Use a final test once; use rolling-origin or expanding-window backtests for model selection.87- Recommended metrics: `MAE`, `RMSE`, `MASE`, `SMAPE`, `MAPE` only when actuals are safely away from zero, `QuantileLoss` for quantiles, and distribution losses for probabilistic models.88- Use `plot_prediction()` for actual-vs-predicted plots. Use `predict_dependency()` and model-specific interpretation only on validation/test or documented sensitivity scenarios.89- For residual diagnostics, compute held-out residuals from predictions and actuals. PyTorch Forecasting does not document one universal residual diagnostics API.9091## Anti-Leakage Rules9293- Never random split rows or series windows across time. Split by temporal cutoffs, then construct datasets.94- Fit `TimeSeriesDataSet`, normalizers, scalers, categorical encoders, target transformations, hyperparameter tuning, and feature selection on train only or inside each backtest fold.95- Use `from_dataset()` for validation/test so train-fitted transformations are reused forward.96- Use `lags=` or manual `groupby(...).shift(lag)` only with past values; never rolling windows that include the forecast timestamp or future.97- Put future covariates in `time_varying_known_*` only if their values are known for every decoder step at prediction time.98- Keep `max_encoder_length`, `max_prediction_length`, `min_prediction_idx`, gap, frequency, and panel cutoffs aligned with the data-prep contract.99- Disable validation/test randomization with `stop_randomization=True`.100101## Common Errors102103- Passing datetimes directly as `time_idx` instead of an integer index.104- Building validation with a new `TimeSeriesDataSet(...)` and refitting scalers/encoders on validation/test.105- Putting observed future target drivers in `time_varying_known_*`.106- Using `NBeats` for covariate or multi-target tasks it does not document.107- Expecting neural models to work well on one short series without enough history.108- Treating `allow_missing_timesteps=True` as NA imputation.109- Ignoring version/import differences for v2 or API-indexed classes.110111## References112113- Read `references/pytorch-forecasting-model-map.md` for supported model names, model capabilities, and API caveats.114- Read `references/pytorch-forecasting-data-validation.md` for data formats, validation, covariates, lags, probabilistic forecasts, plotting, and diagnostics.115- Read `references/official-sources.md` for official sources consulted.116117## Ready Checklist118119- `forecasting-data-prep` contract is complete and anti-leakage risks are resolved or documented.120- `time_idx`, `target`, `group_ids`, covariate roles, horizon, and cutoffs are explicit.121- Train dataset owns fitted encoders/scalers/normalizers; validation/test are built with `from_dataset()`.122- Model choice supports the required covariates, multiple targets, probabilistic output, and horizon.123- Validation uses temporal cutoffs/backtesting; no random split.124- Metrics, plots, quantiles/intervals, and residual diagnostics are computed on held-out periods only.