# Research on Multi-Port Ship Traffic Prediction Method Based on Spatiotemporal Graph Neural Networks
## 2 Methods
### Spatial Layer  
The non-Euclidean nature, irregularity, and sparsity of graphs pose challenges in directly applying convolutional neural networks (CNNs) to process graph features. This limitation has impeded progress in graph representation within the field of machine learning. However, in recent years, researchers have made strides by combining spectral graph theory with Fourier transforms, allowing the definition of convolutional kernels in the spectral domain. This breakthrough has enabled graph convolutions in the spectral domain and has laid the foundation for graph convolutional networks (GCN) [12]. By simplifying the computation process, GCNs have facilitated rapid advancements in graph neural network models.  
Subsequently, the graph attention network (GAT) [13] was proposed as a graph neural network model based on attention mechanisms. GAT controls the aggregation of information from nodes and edges by assigning different learning weights to their neighbors. This allows the model to extract more valuable hidden features. Compared to GCN, GAT offers a more flexible node feature aggregation process, addressing the limitation of GCN in which the fixed adjacency matrix prevents the graph structure from being expandable.  
GAT can be divided into two stages: Stage 1 involves computing global similarity coefficients, while Stage 2 focuses on computing node features based on local attention coefficients.  
Take a graph G = (V, E) with N port nodes as an example, where the historical flow features of each port can be represented as \(X_{i}\in\mathbb{R}^{T\times C}\), with \(T\) being the time dimension and \(C\) being the feature dimension. In the graph attention layer, a trainable shared weight matrix \(W\) is first used to linearly transform the initial features of all nodes. Based on the edge relationships, the transformed node features are then used to calculate the similarity coefficients \(e_{ij}\) between adjacent nodes on each edge. The formula for this stage is as follows, where \(N\) is the set of first-order neighboring nodes of the target node \(i\), \(l\) is the current neural network layer, and the mapping function \(a(*)\) is used to compute the similarity between the flow features of port nodes \(i\) and \(j\).  
\[e_{ij}^{(l)}=a\Big{(}\Big{[}W^{(l)}X_{i}^{(l)}\parallel W^{(l)}X_{j}^{(l)} \Big{]}\Big{)},j\in\mathcal{N}_{i} \tag{3}\]  
In the aggregation stage, the model uses \(softmax\) to normalize the similarity coefficients of all nodes within the first-order neighborhood of the target node \(i\). The normalized coefficients represent the attention coefficients for each node within the neighborhood. The specific formula for the calculation of the attention coefficients is as follows:  
\[\alpha_{ij}^{(l)}=\frac{\exp\Big{(}LeakyReLU\Big{(}e_{ij}^{(l)}\Big{)}\Big{)} }{\sum\limits_{k\in\mathcal{N}_{i}}exp\Big{(}LeakyReLU\Big{(}e_{ik}^{(l)} \Big{)}\Big{)}} \tag{4}\]  
Subsequently, the features of each node are weighted and aggregated based on the calculated attention coefficients, resulting in the new feature \(X_{i}^{(l)}\) of the central node \(i\) at layer \(l\). Here, \(\sigma(*)\) represents an optional activation function.  
\[h_{l}(X_{i})=\sigma\Bigg{(}\sum\limits_{j\in\mathcal{N}_{i}}\alpha_{ij}^{(l)} W^{(l)}X_{j}^{(l)}\Bigg{)} \tag{5}\]Repeating Formulas (4) and (5), we perform feature aggregation for all nodes in the graph, completing the processing of a GAT layer.  
In summary, the overall calculation formula for the spatiotemporal block of the model can be summarized as follows:  
\[h_{I}\Big{(}X^{(l-1)}\Big{)}=tanh\Big{(}WaveNet\Big{(}X^{(l-1)}\Big{)}*W+b\Big{)} \odot\delta\Big{(}GAT\Big{(}X^{(l-1)}\Big{)}*V+c\Big{)} \tag{6}\]