Probabilistic Time Series Forecasting with 🤗 Transformers
An encoder-decoder Transformer can be trained as a global probabilistic forecaster that learns across many related time series and outputs a distribution (Student-t by default) rather than point estimates, with forecasts sampled autoregressively. The walkthrough trains Hugging Face's TimeSeriesTransformerForPrediction from scratch on the Monash tourism_monthly dataset (366 Australian regions, 24-month horizon). It uses GluonTS for lag features, time features, observed-value masks and instance splitting into context/prediction windows. Transformers suit forecasting because they handle missing values via attention masks and train with teacher forcing, but quadratic attention limits window sizes and they can overfit more easily.
Related posts
- Multivariate Probabilistic Time Series Forecasting with Informer
Hugging Face hardMasterInformer, the AAAI'21 best paper model now in Hugging Face Transformers, extends the vanilla Transformer for long-sequence time series forecasting with two changes: ProbSparse attention, which uses a query sparsity measurement on sampled keys to keep only the top c·log T "active" queries and cut attention cost from O(T²) to O(T log T), and a distilling step (Conv1d + ELU + MaxPool) that halves sequence length between encoder layers to reduce memory to O(N·T log T). Code sketches show both mechanisms. A walkthrough then trains InformerModel for multivariate probabilistic forecasting on the 862-series traffic_hourly dataset, using GluonTS's MultivariateGrouper to build the input and independent (diagonal) emission distributions on the output side.
- TimesFM-3: A zero-shot foundation model for multivariate forecasting
Google Research hardMasterTimesFM-3 is a 330M-parameter decoder-only transformer pre-trained on over 1 trillion real and synthetic time points that does zero-shot multivariate forecasting, supporting multiple targets, past-only covariates and known-future covariates. Series are split into 32-step patches with per-series normalization. Future-known covariates use lookahead tokens that concatenate the current patch with future patches. Layers alternate causal temporal attention within each series with full cross-variate attention at each time step. Contiguous Patch Masking replaces autoregressive patch-by-patch decoding, producing the whole horizon and 9 quantiles in one forward pass. It ranks first among pre-trained foundation models on Gift-Eval, FEV-Bench and Time against Chronos-2, Toto 2.0 and TimesFM-2.5, even in univariate mode, and is available on GitHub, Hugging Face and BigQuery's AI.FORECAST.
- Yes, Transformers are Effective for Time Series Forecasting (+ Autoformer)
Hugging Face hardMasterUnivariate Autoformer beats the DLinear linear model on Traffic (MASE 0.910 vs 0.965), Exchange-Rate (1.087 vs 1.690) and Electricity (0.751 vs 0.831) when compared at equivalent size and in the same setting, countering the AAAI 2023 claim that Transformers are ineffective for time series forecasting. Autoformer's two key ideas are a moving-average decomposition layer that splits series into trend and seasonal parts, and an FFT-based autocorrelation mechanism with time-delay aggregation that replaces dot-product self-attention at O(L log L) cost. PyTorch code is given for both. DLinear is just this decomposition layer feeding two linear layers, and it cannot incorporate covariates, which limits it compared with Transformers.
- Patch Time Series Transformer in Hugging Face
Hugging Face mediumAdeptPatchTST splits each univariate channel of a time series into subseries-level patches that act as Transformer tokens. All channels share the same weights, which makes it one global univariate model. Patching keeps local semantics, cuts attention cost quadratically through strides, and lets the model see a longer history. The walkthrough trains PatchTST with Hugging Face Transformers and IBM's tsfm preprocessing on the Electricity dataset, reaching a test MSE of 0.131, close to the paper's result. It then transfers the model to ETTh1 using zero-shot evaluation, linear probing and full fine-tuning.
- Train your first Decision Transformer
Hugging Face hardMasterDecision Transformers recast reinforcement learning as conditional sequence modeling: a GPT-2 model takes the last K timesteps of return-to-go, state and action tokens and autoregressively predicts actions that achieve a desired return. This walkthrough trains an offline Decision Transformer from scratch on the HalfCheetah expert dataset from the Hugging Face hub. A custom data collator normalizes states, computes returns-to-go, scales rewards by 1000, pads sequences and samples trajectories by length. A subclassed model adds an MSE action loss so the standard transformers Trainer can be used, and training takes about 40 minutes in Colab versus roughly 3 hours in the original paper.