Search

Search papers and lessons

A decoder-only foundation model for time-series forecasting

Abhimanyu Das, Weihao Kong, Rajat Sen et al. · 2023

TimesFM is a 200M-parameter Transformer pretrained on public and synthetic time series, so one model forecasts new datasets zero-shot, with no training on them, at close to supervised accuracy.

On this page

  • Colour = topic mix
  • Probability
  • Architectures
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

TimesFM is a time-series foundation model: one Transformer, pretrained once on a large corpus of time series, that forecasts datasets it has never seen without any training on them. The paper's claim is that its zero-shot forecasts come close to the accuracy of models trained separately for each dataset, and sometimes match them. The model reads patches of the series as tokens, predicts the next patch in one shot, and is far smaller than the language models it takes its inspiration from.

Builds on: A Time Series is Worth 64 Words: Long-term Forecasting with Transformers, Time-Series Patching, Transformer, Autoregressive Model · Leads to: a line of pretrained forecasters that are compared against this zero-shot baseline, and the question of whether such models need covariates and probabilistic outputs

Why it mattered

Most forecasting systems train one model per series or per dataset. A retailer, a grid operator or a hospital pharmacy then needs a data pipeline, training runs and tuning for each new series, which is slow and expensive. The paper asks whether a model pretrained on enough varied series can simply be used on a new one, the way a large language model is used on new text. Its answer is yes, at least on the public benchmarks it tests, and that result pushed pretrained forecasting into the same conversation as pretrained language models. Its design choices, patching and output patches longer than input patches, are the ones to compare against in later work.

The problem

Forecasting models face a problem that language models do not. Text has a fixed vocabulary and grammar, while a time series has no shared vocabulary: a sales curve, a river gauge and a heart rate have different scales, sampling intervals and seasonal periods. A foundation model also needs to accept any history length (the context), any horizon, and any granularity from minutes to years.

Data is the second obstacle. Language models train on huge volumes of public text, but there is no equally large public archive of time series with that diversity. The paper's first contribution is therefore a training corpus built from real and synthetic sources, and its second is an architecture that can be pretrained on that corpus efficiently.

Two prior routes did not solve this. Local models such as ARIMA and Prophet are fitted to each series. Global deep models such as DeepAR and N-BEATS are trained on many series, but they are still supervised, and they are used on the same datasets they were trained on, or fine-tuned on new ones. Work that used large language models directly, such as GPT-3 or LLaMA-2 as zero-shot forecasters, was reported to be much weaker than the paper's approach, even though those models are far larger.

How it works

The model turns a context into patches, runs a decoder-only Transformer over the patches, and writes the next output patch. The diagram shows one pass.

flowchart LR
  A[Context of raw values] --> B[Split into input patches]
  B --> C[Residual block turns each patch into a token]
  C --> D[Add positional encoding]
  D --> E[Causal Transformer layers]
  E --> F[Residual block writes the next output patch]
  F --> G[Feed predictions back for longer horizons]

Take a tiny instance: a context of 12 hourly readings, input patches of 4, output patches of 8, and a forecast of 16 readings. Every number is small enough to check by hand.

  1. Cut the context into patches. The 12 readings become 3 contiguous patches of 4: readings 1 to 4, 5 to 8, and 9 to 12. Check: 12 readings divided by patch length 4 gives 3 tokens.

  2. Turn each patch into a token. A residual block (a small multilayer perceptron with a skip connection) maps each 4-value patch to a vector of the model width. The vector gets a positional encoding that marks its slot in the sequence. Check: the sequence the Transformer sees is 3 tokens long, not 12.

  3. Let each token see only the past. Causal attention lets token 2 read tokens 1 and 2, and token 3 read tokens 1, 2 and 3, but never a later token. The Transformer computes all three outputs in one parallel pass, the way a language model does during training. Check: the output for token 1 depends only on readings 1 to 4.

  4. Predict the next 8 readings from each token. Output token 3 is mapped by an output residual block to readings 13 to 20, the next output patch. Each token predicts a whole block, not one value. Check: 8 predicted values, the next 8 readings after the 12 seen.

  5. Extend the horizon by feeding predictions back. To forecast 16 readings, the model first writes readings 13 to 20, appends them to the context, and then writes readings 21 to 28. The forecast the user needs is 16 readings, so 2 passes of 8 cover it. Check: 16 divided by 8 is 2 passes. With output patches of 4 it would take 4 passes.

  6. Train on all positions at once. The loss is the mean squared error between each predicted patch and the readings that actually followed it, averaged over every token in the batch. Check: a token that predicts 8 values contributes 8 squared errors, and the batch averages over all of them.

The one non-standard part of training is the masking. For each series, the training code draws a random number rr between 0 and p−1p - 1 and hides the first rr readings of the first patch. That single trick lets one batch teach the model to forecast from every context length from 1 up to the maximum, not only from multiples of the patch length.

