Search

Search papers and lessons

Time-Series Patching

Level 3 · 9 min read

Time-series patching cuts a series into short overlapping or adjacent windows and feeds each window to a Transformer as one token, which makes long histories affordable.

On this page

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

TL;DR

Time-series patching splits a long series into short windows called patches, and each patch becomes one token for a Transformer. A series of 336 time steps then becomes about 42 tokens instead of 336, so the attention cost falls sharply and the model can read a much longer history. Each patch also keeps a short run of local shape, which a single time step does not.

Builds on: Transformer, Time Series Forecasting, Embedding Layer · Leads to: A Time Series is Worth 64 Words: Long-term Forecasting with Transformers, which uses patching with a Transformer for long-term forecasting

Where it's used

Electricity load. A utility forecasts demand for the next several days from the last few weeks of hourly readings. A history of a few weeks is 500 or more hourly steps, and patching lets a Transformer read all of it at a reasonable cost. The PatchTST paper evaluates on an electricity benchmark of this kind.

Road traffic. Sensors on highways report flow every few minutes. Traffic patterns repeat daily and weekly, so a model that sees a long window can use the same time of day from earlier weeks. Patching is one way to keep such windows short enough to attend over.

Weather. Temperature and humidity are sampled often and show slow trends with fast ripples. Patches of a few steps keep the ripples inside one token, while the sequence of patches carries the trend.

How it works

Follow a tiny series of 12 readings, x1=1x_1 = 1 to x12=12x_{12} = 12, with patch length P=4P = 4 and stride S=2S = 2. The stride is how far the window moves each time, so consecutive patches share two values.

  1. Pad the end. Repeat the last value S=2S = 2 times so the final patch is full. The series becomes 1,2,…,12,12,121, 2, \dots, 12, 12, 12. Check: the padded length is 14, and the padding is just a copy of the last reading.
  2. Cut the patches. Slide a window of length 4 along the series, moving 2 steps each time. The six patches are [1,2,3,4][1,2,3,4], [3,4,5,6][3,4,5,6], [5,6,7,8][5,6,7,8], [7,8,9,10][7,8,9,10], [9,10,11,12][9,10,11,12] and [11,12,12,12][11,12,12,12]. Check: the number of patches is ⌊(12−4)/2⌋+2=6\lfloor (12 - 4) / 2 \rfloor + 2 = 6.
  3. Embed each patch. A learned linear map turns each patch of 4 numbers into a vector of, say, 128 numbers. Then a position code is added so the model knows the order. Check: six tokens of length 128 go into the transformer.
  4. Attend across the tokens. Each token attends to the other five. The attention table has 6×6=366 \times 6 = 36 entries. Without patching, one token per time step would give 12×12=14412 \times 12 = 144 entries. Check: the ratio is 36/144=1/436 / 144 = 1/4, which is (6/12)2(6 / 12)^2.
  5. Predict from the tokens. The token outputs are flattened into one vector, and a linear head maps that vector to all the forecast values for the horizon (the future steps to predict) at once.
flowchart LR
  A[Series of L steps] --> B[Pad the end by S values]
  B --> C[Cut patches of length P every S steps]
  C --> D[Linear embedding plus position code]
  D --> E[Transformer encoder]
  E --> F[Linear head gives forecast]

The number of tokens NN is the one knob that sets the cost, so it is worth knowing how it grows with the window length.

The math

Let LL be the look-back length (how many past steps the model sees), PP the patch length, and SS the stride. The paper pads the end with SS copies of the last value, and the number of patches is N=⌊L−PS⌋+2N = \left\lfloor \frac{L - P}{S} \right\rfloor + 2. Let xp(i)x^{(i)}_p be patch pp of one univariate series (a single variable, such as one sensor), a vector of PP values.

Step 2 in symbols: the patch count

Start with LL values. Each stride moves the window by SS, so the windows that fit inside the original LL values number ⌊(L−P)/S⌋+1\lfloor (L - P) / S \rfloor + 1. The padding adds one final window that starts inside the original series and runs into the padded values, which gives the +2+2 in the formula. Check it against the tiny example: ⌊(12−4)/2⌋+2=6\lfloor (12 - 4) / 2 \rfloor + 2 = 6.

Step 3 in symbols: the embedding

Each patch is mapped to the model width dd by a learned matrix WW, so token pp is zp=Wxp(i)+epz_p = W x^{(i)}_p + e_p, where epe_p is a position code. The same matrix is shared across patches, so the model learns one way to read a window of any position.

Step 4 in symbols: the cost

Self-attention over NN tokens costs about N2dN^2 d operations, so the cost of attention on a series of length LL is about L2dL^2 d without patching and about N2dN^2 d with it. Since N≈L/SN \approx L / S, the ratio of attention costs is about (1/S)2(1 / S)^2. With stride 8, the attention cost falls by a factor of about 64.

Worked examples

Example
1. Tiny: the 12-step series

Use the series 1,…,121, \dots, 12 with P=4P = 4 and S=2S = 2, as in "How it works".

  1. Padded series: 1,…,12,12,121, \dots, 12, 12, 12, length 14.
  2. Patch count: N=⌊(12−4)/2⌋+2=4+2=6N = \lfloor (12 - 4) / 2 \rfloor + 2 = 4 + 2 = 6.
  3. Last patch: indices 11 to 14 of the padded series, which are 11,12,12,1211, 12, 12, 12.
  4. Attention entries: 62=366^2 = 36, compared with 122=14412^2 = 144 for one token per step.
  5. Interpretation: the patch view reduces the attention table by a factor of four here, and the padded last patch keeps the final real reading in the last token.
