Forecaster
LagLlamaForecaster
LagLlama Foundation Model for Time Series Forecasting.
Quickstart
python
from sktime.forecasting.lagllama import LagLlamaForecaster
estimator = LagLlamaForecaster(ckpt_path=None, device=None, context_length=32, num_samples=100, batch_size=1, use_rope_scaling=False, nonnegative_pred_samples=False, use_source_package=False, validation_split=0.2, trainer_kwargs=None, lr=0.0005, aug_prob=0.0)Parameters(12)
- ckpt_pathstr, optional (default=None)
- Path to LagLlama checkpoint file. If None, automatically downloads from HuggingFace: “time-series-foundation-models/Lag-Llama”.
- devicestr, optional (default=None)
- Device for inference (“cpu”, “cuda”, “cuda:0”, etc.). If None, uses CUDA if available, otherwise CPU.
- context_lengthint, optional (default=32)
- Number of past time steps used as context for prediction. LagLlama was trained with context_length=32.
- num_samplesint, optional (default=100)
- Number of sample paths for probabilistic forecasting.
- batch_sizeint, optional (default=1)
- Batch size for prediction.
- use_rope_scalingbool, optional (default=False)
- Whether to use RoPE scaling for handling longer context lengths.
- nonnegative_pred_samplesbool, optional (default=False)
- If True, ensures all predicted samples are passed through ReLU.
- use_source_packagebool, optional (default=False)
- If True, uses the external lag-llama package instead of vendored version.
- validation_splitfloat, optional (default=0.2)
- Fraction of data for validation during pretrain(). Set to None to skip validation.
- trainer_kwargsdict, optional (default=None)
Arguments passed to PyTorch Lightning Trainer during pretrain() (e.g.,
{"max_epochs": 10}). If None, defaults to{"max_epochs": 50}.- lrfloat, optional (default=5e-4)
- Learning rate for fine-tuning during pretrain().
- aug_probfloat, optional (default=0.0)
- Data augmentation probability during pretrain().
Examples
Zero-shot forecasting (default)
>>> from sktime.forecasting.lagllama import LagLlamaForecaster
>>> from sktime.forecasting.base import ForecastingHorizon
>>> from sktime.datasets import load_airline
>>>
>>> y = load_airline ()
>>> forecaster = LagLlamaForecaster (
... context_length = 32,
... num_samples = 100
... )
>>> fh = ForecastingHorizon ([1, 2, 3, 4, 5, 6 ])
>>> forecaster. fit (y, fh = fh) LagLlamaForecaster(
... )
>>> y_pred = forecaster. predict () # Point predictions
>>> # 90% prediction intervals
>>> y_interval = forecaster. predict_interval (coverage = 0.9) Fine-tuning with pretrain() on panel data
>>> from sktime.forecasting.lagllama import LagLlamaForecaster
>>> from sktime.datasets import load_airline
>>> from sktime.utils._testing.hierarchical import (
... _make_hierarchical,
... )
>>>
>>> # Create panel data for pretraining
>>> y_panel = _make_hierarchical (
... hierarchy_levels = (3,), min_timepoints = 50, max_timepoints = 50
... )
>>> # Fine-tune on panel data
>>> forecaster = LagLlamaForecaster (
... context_length = 32,
... num_samples = 100,
... trainer_kwargs = { "max_epochs": 5 },
... lr = 5e-4,
... validation_split = 0.2
... )
>>> forecaster. pretrain (y_panel) # Fine-tune on panel LagLlamaForecaster(
... )
>>> # Now fit to specific series and predict
>>> y = load_airline ()
>>> forecaster. fit (y, fh = [1, 2, 3, 4, 5, 6 ]) LagLlamaForecaster(
... )
>>> y_pred = forecaster. predict ()References
- [1 ] Rasul, Kashif, et al. “Lag-Llama: Towards Foundation Models for Probabilistic Time Series Forecasting.” arXiv preprint arXiv:2310.08278 (2023).