Traffic flow prediction method and device based on space-time attention network and readable medium
By constructing a traffic flow prediction model based on a spatiotemporal attention network and combining GAT, GRU, and Transformer, the problems of insufficient utilization of periodic data and incomplete capture of spatial features in existing models are solved, achieving accurate traffic flow prediction and reducing computational overhead.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAQIAO UNIVERSITY
- Filing Date
- 2023-07-12
- Publication Date
- 2026-05-01
AI Technical Summary
Existing traffic flow prediction models fail to effectively utilize periodic data, fail to fully capture spatial characteristics, and have high time complexity and memory overhead, making it difficult to achieve accurate traffic flow prediction.
A spatiotemporal attention network-based approach is adopted, which combines graph attention network (GAT), gated recurrent unit (GRU), and self-attention mechanism in Transformer to construct a spatiotemporal attention network. This network captures the long-term and short-term temporal dependencies and dynamic and static spatial dependencies of traffic flow, and optimizes the self-attention calculation process of the Transformer model.
It achieves accurate prediction of traffic flow, reduces computational complexity from O(n²) to O(nlogn), improves model response speed, and facilitates practical deployment.
Smart Images

Figure CN117079452B_ABST
Abstract
Description
Traffic flow prediction method, device, and readable medium based on spatiotemporal attention network Technical Field
[0001] This invention relates to the field of traffic flow prediction, and more specifically to a traffic flow prediction method, apparatus, and readable medium based on a spatiotemporal attention network. Background Technology
[0002] With the rapid growth of the global economy, the number of motor vehicles worldwide has been increasing year by year. Along with this dramatic increase, various traffic problems have gradually emerged, such as traffic congestion, traffic pollution, and traffic accidents, posing significant obstacles to urban development in various countries. To better alleviate and ultimately solve these problems, Intelligent Traffic Systems (ITS) have gradually developed and achieved good results. Real-time and accurate traffic flow prediction is the foundation of ITS; an accurate and efficient traffic flow prediction model can greatly assist traffic management departments and drivers in their travel planning. However, due to the high time and space dependence, nonlinearity, and randomness of traffic flow, accurate and real-time prediction remains a considerable challenge.
[0003] In the field of traffic flow prediction, scholars both domestically and internationally have conducted extensive research. The mainstream prediction frameworks fall into two categories: traditional mathematical statistical models and machine learning models. Among them, mathematical statistical models mainly use models with certain mathematical theoretical foundations, such as wavelet analysis theory, ARIMA regression models, grey theory models, and support vector machine models, for prediction. The advantage of this method is that the constructed models have strong interpretability, but the prediction results of this type of model are often unsatisfactory.
[0004] In this era of data explosion, machine learning models rely on various data-driven learning models, such as convolutional neural networks, graph neural networks, and attention mechanisms, to capture and predict the hidden features of input data. These models are characterized by generally poor interpretability due to their direct reliance on data, but their performance is typically good. While these models can effectively uncover the hidden features of traffic data and reasonably reflect the non-linear relationships between input and output data, current models do not adequately address the following three issues:
[0005] 1) Failure to effectively utilize periodic data. Most predictive model inputs only contain recent data, but traffic flow exhibits a clear periodicity. Therefore, historical data from the same time last week and the same time yesterday can also be helpful for today's traffic forecast. However, due to the time shift of the period, this periodicity is not strictly accurate and will have some bias. Therefore, directly using it as model input may affect the actual prediction results.
[0006] 2) Failure to fully capture spatial features. Existing research mostly uses Graph Convolutional Neural Networks (GCNs) to capture node connectivity information. However, GCNs heavily rely on predefined degree and adjacency matrices for feature extraction and cannot assign corresponding weights to different neighboring nodes. Furthermore, in traffic flow prediction tasks, the presence of roads creates highly dynamic spatial dependencies. Using predefined road topologies cannot adequately represent the dynamic spatial dependencies within the traffic network; in other words, fixed graph structures cannot represent the real-world environment.
[0007] 3) High time complexity and memory overhead. Although using the self-attention mechanism in Transformer as the core module of the model significantly improves the prediction performance, it results in high time and memory complexity. Summary of the Invention
[0008] To address the aforementioned technical problems, the embodiments of this application aim to propose a traffic flow prediction method, apparatus, and readable medium based on spatiotemporal attention networks. This method solves the technical problems mentioned in the background section by combining graph attention networks (GAT), gated recurrent units (GRU), and the self-attention mechanism in the Transformer model. It can simultaneously capture the long-term and short-term temporal dependencies and dynamic / static spatial dependencies of traffic flow, achieving accurate prediction of road network traffic. Furthermore, without sacrificing prediction accuracy, the self-attention calculation process in the Transformer model is optimized to reduce computational overhead and facilitate practical deployment.
[0009] In a first aspect, the present invention provides a traffic flow prediction method based on a spatiotemporal attention network, comprising the following steps:
[0010] Historical traffic flow data and road network structure data are obtained, and the historical traffic flow data and road network structure data are processed respectively to obtain the processed historical traffic flow data and adjacency matrix.
[0011] Traffic flow time series is obtained based on the processed historical traffic flow data;
[0012] A spatiotemporal attention network is constructed and trained to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer.
[0013] The traffic flow time series and adjacency matrix are input into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial features are input into the temporal self-attention module to obtain spatiotemporal features. The spatiotemporal features are input into the output layer to predict traffic flow data.
[0014] Preferably, historical traffic flow data and road network structure data are processed separately to obtain processed historical traffic flow data and an adjacency matrix, specifically including:
[0015] Historical traffic flow data is processed using Z-Score standardization to obtain processed historical traffic flow data;
[0016] The road network structure data D is processed using an improved Gaussian kernel function to obtain the adjacency matrix A, calculated as follows:
[0017]
[0018] Where, ω pq Let represent the (p,q) element in the adjacency matrix A, D(p,q) represent the (p,q) element in the road network structure data D, and σ represent the standard deviation of all non-zero elements in the road network structure data D.
[0019] Preferably, a traffic flow time series is obtained based on the processed historical traffic flow data, specifically including:
[0020] A traffic flow time series is constructed based on the recent data R, the data D from the same time yesterday, and the data W from the same time last week in the processed historical traffic flow data. The format of the traffic flow time series is as follows: in, P represents the P sets of recent data used, N represents the number of sensors, C represents the number of features collected by each sensor, and Q represents the time window. |d| represents the |d| group of daily periodic flow data used, whose value is consistent with the prediction time step length, D |d| This indicates that the prediction for the first time step corresponds to the data at the same time point yesterday, and so on, D 1 This indicates that the last time step in the forecast corresponds to the data at the same time point yesterday. |w| represents the |w| sets of periodic flow data used, whose values are consistent with the prediction time step length, W |w| This indicates that the first time step is predicted to correspond to the data at the same time point last week, and so on. W 1 This indicates that the predicted last time step corresponds to the data at the same time point last week.
[0021] Preferably, the GRU layer includes a short GRU layer and a long GRU layer. Recent data is input into the short GRU layer to obtain the hidden state vector of the recent data. Data from the same time yesterday and data from the same time last week are input into the long GRU layer to obtain the hidden state vectors of yesterday's data and last week's data, respectively. The hidden state vectors of the recent data, yesterday's data, and last week's data at each time step are concatenated to obtain the output feature.
[0022] Preferably, the spatial feature extraction layer includes a GAT layer and a spatial self-attention module. The output features are input into the GAT layer, and the attention coefficients are calculated using the following formula:
[0023]
[0024] Where || denotes the concatenation operation of two vectors. W represents the parameters to be trained. This represents the feature of target node i in the output features. and These are the features of the neighboring nodes of the target node i in the output features;
[0025] Update the spatial features of the i-th node at a certain time step in the output features to obtain the static spatial features:
[0026]
[0027] Where σ represents the activation function;
[0028] The adjacency matrix and output features are input into the spatial self-attention module, which includes the following steps:
[0029] Initialize a linear layer
[0030] Adjacency matrix Input to linear layer W S We get A';
[0031] By performing dimensional expansion and transformation on A', a spatial location code is obtained, where, B represents the batch size; t represents the t time steps used for training; N represents the number of sensors; d represents the dimension of the encoded features.
[0032] A S Summing the output features yields the first input data X, which contains token location information. S ;
[0033] The first input data X containing token location informationS The input is an improved multi-head attention mechanism layer, in which the first input data X S The mapping formula for projecting onto a high-dimensional latent subspace using a feedforward neural network is shown below:
[0034] Q S =X S W Q ;
[0035] K S =X S W K ;
[0036] V S =X S W V ;
[0037] in, Q S Vector, K S Vector, V S The weight matrix of the vector;
[0038] For all tokens corresponding to K S The formula for calculating the number of samples when sampling a vector is as follows:
[0039] L sample = c × ln(L);
[0040] Where c is a pre-set hyperparameter; L is the length of the token sequence;
[0041] Calculate Q corresponding to the m-th token S The evaluation score for a vector is given by the following formula:
[0042]
[0043] The Q with the highest evaluation score S Vectors are set as Active Queries, and the rest are Lazy Queries, where l = L sample The formula is shown below:
[0044]
[0045] The formula for calculating the self-attention result of Active Query is shown below:
[0046]
[0047] Where, d KFor K S The feature dimension of a vector;
[0048] The formula for calculating the self-attention operation result of Lazy Query is as follows:
[0049]
[0050] Among them, L V For V S The length of the vector;
[0051] Finally, the self-attention operation results of Active Query and Lazy Query are processed through a series of residual connections, regularization and feedforward neural networks to obtain dynamic space features;
[0052] The static and dynamic spatial features are fused using a gating mechanism to obtain spatial features;
[0053] Specifically, this includes: static spatial features With dynamic spatial characteristics The linear mapping f is followed by an activation function to obtain the gate g, calculated as follows:
[0054]
[0055] Spatial characteristics are calculated using the following formula:
[0056]
[0057] Preferably, spatial features are input into a temporal self-attention module to obtain spatiotemporal features, specifically including:
[0058] The spatial features are reshaped to obtain the reconstructed features;
[0059] The reconstructed features were then subjected to sine and cosine coding and temporal coding respectively, yielding the sine and cosine coding results and the temporal coding results respectively.
[0060] The reconstructed features are added to the sine and cosine encoding results and the temporal encoding results to obtain the second input data containing temporal location information. The second input data is then fed into the improved multi-head attention mechanism layer, and after a series of residual connections, regularization and feedforward neural network processing, the spatiotemporal features are obtained.
[0061] Preferably, the output layer includes a first convolutional layer and a second convolutional layer connected in sequence. Spatiotemporal features are input into the first and second convolutional layers, and traffic flow data is calculated using the following formula:
[0062]
[0063] in, Indicates spatiotemporal characteristics, Y represents the predicted traffic flow data. T represents the time step to be predicted, and 1 indicates that the predicted data is traffic flow data.
[0064] Secondly, the present invention provides a traffic flow prediction device based on a spatiotemporal attention network, comprising:
[0065] The data processing module is configured to acquire historical traffic flow data and road network structure data, process the historical traffic flow data and road network structure data respectively, and obtain the processed historical traffic flow data and adjacency matrix.
[0066] The time series construction module is configured to generate traffic flow time series based on processed historical traffic flow data;
[0067] The model building module is configured to build and train a spatiotemporal attention network to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer.
[0068] The prediction module is configured to input traffic flow time series and adjacency matrix into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial features are input into the temporal self-attention module to obtain spatiotemporal features. The spatiotemporal features are input into the output layer to predict traffic flow data.
[0069] Thirdly, the present invention provides an electronic device including one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any implementation of the first aspect.
[0070] Fourthly, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method as described in any of the implementations of the first aspect.
[0071] Compared with the prior art, the present invention has the following beneficial effects:
[0072] (1) The traffic flow prediction method based on spatiotemporal attention network proposed in this invention is designed for complex traffic flow prediction problems. It can simultaneously capture the long-term and short-term time dependence and dynamic and static spatial dependence of traffic flow, and achieve accurate prediction of traffic flow.
[0073] (2) The traffic flow prediction method based on spatiotemporal attention network proposed in this invention optimizes the self-attention mechanism of the Transformer module in the model, reducing the module's computational complexity from O(n^2) to O(n^2). 2 The computational cost is reduced to O(nlogn), making it more computationally efficient than existing models, with faster response times and easier deployment, providing technical support and reference for traffic management departments and car owners' travel planning. Attached Figure Description
[0074] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0075] Figure 1 is an exemplary device architecture diagram in which an embodiment of this application can be applied;
[0076] Figure 2 is a flowchart illustrating the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0077] Figure 3 is a schematic diagram of the sensor connections in the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0078] Figure 4 is a visualization of the adjacency matrix A of the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application.
[0079] Figure 5 is a schematic diagram of the traffic flow prediction model of the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0080] Figure 6 is a schematic diagram of the GRU layer in the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0081] Figure 7 is a schematic diagram of the spatial feature extraction layer of the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0082] Figures 8 and 9 are the results of the self-attention scores of the first set of data in the PEMS04 validation set of the traffic flow prediction method based on spatiotemporal attention network according to the embodiments of this application.
[0083] Figure 10 shows the self-attention scores of two sensors in the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application.
[0084] Figure 11 is a schematic diagram of the temporal self-attention module of the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0085] Figure 12 is a schematic diagram of the output layer of the traffic flow prediction method based on spatiotemporal attention network according to an embodiment of this application;
[0086] Figure 13 is a schematic diagram of a traffic flow prediction device based on a spatiotemporal attention network according to an embodiment of this application;
[0087] Figure 14 is a schematic diagram of the structure of a computer device suitable for implementing an electronic device according to the embodiments of this application. Detailed Implementation
[0088] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this invention, and not all of them. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.
[0089] Figure 1 illustrates an exemplary device architecture 100 for a traffic flow prediction method or device based on a spatiotemporal attention network that can be applied to embodiments of this application.
[0090] As shown in Figure 1, the device architecture 100 may include terminal devices 101, 102, and 103, a network 104, and a server 105. The network 104 serves as the medium for providing communication links between the terminal devices 101, 102, and 103 and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0091] Users can use terminal devices 101, 102, and 103 to interact with server 105 via network 104 to receive or send messages, etc. Various applications, such as data processing applications and file processing applications, can be installed on terminal devices 101, 102, and 103.
[0092] Terminal devices 101, 102, and 103 can be either hardware or software. When terminal devices 101, 102, and 103 are hardware, they can be various electronic devices, including but not limited to smartphones, tablets, laptops, and desktop computers. When terminal devices 101, 102, and 103 are software, they can be installed in the electronic devices listed above. They can be implemented as multiple software programs or software modules (e.g., software programs or software modules used to provide distributed services) or as a single software program or software module. No specific limitations are imposed here.
[0093] Server 105 can be a server that provides various services, such as a background data processing server that processes files or data uploaded by terminal devices 101, 102, and 103. The background data processing server can process the acquired files or data and generate processing results.
[0094] It should be noted that the traffic flow prediction method based on spatiotemporal attention network provided in this application embodiment can be executed by server 105 or by terminal devices 101, 102, and 103. Correspondingly, the traffic flow prediction device based on spatiotemporal attention network can be set in server 105 or in terminal devices 101, 102, and 103.
[0095] It should be understood that the number of terminal devices, networks, and servers shown in Figure 1 is merely illustrative. Any number of terminal devices, networks, and servers can be used depending on implementation needs. If the data being processed does not need to be obtained remotely, the above device architecture may not include a network, and may only require servers or terminal devices.
[0096] Figure 2 illustrates a traffic flow prediction method based on a spatiotemporal attention network provided by an embodiment of this application, including the following steps:
[0097] S1: Obtain historical traffic flow data and road network structure data, process the historical traffic flow data and road network structure data respectively, and obtain the processed historical traffic flow data and adjacency matrix.
[0098] In a specific embodiment, historical traffic flow data and road network structure data are processed separately to obtain processed historical traffic flow data and an adjacency matrix, specifically including:
[0099] Historical traffic flow data is processed using Z-Score standardization to obtain processed historical traffic flow data;
[0100] The road network structure data D is processed using an improved Gaussian kernel function to obtain the adjacency matrix A, calculated as follows:
[0101]
[0102] Where, ω pq Let represent the (p,q) element in the adjacency matrix A, D(p,q) represent the (p,q) element in the road network structure data D, and σ represent the standard deviation of all non-zero elements in the road network structure data D.
[0103] Specifically, the embodiments of this application apply the traffic flow prediction model to the PEMS04 US highway dataset, which is a highway dataset from California, USA. This dataset is collected in real time every 30 seconds by the Caltrans Performance Measurement System (PEMS), and traffic flow data is aggregated from the raw data every five minutes. The dataset contains two parts: a CSV file containing geographic location information of sensor detection locations (containing three columns of attributes: from, to, and cost, used to describe the distance information between directly connected sensors), and a data file containing total traffic flow, average vehicle speed, and average occupancy at each station. Considering that the theme of the embodiments of this application is traffic flow prediction, and that the fluctuations in the data related to average vehicle speed and average occupancy in the dataset are smaller than the fluctuations in the total traffic flow data, in order to better adhere to the theme and better test the model's prediction performance, only the total traffic flow data is selected for training and testing.
[0104] Specifically, the PEMS04 dataset contains data from 307 sensors, spanning from January 1, 2018 to February 28, 2018, a total of 59 days of data, with a detection interval of 5 minutes. The connection of each sensor is shown in Figure 3.
[0105] Specifically, based on the actual demand of the traffic network to be predicted, continuous time is divided into discrete time intervals, and traffic flow information under each time interval is statistically analyzed; based on the network structure of the traffic network, road sensors are divided into topological nodes to obtain network structure data D.
[0106] First, we define the adjacency matrix A. Since the road network structure data related to the detection location information in the original dataset is not in matrix form, and in graph neural networks, the adjacency matrix is defined as 1 for connected points and 0 for disconnected points, while the connection information in the dataset is the distance information between sensors, this embodiment chooses to redefine the adjacency matrix based on the Gaussian kernel function. The specific calculation formula is as follows:
[0107]
[0108] Where, ω pq σ represents the adjacency weight between points p and q; dist(p,q) represents the distance between points p and q; σ represents the standard deviation of all distances; and κ represents the threshold.
[0109] Before calculation, embodiments of this application convert the original data (from, to, cost) into a symmetric matrix. If two sensors are connected, the corresponding value is cost; if they are not connected, the corresponding value is 0. Finally, all elements on the main diagonal are set to 1, and this matrix is denoted as D. In this matrix, zero elements represent two points that are not connected; non-zero elements represent two points that are connected, and the values represent the distance between the two points. The main diagonal elements (the distance between two identical nodes) are defined as 1.
[0110] Considering the sparse connections between sensors in the PEMS04 dataset, the embodiments of this application make appropriate improvements to the Gaussian kernel function to better adapt to the data. The updated calculation formula is shown below:
[0111]
[0112] Where D(p,q) represents the p and q elements in matrix D; σ represents the standard deviation of all non-zero elements. The calculated adjacency matrix A corresponding to PEMS04 is visualized in Figure 4.
[0113] Secondly, the sensor data is preprocessed:
[0114] 1) Adding a time attribute. Although the data description includes time span information, the sensor data does not contain time information. Therefore, this invention adds a time attribute. For the PEMS04 dataset, the start time is 01 / 01 / 201800:00 and the end time is 02 / 28 / 201823:55, with a time interval of 5 minutes, adding a total of 16992 time information entries.
[0115] 2) Extract five sets of time features. The time encoding in the time self-attention module requires five sets of time feature information: minute of hour, hour of day, day of week, day of month, and day of year.
[0116] 3) Divide the dataset into training, validation, and test sets. In the embodiments of this application, the dataset is divided into training, validation, and test sets in an 8:1:1 ratio.
[0117] 4) Sensor data standardization. To improve model convergence speed, embodiments of this application employ Z-Score standardization for data preprocessing of all sensor data. The calculation formula is shown below:
[0118]
[0119] Where, μ eσ represents the mean of the e-th sensor in the dataset. e x represents the standard deviation of the e-th sensor in the dataset. ef z represents the f-th set of data from the e-th sensor. ef This represents the updated data from the f-th sensor.
[0120] S2, based on the processed historical traffic flow data, obtains the traffic flow time series.
[0121] In a specific embodiment, a traffic flow time series is obtained based on the processed historical traffic flow data, specifically including:
[0122] A traffic flow time series is constructed based on the recent data R, the data D from the same time yesterday, and the data W from the same time last week in the processed historical traffic flow data. The format of the traffic flow time series is as follows: in, P represents the P sets of recent data used, N represents the number of sensors, C represents the number of features collected by each sensor, and Q represents the time window. |d| represents the |d| group of daily periodic flow data used, whose value is consistent with the prediction time step length, D |d| This indicates that the prediction for the first time step corresponds to the data at the same time point yesterday, and so on, D 1 This indicates that the last time step in the forecast corresponds to the data at the same time point yesterday. |w| represents the |w| sets of periodic flow data used, whose values are consistent with the prediction time step length, W |w| This indicates that the first time step is predicted to correspond to the data at the same time point last week, and so on. W 1 This indicates that the predicted last time step corresponds to the data at the same time point last week.
[0123] Specifically, considering the periodicity of traffic flow, specifically in PEMS04 (which contains only two months of sensor data), traffic flow exhibits both daily and weekly periodicity. Therefore, the input data for the traffic flow prediction model includes three time granularities: recent data R, data from the same time yesterday D, and data from the same time last week W. Furthermore, considering the time transition of the period, the embodiments of this application set a time window Q to ensure that the model can fully capture the periodic information. Therefore, the actual input traffic flow time series of the model is... The specific descriptions of the three sets of time-granularity data are as follows:
[0124] 1) Recent data R: Recent data is historical traffic data before the forecast. Recent traffic data has a significant impact on future traffic data. Where P represents the P sets of recent data used, N represents the number of sensors, and C represents the number of features collected by each sensor.
[0125] 2) Data D from the same time yesterday: Since traffic data is determined by people's daily lives, it often has obvious fixed patterns, such as morning and evening rush hours. Therefore, embodiments of this application introduce daily periodic traffic flow data. Where |d| represents the |d| sets of daily periodic flow data used, the value of which is consistent with the length of the prediction time step, D |d| This indicates that the first time step in the forecast corresponds to the data at the same time point yesterday, D. 1 This indicates that the predicted last time step corresponds to the data at the same time point yesterday. Meanwhile, considering the time transition of the cycle, the embodiments of this application choose to use D... |d| D |d|-1 ,...,D 1 The data from the preceding and following Q time steps are also taken into account.
[0126] 3) Data W from the same time last week: Considering that a week includes weekdays and holidays, there will inevitably be differences in traffic flow between weekdays and holidays. Therefore, the embodiments of this application introduce weekly periodic traffic flow data. Where |w| represents the |w| sets of periodic flow data used, the value of which is consistent with the prediction time step length, W |w| This indicates that the first time step in the forecast corresponds to the data from the same point in the previous week, W. 1 This indicates that the predicted last time step corresponds to the same time point last week. Similarly, considering the time shift of the cycle, the embodiments of this application choose to use W... |w| W |w|-1 ,...,W 1 The data from the preceding and following Q time steps are also taken into account.
[0127] S3. Construct and train a spatiotemporal attention network to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer.
[0128] Specifically, referring to Figure 5, the GRU layer is responsible for modeling short-term dependencies; the spatial feature extraction layer includes the GAT layer for extracting static spatial features and the spatial self-attention module for extracting dynamic spatial features. Static and dynamic spatial features are fused through a gating mechanism to obtain spatial features; the temporal self-attention module is used to mine periodic information in traffic flow to capture long-term dependencies; and the output layer is used to change the feature dimension to obtain prediction results.
[0129] First, we construct the GRU layer. GRU is a type of recurrent neural network (RNN), and like LSTM (Long-Short Term Memory), it is a sequence model proposed to address issues such as long-term memory and gradients in backpropagation. Although the experimental results of GRU are similar to those of LSTM, its time cost is lower.
[0130] The GRU uses only two gates: the reset gate and the update gate. The reset gate controls how the previous state information is written into the current candidate set. The update gate controls the degree to which the state information from the previous moment is incorporated into the current state. t-1 x represents the hidden state passed down from the previous node. t This represents the current input, while h t This represents the hidden state of the current node. The mathematical expression for GRU is as follows:
[0131] r t =σ(W r ·[h t-1 ,x t ]);
[0132] z t =σ(W z ·[h t-1 ,x t ]);
[0133]
[0134]
[0135] Because the recurring mechanism of GRU allows the hidden features from the previous time step to be used as part of the input for the current time step, this structure can make good use of the relationships between sequences and achieves outstanding results in short sequence applications. Therefore, the embodiments of this application use two GRU layers (named short GRU layer and long GRU layer) to process recent data and yesterday's and last week's data respectively, so as to generate hidden state vectors at each time step and establish short-term dependencies. The modeling process is shown in Figure 6.
[0136] The spatial feature extraction layer is used to extract spatial features. Specifically, it includes a GAT layer for extracting static spatial features and a spatial self-attention module for extracting dynamic spatial features.
[0137] Traditional deep learning techniques, such as recurrent neural networks and convolutional neural networks, have achieved great success on Euclidean space data such as text and images. However, many important objects and problems in the real world can be naturally or optimally represented by complex graph structures, such as social networks, recommender systems, and transportation networks. These graph structures can encode complex point-to-point relationships to learn richer information representations.
[0138] In recent years, graph deep learning has attracted widespread interest in the research community. Among them, graph neural networks are a very successful learning framework that can handle a wide variety of tasks in numerous applications. Graph neural networks are neural network architectures specifically designed to operate on graph-structured data. Their goal is to iteratively update node representations by aggregating the representations of neighboring nodes and their representations in the previous iteration. Currently, various graph neural networks have been proposed, and the Graph Attention Networks (GAT) used in the embodiments of this application is one type of graph neural network.
[0139] In a graph convolutional neural network (GCN), for a target node i, the importance of its neighboring node j is determined by their edges A. ij The weights of the edges are determined by the similarity between the nodes. However, in practice, the input graph may be noisy, and the edge weights may not reflect the true strength between two nodes. Therefore, a more principled approach is to automatically learn the importance of each neighboring node. GAT is built on this approach and attempts to learn the importance of each neighboring node based on an attention mechanism. As can be seen from the formula for calculating the attention coefficient, the attention coefficient is calculated through the similarity between neighboring nodes. The representation of a new node is a linear combination of the representations of its neighboring nodes.
[0140] In traffic flow prediction tasks, since the road connectivity is known in advance, this invention uses Gaussian Array Mapping (GAT) to extract features from static graphs to capture static spatial features determined by the road topology. The specific operation process is as follows: data from each time step is sequentially fed into the GAT, and each node updates its own node features based on the features of its neighbors and itself, as well as the relationships between them.
[0141] In traffic flow prediction tasks, the spatial dependencies are highly dynamic due to the existence of roads. Even with GAT (Gaussian Attention Array) to assign corresponding weights to each adjacent node, this predefined road topology cannot fully represent the dynamic spatial dependencies within the traffic network. To capture the hidden spatial dependencies that change over time, embodiments of this application design a dynamic spatial feature extraction layer—a spatial self-attention module—to model high-dimensional latent subspaces. Specifically, by projecting the input features of each node onto the high-dimensional latent subspace, and then applying multi-head self-attention computation to the projected features, the dynamic spatial dependencies between nodes are efficiently modeled based on the changing graph signals. The architecture diagram of the spatial self-attention module is shown in Figure 7.
[0142] First, a learnable spatial location code is introduced to input the token location information into the model. Second, the input data containing the token location information is projected onto a high-dimensional latent subspace using a feedforward neural network. Finally, in this embodiment, the first-head attention score of the first set of data in the PEMS04 validation set is used to plot Figures 8 and 9. Figures 8 and 9 show that the classic self-attention mechanism exhibits sparsity, meaning the self-attention score follows a long-tailed distribution. A small number of dot product operations contribute the vast majority of the attention score, while the contribution of the remaining dot product operations is negligible. In Figure 8, each row of data represents the self-attention score of a particular sensor and all sensors, with the sum of each row's scores being 1. For the columns with higher scores in the figure, it indicates that the sensor's spatial characteristics are similar to all sensors at that moment, meaning the sensor's location is on a main road. Identifying main roads at various times will help traffic management departments to intervene in real time and better alleviate traffic pressure.
[0143] Since the sum of the self-attention scores of each sensor and all sensors is fixed (the sum of the numerical elements in each row of Figure 8 is 1), and some areas in the heatmap have higher values, this indicates that there are significant differences in the self-attention scores of some sensors (the dot product of the Q vectors of some sensors with the K vectors of each sensor is significantly different). Therefore, embodiments of this application name the Q vector of such sensors "Active Query" (corresponding to sensor number two in Figure 10). Meanwhile, for sensors whose self-attention scores do not differ significantly, embodiments of this application name their Q vectors "Lazy Query" (corresponding to sensor number five in Figure 10). For "Lazy Query," since... Smaller, that is It approximately follows a uniform distribution, which means that during the attention calculation process... Since some parts of it approximate a uniform distribution, its calculations can be simplified.
[0144] Because the model contains three sets of time-granular data inputs with a large time span, it falls under the category of long-sequence prediction problems. Using traditional sequence models such as RNNs and LSTMs would lead to excessive time overhead and potential data forgetting issues. Therefore, the embodiments of this application choose to leverage the attention mechanism in Transformers to mine periodic information in traffic data to capture long-term dependencies. The temporal self-attention module differs from the spatial self-attention module in two main ways: firstly, the data input becomes X... T ( After the reshape operation, it becomes Secondly, based on the preservation of local context in sine and cosine coding, a set of time codes is added to better incorporate time information. The sine and cosine coding formulas are shown below:
[0145]
[0146]
[0147] Here, pos represents the position of the token in the input sequence, and d is the dimension of the token. The time encoding is implemented using the embedding method, enabling self-learning. The specific process is as follows:
[0148] 1) Extract five sets of features from the time information: minute of hour, hour of day, day of week, day of month, and day of year.
[0149] 2) Use the embedding method to map the five sets of features to the specified dimensions.
[0150] The two codes are added together to obtain the time-location code. The time-location code is then added to the input, which makes the model input contain time-location information.
[0151] In summary, the structure diagram of the temporal self-attention module is shown in Figure 11.
[0152] S4 inputs the traffic flow time series and adjacency matrix into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial features are input into the temporal self-attention module to obtain spatiotemporal features. The spatiotemporal features are input into the output layer to predict traffic flow data.
[0153] In a specific embodiment, the GRU layer includes a short GRU layer and a long GRU layer. Recent data is input into the short GRU layer to obtain the hidden state vector of the recent data. Data from the same time yesterday and data from the same time last week are respectively input into the long GRU layer to obtain the hidden state vector of yesterday's data and the hidden state vector of last week's data. The hidden state vectors of the recent data, yesterday's data, and last week's data at each time step are concatenated to obtain the output feature.
[0154] In a specific embodiment, the spatial feature extraction layer includes a GAT layer and a spatial self-attention module. The output features are input into the GAT layer, and the attention coefficient is calculated using the following formula:
[0155]
[0156] Where || denotes the concatenation operation of two vectors. W represents the parameters to be trained. This represents the feature of target node i in the output features. and These are the features of the neighboring nodes of the target node i in the output features;
[0157] Update the spatial features of the i-th node at a certain time step in the output features to obtain the static spatial features:
[0158]
[0159] Where σ represents the activation function;
[0160] The adjacency matrix and output features are input into the spatial self-attention module, which includes the following steps:
[0161] Initialize a linear layer
[0162] Adjacency matrix Input to linear layer W S We get A';
[0163] By performing dimensional expansion and transformation on A', a spatial location code is obtained, where, B represents the batch size; t represents the t time steps used for training; N represents the number of sensors; d represents the dimension of the encoded features.
[0164] A S Summing the output features yields the first input data X, which contains token location information. S ;
[0165] The first input data X containing token location information S The input is an improved multi-head attention mechanism layer, in which the first input data X S The mapping formula for projecting onto a high-dimensional latent subspace using a feedforward neural network is shown below:
[0166] Q S =X S W Q ;
[0167] K S =X S W K ;
[0168] V S =X S W V ;
[0169] in, Q S Vector, K S Vector, V S The weight matrix of the vector;
[0170] For all tokens corresponding to K S The formula for calculating the number of samples when sampling a vector is as follows:
[0171] L sample = c × ln(L);
[0172] Where c is a pre-set hyperparameter; L is the length of the token sequence;
[0173] Calculate Q corresponding to the m-th token S The evaluation score for a vector is given by the following formula:
[0174]
[0175] The Q with the highest evaluation score S Vectors are set as Active Queries, and the rest are Lazy Queries, where l = L sample The formula is shown below:
[0176]
[0177] The formula for calculating the self-attention result of Active Query is shown below:
[0178]
[0179] Where, d K For K S The feature dimension of a vector;
[0180] The formula for calculating the self-attention operation result of Lazy Query is as follows:
[0181]
[0182] Among them, L V For V S The length of the vector;
[0183] Finally, the self-attention operation results of Active Query and Lazy Query are processed through a series of residual connections, regularization and feedforward neural networks to obtain dynamic space features;
[0184] The static and dynamic spatial features are fused using a gating mechanism to obtain spatial features;
[0185] Specifically, this includes: static spatial features With dynamic spatial characteristics The linear mapping f is followed by an activation function to obtain the gate g, calculated as follows:
[0186]
[0187] Spatial characteristics are calculated using the following formula:
[0188]
[0189] In a specific embodiment, spatial features are input into a temporal self-attention module to obtain spatiotemporal features, specifically including:
[0190] The spatial features are reshaped to obtain the reconstructed features;
[0191] The reconstructed features were then subjected to sine and cosine coding and temporal coding respectively, yielding the sine and cosine coding results and the temporal coding results respectively.
[0192] The reconstructed features are added to the sine and cosine encoding results and the temporal encoding results to obtain the second input data containing temporal location information. The second input data is then fed into the improved multi-head attention mechanism layer, and after a series of residual connections, regularization and feedforward neural network processing, the spatiotemporal features are obtained.
[0193] In a specific embodiment, the output layer includes a first convolutional layer and a second convolutional layer connected in sequence. Spatiotemporal features are input into the first and second convolutional layers, and traffic flow data is calculated using the following formula:
[0194]
[0195] in, Indicates spatiotemporal characteristics, Y represents the predicted traffic flow data. T represents the time step to be predicted, and 1 indicates that the predicted data is traffic flow data.
[0196] Specifically, the output layer uses two classic convolutional layers to change the feature dimension, performs multi-step prediction based on the features extracted by the spatiotemporal feature extraction layer, and the convolutional layers are only responsible for reducing the number of channels. The structure diagram of the output layer is shown in Figure 12. Finally, the traffic flow prediction model can accurately predict traffic flow data.
[0197] The steps S1-S4 above do not represent the order of the steps, but are merely symbolic representations of the steps.
[0198] Referring further to Figure 13, as an implementation of the methods shown in the above figures, this application provides an embodiment of a traffic flow prediction device based on a spatiotemporal attention network. This device embodiment corresponds to the method embodiment shown in Figure 2, and the device can be specifically applied to various electronic devices.
[0199] This application provides a traffic flow prediction device based on a spatiotemporal attention network, comprising:
[0200] Data processing module 1 is configured to acquire historical traffic flow data and road network structure data, process the historical traffic flow data and road network structure data respectively, and obtain the processed historical traffic flow data and adjacency matrix.
[0201] Time series construction module 2 is configured to obtain traffic flow time series based on processed historical traffic flow data;
[0202] Model building module 3 is configured to build and train a spatiotemporal attention network to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer.
[0203] Prediction module 4 is configured to input traffic flow time series and adjacency matrix into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial features are input into the temporal self-attention module to obtain spatiotemporal features. The spatiotemporal features are input into the output layer to predict traffic flow data.
[0204] Referring now to FIG14, a schematic diagram of a computer device 1400 suitable for implementing an electronic device (such as the server or terminal device shown in FIG1) of the embodiments of this application is shown. The electronic device shown in FIG14 is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.
[0205] As shown in Figure 14, the computer device 1400 includes a central processing unit (CPU) 1401 and a graphics processing unit (GPU) 1402, which can perform various appropriate actions and processes according to programs stored in read-only memory (ROM) 1403 or programs loaded from storage portion 1409 into random access memory (RAM) 1404. The RAM 1404 also stores various programs and data required for the operation of the device 1400. The CPU 1401, GPU 1402, ROM 1403, and RAM 1404 are interconnected via bus 1405. An input / output (I / O) interface 1406 is also connected to bus 1405.
[0206] The following components are connected to I / O interface 1406: an input section 1407 including a keyboard, mouse, etc.; an output section 1408 including an LCD, speakers, etc.; a storage section 1409 including a hard disk, etc.; and a communication section 1410 including a network interface card, such as a LAN card, modem, etc. The communication section 1410 performs communication processing via a network such as the Internet. A drive 1411 may also be connected to I / O interface 1406 as needed. A removable medium 1412, such as a hard disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 1411 as needed so that computer programs read from it can be installed into storage section 1409 as needed.
[0207] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 1410, and / or installed from removable medium 1412. When the computer program is executed by central processing unit (CPU) 1401 and graphics processing unit (GPU) 1402, the functions defined in the methods of this application are performed.
[0208] It should be noted that the computer-readable medium described in this application can be a computer-readable signal medium, a computer-readable medium, or any combination thereof. A computer-readable medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor device, or any combination thereof. More specific examples of a computer-readable medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this application, a computer-readable medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution device, apparatus, or device. In this application, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than a computer-readable medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution device, apparatus, or apparatus. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0209] Computer program code for performing the operations of this application can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, and conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or it can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0210] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using dedicated hardware-based means to perform the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0211] The modules described in the embodiments of this application can be implemented in software or hardware. These modules can also be located within a processor.
[0212] In another aspect, this application also provides a computer-readable medium, which may be included in the electronic device described in the above embodiments; or it may exist independently and not assembled into the electronic device. The computer-readable medium carries one or more programs, which, when executed by the electronic device, cause the electronic device to: acquire historical traffic flow data and road network structure data; process the historical traffic flow data and road network structure data respectively to obtain processed historical traffic flow data and an adjacency matrix; obtain a traffic flow time series based on the processed historical traffic flow data; construct and train a spatiotemporal attention network to obtain a traffic flow prediction model, the spatiotemporal attention network including a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer; input the traffic flow time series and adjacency matrix into the traffic flow prediction model, the traffic flow time series into the GRU layer to obtain output features, the output features and adjacency matrix into the spatial feature extraction layer to obtain spatial features, the spatial features into the temporal self-attention module to obtain spatiotemporal features, and the spatiotemporal features into the output layer to predict traffic flow data.
[0213] The above description is merely a preferred embodiment of this application and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of the invention involved in this application is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described inventive concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features with similar functions disclosed in this application.
Claims
1. A traffic flow prediction method based on spatiotemporal attention networks, characterized in that, Includes the following steps: Historical traffic flow data and road network structure data are acquired and processed to obtain processed historical traffic flow data and an adjacency matrix. A traffic flow time series is obtained based on the processed historical traffic flow data. A spatiotemporal attention network is constructed and trained to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer. The traffic flow time series and the adjacency matrix are input into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and the adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial feature extraction layer includes a GAT layer and a spatial self-attention module. The output features are input into the GAT layer, and the attention coefficient is calculated using the following formula: ;in, This represents the concatenation operation of two vectors. 、 The parameters to be trained, This represents the feature of target node i in the output features. and These are the features of the neighboring nodes of the target node i in the output features; the spatial features of the i-th node at a certain time step in the output features are updated to obtain the static spatial features: ;in, The activation function is represented; the adjacency matrix and the output features are input into the spatial self-attention module, which includes the following steps: initializing a linear layer. ;Adjacency matrix Input to the linear layer ,get ;right Dimensional augmentation and transformation are performed to obtain spatial location codes, where, , Represents batch size; Represents the training used One time step; Number of sensors; Represents the encoded feature dimension; The input data is summed with the output features to obtain the first input data containing token location information. ; The first input data containing token location information The input is an improved multi-head attention mechanism layer, in which the first input data... The mapping formula for projecting onto a high-dimensional latent subspace using a feedforward neural network is shown below: ; ; ;in, 、 、 They are respectively vector, vector, The weight matrix of the vector; for all tokens The formula for calculating the number of samples when sampling a vector is as follows: ;in, These are pre-set hyperparameters; Let be the length of the token sequence; calculate the th . Each token corresponds to The evaluation score for a vector is given by the following formula: The highest-scoring evaluation indivual Vectors are set to Active Query, and the rest are set to Lazy Query. The formula is shown below: The formula for calculating the self-attention result of Active Query is shown below: ;in, for The feature dimension of the vector; the self-attention operation result of Lazy Query is calculated using the following formula: ;in, for The length of the vector; finally, the self-attention operation results of Active Query and Lazy Query are processed through a series of residual connections, regularization, and feedforward neural networks to obtain dynamic spatial features; the static spatial features and dynamic spatial features are fused through a gating mechanism to obtain spatial features; specifically including: static spatial features With dynamic spatial characteristics linear mapping Then, the gate is obtained through an activation function. The calculation formula is as follows: The spatial features are calculated using the following formula: The spatial features are input into the temporal self-attention module to obtain spatiotemporal features, which are then input into the output layer to predict traffic flow data.
2. The traffic flow prediction method based on spatiotemporal attention networks according to claim 1, characterized in that, The process of processing the historical traffic flow data and road network structure data to obtain processed historical traffic flow data and an adjacency matrix specifically includes: processing the historical traffic flow data using Z-Score normalization to obtain processed historical traffic flow data; and processing the road network structure data based on an improved Gaussian kernel function. After processing, the adjacency matrix is obtained. The calculation formula is as follows: ;in, Representing the adjacency matrix In element, Represents road network structure data In element, Represents road network structure data The standard deviation of all non-zero elements in the equation.
3. The traffic flow prediction method based on spatiotemporal attention networks according to claim 1, characterized in that, The step of obtaining a traffic flow time series based on the processed historical traffic flow data specifically includes: constructing the traffic flow time series based on the recent data R, the data D from the same time yesterday, and the data W from the same time last week in the processed historical traffic flow data. The format of the traffic flow time series is as follows: ,in, , Indicates the most recent use Group data, Indicates the number of sensors. Q represents the number of features collected by each sensor, and Q represents the time window. , Indicates the use The daily periodic flow data has values consistent with the length of the forecast time step. This indicates that the first time step is predicted to correspond to the data at the same time point yesterday, and so on. This indicates that the last time step in the forecast corresponds to the data at the same time point yesterday. , Indicates the use The data consists of weekly periodic flow data, the values of which are consistent with the length of the prediction time step. This indicates that the first time step is predicted to correspond to the data at the same time point last week, and so on. This indicates that the predicted last time step corresponds to the data at the same time point last week.
4. The traffic flow prediction method based on spatiotemporal attention networks according to claim 3, characterized in that, The GRU layer includes a short GRU layer and a long GRU layer. The recent data is input into the short GRU layer to obtain the hidden state vector of the recent data. The data from the same time yesterday and the data from the same time last week are respectively input into the long GRU layer to obtain the hidden state vector of yesterday's data and the hidden state vector of last week's data. The hidden state vectors of the recent data, yesterday's data, and last week's data at each time step are concatenated to obtain the output feature.
5. The traffic flow prediction method based on spatiotemporal attention networks according to claim 1, characterized in that, The spatial features are input into the temporal self-attention module to obtain spatiotemporal features. Specifically, this includes: reshaping the spatial features to obtain reconstructed features; performing sine and cosine encoding and temporal encoding on the reconstructed features to obtain sine and cosine encoding results and temporal encoding results respectively; adding the reconstructed features with the sine and cosine encoding results and temporal encoding results to obtain second input data containing temporal location information; inputting the second input data into the improved multi-head attention mechanism layer; and then processing it through a series of residual connections, regularization, and feedforward neural networks to obtain spatiotemporal features.
6. The traffic flow prediction method based on spatiotemporal attention networks according to claim 1, characterized in that, The output layer comprises a first convolutional layer and a second convolutional layer connected in sequence. The spatiotemporal features are input into the first and second convolutional layers, and the traffic flow data is calculated using the following formula: ;in, This represents the spatiotemporal characteristics. Y represents the predicted traffic flow data. , This indicates the time step to be predicted, and 1 indicates that the predicted data is traffic flow data.
7. A traffic flow prediction device based on a spatiotemporal attention network, characterized in that, include: The data processing module is configured to acquire historical traffic flow data and road network structure data, and process the historical traffic flow data and road network structure data respectively to obtain processed historical traffic flow data and adjacency matrix. The time series construction module is configured to obtain a traffic flow time series based on the processed historical traffic flow data; The model building module is configured to construct and train a spatiotemporal attention network to obtain a traffic flow prediction model. The spatiotemporal attention network includes a GRU layer, a spatial feature extraction layer, a temporal self-attention module, and an output layer. The prediction module is configured to input the traffic flow time series and the adjacency matrix into the traffic flow prediction model. The traffic flow time series is input into the GRU layer to obtain output features. The output features and the adjacency matrix are input into the spatial feature extraction layer to obtain spatial features. The spatial feature extraction layer includes a GAT layer and a spatial self-attention module. The output features are input into the GAT layer, and the attention coefficient is calculated using the following formula: ;in, This represents the concatenation operation of two vectors. 、 The parameters to be trained, This represents the feature of target node i in the output features. and These are the features of the neighboring nodes of the target node i in the output features; the spatial features of the i-th node at a certain time step in the output features are updated to obtain the static spatial features: ;in, The activation function is represented; the adjacency matrix and the output features are input into the spatial self-attention module, which includes the following steps: initializing a linear layer. ;Adjacency matrix Input to the linear layer ,get ;right Dimensional augmentation and transformation are performed to obtain spatial location codes, where, , Represents batch size; Represents the training used One time step; Number of sensors; Represents the encoded feature dimension; The input data is summed with the output features to obtain the first input data containing token location information. ; The first input data containing token location information The input is an improved multi-head attention mechanism layer, in which the first input data... The mapping formula for projecting onto a high-dimensional latent subspace using a feedforward neural network is shown below: ; ; ;in, 、 、 They are respectively vector, vector, The weight matrix of the vector; for all tokens The formula for calculating the number of samples when sampling a vector is as follows: ;in, These are pre-set hyperparameters; Let be the length of the token sequence; calculate the th . Each token corresponds to The evaluation score for a vector is given by the following formula: The highest-scoring evaluation indivual Vectors are set to Active Query, and the rest are set to Lazy Query. The formula is shown below: The formula for calculating the self-attention result of Active Query is shown below: ;in, for The feature dimension of the vector; the self-attention operation result of Lazy Query is calculated using the following formula: ;in, for The length of the vector; finally, the self-attention operation results of Active Query and Lazy Query are processed through a series of residual connections, regularization, and feedforward neural networks to obtain dynamic spatial features; the static spatial features and dynamic spatial features are fused through a gating mechanism to obtain spatial features; specifically including: static spatial features With dynamic spatial characteristics linear mapping Then, the gate is obtained through an activation function. The calculation formula is as follows: The spatial features are calculated using the following formula: The spatial features are input into the temporal self-attention module to obtain spatiotemporal features, which are then input into the output layer to predict traffic flow data.
8. An electronic device, comprising: One or more processors; A storage device for storing one or more programs, which, when executed by one or more processors, cause the one or more processors to perform the method as described in any one of claims 1-6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Traffic flow prediction method based on space-time attention network
CN115936069A