← Blog/blog/linear-beats-transformers

When one linear layer beat the Transformer, read closely

01

The result that embarrassed a subfield

A forecast here means: given the last L readings of a series (the look-back window), predict the next H readings (the horizon). “Long-term” forecasting pushes H out to 96, 336, even 720 steps — days of hourly data at once. It is the problem behind every capacity plan and every “how much power will the grid need Tuesday” question.

After Transformers conquered language, the field spent 2020–2022 porting them to this task, one ornate variant after another — Informer, Autoformer, FEDformer — each adding machinery to make attention affordable over long sequences, each claiming the new state of the art. Then Zeng, Chen, Zhang and Xu asked a rude question: had anyone checked a linear baseline? They wrote a model with no attention, no layers, no nonlinearity — a single matrix that multiplies the look-back window and out comes the whole forecast — and it beat all of them, “surprisingly… in all cases.”

02

What a “linear forecaster” actually is

The subtlety is in one word: direct. Most classical forecasters are autoregressive — predict one step, feed it back, predict the next — so errors compound down the horizon. LTSF-Linear instead maps the look-back to all H future steps in a single matrix multiply. Each future step gets its own row of weights over the whole window; step 200 can lean on lag 47 directly if that is where its signal lives. Crucially the weights are ordered: lag 1 and lag 24 have different columns. Contrast that with self-attention, which is permutation-invariant at its core — shuffle the inputs and the attention weights follow them — so it has to bolt ordering back on with positional encodings. The paper's thesis in one line: for a signal where order is the entire content, starting from an order-blind mechanism is starting in a hole.

The strong variant, DLinear, adds one idea from 1950s forecasting: split the window into a slow trend (a centered moving average) and the seasonal remainder, run a separate linear layer on each, and add the two forecasts. The decomposition is four lines.

def series_decomp(window, kernel):          # kernel is odd (paper uses 25)
    h = (kernel - 1) // 2
    n = len(window)
    trend = []
    for i in range(n):
        s = 0.0
        for j in range(-h, h + 1):
            idx = min(max(i + j, 0), n - 1)  # replicate-pad the two ends
            s += window[idx]
        trend.append(s / kernel)             # centered moving average
    seasonal = [window[i] - trend[i] for i in range(n)]
    return trend, seasonal                   # smooth drift + the oscillation left over
windowtrend (moving average)seasonal remainder
Fig 1. One look-back window (blue) split by the moving average into a slow trend (green) and the seasonal remainder (orange, oscillating around zero). DLinear forecasts each with its own linear layer; the trend layer can extrapolate drift while the seasonal layer handles the cycle.

Then the forecast is those two linear maps, summed. Set the two weight matrices equal and the split dissolves back into a plain linear layer — so DLinear is strictly a superset of Linear, free to treat drift and oscillation differently.

def dlinear_forecast(window, W_seasonal, W_trend, kernel):
    trend, seasonal = series_decomp(window, kernel)
    # each layer maps the WHOLE look-back straight to the WHOLE horizon, in one shot
    out_s = W_seasonal @ seasonal        # W_seasonal: H x L
    out_t = W_trend    @ trend           # W_trend:    H x L
    return out_s + out_t                 # DLinear. set W_seasonal = W_trend -> plain Linear

We never train these matrices with an optimiser. A one-layer linear network minimising mean-squared error has a closed-form optimum — the same least-squares solve the linear-regression page performs, only widened to many inputs and many outputs. The site runs the TypeScript above (weights stored transposed so one Cholesky factorisation serves every horizon column); the Python and C++ are line-for-line translations. Every number in the charts below is produced by that code, per the site's formula = code rule.

the fit, closed form
# The "layer" is fit in closed form: the least-squares optimum an MSE-trained
# one-layer linear net converges to. Stack the training windows into X (features)
# and their futures into Y (targets), then solve the ridge normal equations.
#   Linear   -> features are the raw window          (L columns)
#   DLinear  -> features are [seasonal, trend]        (2L columns)
A = X.T @ X + z * I          # (P x P), P = L or 2L
W = cholesky_solve(A, X.T @ Y)   # (P x H); one factorisation, every horizon column
# No optimiser, no epochs, no learning rate. It is the /linear page's engine, widened.
03