The math

The task is a map from a context to a horizon. Let y1:L\mathbf{y}_{1:L} be the context, HH the horizon, and y^L+1:L+H\hat{\mathbf{y}}_{L+1:L+H} the forecast. The model is a function ff with

f(y1:L)→y^L+1:L+H.f(\mathbf{y}_{1:L}) \rightarrow \hat{\mathbf{y}}_{L+1:L+H}.

Step 1 in symbols: patches

With input patch length pp, the jj-th patch is y~j=yp(j−1)+1:pj\tilde{\mathbf{y}}_j = \mathbf{y}_{p(j-1)+1:pj}, and a context of length LL gives N=⌊L/p⌋N = \lfloor L / p \rfloor tokens.

Step 2 in symbols: tokens

Each patch, multiplied element by element (⊙\odot) by a mask (1−m~j)(1 - \tilde{\mathbf{m}}_j) that zeroes masked readings, goes through the input residual block and then gets a positional encoding:

tj=InputResidualBlock(y~j⊙(1−m~j))+PEj.\mathbf{t}_j = \texttt{InputResidualBlock}\left(\tilde{\mathbf{y}}_j \odot (1 - \tilde{\mathbf{m}}_j)\right) + \mathrm{PE}_j.

Step 3 in symbols: causal attention

The Transformer output at token jj depends only on tokens up to jj:

oj=StackedTransformer((t1,m˙1),…,(tj,m˙j)),\mathbf{o}_j = \texttt{StackedTransformer}\left((\mathbf{t}_1, \dot{m}_1), \ldots, (\mathbf{t}_j, \dot{m}_j)\right),

where m˙j\dot{m}_j is 1 when every reading in patch jj is masked, so that fully masked patches are not attended to.

Step 4 in symbols: output patches

y^pj+1:pj+h=OutputResidualBlock(oj),\hat{\mathbf{y}}_{pj+1:pj+h} = \texttt{OutputResidualBlock}(\mathbf{o}_j),

where hh is the output patch length. The paper lets hh be longer than pp; that is what makes the step-count arithmetic in the examples work.

Step 6 in symbols: training loss

TrainLoss=1N∑j=1NMSE(y^pj+1:pj+h,ypj+1:pj+h).\texttt{TrainLoss} = \frac{1}{N} \sum_{j=1}^{N} \mathrm{MSE}\left(\hat{\mathbf{y}}_{pj+1:pj+h}, \mathbf{y}_{pj+1:pj+h}\right).

Worked examples

Example
1. Tiny: what the masking sees

Suppose the maximum context is 512 and the input patch length is p=32p = 32.

  1. Sample r=4r = 4, so the first 4 readings of the first patch are hidden.
  2. The first output token now sees 32−4=2832 - 4 = 28 readings, not 32.
  3. The second output token sees the next patch in full, so it sees 28+32=6028 + 32 = 60 readings.
  4. Repeat for every rr from 0 to 31. The first output token then covers context lengths from 1 to 32, and the later tokens cover the rest.

Interpretation: the model is trained on every context length it will meet at inference, even though its patches are 32 long. Without the random mask, it would learn only the multiples of 32.

Example
2. Standard: steps needed for a 256-step horizon

Forecast 256 future readings with a context of 256.

  1. With output patches of 32, each pass writes 32 readings, so the model needs 256/32=8256 / 32 = 8 passes.
  2. With output patches of 128, each pass writes 128 readings, so it needs 256/128=2256 / 128 = 2 passes.
  3. Each pass feeds its predictions back as context, so every extra pass is another chance for error to build up.

Interpretation: a longer output patch cuts the passes from 8 to 2, which is why the paper trains with outputs longer than inputs.

Example
3. Realistic: token count on a 512-step context

Compare two input patch lengths on the same 512-reading context. The cost of self-attention grows with the square of the token count.

  1. With p=32p = 32: 512/32=16512 / 32 = 16 tokens. Attention compares every pair, so 162=25616^2 = 256 pairs.
  2. With p=8p = 8: 512/8=64512 / 8 = 64 tokens, so 642=4,09664^2 = 4{,}096 pairs.
  3. Ratio: 4,096/256=164{,}096 / 256 = 16. The shorter patch needs sixteen times the attention work.
  4. The paper reports that p=32p = 32 trains almost twice as fast as p=16p = 16, and that p=16p = 16 and p=32p = 32 give the best accuracy.

Interpretation: patching is what makes a long context affordable, and the patch length trades speed against accuracy. The paper picks 32 as the practical choice.

Try it: a 1,024-reading context with p=32p = 32 gives how many tokens, and how many pairs does attention compare? Answer: 32 tokens, and 322=1,02432^2 = 1{,}024 pairs.

