Search

Search papers and lessons

Autoformer: Decomposition Transformers with Auto-Correlation

Haixu Wu, Jiehui Xu, Jianmin Wang et al. · 2021

Autoformer forecasts long horizons by splitting off trends inside every layer and matching whole periodic sub-series, cutting mean squared error by 38% on average across six benchmarks.

On this page

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

TL;DR

Autoformer is a Transformer built for forecasting far into the future. It makes two changes to the standard design. First, it splits every layer's output into a slowly moving trend and a repeating seasonal part, so the network refines the trend as it goes instead of removing it once before training. Second, it replaces point-by-point self-attention with an Auto-Correlation step that finds the periods in the series and compares whole sub-series at those lags. Across six benchmarks, its prediction error was 38% lower on average than the strongest prior models.

Builds on: Transformer, Time Series Decomposition, Autocorrelation Function · Leads to: Time Series Forecasting

Why it mattered

Forecasting tasks such as weather early warning, grid planning and disease tracking need predictions many steps ahead, not just the next value. By 2021, Transformers had become the default for sequences, but applying them to long series was costly and often inaccurate. Informer and LogTrans made attention cheaper by looking at only some time points, and they still compared single points at a time.

Autoformer changed the question. It treated decomposition as a block inside the network rather than a preprocessing step, and it treated periodicity as the structure that links past and future. The paper's benchmarks cover electricity, traffic, exchange rates, weather and influenza, which made the claim concrete across domains. Later forecasting papers often report Autoformer as a baseline, which is one sign of how widely its benchmark set and its numbers were adopted.

The problem

Three obstacles stood in the way of accurate long-term forecasts, and the paper names each.

First, the patterns in a long series are tangled together. A trend, a daily cycle and a weekly cycle overlap, so a model that reads raw values directly has trouble finding reliable dependencies.

Second, self-attention compares every pair of time points, so its cost grows with the square of the series length. Sparse versions reduce the cost, but they keep the point-by-point comparison, so the model sees only a few points at a time and wastes information from the rest.

Third, decomposing a series is standard in time series analysis, but in forecasting it has been used only on the past. The future is unknown, so the trend of the forecast cannot be extracted directly, and the interaction between trend and seasonality in the future is lost.

How it works

The walkthrough has two parts, traced on tiny series with round numbers. Part A shows the decomposition, and part B shows how Auto-Correlation finds a period and aggregates sub-series. The full model stacks these blocks in an encoder and a decoder.

Part A: decomposition on a rising line.

  1. Take a short input. Let the series be 2,4,6,8,102, 4, 6, 8, 10, a straight line rising by 2 per step.
  2. Smooth with a moving average. Use a window of three points. At the middle three positions the averages are (2+4+6)/3=4(2+4+6)/3 = 4, (4+6+8)/3=6(4+6+8)/3 = 6 and (6+8+10)/3=8(6+8+10)/3 = 8. Check: each average is the middle value of its window, because the line is straight.
  3. Subtract the trend. The seasonal part is the series minus the trend: 4−4=04 - 4 = 0, 6−6=06 - 6 = 0 and 8−8=08 - 8 = 0 at the middle positions. Check: a straight line has no seasonal part, so every residual is zero.
  4. Keep the split. The block returns the trend and the seasonal part. In Autoformer, the seasonal part goes on through the network, and the trend is kept aside and added back at the end.

Part B: Auto-Correlation on a repeating series.

  1. Measure the lagged similarity. Let the series be 1,−1,1,−1,1,−1,1,−11, -1, 1, -1, 1, -1, 1, -1, which repeats every two steps. For each lag τ\tau, multiply each value by the value τ\tau steps earlier, add the products and divide by the length L=8L = 8. At lag 1 the products sum to −7-7, at lag 2 to 66, at lag 3 to −5-5 and at lag 4 to 44. Dividing by 8 gives R(1)=−0.88R(1) = -0.88, R(2)=0.75R(2) = 0.75, R(3)=−0.63R(3) = -0.63 and R(4)=0.50R(4) = 0.50. Check: lag 2 is highest, because shifting by two steps lines up identical values.
  2. Pick the top lags. The number of lags kept is k=⌊clog⁡L⌋k = \lfloor c \log L \rfloor, with the natural logarithm. With c=1c = 1 and L=8L = 8, this gives ⌊2.08⌋=2\lfloor 2.08 \rfloor = 2. The two largest values are at lags 2 and 4 (0.75 and 0.50), so these are the candidate periods.
  3. Turn the scores into weights. A softmax over the two scores 0.750.75 and 0.500.50 gives weights of about 0.560.56 and 0.440.44. Check: the weights add to 1, and the larger score gets the larger weight.
  4. Aggregate rolled copies. Roll the series by each chosen lag, that is, shift it with wrap-around, and add the rolled copies with their weights. Here shifting by 2 or by 4 returns the same pattern, so the output equals the input. This is the point of the step: sub-series at the same phase of each period are summed together, so repeated structure is reinforced rather than averaged away.