Rebuilding the win

The LTSF benchmarks (electricity, traffic, weather) are, underneath, strongly periodic. So we forecast a seeded stand-in: a gentle upward trend plus a fast daily-style cycle (period 24) and a slow weekly one (period 168), with Gaussian noise — all drawn through the site's seeded RNG. We fit on the first 72% and forecast the held-out tail it never saw. Here is one test window: the model sees the blue history, then has to draw the next 32 steps.

history + actuallinear forecastpersistence
Fig 2. One out-of-sample forecast. The linear layer (green) tracks the future 32-step continuation of the blue series closely; the persistence baseline (grey) — repeat the last value, the honest “I don't know” — flatlines and misses the entire oscillation. Only the horizon portion of each forecast is drawn.

Scored over every test window, the gap is not subtle. Persistence posts a mean-squared error of 5.94; the linear layer, 0.247 — a 95.8% reduction. On the paper's real data the same model beat the Transformers by comparable margins (their numbers are in §07, labelled as theirs). A matrix multiply, doing the work of a research programme.

Fig 3. Test-set MSE on the seeded series (lower is better). The linear and DLinear bars are slivers next to persistence — the point of the paper — and are all but indistinguishable from each other, the point of §04.
04

The clever part barely matters

DLinear is the variant the paper leads with, and the decomposition looks like where the intelligence lives. So test it directly: fit a plain Linear layer on the raw window, fit DLinear with the trend/seasonal split, score both on the same held-out data.

forecasterout-of-sample MSEvs persistence
persistence (repeat last)5.9407
Linear (raw window)0.247095.8%
DLinear (trend + seasonal)0.247095.8%
The decomposition moves the fourth decimal place. On this well-behaved series DLinear improves on plain Linear by 0.000% — noise. That is not a bug in our rebuild; it is the paper's own finding. Their ablation shows Linear, NLinear and DLinear within a hair of each other on most datasets, with DLinear pulling ahead only where a strong, drifting trend gives the separate trend layer something to grip.
05

Why longer memory helps the line but not the Transformer

Here is the paper's sharpest experiment, and the one that actually indicts attention. Feed the models a longer look-back — more history — and see who uses it. A competent forecaster should improve: more context, more of the periodic structure in view. The paper reports that the Transformers do not — their error stays flat or drifts up as the window grows, because self-attention drowns in the added tokens. The linear models improve steadily. We can compute the linear half honestly:

LTSF-Linear test MSE
Fig 4. Our linear forecaster's test MSE as the look-back window grows from 12 to 240 steps (DLinear traces the same curve). Error falls sharply as the window comes to span several full cycles of the period-24 season, then flattens. The paper's Transformers, on the same axis, go the wrong way — the behaviour you want from a forecaster is the behaviour the simple model has.

Why does a line drink up context that a Transformer chokes on? A sinusoid obeys a linear recurrence — its next value is a fixed linear combination of its past values — so a linear map with a long-enough window can lock onto the exact phase and reproduce the cycle indefinitely. Attention, built to find which faraway token to copy, has no such affinity for smooth periodic extrapolation; more positions mostly mean more ways to be distracted. Order-blindness, again, costing exactly where order is everything.

06

The thing a skim misses: a bet on a frozen rhythm

Every weight in that matrix is estimated from the training window and then frozen. The model is not reasoning about the future; it is asserting the future will oscillate the way the past did — same periods, same phase relationships. On the stationary benchmarks that assumption holds beautifully, which is precisely why a fixed linear map can look miraculous. So we break it in the one way a real series will: partway through, we change the dominant period from 24 to 40 — a regime shift, the market equivalent of a new supply schedule or a change in behaviour — and forecast across it with the same recipe.

