Transformer vs LSTM for Time Series: Which Works Better?


In this article, you will learn how to build, train, and compare an LSTM and a transformer for next-day univariate time series forecasting on real public transit data.

Topics we will cover include:

  • Structuring and windowing a time series for supervised learning.
  • Implementing compact LSTM and transformer architectures in PyTorch.
  • Evaluating and comparing models with MAE and RMSE on held-out data.

All right, full steam ahead.

Transformer vs LSTM for Time Series: Which Works Better?

Transformer vs LSTM for Time Series: Which Works Better?
Image by Editor

Introduction

From daily weather measurements or traffic sensor readings to stock prices, time series data are present nearly everywhere. When these time series datasets become more challenging, models with a higher level of sophistication — such as ensemble methods or even deep learning architectures — can be a more convenient option than classical time series analysis and forecasting techniques.

The objective of this article is to showcase how two deep learning architectures are trained and used to handle time series data — long short term memory (LSTM) and the transformer. The main focus is not merely leveraging the models, but understanding their differences when handling time series and whether one architecture clearly outperforms the other. Basic knowledge of Python and machine learning essentials is recommended.

Problem Setup and Preparation

For this illustrative comparison, we will consider a forecasting task on a univariate time series: given the temporally ordered previous N time steps, predict the (N+1)th value.

In particular, we will use a publicly available version of the Chicago rides dataset, which contains daily recordings for bus and rail passengers in the Chicago public transit network dating back to 2001.

This initial piece of code imports the libraries and modules needed and loads the dataset. We will import pandas, NumPy, Matplotlib, and PyTorch — all for the heavy lifting — along with the scikit-learn metrics that we will rely on for evaluation.

Since the dataset contains post-COVID real data about passenger numbers — which may severely mislead the predictive power of our models due to being very differently distributed than pre-COVID data — we will filter out records from January 1, 2020 onwards.

A simple plot will do the job to show what the filtered data looks like:

Chicago rides time series

Chicago rides time series dataset plotted

Next, we split the time series data into training and test sets. Importantly, in time series forecasting tasks — unlike classification and regression — this partition cannot be done at random, but in a purely sequential fashion. In other words, all training instances come chronologically first, followed by test instances. This code takes the first 80% of the time series as a training set, and the remaining 20% for testing.

Furthermore, raw time series must be converted into labeled sequences (x, y) spanning a fixed time window to properly train neural network-based models upon them. For example, if we use a time window of N=30 days, the first instance will span the first 30 days of the time series, and the associated label to predict will be the 31st day, and so on. This gives the dataset an appropriate labeled format for supervised learning tasks without losing its important temporal meaning:

We are now ready to train, evaluate, and compare our LSTM and transformer models!

Model Training

We will use the PyTorch library for the modeling stage, as it provides the necessary classes to define both recurrent LSTM layers and encoder-only transformer layers suitable for predictive tasks.

First up, we have an LSTM-based RNN architecture like this:

As for the encoder-only transformer for next-day time series forecasting, we have:

Note that the last layer in both architectures follows a similar pattern: its input shape is the hidden representation dimensionality (32 in our example), and one single neuron is used to perform a single forecast of the next-day total rides.

Time to train the models and evaluate both models’ performance with the test data:

We will compare how the models performed for a univariate time series forecasting task using two common metrics: mean absolute error (MAE) and root mean squared error (RMSE).

Results Discussion

Here are the results we obtained:

The results are incredibly similar between the two models, making it difficult to determine whether one is better than the other (if we look closely, the transformer performs a tiny bit better, but the difference is truly negligible).

Why are the results so similar? Univariate time series forecasting on data that follow a reasonably consistent pattern over time, such as the dataset we consider, can yield similar results across these models because both have enough capacity to solve this problem — even though the complexity of each architecture here is intentionally minimal. I suggest you try the entire process again without filtering the post-COVID instances, keeping the same 80/20 ratio for training and testing over the entire original dataset, and see if the difference between the two models increases (feel free to comment below with your findings).

Besides, the forecasting task is very short-term: we are just predicting the next-day value, instead of having a more complex label set y that spans a subsequent time window to the one considered for inputs X. If we predicted values 30 days ahead, the difference between the models’ errors would likely widen, with the transformer arguably outperforming the LSTM (although this might not always be the case).

Wrapping Up

This article showcased how to address a time series forecasting task with two different deep learning architectures: LSTM and the transformer. We guided you through the entire process, from obtaining the data to training the models, evaluating them, comparing, and interpreting results.



Source link