pip install pytorch-forecasting★Pulls in PyTorch Lightning. GPU strongly recommended for the transformer models.# flow: DataFrame -> TimeSeriesDataSet -> model.from_dataset -> Trainer.fit -> predict★Input is a long-format DataFrame: one row per (series, timestep), with a numerictime_idx.from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformerPlusimport lightning.pytorch as plfor the Trainer.
training = TimeSeriesDataSet( df[df.time_idx <= cutoff], time_idx="time_idx", target="sales", group_ids=["store", "item"], max_encoder_length=60, max_prediction_length=14)★The essentials:time_idx(int),target,group_ids(what identifies each series), and the encoder (lookback) & prediction (horizon) window lengths.static_categoricals=["store"], time_varying_known_reals=["time_idx", "price"], time_varying_unknown_reals=["sales"]★Known covariates are available in the future (calendar, planned price); unknown are only observed up to now (the target, sensors). Getting this split right is critical.target_normalizer=GroupNormalizer(groups=["store","item"])★Normalize the target per series so a big store doesn't dominate — scaling is inverted automatically at predict time.static_reals=..., time_varying_known_categoricals=..., add_relative_time_idx=TrueExtra feature buckets + convenience flags (add_target_scales,add_encoder_length).
validation = TimeSeriesDataSet.from_dataset(training, df, predict=True, stop_randomization=True)★Derive val/test from the training set so encoders/normalizers match exactly.predict=True= one forecast per series from the latest point.train_dl = training.to_dataloader(train=True, batch_size=128, num_workers=4) val_dl = validation.to_dataloader(train=False, batch_size=256)★Turn datasets into PyTorch dataloaders for the Trainer.