Unlocking the Potential of Transformers in Time Series Forecasting with Sharpness-Aware Minimization and Channel-Wise Attention

Romain Ilbert, Ambroise Odonnat, Vasilii Feofanov, Aladin Virmaux, Giuseppe Paolo, Themis Palpanas, Ievgen Redko

This paper introduces SAMformer, a lightweight, shallow transformer architecture that finally enables these powerful models to outperform simple linear baselines in long-term time series forecasting. By combining channel-wise attention with Sharpness-Aware Minimization (SAM), it overcomes the training instabilities and "entropy collapse" that have historically hindered transformers in this domain.

What Problem Does This Paper Address?

Despite their dominance in natural language processing and computer vision, Transformers have surprisingly struggled with multivariate long-term time series forecasting. They often lose out to simple linear models or MLP-based architectures like TSMixer.

The authors investigate why this happens. Using a toy linear forecasting problem, they demonstrate that standard transformers fail to converge to optimal solutions even when the underlying data generation is simple. They pinpoint two primary failure modes:

  1. Entropy Collapse: Attention matrices quickly become identity-like during training, meaning the model stops learning useful feature interactions.
  2. Sharp Loss Landscapes: Transformers tend to converge to very "sharp" local minima, which leads to poor generalization on unseen data.

Methods

The authors propose SAMformer, a streamlined architecture designed to address these specific weaknesses. Its key components include:

  • Shallow Architecture: A single-layer transformer encoder, which reduces complexity and overfitting risk.
  • Channel-Wise Attention: Unlike standard temporal attention that looks at relationships between time steps ($L \times L$), SAMformer uses channel-wise attention to model correlations between different features or variables ($D \times D$). This is both more relevant for many forecasting tasks and computationally cheaper since the number of features $D$ is typically much smaller than the sequence length $L$.
  • Sharpness-Aware Minimization (SAM): This is the critical optimization step. Instead of just minimizing training loss, SAM seeks out "flat" regions in the loss landscape. This prevents the model from getting stuck in the sharp, poorly-generalizing local minima typical of standard transformer training.
  • Reversible Instance Normalization (RevIN): A standard preprocessing step to handle distribution shifts and non-stationary data common in time series.
Figure 4: SAM - former

Datasets

The model was evaluated on 8 widely-used real-world multivariate time series benchmarks:

  • ETT (ETTh1, ETTh2, ETTm1, ETTm2): Electricity Transformer Temperature data at different granularities.
  • Electricity: Hourly consumption from 321 clients.
  • Exchange: Daily currency exchange rates.
  • Traffic: Road occupancy rates from 862 sensors.
  • Weather: Meteorological indicators like temperature and pressure.

Key Insights and Ideas

  • Attention is the Bottleneck: The authors show that a "Random Transformer" (with fixed, random attention weights) actually generalizes better than a fully trained one, proving that the standard way we train attention is what causes the failure.
  • Escaping Entropy Collapse: Standard training leads to attention matrices that look like identity matrices almost immediately. SAM helps maintain "expressive" attention, allowing the model to continue learning feature correlations.
  • Flatness is Key for Generalization: By using SAM, the authors achieved a loss landscape with a maximum Hessian eigenvalue ($\lambda_{max}$) orders of magnitude smaller than standard training, directly translating to better test performance.
Standard transformers quickly collapse to identity-like attention matrices, losing the ability to model feature interactions.

Key Results

SAMformer sets a new state-of-the-art for multivariate long-term forecasting:

  • Superior Accuracy: It outperforms the previous state-of-the-art (SOTA), TSMixer, by 14.33% on average across all datasets and horizons. It also beats the best previous transformer-based model, FEDformer, by 12.36%.
  • Extreme Efficiency: Despite its performance, SAMformer has ~4 times fewer parameters than TSMixer and is orders of magnitude smaller than typical transformer models.
  • Foundation Model Performance: It performs on par with (and sometimes better than) massive foundation models like MOIRAI (which has up to 314M parameters), despite being a fraction of the size.
  • Robustness: As shown in Figure 1, SAMformer (Transformer + SAM) is the only variant that successfully reaches the optimal "Oracle" solution on synthetic data, while others overfit or fail to converge.
Validation loss on synthetic data shows that only the SAM-optimized transformer reaches the optimal Oracle solution.

Limitations and Future Work

  • Shallow Focus: The current work focuses on a single-layer architecture. While highly effective, exploring whether deeper SAM-optimized transformers can unlock even more potential is an open question.
  • Full-shot vs. Zero-shot: SAMformer excels in the "full-shot" setting (trained on the target dataset). It does not aim to replace the zero-shot capabilities of massive foundation models trained on billions of samples, though it serves as a much more efficient alternative for specific tasks.
  • Hyperparameter Sensitivity: While SAMformer requires less tuning than some competitors, the neighborhood size parameter ($\rho$) in SAM still needs careful selection to balance flatness and accuracy.

Summary updated

Abstract

Transformer-based architectures achieved breakthrough performance in natural language processing and computer vision, yet they remain inferior to simpler linear baselines in multivariate long-term forecasting. To better understand this phenomenon, we start by studying a toy linear forecasting problem for which we show that transformers are incapable of converging to their true solution despite their high expressive power. We further identify the attention of transformers as being responsible for this low generalization capacity. Building upon this insight, we propose a shallow lightweight transformer model that successfully escapes bad local minima when optimized with sharpness-aware optimization. We empirically demonstrate that this result extends to all commonly used real-world multivariate time series datasets. In particular, SAMformer surpasses current state-of-the-art methods and is on par with the biggest foundation model MOIRAI while having significantly fewer parameters. The code is available at https://github.com/romilbert/samformer.
BibTeX
@inproceedings{ilbert2024unlocking,
  author = {Romain Ilbert and Ambroise Odonnat and Vasilii Feofanov and Aladin Virmaux and Giuseppe Paolo and Themis Palpanas and Ievgen Redko},
  title = {{Unlocking the Potential of Transformers in Time Series Forecasting with Sharpness-Aware Minimization and Channel-Wise Attention}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=8kLzL5QBh2},
}
Figure