Llm forecast
polars_ts.adapters.llm_forecast
LLM-based forecasting adapters (Time-LLM, LLM-PS).
Reprograms LLMs for time series forecasting by converting temporal patterns into representations that pre-trained language models can process.
Time-LLM: Patches input series into tokens, projects via cross-attention with text prototypes, then decodes forecasts through a frozen LLM backbone.
LLM-PS (Pattern & Semantic): Multi-scale CNN extracts local patterns at different granularities, then an LLM backbone reasons over the combined multi-scale representation.
References
Time-LLM (Jin et al., ICLR 2024). LLM-PS (2025). arXiv:2503.09656.
_TimeLLMNet
Bases: Module
Simplified Time-LLM: patch embedding → cross-attention → MLP decoder.
Converts time series patches into a sequence of tokens, applies learned cross-attention with text-like prototypes, then decodes forecasts.
forward(x)
Forward pass.
Parameters
x : Tensor of shape (batch, input_size)
Returns
Tensor of shape (batch, h)
_LLMPSNet
Bases: Module
LLM-PS: multi-scale CNN pattern extraction + MLP semantic decoder.
Extracts patterns at multiple temporal scales via parallel convolutions, concatenates the multi-scale features, and decodes forecasts.
forward(x)
Forward pass.
Parameters
x : Tensor of shape (batch, input_size)
Returns
Tensor of shape (batch, h)
TimeLLMForecaster
Time-LLM forecaster: patch → cross-attention with prototypes → decode.
Parameters
h Forecast horizon. input_size Lookback window length. patch_len Length of each input patch. d_model Hidden dimension. n_heads Number of attention heads. n_prototypes Number of learnable text-like prototypes. lr Learning rate. max_epochs Maximum training epochs. id_col, time_col, target_col Column names.
fit(df)
Train the Time-LLM model.
predict(df)
Generate forecasts for each series.
LLMPSForecaster
LLM-PS forecaster: multi-scale CNN pattern extraction → decode.
Parameters
h Forecast horizon. input_size Lookback window length. kernel_sizes CNN kernel sizes for multi-scale pattern extraction. d_model Hidden dimension. lr Learning rate. max_epochs Maximum training epochs. id_col, time_col, target_col Column names.
fit(df)
Train the LLM-PS model.
predict(df)
Generate forecasts for each series.