Traffic Volume Prediction using Memory-Based Recurrent Neural Networks:
  A comparative analysis of LSTM and GRU
Predicting traffic volume in real-time can improve both traffic flow and road
safety. A precise traffic volume forecast helps alert drivers to the flow of
traffic along their preferred routes, preventing potential deadlock situations.
Existing parametric models cannot reliably forecast traffic volume in dynamic
and complex traffic conditions. Therefore, in order to evaluate and forecast
the traffic volume for every given time step in a real-time manner, we develop
non-linear memory-based deep neural network models. Our extensive experiments
run on the Metro Interstate Traffic Volume dataset demonstrate the
effectiveness of the proposed models in predicting traffic volume in highly
dynamic and heterogeneous traffic environments.
Lokesh Chandra Das
Traffic Volume Prediction using Memory-Based Recurrent Neural Networks: A comparative analysis of LSTM and GRU

###### Abstract

Predicting traffic volume in real-time can improve both traffic flow and road safety. A precise traffic volume forecast helps alert drivers to the flow of traffic along their preferred routes, preventing potential deadlock situations. Existing parametric models cannot reliably forecast traffic volume in dynamic and complex traffic conditions. Therefore, in order to evaluate and forecast the traffic volume for every given time step in a real-time manner, we develop non-linear memory-based deep neural network models. Our extensive experiments run on the Metro Interstate Traffic Volume dataset demonstrate the effectiveness of the proposed models in predicting traffic volume in highly dynamic and heterogeneous traffic environments.

## I Introduction

Rapid socioeconomic development aids in the growth of large-scale, expanding smart cities with easy access to communication technologies. Modern mobile and vehicular communication technology promotes Intelligent Transportation Systems (ITS), resulting in an exponential increase in the number of vehicles on the road each year. Traffic congestion has become one of the most pressing issues to address[1] and it is creating deadlock situations for large cities as well as for medium and small cities[2]. Traffic congestion can be mitigated using traditional methods by changing the road and urban infrastructures. However, redesigning the city structure to improve the traffic pattern and reduce congestion is very expensive, let alone time-consuming. Therefore, dynamic route planning, optimizing road allocations, managing traffic on urban roads, and using modern technologies to understand traffic patterns better are crucial tasks to reduce traffic congestion successfully. Forecasting the future traffic status based on past historical data is a way of reducing traffic congestion[3]. An accurate traffic volume prediction can alleviate traffic congestion and optimize traffic distributions.

Different techniques for traffic flow prediction have been studied lately. These methods can be generally classified as naive, parametric, and non-parametric. The naive model does not make any assumptions, and it is computationally fast. However, it has low accuracy. The parametric method includes different time-series methods. One of the most widely used parametric methods is the ARIMA (autoregressive integrated moving average) model[4]. The ARIMA model is practically effective in predicting traffic flow and has been a benchmark. However, the parametric approaches can achieve better performances if the time-series data shows a regular pattern. In particular, the parametric methods fail to perform better when the traffic pattern varies in nature. Non-parametric methods address the issue of parametric methods, and various non-parametric methods such as non-parametric regression, support vector machine, Kalman filtering, and neural network predictors are being used in traffic volume predictions. Deep neural networks have been shown to be superior at dealing with traffic forecasting. Recurrent neural networks (RNN), especially long-short Term memory (LSTM), demonstrated their advantages in modeling and predicting traffic flow.

In this project, we develop traffic volume prediction using long-short term memory (LSTM) and gated recurrent units (GRU) and evaluate our model on Metro inter-state traffic dataset[5].

## II Related Work

Traffic forecasting is critically important, and it has been extensively studied lately. Traffic prediction methods can be divided into parametric and non-parametric methods. Previously, researchers have applied various parametric methods to predict traffic flow. The autoregressive integrated moving average is one of the most widely used parametric models in time-series prediction datasets. Chen et al.[6] predicted traffic flow using the autoregressive integrated moving average (ARIMA) model. However, other parametric models have also been used by researchers. Kumar et al.[7] developed Kalman filtering techniques to forecast the traffic flow. Dong et al.[8] applied the gradient-boosting decision tree algorithm to perform short-term traffic flow prediction.

