A traffic flow prediction method based on Transformer spatiotemporal graph convolutional network
By introducing the Transformer spatiotemporal graph convolution network in traffic flow prediction, combining the structural multi-head self-attention module and the dynamic evolution graph convolution module, the problem of difficult to capture the dynamic spatial correlation and insufficient long-term prediction accuracy of existing methods is solved. Achieve higher prediction accuracy.
Patent Information
- Application Number
- CN202210009238.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-06
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2042-01-06
AI Technical Summary
Existing traffic flow prediction methods are difficult to capture the dynamic spatial correlation of traffic flow data, and RNN-based methods have problems of gradient disappearance or explosion during long-term prediction.
A traffic flow prediction method based on Transformer spatiotemporal graph convolution network is designed. The road network topological structure information is extracted through the structure of multi-head self-attention module, and the dynamic evolution graph convolution module learns new graphs for each time slice, and uses self-attention. The mechanism dynamically adjusts the correlation intensity between nodes and extracts long-term time correlation.
This method can better extract the spatial and temporal correlation of traffic flow data, improve the accuracy of long-term prediction, and avoid the loss of effective traffic flow data features.
Smart Images

Figure CN114330671B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of traffic flow prediction and relates to a traffic flow prediction method based on a Transformer spatiotemporal graph convolutional network. Background Art
[0002] With the advancement of urbanization, the number of motor vehicles in my country has continued to rise. As of December 2021, the number of motor vehicles reached 393 million, ranking first in the world. Traffic congestion has gradually become one of the major social problems. The application and development of intelligent transportation systems can help alleviate traffic congestion, and traffic flow prediction is one of the keys to ensure the effectiveness of intelligent transportation systems. Accurate traffic flow prediction, especially long-term prediction, can help travelers obtain future traffic information in advance and make better travel plans on the one hand; on the other hand, it can help managers formulate reasonable traffic induction plans and improve traffic operation efficiency, thereby alleviating traffic congestion. Therefore, accurate long-term traffic flow prediction is crucial.
[0003] Traffic flow prediction methods are mainly divided into three categories: time series analysis methods, machine learning methods, and deep learning methods. Since traffic flow data is continuous time series data generated by deployed detectors at fixed time intervals. In the early days, time series analysis methods such as autoregressive integrated moving average (ARIMA) and Kalman filtering were mainly used for prediction. However, they are all based on linear assumptions and require the data to be smooth. However, actual traffic flow data is usually nonlinear, which makes it difficult for time series analysis methods to obtain satisfactory results.
[0004] Machine learning methods such as vector autoregression (VAR) and support vector regression (SVR) perform feature engineering modeling on traffic data, revealing nonlinear relationships in the data, and then continuously adjusting and optimizing model parameters to predict traffic trends. However, machine learning modeling requires professional design and is somewhat difficult. Moreover, the above two methods only consider time information and ignore the importance of spatial information for accurate prediction.
[0005] Deep learning can automatically extract more complex features and has attracted widespread attention in traffic flow prediction. Existing studies have shown that methods based on recurrent neural networks (RNNs), such as long short-term memory networks (LSTMs), have achieved better results than the above-mentioned time series analysis methods and machine learning methods. In order to simultaneously learn the spatiotemporal correlation of traffic data, the method of fusing convolutional neural networks (CNNs) and RNNs is adopted. However, CNNs cannot process non-Euclidean data, resulting in the loss of spatial information contained in the road network. Therefore, the spatiotemporal graph convolutional network framework is widely used in traffic flow prediction. Among them, the nodes of the graph are traffic detectors, and the edges and weights are determined by the connectivity and Euclidean distance between the detectors. To this end, some scholars have proposed introducing graph convolutional networks (GCNs) into the field of traffic flow prediction, using spatiotemporal convolutions to simultaneously capture spatiotemporal correlations. Other scholars have further captured spatial correlations by constructing an adaptive adjacency matrix, and used temporal convolutional networks (TCNs) to capture temporal correlations.
[0006] Although the above methods have achieved good results in traffic flow prediction, in fact, the spatial correlation of traffic flow data is dynamically affected by factors such as road structure and grade, weather changes, and sudden accidents. However, it is difficult for existing methods to capture this spatial dynamic change. Moreover, for long-term predictions, RNN-based methods have the problem of gradient disappearance or explosion; CNN-based methods are limited by limited receptive fields. Although TCN achieves an exponentially growing receptive field. However, stacked convolutional layers still reduce the performance of the model. Therefore, existing methods also have certain difficulties in long-term prediction. Transformer uses a highly parallel self-attention mechanism to achieve efficient sequence learning, which can adaptively capture long-term temporal dependencies from the input sequence. Summary of the invention
[0007] In view of this, the purpose of the present invention is to provide a traffic flow prediction method based on Transformer spatiotemporal graph convolution network, design a new Transformer based Spatial-Temporal Graph Convolution Network model (Transformer basedSpatial-Temporal Graph Convolution Network, Trans-STGCN), design a structural multi-head self-attention module to extract road network topological structure information; design a dynamic evolution graph convolution module to learn a new graph for each time slice, and use the self-attention mechanism to adaptively and dynamically adjust the correlation strength between nodes; design a temporal multi-head self-attention module and time position embedding to extract time correlation. And by optimizing the Transformer structure, Trans-STGCN has a more accurate long-term prediction capability. This method can better extract spatiotemporal correlation and improve prediction accuracy.
[0008] In order to achieve the above object, the present invention provides the following technical solutions:
[0009] A traffic flow prediction method based on Transformer spatiotemporal graph convolutional network, the method comprising the following steps:
[0010] S1. Construct a static adjacency matrix based on the detectors deployed on the road network and their connectivity and Euclidean distance;
[0011] S2, merging the raw traffic flow data collected by the detector at a specified time interval;
[0012] S3, construct a traffic flow spatiotemporal graph G = (V, E, A), and normalize the data set using the maximum and minimum method, and divide the data set into a training set and a test set;
[0013] S4. Construct a Transformer-based Spatial-Temporal Graph Convolution Network prediction model Trans-STGCN (Transformerbased Spatial-Temporal Graph Convolution Network, Trans-STGCN); the prediction model uses Transformer as the backbone network, which includes multiple residual blocks, layer normalization blocks, time position embedding, dynamic evolution graph convolution modules and multi-head self-attention modules. The multi-head self-attention module includes a structural multi-head self-attention module and a temporal multi-head self-attention module. The residual block and layer normalization block are serially connected at the end of each dynamic evolution graph convolution module and multi-head self-attention module. The residual block is used to avoid the loss of effective traffic flow data features; the layer normalization block is used to accelerate the training process of the model to make it converge faster; the structural multi-head self-attention module is used to extract road network topology information; the time position embedding and temporal multi-head self-attention module are used to extract the long-term temporal correlation of traffic flow data; the dynamic evolution graph convolution module is used to extract the spatial correlation of traffic flow data;
[0014] S5. Training prediction model: using the training set as the input of the spatiotemporal graph convolutional network prediction model, solving the optimal parameters of the model to complete model training;
[0015] S6. Use the trained spatiotemporal graph convolutional network prediction model to predict traffic flow on the test set, and evaluate and analyze the prediction error based on the prediction results and actual traffic data.
[0016] Further, in step S1, it specifically includes: using the deployed detectors as nodes of the graph; the roads between the detectors are connected by edges between the corresponding nodes; the weight of the static adjacency matrix is calculated by the distance between the two detectors, where a ij Indicates vi and v j The weight between the two sensors, represents the distance between detectors, σ is the standard deviation of the distance, and a weighted static adjacency matrix A∈R is obtained N×N .
[0017] Further, in step S2, it specifically includes: obtaining traffic flow data within the same time range of N detectors, the original traffic flow data is the number of vehicles passed by the detector within a certain time interval; the specified time interval is 5 minutes, and the traffic flow data at a certain moment refers to the traffic flow data that has passed the specified time interval (for example, 5 minutes) from this moment; the data set format is as follows:
[0018]
[0019] in represents the traffic flow data of all nodes at time t, is the traffic flow data of node i at time t, and T is the length of the time series in units of 5 minutes.
[0020] Further, in step S3, the traffic flow spatiotemporal graph G = (V, E, A) is used as the matrix for traffic flow data on time slice t Represents, where V is the set of N nodes, E is the set of edges between two nodes, and A is the static adjacency matrix;
[0021] The maximum and minimum normalized calculation expressions are:
[0022]
[0023] Among them, minX t and maxX t are the minimum and maximum values of the traffic flow data at time t, is the traffic flow data of node i after normalization at time t; predict the future T f When the traffic flow is at the moment, select the first T p The traffic flow at time intervals is taken as input, that is: take tT p +1, tT p +2,…, the traffic flow at time t is used as network input:
[0024]
[0025] Among them, T f Indicates the predicted future time length, T p Represents the historical time length of the input, and t is the current moment.
[0026] Further, in step S4, the construction of the Transformer spatiotemporal graph convolutional network prediction model specifically includes: before the input enters the encoder / decoder, adding a structural multi-head self-attention module to extract the road network topology information; then replacing the sine-cosine position encoding with one-hot encoding, and replacing the original Transformer multi-head self-attention block with a temporal multi-head self-attention module to extract the long-term temporal correlation of traffic flow data; removing the original feedforward neural network layer of the Transformer and replacing it with a dynamically evolving graph convolution module to extract spatial correlation.
[0027] Further, the step S4 specifically includes the following steps:
[0028] S41. Design and implementation of structured multi-head self-attention module:
[0029] The input matrix Transformed into a high-dimensional representation through linear projection Convert X′ to index Q through the weight matrix S , key K S Sum value V S , the calculation formula is as follows:
[0030]
[0031] in, and is the corresponding weight matrix;
[0032] Add the adjacency matrix to calculate the attention weight. The calculation formula is as follows:
[0033] score=softmax(a(Q S ,K S ))=softmax(σ(Aα T [Q S ,K S ]))
[0034] in is the weight vector, A is the weighted static adjacency matrix, [·,·] is the connection operation, σ(·) is the nonlinear activation function, a(Q S ,K S ) is the index Q S With key K S The similarity between features, score represents the attention weight between features;
[0035] Compare score with value V S Multiply to get a head self-attention vector head i :
[0036] head=scoreVS
[0037] The final structure of multi-head self-attention result is:
[0038]
[0039]
[0040] Among them, h is the number of attention heads, is the final output weight matrix;
[0041] S42. Design and implementation of time position embedding:
[0042] The output X of the structured multi-head self-attention module S T on the same day as the historical week f Traffic records X g And the historical day T f Traffic records X l Connect to get an N×(w*T f +d*T f +T p ) of the new sequence [X S ,X g ,X l ], where [·] represents the connection operation, X S is the output of the structured multi-head self-attention module, T p is the input history length, T f is the length of time for the forecast;
[0043] Use one-hot encoding to encode X S , X g and X l Perform time position encoding, connect the encoding results to obtain a time position embedding vector TE; then add the time position embedding vector TE to the above new sequence as the input of the encoder / decoder, that is, in, Represents a bitwise addition operation;
[0044] S43. Design and implementation of temporal multi-head self-attention module:
[0045] The input X containing spatial structure information and temporal position information (0) Transformed into index Q through weight matrix T , key K T Sum value V T ;
[0046] Calculate the relative position vector. When calculating h j For h iWhen considering the attention features, the relative position distance between time i and time j is considered; the maximum intercept distance m is set, and when the relative distance is greater than m, the relative position distance is equal to m; h i and h j The relative position relationship between them can be expressed as and
[0047] Adding relative position information to the attention mechanism, the calculation formula is as follows:
[0048]
[0049] head=score(V T +rp V )
[0050] The result of the temporal multi-head self-attention module is:
[0051]
[0052]
[0053] Among them, h is the number of attention heads, is the final output weight matrix;
[0054] The obtained temporal multi-head self-attention results are subjected to residual connection and layer normalization operations. The layer normalization calculation formula is as follows:
[0055]
[0056] Where E(·) represents the expectation, Var(·) represents the variance, and ∈ is a very small number to avoid division by zero;
[0057] Then perform the residual connection operation, X (0) +LN(H);
[0058] For the l-th layer encoder (decoder), input X (l-1) After performing multi-head self-attention on all nodes, the output is
[0059] S44. Design and implementation of dynamic evolution graph convolution module:
[0060] Use the gated recurrent unit (GRU) to evolve the GCN parameters, namely W t =GRU(H t ,W t-1 ); The GRU calculation process is as follows:
[0061] Z t =sigmoid(W ZX t +U Z H t-1 +B Z )
[0062] R t =sigmoid(W R X t +U R H t-1 +B R )
[0063]
[0064]
[0065] Among them, in order to more easily identify the function of GRU, use X t , H t-1 , and H t As a local variable name instead of H t , W t and W t-1 , ⊙ represents dot product; input is node feature matrix H t (i.e., the output of the temporal multi-head self-attention module after residual connection and layer normalization) and the GCN parameter W of the previous time slice t-1 , the output is the GCN parameter W at the current time t ; In this method, DEGCU (Dynamic Evolving Graph Convolution Unit) is used to refer to the dynamic evolving graph convolution operation;
[0066] Perform matrix multiplication and softmax nonlinear activation calculation on the output of the temporal multi-head self-attention module to obtain the spatial self-attention matrix S t , the calculation formula is as follows:
[0067]
[0068] Among them, S ij represents the correlation strength between node i and node j, is the output of the temporal multi-head self-attention module;
[0069] Generate two static random initial nodes embedding E1 and S t Multiplying by E1 and E2 respectively, we get the source node embedding DE1 and the target node embedding DE2. The calculation formula is as follows:
[0070]
[0071]
[0072] Then the dynamic adjacency matrix DA is calculated using the similarity between nodes. The calculation formula is as follows:
[0073]
[0074] Among them, γ is a hyperparameter that controls the saturation rate of the activation function, and DA t ∈R N×N is the dynamic adjacency matrix at time t;
[0075] The static adjacency matrix and the dynamic adjacency matrix are used for the dynamically evolving graph convolution operation respectively, and we get:
[0076]
[0077]
[0078] And use and Calculate a gate value δ, the calculation formula is as follows:
[0079]
[0080] Among them, f1 and f2 are applied respectively in and Linear projection on ;
[0081] Finally, the output of the dynamic evolution convolution module is determined by the gate value δ and The weighted calculation formula is as follows:
[0082]
[0083] The output of the dynamic evolving convolution module is subjected to residual connection and layer normalization processing (the same operation as described in step S43).
[0084] Further, in step S5, training the prediction model includes:
[0085] Select the objective function, use the training set as the input of the spatiotemporal graph convolutional network prediction model, and solve the optimal model parameters to complete the model training;
[0086] During the training process, the historical data is passed through the structured multi-head self-attention module to extract the spatial structure information, and the new input X is obtained by adding the time position information (0) Input encoder, the encoder passes the input sequence X through L encoder layers (0) Mapped to the intermediate representation sequence X (L) , the decoder uses L decoder layers to generate prediction results The decoder input is X (L)and historical real data; using the mean absolute error (MAE) as the loss function, the calculation formula is as follows:
[0087]
[0088] in, is the model prediction output, y is the actual traffic flow data, n is the number of samples, and the model is optimized through reverse mode automatic differentiation and Adam.
[0089] Further, in step S6, it specifically includes:
[0090] During testing, the historical real data input to the decoder is replaced by the predicted values generated by the model itself, and the decoder input is X (L) and all previously generated The final prediction results are denormalized and converted into real traffic flow data in order to evaluate and analyze the prediction error. The following evaluation criteria are used to evaluate and analyze the prediction error:
[0091] 1) Mean Absolute Error (MAE):
[0092]
[0093] 2) Root Mean Square Error (RMSE):
[0094]
[0095] 3) Mean Absolute Percent Error (MAPE):
[0096]
[0097] in, is the model prediction output, y is the actual traffic flow data, and n is the number of samples; the smaller the mean absolute error, mean absolute percentage error, and root mean square error, the higher the accuracy of the model prediction results.
[0098] The beneficial effects of the present invention are:
[0099] The present invention proposes a traffic flow prediction method based on Transformer spatiotemporal graph convolutional network, which aims to improve the accuracy of traffic flow prediction. In the spatial dimension, the road network topological structure information is extracted through the structural multi-head self-attention module; further, through the dynamic evolution graph convolution module, the dynamic spatial correlation is extracted for each time slice by using graph convolution and gated recurrent unit (GRU), and the dynamic adjacency matrix is constructed by self-attention mechanism, so that the model can adaptively and dynamically adjust the correlation strength between nodes. In the temporal dimension, long-term temporal correlation is extracted by temporal position embedding and temporal multi-head self-attention module. Compared with traditional methods, the prediction method can effectively extract the spatiotemporal correlation in traffic flow data and more fully mine the information in traffic flow data, so it can improve its prediction accuracy. In addition, the method uses residual connection and layer normalization, which to a certain extent avoids the loss of effective traffic flow data features and improves the prediction accuracy.
[0100] Other advantages, objectives and features of the present invention will be described in the following description to some extent, and to some extent, will be obvious to those skilled in the art based on the following examination and study, or can be taught from the practice of the present invention. The objectives and other advantages of the present invention can be realized and obtained through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0101] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention will be described in detail below in conjunction with the accompanying drawings, wherein:
[0102] Figure 1 is a flow chart of the method of the present invention;
[0103] Figure 2 This is a model structure diagram of the Trans-STGCN of the present invention;
[0104] Figure 3 Schematic diagram of the dynamic evolution graph convolution unit;
[0105] Figure 4 Schematic diagram of the dynamic evolution graph convolution module;
[0106] Figure 5 This is a diagram of traffic flow data prediction results of the Trans-STGCN model of the present invention. DETAILED DESCRIPTION
[0107] The technical solution of the present invention is described in detail below with reference to the accompanying drawings.
[0108] Figure 1 is a flow chart of the method of the present invention, such as Figure 1As shown, the present invention provides a traffic flow prediction method based on Transformer spatiotemporal graph convolutional network, which is implemented in the following steps:
[0109] Step 1: The steps of constructing a static adjacency matrix based on the detectors deployed on the road network and their connectivity and Euclidean distance include: using the deployed detectors as nodes of the graph; the road connectivity between the detectors corresponds to the edge connection between the nodes; the weight of the static adjacency matrix is calculated by the distance between two detectors, where a ij Indicates v i and v j The weight between the two sensors, Represents the distance between detectors, and σ is the standard deviation of the distance. We obtain a weighted static adjacency matrix A∈R N×N .
[0110] Step 2: Merge the raw traffic flow data collected by the detectors at the specified time interval. Obtain the traffic flow data of N detectors within the same time range. The raw traffic flow data is the number of vehicles passing by the detectors within a certain time interval. The specified time interval is 5 minutes. The traffic flow data at a certain moment refers to the traffic flow data from this moment through the specified time interval (for example, 5 minutes). The data set format is as follows:
[0111]
[0112] in represents the traffic flow data of all nodes at time t, is the traffic flow data of node i at time t, and T is the length of the time series in units of 5 minutes.
[0113] Step 3: Construct the traffic flow spatiotemporal graph G = (V, E, A), normalize the data using the maximum and minimum method, and divide the data set into a training set and a test set. The traffic flow spatiotemporal graph G = (V, E, A) on time slice t is represented by the matrix Indicates that, V is the set of N nodes, E is the set of edges between two nodes, and A is the static adjacency matrix.
[0114] The maximum and minimum normalized calculation expressions are:
[0115]
[0116] Among them, minX t and maxX t are the minimum and maximum values of the traffic flow data at time t, is the traffic flow data of node i after normalization operation at time t. In the present invention, the future T f When the traffic flow is at the moment, select the first T p The traffic flow at time intervals is taken as input, that is: take tT p +1, tT p +2,…, the traffic flow at time t is used as network input:
[0117]
[0118] Among them, T f Indicates the predicted future time length, T p Represents the historical time length of the input, and t is the current moment.
[0119] Step 4: Figure 2 As shown, Figure 2 The model structure diagram of the Trans-STGCN of the present invention is that the Trans-STGCN provided by the embodiment of the present invention is improved on the basis of the standard Transformer. Specifically, the present invention adds a structural multi-head self-attention module to extract the road network topology information before the input enters the encoder / decoder. Then, the sine-cosine position encoding is replaced by one-hot encoding, and the original multi-head self-attention block of the Transformer is replaced by the temporal multi-head self-attention module to extract the long-term temporal correlation of the traffic flow data. In addition, the present invention removes the original feedforward neural network layer of the Transformer and replaces it with a dynamically evolving graph convolution module to extract spatial correlation.
[0120] 4.1: Design and implementation of the structured multi-head self-attention module.
[0121] The input matrix Transformed into a high-dimensional representation through linear projection Convert X′ to index Q through the weight matrix S , key K S Sum value V S , the calculation formula is as follows:
[0122]
[0123] in, and is the corresponding weight matrix.
[0124] Add the adjacency matrix to calculate the attention weight. The calculation formula is as follows:
[0125] Score = softmax(a(Q S ,K S ))=softmax(σ(Aα T[Q S ,K S ]))
[0126] in is the weight vector, A is the weighted static adjacency matrix, [·,·] is the connection operation, σ(·) is the nonlinear activation function, a(Q S ,K S ) is the index Q S With key K S The score represents the attention weight between features.
[0127] Compare score with value V S Multiply to get a head self-attention vector head i :
[0128] head=scoreV S
[0129] The final structure of multi-head self-attention result is:
[0130]
[0131]
[0132] Among them, h is the number of attention heads, is the final output weight matrix.
[0133] 4.2: Design and implementation of temporal position embedding.
[0134] The output X of the structured multi-head self-attention module S T on the same day as the historical week f Traffic records X g And the historical day T f Traffic records X l Connect to get an N×(w*T f +d*T f +T p ) of the new sequence [X S ,X g ,X l ], where [·] represents the connection operation, X S is the output of the structured multi-head self-attention module, T p is the input history time length, T f is the length of time for prediction.
[0135] Use one-hot encoding to encode X S , X g and X lPerform time position encoding, connect the encoding results to obtain a time position embedding vector TE; then add the time position embedding vector TE to the above new sequence as the input of the encoder / decoder, that is, in, Represents a bitwise addition operation.
[0136] 4.3: Design and implementation of the temporal multi-head self-attention module.
[0137] The input X containing spatial structure information and temporal position information (0) Transformed into index Q through weight matrix T , key K T Sum value V T .
[0138] Calculate the relative position vector. When calculating h j For h i When calculating the attention features, the relative position distance between time i and time j is considered. Set the maximum intercept distance m. When the relative distance is greater than m, the relative position distance is equal to m. i and h j The relative position relationship between them can be expressed as and
[0139] Adding relative position information to the attention mechanism, the calculation formula is as follows:
[0140]
[0141] head=score(V T +rp V )
[0142] The result of the temporal multi-head self-attention module is:
[0143]
[0144]
[0145] Among them, h is the number of attention heads, is the final output weight matrix.
[0146] The obtained temporal multi-head self-attention results are subjected to residual connection and layer normalization operations. The layer normalization calculation formula is as follows:
[0147]
[0148] Among them, E(·) represents expectation, Var(·) represents variance, and ∈ is a very small number used to avoid division by zero.
[0149] Then perform the residual connection operation, X (0) +LN(H).
[0150] For the l-th layer encoder (decoder), input X (l-1) After performing multi-head self-attention on all nodes, the output is
[0151] 4.4: Design and implementation of the dynamically evolving graph convolution module.
[0152] Figure 3 Schematic diagram of the dynamic evolution graph convolution unit. Figure 4 This is a schematic diagram of the dynamic evolution graph convolution module. Figure 3 , Figure 4 As shown, in order to capture dynamic spatial correlation, this design uses GRU to evolve GCN parameters, namely W t =GRU(H t ,W t-1 ). The GRU calculation process is as follows:
[0153] Z t =sigmoid(W Z X t +U Z H t-1 +B Z )
[0154] R t =sigmoid(W R X t +U R H t-1 +B R )
[0155]
[0156]
[0157] Among them, in order to more easily identify the function of GRU, use X t , H t-1 , and H t As a local variable name instead of H t , W t and W t-1 , ⊙ represents dot product. Input is node feature matrix H t (i.e., the output of the temporal multi-head self-attention module after residual connection and layer normalization) and the GCN parameter W of the previous time slice t-1 , the output is the GCN parameter W at the current time tIn this design, DEGCU (Dynamic Evolving Graph Convolution Unit) is used to refer to the dynamic evolving graph convolution operation.
[0158] Perform matrix multiplication and softmax nonlinear activation calculation on the output of the temporal multi-head self-attention module to obtain the spatial self-attention matrix S t , the calculation formula is as follows:
[0159]
[0160] Among them, S ij represents the correlation strength between node i and node j, is the output of the temporal multi-head self-attention module.
[0161] Generate two static random initial nodes embedding E1 and S t Multiplying by E1 and E2 respectively, we get the source node embedding DE1 and the target node embedding DE2. The calculation formula is as follows:
[0162]
[0163]
[0164] Then the dynamic adjacency matrix DA is calculated using the similarity between nodes. The calculation formula is as follows:
[0165]
[0166] Among them, γ is a hyperparameter that controls the saturation rate of the activation function, and DA t ∈R N×N is the dynamic adjacency matrix at time t.
[0167] The static adjacency matrix and the dynamic adjacency matrix are used for the dynamically evolving graph convolution operation respectively, and we get:
[0168]
[0169]
[0170] And use and Calculate a gate value δ, the calculation formula is as follows:
[0171]
[0172] Among them, f1 and f2 are applied respectively in and Linear projection on .
[0173] Finally, the output of the dynamic evolution convolution module is determined by the gate value δ and The weighted calculation formula is as follows:
[0174]
[0175] The output of the dynamic evolution convolution module is subjected to residual connection and layer normalization processing (the same operation as described in 4.3).
[0176] For the l-th layer encoder, the input H (l-1) After performing dynamic evolution graph convolution on all nodes, the output is
[0177] Step 5: Train the prediction model.
[0178] Select the objective function, use the training set as the input of the spatiotemporal graph convolutional network prediction model, solve the optimal model parameters and complete the model training.
[0179] During the training process, the historical data is passed through the structured multi-head self-attention module to extract the spatial structure information, and the new input X is obtained by adding the time position information (0) Input encoder, the encoder passes the input sequence X through L encoder layers (0) Mapped to the intermediate representation sequence X (L) , the decoder uses L decoder layers to generate prediction results The decoder input is X (L) and historical real data. This design uses the mean absolute error (MAE) as the loss function, and the calculation formula is as follows:
[0180]
[0181] in, is the model prediction output, y is the actual traffic flow data, and n is the number of samples. The model is optimized by reverse mode automatic differentiation and Adam.
[0182] Step 6: Use the trained spatiotemporal graph convolutional network prediction model to predict traffic flow on the test set, and evaluate and analyze the prediction error based on the prediction results and actual traffic data.
[0183] During testing, the historical real data input to the decoder is replaced by the predicted values generated by the model itself, and the decoder input is X (L) and all previously generated The final prediction results are denormalized and converted into real traffic flow data to evaluate and analyze the prediction error. In this embodiment, the following evaluation criteria are used to evaluate and analyze the prediction error:
[0184] 1) Mean Absolute Error (MAE):
[0185]
[0186] 2) Root Mean Square Error (RMSE):
[0187]
[0188] 3) Mean Absolute Percent Error (MAPE):
[0189]
[0190] in, is the model prediction output, y is the actual traffic flow data, and n is the number of samples. The smaller the mean absolute error, mean absolute percentage error, and root mean square error, the higher the accuracy of the model prediction results.
[0191] Example:
[0192] 1) Select experimental data
[0193] The data collected every 30 seconds by the Performance Measurement System (PeMS) of the California State Transportation Agency (CalTrans) is selected as the data set. The original traffic flow data is aggregated into 5-minute intervals, including the data of 170 detectors of Dstrict08 from July 1, 2016 to August 31, 2016. The data of the first 43 days is used as the training set for model parameter training. The data of the last 19 days is used as the test set for prediction.
[0194] 2) Parameter determination
[0195] In the process of building the model, the main parameters involved are: the number of encoder / decoder layers L, the amount of historical traffic flow data T p , predicted traffic flow data volume T f , model dimension d model , maximum interception distance m, historical days d, historical weeks w, convolution kernel K, number of self-attention heads h, control activation function saturation rate parameter γ, the parameter settings are shown in Table 1.
[0196] parameter Settings parameter Settings parameter Settings parameter Settings parameter Settings L 4 <![CDATA[T p ]]> 12 <![CDATA[T f ]]> 12 <![CDATA[d model ]]> 64 m 2 d 1 w 1 K 3 h 8 γ 3
[0197] Table 1
[0198] 3) Experimental results
[0199] Six baseline methods (ARIMA, SVR, FC-LSTM, STGCN, GMAN, ASTGNN) were selected together with the method proposed in this paper to perform the traffic flow prediction task. The results are shown in Table 2.
[0200] method MAE RMSE MAPE(%) ARIMA 25.73 45.30 19.88 SVR 24.99 36.05 17.39 FC-LSTM 24.71 34.66 18.50 STGCN 18.01 11.32 28.44 GMAN 14.66 22.89 9.47 ASTGNN 12.72 22.60 8.78 Trans-STGCN(ours) 12.63 22.45 8.68
[0201] Table 2
[0202] According to Table 2, our STDE-GCN shows superior performance compared with existing prediction methods. In addition, the present invention also selects the prediction results of a detector for one day and the true value for visualization. The results are as follows: Figure 5 shown.
[0203] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified without departing from the purpose and scope of the technical solution, which should be included in the scope of the claims of the present invention.
Claims
1. A traffic flow prediction method based on Transformer spatiotemporal graph convolutional network, characterized by: The method comprises the following steps: S1. Construct a static adjacency matrix based on the detectors deployed on the road network and their connectivity and Euclidean distance; S2, merging the raw traffic flow data collected by the detector at a specified time interval; S3, construct a traffic flow spatiotemporal graph G = (V, E, A), and normalize the data set using the maximum and minimum method, and divide the data set into a training set and a test set; S4. Construct Transformer-based spatiotemporal graph convolutional network prediction model Trans-STGCN; S5. Training prediction model: using the training set as the input of the spatiotemporal graph convolutional network prediction model to solve the optimal model parameters and complete model training; S6. Use the trained spatiotemporal graph convolutional network prediction model to predict traffic flow on the test set, and evaluate and analyze the prediction error based on the prediction results and actual traffic data; In step S1, the following steps are specifically performed: the deployed detectors are used as nodes of the graph; the paths between the detectors are connected by edges between the corresponding nodes; the weight of the static adjacency matrix is calculated by the distance between the two detectors. where a ij Indicates v i and v j The weight between the two sensors, represents the distance between detectors, σ is the standard deviation of the distance, and a weighted static adjacency matrix A∈R is obtained N×N ; In step S2, it specifically includes: obtaining traffic flow data within the same time range of N detectors, the original traffic flow data is the number of vehicles passing by the detector within a certain time interval; the specified time interval is 5 minutes, and the traffic flow data at a certain moment refers to the traffic flow data that has passed the specified time interval from this moment; the data set format is as follows: in represents the traffic flow data of all nodes at time t, is the traffic flow data of node i at time t, T is the length of the time series in units of 5 minutes; In step S3, the traffic flow spatiotemporal graph G = (V, E, A) is used as the matrix for the traffic flow data at time slice t. Represents, where V is the set of N nodes, E is the set of edges between two nodes, and A is the static adjacency matrix; The maximum and minimum normalized calculation expressions are: Among them, minX t and maxX t are the minimum and maximum values of the traffic flow data at time t, is the traffic flow data of node i after normalization at time t; predict the future T f When the traffic flow is at the moment, select the first T p The traffic flow at time intervals is taken as input, that is: take tT p +1,tT p +2,…, the traffic flow at time t is used as network input: Among them, T f Indicates the predicted future time length, T p Represents the input history time length, t is the current moment; In step S4, the construction of the Transformer spatiotemporal graph convolutional network prediction model specifically includes: before the input enters the encoder / decoder, adding a structural multi-head self-attention module to extract the road network topology information; then replacing the sine-cosine position encoding with one-hot encoding, and replacing the original Transformer multi-head self-attention block with a temporal multi-head self-attention module to extract the long-term temporal correlation of traffic flow data; removing the original feedforward neural network layer of the Transformer and replacing it with a dynamically evolving graph convolution module to extract spatial correlation; The step S4 specifically comprises the following steps: S41. Design and implementation of structured multi-head self-attention module: The input matrix Transformed into a high-dimensional representation through linear projection Convert X' to index Q through the weight matrix S , key K S Sum value V S , the calculation formula is as follows: in, and is the corresponding weight matrix; Add the adjacency matrix to calculate the attention weight. The calculation formula is as follows: score=softmax(a(Q S ,K S ))=softmax(σ(Aα T [Q S ,K S ])) in is the weight vector, A is the weighted static adjacency matrix, [·,·] is the connection operation, σ(·) is the nonlinear activation function, a(Q S ,K S ) is the index Q S With key K S The similarity between features, score represents the attention weight between features; Compare score with value V S Multiply to get a head self-attention vector head i : head=scoreV S The final structure of multi-head self-attention result is: Among them, h is the number of attention heads, is the final output weight matrix; S42. Design and implementation of time position embedding: The output X of the structured multi-head self-attention module S T on the same day as the historical week f Traffic records X g And the historical day T f Traffic records X l Connect to get an N×(w*T f +d*T f +T p ) of the new sequence [X S ,X g ,X I ], where [·] represents the connection operation, X S is the output of the structured multi-head self-attention module, T p is the input history time length, T f is the length of time for the forecast; Use one-hot encoding to encode X S , X g and X l Perform time position encoding, connect the encoding results to obtain a time position embedding vector TE; then add the time position embedding vector TE to the above new sequence as the input of the encoder / decoder, that is, X (0) =[X S ,X g ,X l ]⊕TE, where ⊕ represents a bit-by-bit addition operation; S43. Design and implementation of temporal multi-head self-attention module: The input X containing spatial structure information and temporal position information (0) Transformed into index Q through weight matrix T , key K T Sum value V T ; Calculate the relative position vector. When calculating h j For h i When considering the attention features, the relative position distance between time i and time j is considered; the maximum intercept distance m is set, and when the relative distance is greater than m, the relative position distance is equal to m; h i and h j The relative position relationship between them can be expressed as and Adding relative position information to the attention mechanism, the calculation formula is as follows: head=score(V T +rp V ) The result of the temporal multi-head self-attention module is: Among them, h is the number of attention heads, is the final output weight matrix; The obtained temporal multi-head self-attention results are subjected to residual connection and layer normalization operations. The layer normalization calculation formula is as follows: Where E(·) represents the expectation, Var(·) represents the variance, and ∈ is a very small number to avoid division by zero; Then perform the residual connection operation, X (0) +LN(H); For the l-th layer encoder, input X (l-1) After performing multi-head self-attention on all nodes, the output is S44. Design and implementation of dynamic evolution graph convolution module: Use GRU to evolve GCN parameters, namely W t =GRU(H t ,W t-1 ); The GRU calculation process is as follows: Z t =sigmoid(W Z X t +U Z H t-1 +B Z ) R t =sigmoid(W R X t +U R H t-1 +B R ) Among them, in order to more easily identify the function of GRU, use X t , H t-1 , and H t As a local variable name instead of H t , W t and W t-1 , ⊙ represents dot product; input is node feature matrix H t and the GCN parameter W of the previous time slice t-1 , the output is the GCN parameter W of the current time slice t ;DEGCU is used to refer to the dynamically evolving graph convolution operation; Perform matrix multiplication and softmax nonlinear activation calculation on the output of the temporal multi-head self-attention module to obtain the spatial self-attention matrix S t , the calculation formula is as follows: Among them, S ij represents the correlation strength between node i and node j, is the output of the temporal multi-head self-attention module; Generate two static random initial nodes embedding E1 and S t Multiplying by E1 and E2 respectively, we get the source node embedding DE1 and the target node embedding DE2. The calculation formula is as follows: Then the dynamic adjacency matrix DA is calculated using the similarity between nodes. The calculation formula is as follows: Among them, γ is a hyperparameter that controls the saturation rate of the activation function, and DA t ∈R N×N is the dynamic adjacency matrix at time t; The static adjacency matrix and the dynamic adjacency matrix are used for the dynamically evolving graph convolution operation respectively, and we get: And use and Calculate a gate value δ, the calculation formula is as follows: Among them, f1 and f2 are applied respectively in and Linear projection on ; Finally, the output of the dynamic evolution convolution module is determined by the gate value δ and The weighted calculation formula is as follows: The output of the dynamic evolving convolution module is subjected to residual connection and layer normalization processing by the operation in step S43; For the l-th layer encoder, the input H (l-1) After performing dynamic evolution graph convolution on all nodes, the output is In step S5, the prediction model is trained: Select the objective function, use the training set as the input of the spatiotemporal graph convolutional network prediction model, and solve the optimal model parameters to complete the model training; During the training process, the historical data is passed through the structured multi-head self-attention module to extract the spatial structure information, and the new input X is obtained by adding the time position information (0) Input encoder, the encoder passes the input sequence X through L encoder layers (0) Mapped to the intermediate representation sequence X (L) , the decoder uses L decoder layers to generate prediction results The decoder input is X (L) and historical real data; using the mean absolute error (MAE) as the loss function, the calculation formula is as follows: in, is the model prediction output, y is the actual traffic flow data, n is the number of samples, and the model is optimized through reverse mode automatic differentiation and Adam.
2. The traffic flow prediction method based on Transformer spatiotemporal graph convolutional network according to claim 1 is characterized by: In step S6, it specifically includes: During testing, the historical real data input to the decoder is replaced by the predicted values generated by the model itself, and the decoder input is X (L) and all previously generated The final prediction results are denormalized and converted into real traffic flow data in order to evaluate and analyze the prediction error. The following evaluation criteria are used to evaluate and analyze the prediction error: 1) Mean absolute error MAE: 2) Root mean square error RMSE: 3) Mean absolute percentage error MAPE: in, is the model prediction output, y is the actual traffic flow data, and n is the number of samples; the smaller the mean absolute error, mean absolute percentage error, and root mean square error, the higher the accuracy of the model prediction results.
Citation Information
Patent Citations
Optimized traffic flow prediction model based on space-time diagram convolutional network
CN113505536A
Traffic flow prediction method based on asynchronous space-time expansion graph convolutional network
CN113570859A