flowchart TB
  A["Past series"] --> B["Decomposition: seasonal and trend"]
  B --> C["Encoder layers: Auto-Correlation, decomposition, feed-forward"]
  C --> D["Decoder: seasonal refinement with Auto-Correlation"]
  C --> D
  B --> E["Trend part, copied from the last half of the input"]
  E --> F["Trend accumulated across decoder layers"]
  D --> G["Forecast = seasonal output + accumulated trend"]
  F --> G

The decoder starts from the second half of the input's seasonal part, padded with zeros for the future, and the trend part padded with the mean. This is why the decomposition is done on hidden states during the forecast, not on the raw future, which is unknown.

012345−1−0.500.51lag τ (steps)autocorrelation R(τ)
  • R(τ), period-2 series
Autocorrelation of the repeating series. The peaks at lags 2 and 4 are the candidate periods that Auto-Correlation keeps and weights.

The math

Definition: Auto-Correlation mechanism

Given queries Q\mathcal{Q}, keys K\mathcal{K} and values V\mathcal{V} for a series of length LL, the mechanism picks the kk lags τ1,…,τk\tau_1, \ldots, \tau_k with the largest cross-correlation RQ,K(τ)\mathcal{R}_{\mathcal{Q},\mathcal{K}}(\tau), weights them by a softmax over those scores, and returns the weighted sum of the values rolled by each lag:

AutoCorrelation(Q,K,V)=∑i=1kRoll(V,τi) R^Q,K(τi)\mathrm{AutoCorrelation}(\mathcal{Q}, \mathcal{K}, \mathcal{V}) = \sum_{i=1}^{k} \mathrm{Roll}(\mathcal{V}, \tau_i)\, \widehat{\mathcal{R}}_{\mathcal{Q},\mathcal{K}}(\tau_i)

Here Roll(V,τ)\mathrm{Roll}(\mathcal{V}, \tau) shifts the series forward by τ\tau steps and moves the values pushed past the end back to the start. R^\widehat{\mathcal{R}} is the softmax-normalized score, so the weights sum to 1.

Step 1 in symbols: the decomposition

The moving average AvgPool\mathrm{AvgPool}, applied after padding so that the length stays LL, gives the trend Xt\mathcal{X}_t, and the seasonal part is what is left:

Xt=AvgPool(Padding(X)),Xs=X−Xt\mathcal{X}_t = \mathrm{AvgPool}(\mathrm{Padding}(\mathcal{X})), \qquad \mathcal{X}_s = \mathcal{X} - \mathcal{X}_t

In part A of the walkthrough, the trend at the middle positions is 44, 66 and 88, and the seasonal part is zero.

Step 2 in symbols: lagged similarity

The autocorrelation of a process at lag τ\tau is the average product of each value with the value τ\tau steps before it:

R(τ)=lim⁡L→∞1L∑t=1LXt Xt−τ\mathcal{R}(\tau) = \lim_{L \to \infty} \frac{1}{L} \sum_{t=1}^{L} \mathcal{X}_t \, \mathcal{X}_{t - \tau}

For a finite series, the paper's computation uses the same average. In part B, the six products at lag 2 are all +1+1, so their sum is 6, and dividing by L=8L = 8 gives 0.750.75.

Step 3 in symbols: choosing lags and weights

The lags are the top-kk values, with k=⌊clog⁡L⌋k = \lfloor c \log L \rfloor for a hyperparameter cc:

τ1,…,τk=arg⁡Topkτ∈{1,…,L}RQ,K(τ)\tau_1, \ldots, \tau_k = \mathop{\arg\mathrm{Topk}}_{\tau \in \{1, \ldots, L\}} \mathcal{R}_{\mathcal{Q},\mathcal{K}}(\tau)

The softmax weights are R^(τi)=exp⁡(R(τi))/∑j=1kexp⁡(R(τj))\widehat{\mathcal{R}}(\tau_i) = \exp(\mathcal{R}(\tau_i)) / \sum_{j=1}^{k} \exp(\mathcal{R}(\tau_j)). In part B, exp⁡(0.75)/(exp⁡(0.75)+exp⁡(0.50))≈0.56\exp(0.75) / (\exp(0.75) + \exp(0.50)) \approx 0.56.

Step 4 in symbols: why it is cheap

Computing R\mathcal{R} directly for every lag costs O(L2)O(L^2). The Wiener–Khinchin theorem says that the autocorrelation is the inverse Fourier transform of the power spectrum, the product of the transform with its own conjugate. A fast Fourier transform computes all lags at once in O(Llog⁡L)O(L \log L). Because only k=O(log⁡L)k = O(\log L) lags are aggregated, each aggregation step is also O(Llog⁡L)O(L \log L). This is the source of the paper's complexity claim.

