Skip to content

Patchtst

polars_ts.dl.patchtst

PatchTST: Patch-based Transformer for Time Series.

Implements a channel-independent PatchTST forecaster (Nie et al., ICLR 2023). The input is split into fixed-length patches, each projected to a token embedding, then processed by a standard transformer encoder. The final representation is linearly mapped to the forecast horizon.

References

Nie et al. (2023). A Time Series is Worth 64 Words: Long-term Forecasting with Transformers. ICLR.

_PatchTSTNet

Bases: Module

PatchTST network.

forward(x)

Forward pass.

Parameters

x : torch.Tensor Input of shape (batch, input_size).

Returns

torch.Tensor Forecast of shape (batch, h).

PatchTSTForecaster

PatchTST time series forecaster.

Parameters

h Forecast horizon. input_size Lookback window size. Must be divisible by patch_len. patch_len Length of each patch. d_model Transformer embedding dimension. n_heads Number of attention heads. n_layers Number of transformer encoder layers. d_ff Feedforward dimension. Defaults to 4 * d_model. dropout Dropout rate. max_epochs Maximum training epochs. lr Learning rate. batch_size Training batch size. id_col, time_col, target_col Column names.

fit(df)

Train the PatchTST model on historical data.

Parameters

df Panel DataFrame with historical observations.

Returns

PatchTSTForecaster Self, for chaining.

predict(df)

Generate forecasts for each series.

Parameters

df Panel DataFrame (uses last input_size observations per series).

Returns

pl.DataFrame Columns: [id_col, time_col, y_hat].