Results

The paper evaluates zero-shot on three public groups that were held out from pretraining. The Monash archive has 30 datasets; filtering out those with missing values leaves 18, scored by mean MAE scaled by a naive last-value baseline and averaged with a geometric mean. TimesFM is the top model there. It is slightly better than N-BEATS, within statistical significance, it beats the supervised DeepAR, and it improves on the zero-shot LLMTime (GPT-3 with prompting) by more than 25 percent.

The Darts group has 8 univariate series. TimesFM is within statistical significance of the best methods, which are LLMTime and seasonal ARIMA. The paper notes that each series is a single time series, so classical methods are competitive, and that LLMTime may have seen these series in training, which it cannot rule out.

On four ETT datasets (ETTh1, ETTh2, ETTm1, ETTm2) at horizons 96 and 192, averaged over 8 tasks, TimesFM and the supervised PatchTST are the best, and the other long-horizon methods are clearly worse. The evaluation uses the last test window to keep the cost of LLM baselines manageable, so it is a narrower test than rolling validation.

Fine-tuning gives a separate result. With 10 percent of the ETT training data, the fine-tuned model's average MAE on ETTh1 is 0.426, against 0.525 for a GPT-2-based model (GPT4TS), 0.542 for PatchTST and 0.600 for DLinear. This is fine-tuned, not zero-shot, so it answers a different question.

Ablations add detail. Error falls steadily as training compute grows across model sizes of 17M, 70M and 200M parameters. Removing the synthetic data makes almost no difference on the hourly ETTh datasets, but it hurts on the 15-minute ETTm datasets and on Monash, where granularities such as quarterly and yearly are underrepresented.

Limits and tradeoffs

It is tempting to think a foundation model is always the best first choice. In fact the paper's gains are on public benchmarks, and on several of them the model is only within statistical significance of the best supervised method; a dataset-specific model with proper tuning can still win, and the paper's own fine-tuning results show the value of adapting the model.

It is tempting to think a longer output patch is always better. In fact the paper notes that a long output patch is hard for series shorter than the patch, such as monthly or yearly data, so the output length has to be chosen against the granularities in the corpus.

It is tempting to think covariates such as prices or weather are needed for good forecasts. The paper's model uses none, because large corpora with meaningful covariates are hard to find. It lists covariates and probabilistic output as open problems, and it describes simple ways to add covariates later.

ChoiceBenefitCost
Longer output patchFewer autoregressive passesHard for series shorter than the patch
Shorter input patchMore tokens, finer detailAttention cost grows with the square of the token count
Synthetic data in the mixCovers rare granularitiesExtra data to generate and mix
Decoder-only trainingOne pass trains every positionNeeds masking to cover all context lengths

Reading guide

Check yourself

QuestionWhat is the difference between zero-shot and supervised forecasting in this paper?Show answer

A supervised model is trained or tuned on the dataset it forecasts. A zero-shot model, such as TimesFM on the Monash, Darts and ETT benchmarks, is pretrained on other data and used on these datasets without any training on them. The paper's claim is that the zero-shot model comes close to supervised models that were trained for each dataset.

QuestionWith a context of 512 and input patch length 16, how many tokens does the Transformer see, and how does that compare with patch length 32?Show answer

With patch length 16, there are 512/16=32512 / 16 = 32 tokens. With patch length 32, there are 512/32=16512 / 32 = 16 tokens. The Transformer sees twice as many tokens with the shorter patch, so its attention costs about four times as much, since attention cost grows with the square of the token count.

QuestionA model with output patch 128 must forecast 384 readings. How many passes does it need, and what would change if the output patch were 32?Show answer

It needs 384/128=3384 / 128 = 3 passes. With output patch 32 it would need 384/32=12384 / 32 = 12 passes. Each extra pass feeds predictions back as context, so the shorter output patch gives more chances for errors to build up.

QuestionWhy does the paper add random masking of the first patch during training?Show answer

Because the model must forecast from any context length at inference, but patches are a fixed length. Without masking, it would only see context lengths that are multiples of the patch length. Hiding a random number of readings in the first patch lets every context length from 1 up to the maximum appear in training, so one batch covers them all.

Sources

  • Das, A., Kong, W., Sen, R., Zhou, Y. A decoder-only foundation model for time-series forecasting. arXiv:2310.10688 (2023). https://arxiv.org/abs/2310.10688
  • Nie, Y., Nguyen, N. H., Sinthong, P., Kalagnanam, J. A Time Series is Worth 64 Words: Long-term Forecasting with Transformers. arXiv:2211.14730 (2022). https://arxiv.org/abs/2211.14730

Read the full paper

Abhimanyu Das, Weihao Kong, Rajat Sen et al., 2023. arXiv:2310.10688