Skip to content
SK Subham KumarTSF Hub · time-series forecasting
Transformers2024AttentionMultivariate

iTransformer

Inverted Transformer

Flip the Transformer: make each variable’s whole history one token, so attention learns relationships between variables while feed-forward layers learn each series’ dynamics.

Overview

Classic time-series Transformers make each time step a token that mixes all variables, which blurs variables with different units and lags, and makes attention over hundreds of steps costly. iTransformer inverts this: each variable is a token, embedded from its entire look-back window.

Self-attention then captures multivariate correlations (how load relates to temperature), while the shared feed-forward network learns temporal patterns within each series. It uses standard Transformer parts with no architectural changes, and achieved state-of-the-art results on several multivariate benchmarks.

How it works

For \(N\) variables with look-back \(L\), embed each series \(X_{:,n}\in\mathbb R^{L}\) as a token:

\[h_n^{0}=\mathrm{MLP}(X_{:,n}),\qquad H^{l+1}=\mathrm{TrmBlock}(H^{l}),\quad H\in\mathbb R^{N\times D}\] \[\hat Y_{:,n}=\mathrm{Projection}(h_n^{L})\in\mathbb R^{H}\]

Attention scores between tokens are variable-to-variable correlations; layer norm is applied per variable token.

When to use it

Use it for

  • Many interacting series: sensors, regional load, traffic networks
  • Long look-back windows (cost scales with variables, not steps)
  • When cross-variable structure matters

Avoid it for

  • Univariate problems (nothing for attention to relate)
  • Very many variables with little data

Key settings

SettingWhat it controlsTypical value
n_seriesNumber of variables (tokens)dataset-specific
input_sizeLook-back window per token96–720
hidden_size / n_headsEmbedding width and attention heads128–512 / 8
e_layersNumber of Transformer blocks2–4

Python example

Follows the library’s documented API · needs PyTorch/TensorFlow, not run hereDownload itransformer.py
# pip install neuralforecast   (needs PyTorch)
import pandas as pd
from neuralforecast import NeuralForecast
from neuralforecast.models import iTransformer

URL = "https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv"
raw = pd.read_csv(URL, parse_dates=["date"])
# long format: one row per (variable, timestamp): iTransformer turns each variable into one token
df = raw.melt(id_vars="date", var_name="unique_id", value_name="y").rename(columns={"date": "ds"})
H, n_series = 96, df["unique_id"].nunique()      # 7 variables
cut = df["ds"].sort_values().unique()[-H]
train, test = df[df["ds"] < cut], df[df["ds"] >= cut]

model = iTransformer(h=H, input_size=336, n_series=n_series, hidden_size=128, n_heads=8,
                     e_layers=2, scaler_type="standard", max_steps=1000, random_seed=1)
nf = NeuralForecast(models=[model], freq="h")
nf.fit(df=train)
fc = nf.predict().merge(test, on=["unique_id", "ds"])
print(fc.groupby("unique_id").apply(lambda g: (g["y"] - g["iTransformer"]).abs().mean()).round(3))

Tips & pitfalls

  • Scale each variable separately; tokens with wildly different scales hurt attention.
  • Longer look-back windows help iTransformer more than they help temporal-token Transformers.
  • Compare against PatchTST (channel-independent) to see whether cross-variable attention helps your data.

References

  1. Liu, Y., Hu, T., Zhang, H., Wu, H., Wang, S., Ma, L., & Long, M. (2024). iTransformer: Inverted Transformers Are Effective for Time Series Forecasting. International Conference on Learning Representations (ICLR).