Multi-head form

With hh heads, the channels are split into hh groups of size dmodel/hd_{\mathrm{model}}/h, each head runs the Auto-Correlation on its own group, and the outputs are concatenated and projected back with a learned matrix. This mirrors standard multi-head attention, with Auto-Correlation in place of the attention step.

Results

The paper reports mean squared error (MSE) and mean absolute error (MAE) on six benchmarks. Lower is better. Table 1 covers multivariate forecasts with four horizons. The input length is 96 for the first five benchmarks and 36 for the influenza benchmark. The rows below use the horizon of 336 steps, except the influenza row, which uses horizon 60. The best baseline values are the previous state-of-the-art MSE the paper cites.

BenchmarkAutoformer MSEBest baseline MSE
ETT (electricity transformer)0.3391.334
Electricity (321 customers)0.2310.280
Exchange (8 countries)0.5091.357
Traffic (California freeways)0.6220.733
Weather (21 indicators)0.3590.455
ILI (influenza, input 36, horizon 60)2.7704.882

For the ETT benchmark at the 336-step horizon, the error falls by roughly three quarters, which the paper reports as 74%. On the Exchange benchmark, which has no obvious periodicity, the reduction at 336 steps is 61% (1.357 to 0.509). Across all these settings, the paper reports a 38% average reduction in MSE.

The paper also finds that Autoformer's error rises slowly as the horizon grows. In the table of ETT results, the error at horizon 720 is 0.422 for Autoformer and 3.379 for Informer, where the gap widens with the horizon. The paper reads this as long-term robustness.

Two ablations explain where the gain comes from. Table 3 adds the progressive decomposition block to other Transformer-based models. For Transformer at horizon 336 on ETT, the MSE drops from 1.413 with the original architecture to 0.375 with the decomposition architecture, and the gain holds for Informer and LogTrans as well. Table 4 replaces Auto-Correlation with other attention mechanisms on the same architecture. At input length 96 and horizon 336, Auto-Correlation gives 0.339, against 0.375 for full attention, 0.362 for LogSparse and 0.481 for ProbSparse. At input length 192 and horizon 1440, full attention runs out of memory, while Auto-Correlation gives 0.503.

Limits the paper states or implies.

  • The hyperparameter cc controls how many lags are aggregated, so it trades accuracy against cost. The paper tries values from 1 to 3, and the best value differs across datasets, so it has to be tuned.
  • The method rests on periodicity. The Exchange benchmark has no obvious cycle, and Autoformer still does well there, but the mechanism is designed around periodic structure.
  • Each result is a single architecture trained with a fixed recipe, with runs repeated three times. The paper's appendices report standard deviations, but the main tables show point estimates.

Reading guide

Read the paper in this order, with these lessons open alongside it.

Check yourself

QuestionIn Auto-Correlation, how is the number of lags kept chosen, and why is it not a fixed number?Show answer

The number of lags is k=⌊clog⁡L⌋k = \lfloor c \log L \rfloor, where LL is the series length and cc is a hyperparameter. It grows slowly with the length, which keeps the aggregation step at O(Llog⁡L)O(L \log L) cost. A fixed kk would make the cost grow faster than necessary for long inputs and would not match the number of periods a long series may contain.

QuestionIn part B of the walkthrough, the lag-2 score is 0.75 and the lag-4 score is 0.50. What are their softmax weights, and why does the larger score matter?Show answer

The weight for lag 2 is e0.75/(e0.75+e0.50)≈0.56e^{0.75} / (e^{0.75} + e^{0.50}) \approx 0.56, and for lag 4 it is about 0.440.44. The weights sum to 1. The larger score gets more weight, because the softmax exponentiates each score: a difference of 0.25 in scores becomes a weight ratio of e0.25≈1.28e^{0.25} \approx 1.28.

QuestionWhy does the paper decompose the hidden states inside the network rather than only decomposing the past input before training?Show answer

The future is unknown, so its trend cannot be extracted from the data directly. Decomposing inside each layer lets the network refine the trend as it predicts, and the interaction between the trend and the seasonal part is modeled throughout the forecast. Pre-decomposing only the past misses those interactions, and the paper reports that it can even hurt accuracy at long horizons.

QuestionA team forecasts hourly electricity demand 168 hours ahead from a 96-hour input and finds that full self-attention runs out of memory on their GPU. Would Auto-Correlation help, and what should they check before relying on it?Show answer

Auto-Correlation costs O(Llog⁡L)O(L \log L) in both memory and time, while full self-attention grows with the square of the length, so it is likely to fit where full attention does not. Before relying on it, they should check that the demand series has a clear periodic structure, such as a daily or weekly cycle, because the mechanism picks its lags from periodicity. They should also tune cc on validation data, since the best value differs across datasets.

Sources

Read the full paper

Haixu Wu, Jiehui Xu, Jianmin Wang et al., 2021. arXiv:2106.13008