Traffic flow multi-step prediction method based on multi-scale grid attention neural network
Through multi-scale grid attention neural network, long-distance dependence and spatial dependence between regions are constructed, which solves the problem of insufficient traffic flow pattern recognition and long-distance spatio-temporal region interaction relationship capture of existing models on different scales, and achieves more accurate traffic flow prediction.
Patent Information
- Application Number
- CN202510380507.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-28
- Publication Date
- 2025-08-01
AI Technical Summary
The existing traffic flow prediction models lack the ability to recognize specific traffic flow patterns on different scales and capture the interaction relationship between long-distance spatiotemporal regions, resulting in insufficient prediction accuracy.
A multi-scale grid attention neural network is adopted. By dividing the geographical area to be predicted into multiple spatial grids, combining encoding and decoding calculations, long-distance dependencies and spatial dependencies between regions are constructed, and flow states at different scales are captured using ConvLSTM, and information fusion and prediction are achieved through the grid attention model.
It improves the accuracy of traffic flow prediction, can better identify potential connections between different urban areas, and improves prediction accuracy at different scales.
Smart Images

Figure CN120408133A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a traffic flow prediction method, in particular to a multi-step traffic flow prediction method based on a multi-scale grid attention neural network. Background Art
[0002] Accurate prediction of traffic flow is one of the key technologies for the daily operation and management decision-making of smart cities. Multi-step traffic flow prediction refers to predicting the traffic flow state in multiple future time slices given the current traffic flow situation in several time slices. In the technical field of multi-step traffic flow prediction, traditional statistical and dynamic methods have gradually been replaced by neural networks based on deep learning. Neural networks for multi-step traffic flow prediction can be divided into three categories: seq2seq-based models, GNN-based models, and transformer-based models.
[0003] However, the current models mainly have two defects. On the one hand, they lack the ability to identify and capture specific traffic flow patterns at different scales; for example, in a small spatio-temporal slice area, the road connections of a region to adjacent regions form its own traffic flow pattern in a very short time. On a larger spatio-temporal scale, the area not only consists of roads, but may also contain different types of urban functional areas; on weekdays, the main traffic flow is between residential areas and commercial areas, while on weekends, the focus of traffic flow will shift to residential areas and consumer goods areas. If the road connections between regions are dense, the change rate of traffic flow in both cases will also be faster, while for a larger span between regions that require traffic connections to be established through multiple intermediate regions, the change in traffic flow will be relatively slower. On the other hand, they lack the ability to capture the interaction relationship between long-distance spatio-temporal regions; the change in urban traffic flow is not only related to the change in time, but also related to the spatial relationship between regions. As time goes by, the change in traffic flow around the city will affect the traffic flow in the core area.
[0004] Therefore, the existing models not only lack an effective method for capturing specific traffic flow patterns at different scales, but also lack a method for capturing the interaction relationship between long-distance spatio-temporal regions.
[0005] An existing traffic flow prediction method and system based on temporal dependence attention sparse convolution with the publication number CN116153087A discloses that: First, the target city is divided into multiple grid regions, and at the same time, the concepts of traffic inflow and traffic outflow are defined to calculate the traffic inflow and traffic outflow of any grid region. Then, historical traffic flow data is generated based on the traffic inflow and outflow of this region, and a convolutional layer is used to extract hidden features to obtain a three-dimensional feature tensor. Then, based on the improved ConvLSTM network, spatio-temporal feature sequences are extracted from the three-dimensional feature tensor, and then the temporal correlation attention features between multiple time intervals are calculated. Then, the deeply extracted spatio-temporal features and temporal correlation attention features are fused to obtain a fused feature. Finally, based on this fused feature, the three-dimensional feature tensor in the future time interval is predicted.
[0006] However, in the above-mentioned published document, when using ConvLSTM to extract the characteristics of crowd flow, the input data scale is still the spatio-temporal data of the original size. The relationship between the spatio-temporal feature regions extracted by ConvLSTM is limited by the size of the convolution kernel, and it can only reflect local traffic flow characteristics, ignoring the influence of spatial relationships on prediction, and there are defects in long-distance dependence relationships, thus affecting the accuracy of traffic flow prediction. Summary of the Invention
[0007] The purpose of the present invention is to provide a multi-step traffic flow prediction method based on a multi-scale grid attention neural network. The present invention has the characteristics of capturing spatial and temporal features of different scales, constructing long-distance dependence relationships and spatial dependence relationships between regions, and improving the accuracy of traffic flow prediction.
[0008] The technical solution of the present invention: A multi-step traffic flow prediction method based on a multi-scale grid attention neural network includes the following steps:
[0009] S1. Divide the geographical area to be predicted into W×H spatial grids;
[0010] S2. Let P t ∈R W×H×2 be a spatio-temporal slice of the geographical area to be predicted, where t is the time slice, and a spatio-temporal slice contains W×H spatial grids, and each spatial grid contains the inflow number and the outflow number;
[0011] S3. Let X = {X1, X2, X3,..., X n} be a data set, where X t ∈R S×W×H×2 = {P t , P t+1 , P t+2 ,..., P t+S}\(Traffic flow\) at time \(t\), \(S\) ranges from \(4\) to \(32\), \(t = [1,...,n]\); Let \(Y=\{Y S+2 ,Y S+3 ,Y S+4 ,...,Y S+n-O+1 \}\) be the label of the dataset \(X\), where \(Y S+t \in R O×W×H×2 =\{P S+t+1 ,P S+t+2 ,...,P S+t+O \}\), \(t = [1,...,n - O]\), called the label of the traffic flow at time \(t\), \(O\) is the number of prediction time slice steps;
[0012] S4. For the dataset \(X\), perform forward calculation through the grid attention neural network model to obtain the predicted values of the traffic flow for the future \(O\) - step spatio - temporal slices;
[0013] The forward calculation includes encoding calculation and decoding calculation. The encoding calculation includes the following steps:
[0014] S5.1 Let \(block\) be the scale parameter, with a value range of \(\{1,2,4,8\}\), and the default value is \(2\);
[0015] S5.2 Define \(states\) as an array of size \(block + 1\), and each item in the array is a zero - filled tensor of shape \(W\times H\times16\); Define \(Hset\) as an array of size \(S\);
[0016] S5.3 Let \(j = 1\);
[0017] S5.4 Perform low - scale mode calculation operations on \(P t+j \) to obtain the feature tensor \(h t+j \) and the state tensor \(S t+j \);
[0018] S5.5 Let \(states[0]=\{h t+j ,S t+j \}\), \(Hset[j]=h t+j \);
[0019] S5.6 Perform high - scale mode calculation operations on \(h t+j \) and \(states[1]\) to obtain the feature tensor \(k t+j \) and the state tensor \(S' t+j;
[0020] S5.7 Let \(j = j + 1\). If \(j\leq S\), then execute S5.4, otherwise execute S6;
[0021] The decoding calculation includes the following steps:
[0022] S6.1 Let \(j = 1\);
[0023] S6.2 Let h be a tensor of all zeros with shape W / 2 b ×H / 2 b ×32*b;
[0024] S6.3 Let b = block;
[0025] S6.4 Perform a ConvLSTM operation on h with 32*b input channels and 32*b output channels to obtain a feature tensor h';
[0026] S6.5 Perform four deconvolution operations on the feature tensor h' to obtain a feature map tensor h";
[0027] S6.6 Perform a deconvolution operation on the feature map tensor h" to obtain a feature map tensor k;
[0028] S6.7 Perform an ELU activation operation on k to obtain k' with the same tensor shape;
[0029] S6.8 Let states[b] = {k', S}, h = k';
[0030] S6.9 Let b = b - 1, if b ≥ 1, then execute S6.4, otherwise execute S6.10;
[0031] S6.10 Perform a ConvLSTM operation on h with 16 input channels and 2 output channels to obtain a feature tensor h';
[0032] S6.11 Perform grid attention operations on h and Hset respectively to obtain a tensor Y j ';
[0033] S6.12 Let j = j + 1, if j ≤ O, then execute S6.3, otherwise the decoding process ends, and the obtained Y' = {Y' j |j = [1...O]}.
[0034] In the aforementioned traffic flow multi-step prediction method based on a multi-scale grid attention neural network, it is characterized in that: the low-scale mode calculation operation in step S5.4 includes the following steps:
[0035] S5.4.1 Perform a convolution operation on P t in X t+j with a convolution kernel size of 1×1, a convolution stride of 1, a convolution padding of 0, 2 convolution input channels, and 16 convolution output channels to obtain 16 feature map tensors P' t+j of P t+j , and the shape of P' t+j is W×H×16;
[0036] S5.4.2 Perform the ELU activation on the feature map tensor P’ t+j to obtain the activation tensor P” with the same shape t+j ;
[0037] S5.4.3 Perform the ConvLSTM operation on P” t+j where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 16, and the number of convolution output channels is 16, to obtain the feature tensor h t+j and the state tensor S t+j , and the shapes of both tensors are W×H×16.
[0038] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, the high-scale mode calculation operation in step S5.6 includes the following steps:
[0039] S5.6.1 Let b = 1;
[0040] S5.6.2 Perform the convolution operation on h t+j where the convolution kernel size is 3×3, the convolution stride is 2, the convolution padding is 1, the number of convolution input channels is 16*b, and the number of convolution output channels is 32*b, to obtain 32 feature map tensors h’ t+j of h t+j , and the shape of h’t+j is W / 2 b ×H / 2 b ×32*b;
[0041] S5.6.3 Perform the ELU activation on the feature map tensor h’ t+j to obtain the activation tensor h” with the same shape t+j ;
[0042] S5.6.4 Perform four convolution operations on h” t+j where each convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 32*b, and the number of convolution output channels is 32*b. After the convolution operation, 32*b feature map tensors h”’ t+j of h” t+j are output, and the shape of the tensor remains unchanged;
[0043] S5.6.5 Perform the ConvLSTM operation on h”’ t+j where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 32*b, and the number of convolution output channels is 32*b, to obtain the feature tensor k t+j and the state tensor S’ t+j , and the shapes of both tensors remain unchanged;
[0044] S5.6.6 Let states[b] = {k t+j , S’ t+j}, h t+j = k t+j ;
[0045] S5.7.7 Let b = b + 1. If b ≤ block, execute S5.6.2; otherwise, execute S6.
[0046] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, step S6.4 is specifically as follows: Perform a ConvLSTM operation on h, with a convolution kernel size of 3×3, a convolution stride of 1, a convolution padding of 1, a convolution input channel number of 32*b, and a convolution output channel number of 32*b, to obtain a feature tensor h’ and a state tensor S, both of which have a shape of W / 2 b ×H / 2 b ×32*b.
[0047] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, step S6.5 is specifically as follows: Perform four transposed convolution operations on h’, each with a transposed convolution kernel size of 3×3, a transposed convolution stride of 1, a transposed convolution padding of 1, a transposed convolution input channel number of 32*b, and a transposed convolution output channel number of 32*b, to obtain 32*b feature map tensors h” of h’, and the shape of the tensor remains unchanged.
[0048] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, step S6.6 is specifically as follows: When performing a transposed convolution operation on h”, the transposed convolution kernel size is 3×3, the transposed convolution stride is 2, the transposed convolution padding is 1, the transposed convolution input channel number is 32*b, and the transposed convolution output channel number is 16*b, to obtain 16*b feature map tensors k of h”, and the shape of k is W / 2 b-1 ×H / 2 b-1 ×16*b.
[0049] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, step S6.10 is specifically as follows: Perform a ConvLSTM operation on h, where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 16, and the convolution output channel number is 2, to obtain a feature tensor h’ and a state tensor S, both of which have a shape of W×H×2.
[0050] In the aforementioned traffic flow multi-step prediction method based on the multi-scale grid attention neural network, the grid attention operation in step S6.11 includes the following steps:
[0051] S6.11.1 Perform a convolution operation on Hset, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 2, and the number of convolution output channels is 2, to obtain two feature map tensors Hset’ of Hset, and the shape of Hset’ is S×W×H×2;
[0052] S6.11.2 Perform a convolution operation on h, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 2, and the number of convolution output channels is 2, to obtain two feature map tensors h’ of h, and the shape of h’ is W×H×2;
[0053] S6.11.3 Multiply the matrices of Hset’ and h’ to obtain a tensor att, the shape of att is S×W×H×W×H×2, perform a softmax operation on att to obtain a tensor att’ with the same shape, indicating that the S encodings of W×H are spatio-temporal slices, and the decoding of W×H is the weight of the spatio-temporal slice;
[0054] S6.11.4 Multiply the matrices of Hset’ and att’, and then add them to h’ to obtain a tensor Y j ’, and the shape of the tensor Y j ’ is W×H×2, which is the prediction result at the j-th step.
[0055] In the aforementioned traffic flow multi-step prediction method based on a multi-scale grid attention neural network, it also includes the training of the grid attention neural network model, and the training process includes the following steps:
[0056] a. Define the type of the neural network parameter optimizer optim as Adam, where the learning rate is 0.001;
[0057] b. Define the type of the learning rate regulator sche as ExponentialLR, where the gamma parameter value is 0.995;
[0058] c. Define maxepoch = 100;
[0059] d. Let epoch = 1;
[0060] e. Let batch = 1;
[0061] f. Randomly obtain a data from the dataset X to respectively obtain the traffic flow X t at time t and the label Y S+t;
[0062] g. Perform the forward calculation of the grid attention neural network model on X t to obtain Y’;
[0063] h. For YS+t Calculate the RMSE and MAPE losses for Y and Y', denoted as LOSS R and LOSS M , and calculate the total loss LOSS = LOSS R + LOSS M ;
[0064] i. Perform backpropagation on LOSS and update the neural network parameter optimizer optim;
[0065] j. Let batch = batch + 1. If batch ≤ n, execute step f; otherwise, execute step k;
[0066] k. Execute the built-in algorithm of the learning rate regulator sche to update the learning rate;
[0067] l. Let epoch = epoch + 1. If epoch ≤ maxepoch, execute step f; otherwise, the training process ends.
[0068] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0069] The present invention integrates the spatial and temporal features of traffic flow at multiple scales, and constructs the long-distance regional dependence relationship and spatial dependence relationship between regions through the grid attention neural network model, accurately identifying the potential connections between different urban regions, so as to achieve more accurate future traffic flow prediction for urban regions at different scales.
[0070] Specifically, the grid attention neural network model is based on a multi-scale network structure, combined with a pyramid-shaped encoding and decoding structure. It constructs the regional flow state maps at different scales through downsampling, and uses ConvLSTM to capture the flow state at the corresponding scale, improving the area of the region that can be covered by a single network; in the case of high scales, it constructs the long-distance regional dependence relationship through the convolutional neural network, making up for the defect of ConvLSTM in long-distance dependence relationships.
[0071] In addition, due to the process of information fusion in the improvement of the feature map scale, to avoid information loss, the present invention realizes the mapping of the spatio-temporal relationship between the prediction region and the historical region through the grid attention method, allowing the attention calculation between regions to ignore the influence of the distance relationship, intuitively representing the long-distance dependence relationship while reflecting the spatial dependence relationship at different scales, and improving the prediction accuracy of traffic flow.
[0072] Therefore, the present invention has the characteristics of capturing the spatial and temporal features at different scales, constructing the long-distance dependence relationship and spatial dependence relationship between regions, and improving the accuracy of traffic flow prediction. Description of the Drawings
[0073] Figure 1 It is a graph of the inflow and outflow numbers within 30 minutes in Embodiment 2 of the present invention.
[0074] Figure 2 It is an RMSE loss graph of the grid attention neural network model and other models in Embodiment 2 of the present invention.
[0075] Figure 3 It is a MAPE loss graph of the grid attention neural network model and other models in Embodiment 2 of the present invention.
[0076] Figure 4 It is a comparison graph of the predicted values and the true values in Embodiment 2 of the present invention.
[0077] Figure 5 It is a model without the grid attention module ( Figure 5 A) and the model of the present invention ( Figure 5 B) of the prediction error graph.
[0078] Figure 6 It is a heat map of the influence of the prediction results of all regions in the past four time slices on a single grid in Embodiment 2 of the present invention. Detailed Implementation Manner
[0079] The present invention will be further described below in conjunction with embodiments, but it shall not be used as a basis for limiting the present invention.
[0080] Embodiment 1:
[0081] A multi-step traffic flow prediction method based on a multi-scale grid attention neural network includes the following steps:
[0082] S1. Divide the geographical area to be predicted into W×H spatial grids, where W and H respectively represent the width and height of the spatial grid. Depending on the range of the geographical area to be predicted, the values of W and H are different, and W and H can take values between 8 and 128.
[0083] S2. Let P t ∈R W×H×2 be a spatio-temporal slice of the geographical area to be predicted, where t is the time slice. A spatio-temporal slice contains W×H spatial grids, and each spatial grid contains an inflow number and an outflow number. The inflow number represents the inflow traffic number (IF) within the time slice, and the outflow number represents the outflow traffic number (OF) within the time slice.
[0084] S3. Let X = {X1, X2, X3,..., X n} be a data set, where X t ∈R S×W×H×2 = {P t , Pt+1 , P t+2 ,..., P t+S}, which is the traffic flow at time t, where S ranges from 4 to 32 and t = [1,..., n]; let Y = {Y S+2 , Y S+3 , Y S+4 ,..., Y S+n-O+1} be the label of the dataset X, where Y S+t ∈ R O×W×H×2 = {P S+t+1 , P S+t+2 ,..., P S+t+O}, t = [1,..., n - O], which is called the label of the traffic flow at time t. O is the number of prediction time slice steps, and O takes values from 1 to S. When O is 1, it is a single-step prediction.
[0085] For each X t , when t ranges from [1...n - O], there is a corresponding Y S+t .
[0086] S4. Given the above dataset X, perform forward calculation through the grid attention neural network model. The purpose of forward calculation is to obtain the predicted values of the traffic flow for the future O-step spatio-temporal slices for any given traffic flow.
[0087] The grid attention neural network model includes an encoding module and a decoding module, where the encoding module and the decoding module are connected by a grid attention module. The encoding module is used to capture the feature tensors at low scales and high scales in the past, so as to obtain traffic flow patterns at different scales. The grid attention module is used to build a direct relationship between the prediction region and all grids in the historical time slices by referring to the initial embedding state data of multiple historical time slices through attention. The decoding module is used to implement parameter learning for multi-step prediction and obtain the predicted values of the traffic flow for the future O-step spatio-temporal slices.
[0088] Forward calculation includes encoding calculation and decoding calculation.
[0089] S5. Perform encoding calculation on X t , and the specific calculation process is as follows:
[0090] S5.1 Let block be the scale parameter, and its value range is {1, 2, 4, 8}, and the default value is 2;
[0091] S5.2 Define states as an array of size block + 1, and each item in the array is a zero tensor of shape W × H × 16; define Hset as an array of size S;
[0092] S5.3 Let j = 1;
[0093] S5.4 Pair with P t+j Perform low-scale mode calculation operation:
[0094] S5.4.1 Input X t , t = [1,..., n], perform convolution operation on P in X t where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 0, the number of convolution input channels is 2, and the number of convolution output channels is 16, obtaining 16 feature map tensors P' of P t+j t+j with the shape of P' being W×H×16; t+j P' t+j
[0095] S5.4.2 Apply ELU activation to the feature map tensor P' t+j to obtain the activation tensor P" with the same shape t+j ;
[0096] S5.4.3 Perform ConvLSTM (Convolutional Long Short-Term Memory Network) operation on P" t+j where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 16, and the number of convolution output channels is 16, obtaining the feature tensor h and the state tensor S in the low-scale mode t+j t+j , and the shapes of both tensors are W×H×16;
[0097] S5.5 Let states[0] = {h t+j , S t+j}, Hset[j] = h t+j ;
[0098] S5.6 Perform high-scale mode calculation operation on h and states[1], and the specific calculation process is as follows: t+j
[0099] S5.6.1 Let b = 1;
[0100] S5.6.2 Perform convolution operation on h t+j where the convolution kernel size is 3×3, the convolution stride is 2, the convolution padding is 1, the number of convolution input channels is 16*b, and the number of convolution output channels is 32*b, obtaining 32 feature map tensors h' of h t+j t+j h' t+j with the shape of h' being W / 2 b ×H / 2 b ×32*b;
[0101] S5.6.3 Apply ELU activation to the feature map tensor h' t+j Perform ELU activation to obtain the activated tensor h” with unchanged shape t+j ;
[0102] S5.6.4 performs four convolution operations on h” t+j Each convolution has a kernel size of 3×3, a convolution stride of 1, a convolution padding of 1, a convolution input channel number of 32*b, and a convolution output channel number of 32*b. After the convolution operations are completed, 32*b feature map tensors h”’ t+j about h” are output t+j , and the shape of the tensor remains unchanged;
[0103] S5.6.5 performs ConvLSTM operation on h”’ t+j where the kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 32*b, and the convolution output channel number is 32*b, to obtain the feature tensor k t+j and the state tensor S’ t+j , and the shapes of these two tensors remain unchanged;
[0104] S5.6.6 sets states[b] = {k t+j , S’ t+j}, and h t+j = k t+j ;
[0105] S5.6.7 sets b = b + 1. If b ≤ block, execute S5.6.2, otherwise execute S5.7;
[0106] S5.7 sets j = j + 1. If j ≤ S, execute S5.4, otherwise execute S6;
[0107] S6. Decode and calculate states. The specific calculation process is as follows:
[0108] S6.1 sets j = 1;
[0109] S6.2 sets h to a tensor of all zeros with a shape of W / 2 b ×H / 2 b ×32*b;
[0110] S6.3 sets b = block;
[0111] S6.4 performs ConvLSTM operation on h, where the kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 32*b, and the convolution output channel number is 32*b, to obtain the feature tensor h’ and the state tensor S. The shapes of these two tensors are both W / 2 b ×H / 2 b ×32*b;
[0112] Perform four deconvolution operations on h', with a deconvolution kernel size of 3×3 each time, a deconvolution stride of 1, a deconvolution padding of 1, a deconvolution input channel number of 32*b, and a deconvolution output channel number of 32*b, to obtain 32*b feature map tensors h" of h', and the shape of the tensors remains unchanged;
[0113] Perform a deconvolution operation on h", where the deconvolution kernel size is 3×3, the deconvolution stride is 2, the deconvolution padding is 1, the deconvolution input channel number is 32*b, and the deconvolution output channel number is 16*b, to obtain 16*b feature map tensors k of h", and the shape of k is W / 2 b-1 ×H / 2 b-1 ×16*b;
[0114] Perform an ELU activation operation on k to obtain k' with an unchanged tensor shape;
[0115] Let states[b] = {k', S}, h = k';
[0116] Let b = b - 1, if b ≥ 1, then execute S6.4, otherwise execute S6.10;
[0117] Perform a ConvLSTM operation on h, where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 16, and the convolution output channel number is 2, to obtain a feature tensor h' and a state tensor S, and the shapes of both tensors are W×H×2;
[0118] Perform grid attention operations on h and Hset respectively, and the specific operation process is as follows:
[0119] Perform a convolution operation on Hset, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 2, and the convolution output channel number is 2, to obtain 2 feature map tensors Hset' of Hset, and the shape of Hset' is S×W×H×2;
[0120] Perform a convolution operation on h, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 2, and the convolution output channel number is 2, to obtain 2 feature map tensors h' of h, and the shape of h' is W×H×2;
[0121] S6.11.3 Multiply the matrices of Hset’ and h’ to obtain the tensor att. The shape of att is S×W×H×W×H×2. Perform the softmax operation on att to obtain the tensor att’ with the same shape, indicating that the S encodings of W×H are spatio-temporal slices, and the decoding of W×H is the weight of the spatio-temporal slice;
[0122] S6.11.4 Multiply the matrices of Hset’ and att’, and then add the result to h’ to obtain the tensor Y j ’. The shape of the tensor Y j ’ is W×H×2, which is the prediction result at the j-th step;
[0123] S6.12 Let j = j + 1. If j ≤ O, execute S6.3; otherwise, the decoding process ends, and the obtained Y’ = {Y’ j |j = [1...O]} is the result of the forward calculation.
[0124] The present invention also includes the training of the grid attention neural network model in S4. The specific training process is as follows:
[0125] a. Define the type of the neural network parameter optimizer optim as Adam, with a learning rate of 0.001;
[0126] b. Define the type of the learning rate regulator sche as ExponentialLR, with a gamma parameter value of 0.995;
[0127] c. Define maxepoch = 100;
[0128] d. Let epoch = 1;
[0129] e. Let batch = 1;
[0130] f. Randomly obtain a data from the dataset X to respectively obtain the traffic flow X t at time t and the label Y S+t;
[0131] g. Perform the forward calculation of the grid attention neural network model on X t to obtain Y’;
[0132] h. Calculate the RMSE and MAPE losses for Y S+t and Y’, denoted as LOSS R and LOSS M , and calculate the total loss LOSS = LOSS R + LOSS M ;
[0133] i. Perform backpropagation on LOSS and update the neural network parameter optimizer optim;
[0134] j. Let batch = batch + 1. If batch ≤ n, execute step f; otherwise, execute step k;
[0135] k. Execute the built-in algorithm of the learning rate regulator sche to update the learning rate;
[0136] l. Let epoch = epoch + 1. If epoch ≤ maxepoch, execute step f; otherwise, the training process ends.
[0137] Embodiment 2:
[0138] To further explain the specific usage method and actual effect of the present invention, it is illustrated by a specific application example of Embodiment 2. Select the taxi trajectory data in Beijing from 2016 to 2018 for three years as the data set X, and perform grid processing on all taxi trajectory data. The specific method is as follows:
[0139] Divide the eight major urban areas in Beijing into grid areas of W×H = 32×32, take 30 minutes as a time slice, and calculate the number of incoming vehicles and the number of outgoing vehicles in each grid within 30 minutes from 2016 to 2018 according to the taxi trajectory data. Taking a certain time slice as an example, the inflow number (IF) and the outflow number (OF) within these 30 minutes are as Figure 1 shown.
[0140] After filtering out the abnormal and missing time slices in the inflow number (IF) and the outflow number (OF), a total of 18,130 valid time slices are obtained. Randomly select 70% of them for training, select S = 4, O = 5, and train the grid attention neural network model according to the training method in the present invention. Use the remaining 30% to test the trained grid attention neural network model (denoted as MN-STFN), and compare the test results with other commonly used models (such as LSTM model, ConvLSTM model, SA-ConvLSTM model, ST-Resnet model, ATTConvLSTM) for the RMSE loss, MAPE loss, and the real results respectively. The comparison results are shown in Figures 2-4 shown.
[0141] From Figures 2-3 it can be seen that compared with other models, the grid attention neural network model of the present invention has obtained the minimum values for the RMSE loss and MAPE loss in multi-step prediction.
[0142] From Figure 4 it can be seen that the prediction result of the present invention basically fits the trend of the real result, and the prediction result has a high accuracy.
[0143] To more intuitively visualize the impact of grid attention on the model, Figure 5 the prediction errors of two models with and without the grid attention module were compared. It can be seen that for the model without the grid attention module ( Figure 5 A), the prediction errors are distributed around all traffic flow routes, while for the model with the grid attention module ( Figure 5 B), that is, the grid attention neural network model of the present invention, the prediction errors are reduced to near the main roads with fast urban traffic flow, with smaller errors and higher accuracy.
[0144] Figure 6 The heatmap of the impact of all regions on the prediction results of a single grid for the past four time slices (x1 is the time slice from 14:30 to 15:00, x2 is the time slice from 15:00 to 15:30, x3 is the time slice from 15:30 to 16:00, and x4 is the time slice from 16:00 to 16:30). In the heatmap, the brighter the color of the region, the greater the connection between the flow in that region and the predicted value of the predicted region, while the darker the color, the smaller the impact of the flow in that region on the predicted region. From Figure 6 the markings in it, it can be seen that the network tends to establish connections between individual regions and important traffic nodes, and as the time span increases, the network reduces the connections in hot regions and tends to establish global connections for a single region. This further proves the effectiveness of the grid attention neural network model in establishing global dependencies.
[0145] It should be understood that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them. For those skilled in the art, the technical solutions recorded in the above embodiments can be modified, or some of the technical features can be equivalently replaced; and all such modifications and replacements should fall within the protection scope of the appended claims of the present invention.
Claims
1. A multi-step traffic flow prediction method based on a multi-scale grid attention neural network, characterized in that: It includes the following steps: S1. Divide the geographical area to be predicted into W×H spatial grids; S2. Let P t ∈R W×H×2 be a spatio-temporal slice of the geographical area to be predicted, where t is a time slice, and a spatio-temporal slice contains W×H spatial grids, and each spatial grid contains an inflow number and an outflow number; S3. Let \(X = \{X_1, X_2, X_3, \ldots, X\) n \} be the data set, where \(X\) t \(\in R\) S×W×H×2 \(= \{P\) t , P\) t+1 , P\) t+2 , \ldots, P\) t+S \} is the traffic flow at time \(t\), \(S\) is from 4 to 32, \(t = [1, \ldots, n]\); Let \(Y = \{Y\) S+2 , Y\) S+3 , Y\) S+4 , \ldots, Y\) S+n-O+1 \} be the label of the data set \(X\), where \(Y\) S+t \(\in R\) O×W×H×2 \(= \{P\) S+t+1 , P\) S+t+2 , \ldots, P\) S+t+O \}, \(t = [1, \ldots, n - O]\), which is called the label of the traffic flow at time \(t\), and \(O\) is the number of prediction time slice steps; S4. For the dataset X, perform forward calculation through the grid attention neural network model to obtain the predicted traffic flow values of the future O-step spatio-temporal slices; The forward calculation includes encoding calculation and decoding calculation. The encoding calculation includes the following steps: S5.1 Let block be the scale parameter, with a value range of {1, 2, 4, 8}, and the default value is 2; S5.2 Define states as an array of size block + 1, and each item in the array is a zero tensor of shape W×H×16; Define Hset as an array of size S; S5.3 Let j = 1; S5.4 pairs with P t+j Perform a low-scale mode calculation operation to obtain the feature tensor h t+j and the state tensor S t+j ; S5.5 Let states[0] = {h t+j , S t+j}, Hset[j] = h t+j ; S5.6 pairs with h t+j and states[1] perform high-scale mode calculation operations to obtain the feature tensor k t+j and the state tensor S' t+j; S5.7 Let j = j + 1. If j ≤ S, execute S5.4, otherwise execute S6; The decoding calculation includes the following steps: S6.1 Let j = 1; S6.2 Let h be a tensor of all zeros with shape W / 2 b ×H / 2 b ×32*b; S6.3 Let b = block; S6.4 Perform a ConvLSTM operation on h with a convolution input channel number of 32*b and a convolution output channel number of 32*b to obtain the feature tensor h'; S6.5 Perform four deconvolution operations on the feature tensor h' to obtain the feature map tensor h"; S6.6 Perform a deconvolution operation on the feature map tensor h" to obtain the feature map tensor k; S6.7 Perform an ELU activation operation on k to obtain k' with the same tensor shape; S6.8 Let states[b] = {k', S}, h = k'; S6.9 Let b = b - 1. If b ≥ 1, execute S6.4, otherwise execute S6.10; S6.10 Perform a ConvLSTM operation on h with a convolution input channel number of 16 and a convolution output channel number of 2 to obtain the feature tensor h'; S6.11 Perform grid attention operations on h and Hset respectively to obtain tensor Y j ’; S6.12 Let j = j + 1. If j ≤ O, then execute S6.3; otherwise, the decoding process ends, and the obtained Y’ = {Y’ j | j = [1...O]}.
2. The multi-step traffic flow prediction method based on the multi-scale grid attention neural network according to claim 1, characterized in that: The low-scale mode calculation operation in step S5.4 includes the following steps: S5.4.1 Convolution operation on X t for P t+j in it, with a convolution kernel size of 1×1, a convolution stride of 1, a convolution padding of 0, a convolution input channel number of 2, and a convolution output channel number of 16, obtaining 16 feature map tensors P' t+j for P t+j , and the shape of P' t+j is W×H×16; S5.4.2 Perform ELU activation on the feature map tensor P’ t+j to obtain the activation tensor P” with the same shape t+j ; S5.4.3 on P t+j Perform ConvLSTM operation, where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 16, and the number of convolution output channels is 16, to obtain the feature tensor h t+j and the state tensor S t+j , and the shapes of both tensors are W×H×16.
3. The multi-step traffic flow prediction method based on the multi-scale grid attention neural network according to claim 1, characterized in that: The high-scale mode calculation operation in step S5.6 includes the following steps: S5.6.1 Let b = 1; S5.6.2 for h t+j Perform a convolution operation, where the convolution kernel size is 3×3, the convolution stride is 2, the convolution padding is 1, the number of convolution input channels is 16*b, and the number of convolution output channels is 32*b, to obtain 32 feature map tensors h' t+j for h t+j , and the shape of h't+j is W / 2 b ×H / 2 b ×32*b; S5.6.3 performs ELU activation on the feature map tensor h’ t+j to obtain an activation tensor h” with the same shape t+j ; S5.6.4 for h” t+j Perform four convolution operations. Each time, the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 32*b, the number of convolution output channels is 32*b, and after the convolution operation, 32*b feature map tensors h” t+j about h” are output t+j , and the shape of the tensor remains unchanged; S5.6.5 for h”’ t+j Perform ConvLSTM operation, where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 32*b, and the number of convolution output channels is 32*b, to obtain the feature tensor k t+j and the state tensor S’ t+j , and the shapes of these two tensors remain unchanged; S5.6.6 Let states[b] = {k t+j , S’ t+j}, h t+j = k t+j ; S5.7.7 Let b = b + 1. If b ≤ block, execute S5.6.2, otherwise execute S6.
4. The traffic flow multi-step prediction method based on the multi-scale grid attention neural network according to claim 1, characterized in that: Step S6.4 specifically involves performing a ConvLSTM operation on h with a convolution kernel size of 3×3, a convolution stride of 1, a convolution padding of 1, a convolution input channel number of 32*b, and a convolution output channel number of 32*b, resulting in a feature tensor h' and a state tensor S, both of which have a shape of W / 2 b ×H / 2 b ×32*b。 5. The traffic flow multi-step prediction method based on the multi-scale grid attention neural network according to claim 1, characterized in that: Specifically, in step S6.5, perform four deconvolution operations on h'. Each deconvolution kernel size is 3×3, the deconvolution stride is 1, the deconvolution padding is 1, the deconvolution input channel number is 32*b, and the deconvolution output channel number is 32*b to obtain 32*b feature map tensors h" of h', and the shape of the tensor remains unchanged.
6. The multi-step traffic flow prediction method based on a multi-scale grid attention neural network according to claim 1, characterized in that: Specifically, in step S6.6, when performing deconvolution on h”, the deconvolution kernel size is 3×3, the deconvolution stride is 2, the deconvolution padding is 1, the number of input channels of deconvolution is 32*b, and the number of output channels of deconvolution is 16*b, obtaining 16*b feature map tensors k of h”, and the shape of k is W / 2 b-1 ×H / 2 b-1 ×16*b.
7. The multi-step traffic flow prediction method based on a multi-scale grid attention neural network according to claim 1, characterized in that: Specifically, in step S6.10, perform a ConvLSTM operation on h, where the convolution kernel size is 3×3, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 16, and the convolution output channel number is 2 to obtain the feature tensor h' and the state tensor S, and the shapes of both tensors are W×H×2.
8. The multi-step traffic flow prediction method based on a multi-scale grid attention neural network according to claim 1, characterized in that: The grid attention operation in step S6.11 includes the following steps: S6.11.1 Perform a convolution operation on Hset, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the convolution input channel number is 2, and the convolution output channel number is 2 to obtain 2 feature map tensors Hset' of Hset, and the shape of Hset' is S×W×H×2; Perform a convolution operation on h, where the convolution kernel size is 1×1, the convolution stride is 1, the convolution padding is 1, the number of convolution input channels is 2, and the number of convolution output channels is 2, to obtain two feature map tensors h' of h. The shape of h' is W×H×2; Perform a matrix multiplication on Hset' and h' to obtain a tensor att. The shape of att is S×W×H×W×H×2. Perform a softmax operation on att to obtain a tensor att' with the same shape, indicating that the S encodings of W×H are spatio-temporal slices, and the weights for decoding W×H are spatio-temporal slices; S6.11.4 Multiply the matrices of Hset’ and att’, and then add them to h’ to obtain the tensor Y j ’, the tensor Y j ’ has the shape of W×H×2, which is the prediction result of the j-th step.
9. The traffic flow multi-step prediction method based on the multi-scale grid attention neural network according to claim 1, characterized in that: It also includes the training of the grid attention neural network model. The training process includes the following steps: a. Define the type of the neural network parameter optimizer optim as Adam, where the learning rate is 0.001; b. Define the type of the learning rate scheduler sche as ExponentialLR, where the gamma parameter value is 0.995; c. Define maxepoch = 100; d. Let epoch = 1; e. Let batch = 1; f. Randomly obtain a data from the dataset X to respectively obtain the traffic flow X at time t t and the label Y S+t; g. For X t Perform the forward calculation of the grid attention neural network model to obtain Y'; h, for Y S+t and Y', calculate the RMSE and MAPE losses, denoted as LOSS R and LOSS M , and calculate the total loss LOSS = LOSS R + LOSS M ; i. Perform backpropagation on LOSS and update the neural network parameter optimizer optim; j. Let batch = batch + 1. If batch ≤ n, then execute step f, otherwise execute step k; k. Execute the built-in algorithm of the learning rate scheduler sche to update the learning rate; l. Let epoch = epoch + 1. If epoch ≤ maxepoch, then execute step f, otherwise the training process ends.
Citation Information
Patent Citations
Traffic flow prediction method and system based on time sequence dependent attention sparse convolution
CN116153087A