Fig 5. The same linear forecaster, on a series whose dominant cycle changes right before the forecast region. Its error jumps from 0.247 (stable) to 5.57 — a 23× blow-up — and its 95.8% edge over persistence collapses to 26.8%. The weights were tuned to a rhythm that no longer plays.

None of this makes the paper wrong; it makes it precise. Its claim is about the LTSF benchmarks as they exist, and on those it is correct and important. The caveat is only that the win is inseparable from a property of the data — a stable spectrum — that the model never checks and cannot defend.

07

The paper’s own numbers

For honesty, here is the paper's headline table — real MSE on the standard datasets, not our synthetic stand-in. Lower is better; DLinear (highlighted) is beneath every Transformer in every row.

dataset · horizonDLinearFEDformerAutoformerInformer
ETTh1 · 960.3750.3760.4490.865
ETTm2 · 960.1670.2030.2550.365
Electricity · 960.1400.1930.2010.274
Traffic · 960.4100.5870.6130.719
Weather · 960.1760.2170.2660.300
ETTh1 · 7200.4720.5060.5141.181
Electricity · 7200.2030.2460.2540.373
Reported MSE from Zeng et al. (2023), Table 2, univariate horizon steps as labelled. Informer was itself an AAAI-2021 best-paper Transformer; DLinear halves its error on several datasets. These are the paper's figures, quoted; our seeded reproduction (§03) shows the same ordering on data we control end to end.
08

What happened next, and what I’d probe

The paper landed as a provocation, and the field answered it — not by defending the old Transformers but by fixing them. Within a year PatchTST (Nie et al., ICLR 2023) gave attention two things it had been missing: it chops the series into patches (so a token is a little sub-window, not a single timestamp) and forecasts each channel independently. That put a Transformer back on top of DLinear across the same benchmarks. The lesson was never “linear wins” — it was “your baseline and your tokenisation were wrong,” and both camps were better for the fight.

  • Does the regime-shift break survive a trend-only decomposition? Our seasonal layer bets on the old period. Freezing the seasonal layer and letting only the moving-average trend forecast across the shift would isolate how much of DLinear's robustness is real versus borrowed from stationarity.
  • Where does linearity actually snap? Swap the clean cycle for a nonlinear, state-dependent one and a straight matrix must fail. A small neural net on the same windows would show exactly how much curvature the benchmarks were hiding — and whether the Transformers were solving a problem the data never posed.
  • The honest baseline habit. The durable takeaway is methodological: before crowning a complex model, fit the one-line one. It is the forecasting cousin of this site's linear-regression refrain — a line through the data is not the answer, but it is the number every fancier answer has to beat.

The engine underneath all of this is the plainest fit there is — least squares, a matrix solved once. Go widen it in your head on the linear-regression page: picture the inputs as a strip of recent history and the output as the next stretch of it. LTSF-Linear is a genuinely humbling result — a subfield out-run by its own missing baseline. Just remember what the line is really promising: that tomorrow keeps yesterday's rhythm.

References

  1. Zeng, A., Chen, M., Zhang, L., & Xu, Q. (2023). Are Transformers Effective for Time Series Forecasting?. AAAI 2023 (arXiv:2205.13504)
  2. Nie, Y., Nguyen, N. H., Sinha, P., & Kalagnanam, J. (2023). A Time Series is Worth 64 Words: Long-term Forecasting with Transformers (PatchTST). ICLR 2023
  3. Zhou, H., Zhang, S., Peng, J., Zhang, S., Li, J., Xiong, H., & Zhang, W. (2021). Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting. AAAI 2021 (best paper)
  4. Wu, H., Xu, J., Wang, J., & Long, M. (2021). Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Forecasting. NeurIPS 2021
  5. Cleveland, R. B., Cleveland, W. S., McRae, J. E., & Terpenning, I. (1990). STL: A Seasonal-Trend Decomposition Procedure Based on Loess. Journal of Official Statistics 6(1), 3–73
  6. Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). Attention Is All You Need. NeurIPS 2017