Deep learning-based approaches have recently received a lot of attention from researchers, and they provide a more accurate estimation of traffic volume prediction than traditional parametric methods. The long-short-term memory (LSTM) and gated recurrent unit (GRU) is capable of holding a long sequence of past observations and making a correlation in sequence prediction, which makes them more suitable for time-series datasets. Many researchers are using LSTM and GRU for predicting traffic conditions. Zhao et al.[9] used LSTM to predict short-time traffic. Fu et al.[10] used LSTM and GRU for traffic flow prediction. Some researchers use a graph attention network to predict the traffic pattern[11]. In this project, we use LSTM and GRU to predict future traffic flow and evaluate the model using a metro interstate traffic volume dataset.
### _Problem Formulation_

We consider the real-time traffic volume prediction as a multi-variate time-series problem where our model will approximately estimate future traffic flow based on the current and \(t-\)hours of historical observations. Specifically, our objective is to foretell future traffic volume at time step \(T_{t+1},T_{t+2},...T_{t+f}\), where \(f\) is the future prediction horizon using past observations from time steps \(T_{t-l},T_{t-l-1},...,T_{t-1},T_{t}\) where \(l\) is the length of past observations used to predict the future traffic flow.

## III Method

We use memory-based recurrent neural network models, e.g., long-short-term memory (LSTM) and gated recurrent units (GRU), to predict future traffic flow.

### _Long-Short Term Memory (LSTM)_

Long-Short-term Memory networks (LSTMs)[12] are designed to learn long-term dependencies and effectively deal with the vanishing gradient problem of recurrent neural networks(RNNs)[13, 14]. Because it can hold long sequences while predicting the current output, LSTM is well-suited for applications that make predictions based on time-series data. The memory block of the LSTM cell makes it easy to hold the sequence information. The memory block has memory cells and three gates: the forget gate, the input gate, and the output gate. The basic structure of an LSTM cell is depicted in Fig.1.

The forget gate determines which information should propagate for the next time sequence through the sigmoid activation function. The input gate decides which information is necessary for the current state, and the output gate regulates what to output to the next state, e.g., the current output and the value of the next hidden state \(h_{t}\). The equations to update the gates are as follows:

\[f_{t} =\sigma(W_{f}.[h_{t-1},x_{t}]+b_{f})\] \[i_{t} =\sigma(W_{i}.[h_{t-1},x_{t}]+b_{i})\] \[\tilde{C}_{t} =\tanh(W_{C}.[h_{t-1},x_{t}]+b_{C})\] \[C_{t} =f_{t}*C_{t-1}+i_{t}*\tilde{C}_{t}\] \[o_{t} =\sigma(W_{o}.[h_{t-1},x_{t}]+b_{o})\] \[h_{t} =o_{t}*\tanh(C_{t})\]

where \(f_{t}\) is the forget gate, \(i_{t}\) is the input gate, and \(o_{t}\) is the output gate, respectively; \(C_{t}\) is a memory cell to hold the sequences from the previous states, and \(h_{t}\) is the output for the next state; \(W^{*}\) is weight, and \(b^{*}\) is bias.

### _Gated Recurrent Unit (GRU)_

Gated Recurrent Unit (GRU) replaces the three gates of LSTM with two gates: the reset gate and the update gate. These gates use sigmoid activation functions linked by LSTMs, constraining their values between \(0\) and \(1\). Intuitively, the reset gate controls how much of the previous state we might still want to remember, and an update gate would allow us to control how much of the new state is just a copy of the old state. Fig. 2 illustrates the inputs for both the reset and update gates in a GRU, given the input of the current time step and the hidden state of the previous time step. The mathematical mechanism for the GRU is as follows:

\[r_{t} =\sigma(W_{r}.[h_{t-1},x_{t}]+b_{r})\] \[z_{t} =\sigma(W_{z}.[h_{t-1},x_{t}]+b_{z})\] \[\tilde{H}_{t} =\tanh(W_{h}.[(h_{t-1}\odot r_{t}),x_{t}]+b_{h})\] \[h_{t} =z_{t}\odot h_{t-1}+(1-z_{t})\odot\tilde{H}_{t}\]

where \(r_{t}\) is the reset gate, \(z_{t}\) is the update gate, and \(h_{t}\) is the current time step's output. The rest of the symbols contain the same meaning as described in the LSTM section.

### _Dataset_

We use Metro Interstate Traffic Volume Data Set[5]. This is a multivariate, sequential, time-series dataset collected from the westbound I-94 at Minneapolis-St. Paul, Minnesota. The dataset is relatively large and has 48204 instances with nine features. The dataset collected traffic volume data in an hourly manner from 2012 to 2018, taking into account weather features and holidays that impact the traffic volume.

### _Data Preprocessing_

We cannot directly feed data into the LSTM or GRU models, unlike other deep learning models such as CNN and RNN. We have to convert it into a specific format. Input format should have at least time steps and a number

Fig. 1: A structure of an LSTM cell

Fig. 2: A structure of a GRU cell
of features. Generally, in time series prediction, we use \(t\)-hours of observations as input to the network, and the model will produce output at \(t+1\) hour. Here, we use the past \(t\)-hours of data to predict the next \(n\) hours' traffic volume. The dataset also contains some categorical values that needed to be converted into numerical values. Moreover, the attributes are in different scales. The statistics of the dataset are shown in Fig.3. It is clear that traffic_volume attribute values are very larger compared to rain_1h values. So we need to scale them to reduce the bias. We use the MinMaxScaler technique to normalize the feature values between 0 and 1. We split the dataset into training and testing sets. We use data from the years 2012-2017 for training among which 20% was used for validation purposes and the last year's data was used for testing purposes. From the extensive data analysis, we found

## IV Experimental Setup

We run a total of 4 experiments with different settings, taking 6 to 24 hours of past observations to predict the next hour's traffic volume. We also did an experiment to see how the features impacted traffic volume prediction. In one set of experiments, we select all features from the dataset, and in another set of experiments, we only consider four features, namely _temperature_, _rain_1h_, _clouds_all_, and _traffic volume_. We run experiments on two sets of neural networks. The table I shows the hyperparameters for our deep learning model. For experiment 2, we just changed the hidden number of units as like \([256,128,64,32]\) and the number of epochs is \(500\). The rest of the hyperparameters are the same as those shown in the table. The training is stopped if the validation error does not improve for at least five consecutive runs. The main evaluation metrics are mean squared error (MSE), mean absolute error (MAE), and mean absolute percentage error (MAPE) as shown in equations 1, 2, and 3.

\[MSE(y,\hat{y})=\frac{1}{n}\sum_{i=1}^{n}(y_{i}-\hat{y}_{i})^{2} \tag{1}\]

\[MAE(y,\hat{y})=\frac{1}{n}\sum_{i=1}^{n}|y_{i}-\hat{y}_{i}| \tag{2}\]

\[MAPE(y,\hat{y})=\frac{1}{n}\sum_{i=1}^{n}\frac{|y_{i}-\hat{y}_{i}|}{\text{max }(\epsilon,|y_{i}|)} \tag{3}\]

where \(y\) is actual traffic volume, \(y_{i}\) is the predicted traffic volume and \(\epsilon\ll 1\) used to avoid any undefined results caused by if \(|y_{i}|\) is zero.

## V Results

