Skip to content
SK Subham KumarTSF Hub · time-series forecasting
Deep learning2023ConvolutionalPeriodicityMulti-task

TimesNet

Temporal 2D-Variation Modelling

Find the dominant periods with an FFT, fold the 1-D series into a 2-D grid (period × cycle), and let image-style convolutions learn variation within and across cycles.

Overview

Periodic series vary in two ways: within a period (the shape of a day) and across periods (how days differ from each other). In 1-D these are tangled; TimesNet untangles them. It uses an FFT to find the top-\(k\) periods, reshapes the series into a 2-D tensor for each period (rows = cycles, columns = positions in the cycle), and applies a parameter-efficient Inception block of 2-D convolutions.

The results for each period are reshaped back and combined, weighted by the FFT amplitudes. The same backbone works for forecasting, imputation, classification and anomaly detection.

How it works

For input \(X_{1D}\in\mathbb R^{T\times C}\), find periods from the amplitude spectrum:

\[A=\mathrm{Avg}\big(\lvert\mathrm{FFT}(X_{1D})\rvert\big),\quad \{f_1,\dots,f_k\}=\mathrm{TopK}(A),\quad p_i=\left\lceil T/f_i\right\rceil\] \[X_{2D}^{i}=\mathrm{Reshape}_{p_i\times f_i}(X_{1D}),\qquad \hat X^{i}=\mathrm{Inception}(X_{2D}^{i})\] \[X_{1D}^{\text{out}}=\sum_{i=1}^{k}\mathrm{Softmax}(A_{f_1},\dots,A_{f_k})_i\cdot\mathrm{Reshape}_{1D}(\hat X^{i})\]

When to use it

Use it for

  • Energy, traffic and weather series with clear cycles
  • One backbone for forecasting, imputation and anomaly detection
  • Medium to large datasets

Avoid it for

  • Aperiodic series (random-walk prices)
  • Small datasets or tight compute budgets

Key settings

SettingWhat it controlsTypical value
top_kNumber of dominant periods2–5
num_kernelsKernels in the Inception block6
hidden_size / conv_hidden_sizeModel width32–128
input_sizeLook-back window2–4 × longest period

Python example

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

URL = "https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv"
raw = pd.read_csv(URL, parse_dates=["date"])
df = raw.rename(columns={"date": "ds", "OT": "y"}).assign(unique_id="ETTh1")[["unique_id", "ds", "y"]]
H = 96
train, test = df.iloc[:-H], df.iloc[-H:]

# top_k: how many dominant periods (found with an FFT) to fold the series by
model = TimesNet(h=H, input_size=336, top_k=3, num_kernels=6, hidden_size=64, conv_hidden_size=64,
                 scaler_type="standard", max_steps=1000, random_seed=1)
nf = NeuralForecast(models=[model], freq="h")
nf.fit(df=train)
fc = nf.predict()
print(f"TimesNet MAE over 96 hours: {abs(test['y'].values - fc['TimesNet'].values).mean():.3f}")

Tips & pitfalls

  • Make the look-back window cover several cycles of the longest period you care about.
  • Inspect the FFT yourself first: if no clear peaks exist, TimesNet’s main idea won’t help.
  • Benchmark against DLinear and PatchTST, which are often competitive and cheaper.

References

  1. Wu, H., Hu, T., Liu, Y., Zhou, H., Wang, J., & Long, M. (2023). TimesNet: Temporal 2D-Variation Modeling for General Time Series Analysis. International Conference on Learning Representations (ICLR).