Example
2. Standard: a 336-step look-back with patch length 16 and stride 8

The PatchTST setting uses a look-back of L=336L = 336 with P=16P = 16 and S=8S = 8.

  1. Patch count: N=⌊(336−16)/8⌋+2=40+2=42N = \lfloor (336 - 16) / 8 \rfloor + 2 = 40 + 2 = 42.
  2. Attention entries with patches: 422=1,76442^2 = 1{,}764.
  3. Attention entries without patches: 3362=112,896336^2 = 112{,}896.
  4. Ratio: 1,764/112,896=1/641{,}764 / 112{,}896 = 1/64, which is (1/8)2(1/8)^2.
  5. Interpretation: the 42-token model does about 64 times less attention work than a model that treats each step as a token, and it still sees the full 336 steps.
Example
3. Realistic: an hourly electricity window

A utility keeps 720 hourly readings, which is 30 days. It uses P=16P = 16 and S=8S = 8.

  1. Patch count: N=⌊(720−16)/8⌋+2=88+2=90N = \lfloor (720 - 16) / 8 \rfloor + 2 = 88 + 2 = 90.
  2. Attention entries: 902=8,10090^2 = 8{,}100, compared with 7202=518,400720^2 = 518{,}400 without patches.
  3. Interpretation: 30 days of hourly history is affordable as 90 tokens, where the same window one step at a time would be a very large attention table. The patch length also keeps each token's 16 hours of shape together, so a daily cycle can sit inside one or two tokens.
  4. Try it: the same utility wants 1,440 hourly readings (60 days) with P=16P = 16 and S=8S = 8. How many tokens? Answer: N=⌊(1440−16)/8⌋+2=178+2=180N = \lfloor (1440 - 16) / 8 \rfloor + 2 = 178 + 2 = 180.

Common mistakes and tradeoffs

It is tempting to think that a patch is just a sliding window of the input. In fact the patch is the unit of attention and the embedding maps it to a learned vector, so the model learns what a window means. Overlapping patches (stride less than length) add tokens and cost, and non-overlapping patches (stride equal to length) are cheapest.

It is tempting to think that longer patches are always better because they shrink the sequence further. In fact a longer patch lowers the time resolution the model can attend to, and the best length depends on the data and its sampling rate, so it is tuned on held-out data rather than set by rule.

It is tempting to think that patching replaces normalisation. In fact the series still needs scaling. The paper applies instance normalisation by standardising each series to zero mean and unit standard deviation before patching, then adds the mean and deviation back to the forecast, which matters when different series sit at different levels.

When to use it. The look-back is long, many steps matter, and a Transformer is the model of choice. Patching is what makes a long look-back affordable.

When not to use it. The series is short, so there is little to save, or a simple model already forecasts well. A linear model over the same window, such as the one in DLinear, is much cheaper and easier to train.

ChoiceEffectWatch out for
Stride equal to patch lengthFewest tokens, no overlapPatch edges can split a feature
Stride less than patch lengthMore tokens, smoother coverageCost rises as 1/S21 / S^2
Longer patchCoarser tokensLess time detail per token

History

Transformers appeared in 2017 and were first used on text. In 2020 the Vision Transformer cut an image into fixed-size patches and treated each patch as a token, which showed that the same trick worked on a grid of pixels. Time-series models based on Transformers followed, but attention over every time step was expensive for long histories. The PatchTST paper, posted to arXiv in November 2022, applied the patch idea to time series, with overlapping windows, one series per channel (each variable is modelled as its own stream), and a shared Transformer, and reported gains on long-horizon forecasting benchmarks.

Recap

Check yourself

QuestionRecall: what is the difference between a patch and a single time step when a Transformer reads a series?Show answer

A single time step is one token holding one value, so attention runs over every step. A patch is a window of several consecutive values embedded as one token, so attention runs over fewer tokens and each token carries local shape.

QuestionA series has 200 steps with patch length 16 and stride 8. How many patches are there?Show answer

N=⌊(200−16)/8⌋+2=⌊184/8⌋+2=23+2=25N = \lfloor (200 - 16) / 8 \rfloor + 2 = \lfloor 184 / 8 \rfloor + 2 = 23 + 2 = 25.

QuestionWhy does doubling the stride from 4 to 8 reduce the attention cost by about a factor of four, and not by a factor of two?Show answer

Attention cost grows with the square of the number of tokens. Doubling the stride roughly halves the number of tokens, and halving it again reduces the square by a factor of four: (1/2)2=1/4(1/2)^2 = 1/4.

QuestionA forecaster uses patches and gets worse results when it doubles the patch length. What is a reasonable first explanation?Show answer

A longer patch gives each token a coarser view, so the model loses fine time detail inside each token. The fix is usually to try a smaller patch length or a stride that keeps overlap, then check whether the error falls on held-out data.

Sources

  • Nie et al., "A Time Series is Worth 64 Words: Long-term Forecasting with Transformers", 2022 (arXiv:2211.14730).
  • Dosovitskiy et al., "An Image is Worth 16x16 Words", 2020 (arXiv:2010.11929).
  • Hyndman and Athanasopoulos, "Forecasting: Principles and Practice", 3rd edition.