We implemented the traffic volume prediction problem in Python based on Keras and Tensorflow. To train and test the proposed model, a workstation equipped with an Intel Xeon Gold 5222 processor, an NVIDIA(r) RTXTM A4000 graphics card, and 48GB of RAM running Windows 11 OS is used. Empirically, the autoregressive integrated moving average (ARIMA) is not suitable when the dataset is more complex and has long sequences. So, we directly choose LSTM and GRU, and ARIMA is out of the scope of the project. In our

Fig. 4: Features contains outliers

Fig. 5: Features after removing outliers

Fig. 3: Statistics of the dataset
experiment, we compare the LSTM and GRU performances by varying the neural network settings and also by varying the length of historical observations. We compare the mean squared error (MSE), mean absolute error (MAE), and mean absolute percentage error (MAPE) for both GRU and LSTM with different neural network settings, taking into account all features vs. a reduced number of features.

Fig. 6 shows the comparative results of two models (e.g., LSTM and GRU) for different feature sets and neural network settings when the length of past observation is set to 24 hours. Although there is no significant pattern in the results, it can be seen that both the GRU and LSTM models produce better results in terms of MSE and MAE. On the other hand, when training does not require preserving a very long history, GRU performs better than LSTM. Fig.7 shows the results when the length of the past observation is set to 6 hours. It can be observed that when the past observation history is very limited, GRU performs better compared with the LSTM in all three evaluation metrics for both neural network settings. Fig.8 and Fig.9 depict the predicted traffic volume results at various times. Here, the blue line indicates the actual traffic volume at that particular hour and the orange line represents the predicted traffic volume for that particular hour using the proposed models. The results clearly indicate that the LSTM and GRU can potentially provide accurate traffic volume as close to the actual values as possible. However, GRU outperforms LSTM when the past observation sequences are small, and LSTM performs better when the dataset is complex and requires the use of very long sequences to predict future traffic volume. Fig.10 shows the convergence of the LSTM and GRU models. Both models take more training time to get convergent when the number of features is less. However, GRU takes more time compared to LSTM. The reason may be that GRU does not use any memory units to control the flow of information.

## VI Discussion

In this section, I will discuss challenges, limitations, computational efficiency, and potentially other methods for predicting traffic volume. The Traffic Volume Prediction repository contains the implementation, dataset, and trained models. Instructions on how to run can be found in the README.md file.

### _Challenges and Limitations_

Preparing the dataset to feed into the LSTM and GRU networks was one of the more challenging tasks as it added another dimension (e.g., time) to the input shape. Moreover, to find a proper set of hyperparameters, the network required a significant amount of time, which was very challenging given the limited amount of computational resources. Finally, we have used MinMaxScaler to normalize the dataset. Whenever we compared the results with ground truth, we had to convert them back to their original format, which also requires a lot of work.

### _Computational Time and Memory_

LSTM and GRU are computationally expensive and require a lot of memory, as they use memory to store historical observations internally. To finish a single training epoch given the above network structures and hyperparameters settings, the LSTM and GRU took approximately \(25\sim 28s\).

## VII Conclusion

In this project, we develop memory-based deep recurrent neural network models, namely LSTM and GRU, to predict traffic volume. The models are evaluated on the widely used

Fig. 6: MSE, MAE, and MAPE errors of LSTM and GRU by varying neural network settings. The network uses 24 hours of observations to predict the next hour’s traffic volume.

Fig. 7: MSE, MAE, and MAPE errors of LSTM and GRU by varying neural network settings. The network uses 6 hours of observations to predict the next hour’s traffic volume.
[MISSING_PAGE_POST]
Metro Interstate Traffic Volume dataset. The experimental results demonstrate the effectiveness of the proposed LSTM and GRU models. Recently, many researchers used graph-based attention modules to predict time series data. Graph-based multi-attention networks could be applied to predict traffic volume as well. To obtain better predictions, however, complete hyperparameter tuning and extensive experiments are required, which are our future works.
