Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Every moment looks at every other
A transformer reads a whole stretch of history at once. For each part of it, attention asks which other parts are most useful, for example the same hour yesterday, and mixes their information. To keep the cost down, the series is cut into short pieces called patches, like words in a sentence. Because attention by itself ignores order, each patch also gets a label saying where it sits.
In 2023 a paper titled ‘Are transformers effective for time series forecasting?’ showed that a single linear layer beat many published transformer forecasters on standard benchmarks.
- 1. Attention lets each part of the window…
- 2. Patches are…
- 3. Attention by itself ignores…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Self-attention: queries Q = XW_q, keys K = XW_k and values V = XW_v; output softmax(QKᵀ/√d)V. Every token mixes information from all tokens.
- Permutation equivariance: without positional encodings, permuting the input tokens just permutes the outputs. Order must be injected with sinusoidal or learned position embeddings.
- Point tokens versus patches: one token per time step costs O(L²). PatchTST (2023) uses patches of 8–16 steps as tokens, which is cheaper and gives better local semantics.
- Channel independence: many models treat each variable of a multivariate series separately with shared weights.
- Instance normalisation (RevIN): remove each window’s mean (and scale) before the model and add it back after, to cope with level shifts.
- Model zoo: Informer, Autoformer and FEDformer (efficient attention, decomposition), PatchTST, iTransformer (variables as tokens), Temporal Fusion Transformer (interpretable, with covariates).
- 1. Scaled dot-product attention divides scores by…
- 2. PatchTST uses as tokens…
- 3. RevIN normalises…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.A patch transformer in NumPy
Exercise 1.
- A window of 96 values cut into 8 patches of 12, each embedded linearly to d = 16, plus learned positions.
- One single-head self-attention layer with a residual connection, then a flattening linear head to 24 forecasts.
- The window mean is removed before the model and added back after.
- A gradient check, and a permutation test with and without positional embeddings.
Practice.
- Always include linear and seasonal-naive baselines on the same window and horizon.
- Use patching and instance normalisation; they matter more than the attention variant.
- Tune the lookback window: longer windows help linear models and patch transformers but hurt point-token transformers.
- Prefer pretrained or global models when individual series are short.
- Tools: NeuralForecast (PatchTST, iTransformer, TFT), PyTorch Forecasting (TFT), Hugging Face transformers time-series models.
import numpy as np
def attention(Q, K, V):
S = Q @ K.T / np.sqrt(Q.shape[-1])
A = np.exp(S - S.max(-1, keepdims=True))
A /= A.sum(-1, keepdims=True)
return A @ V- 1. A transformer forecaster should be compared with…
- 2. Which matters a lot for transformer forecasters?
- 3. A model with interpretable attention over covariates:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Equivariance. If the tokens are permuted by π, then Q, K and V are permuted by π. The score matrix is permuted on both rows and columns, and the softmax output is permuted the same way. Each token therefore gets the same output as before, just in a new place. In Exercise 1, swapping the first two patches swapped their attention outputs exactly. Learned positional embeddings broke the symmetry. Here the flattening head is also position-specific, which is one reason PatchTST uses it.
Backpropagation through softmax. For A = softmax(S), the gradient is dS = A ⊙ (dA − Σ_j A_j dA_j) row by row. Gradients then flow to Q and K through S = QKᵀ/√d, and to V through AV. The hand-written gradients matched finite differences to below 10⁻⁶.
Why linear models compete. A linear map from a 96-value window to 24 future values can represent any fixed combination of seasonal patterns. That covers most of the predictable structure in many benchmark series. Attention adds the ability to weight history depending on the content, which helps only when such patterns exist and there is enough data to learn them.
- 1. Permuting tokens without positions…
- 2. The softmax backward pass is…
- 3. A linear window-to-horizon map can represent…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.On a series with two seasonal cycles, a regime-dependent nonlinearity and a drifting level, does a patch transformer beat a ridge linear forecaster, with little or with more training data?
History.
- A. Vaswani et al. (2017) introduced the transformer.
- S. Li et al. (2019) applied it to forecasting; Informer (2021) made it efficient for long windows; Autoformer (2021) and FEDformer (2022) added decomposition and frequency attention.
- A. Zeng et al. (2023) showed that a one-layer linear model (DLinear) beat many of them.
- Y. Nie et al. (2023) answered with PatchTST, using patches and channel independence, and Y. Liu et al. (2024) proposed iTransformer.
Exercise 2.
- The series combines cycles of period 24 (amplitude drifting over 900 steps, clipped while the 60-step cycle is high) and 60, a random-walk level and noise.
- Forecast 24 steps from 96, test on the last 1000 points, and train on the preceding 400 or 2400 points.
- Models: seasonal naive (repeat the last 24); ridge regression from the mean-removed window to the horizon; and the one-layer patch transformer (Adam, 70 epochs for the small set and 20 for the large).
Seed 0, test MSE:
| training points | seasonal naive | linear | patch transformer |
|---|---|---|---|
| 400 | 1.4 | 0.11 | 0.12 |
| 2400 | 1.4 | 0.093 | 0.085 |
- The transformer and the linear model were within about 10% of each other. Linear was slightly better with little data in all three seeds; with more data neither won consistently.
- Both were 11–16 times better than seasonal naive, which ignores the 60-step cycle.
- More data helped both.
- Seeds 1 and 2: with 400 points, linear 0.095 and 0.1 against transformer 0.1 and 0.11. With 2400 points, linear 0.079 and 0.08 against transformer 0.076 and 0.083.
Transformers for time series in practice
- Benchmarks: on standard long-horizon datasets (ETT, Electricity, Traffic, Weather), PatchTST and iTransformer are strong, but tuned linear and MLP models (DLinear, TiDE) are close.
- Covariates and interpretability: the Temporal Fusion Transformer handles static and known-future inputs and shows which inputs mattered.
- Foundation models: large transformers pretrained on many series (TimesFM, Chronos, Moirai) forecast zero-shot; see the next topic.
- Lesson: architecture matters less than normalisation, the lookback window, honest baselines and evaluation.
References
- A. Zeng, M. Chen, L. Zhang and Q. Xu (2023). “Are transformers effective for time series forecasting?” AAAI.
- Y. Nie, N. H. Nguyen, P. Sinthong and J. Kalagnanam (2023). “A time series is worth 64 words: long-term forecasting with transformers.” ICLR.
- H. Zhou et al. (2021). “Informer: beyond efficient transformer for long sequence time-series forecasting.” AAAI.
- B. Lim, S. Ö. Arık, N. Loeff and T. Pfister (2021). “Temporal Fusion Transformers for interpretable multi-horizon time series forecasting.” International Journal of Forecasting.
- Y. Liu et al. (2024). “iTransformer: inverted transformers are effective for time series forecasting.” ICLR.