# HTSA-LSTM: Leveraging Driving Habits for Enhanced Long-Term Urban Traffic Trajectory Prediction
## 2 Model Development
### Attention Mechanism
#### 2.2.2 Spatial Attention Mechanism  
In the HTSA-LSTM model, the tensor cell values at time \(t\) are represented as follows:  
\[G_{t}=G_{t}^{1},\ldots,G_{t}^{n},\ldots,G_{t}^{N},G_{t}\in\mathbb{R}^{d\times N },G_{t}^{n}\in\mathbb{R}^{d\times 1} \tag{13}\]  
where \(N\) denotes the total number of tensor cells (\(N=3\times 13=39\)). The tensor cell \(G_{t}^{N}\) is defined as follows:  
\[G_{t}^{n}=\left\{\begin{array}{l}H_{t}^{v},if\text{ {all vehicles }}v\text{ {are within network cell }}n\\ 0\in\mathbb{R}^{d\times 1},\text{{otherwise}}\end{array}\right. \tag{14}\]  
The spatial attention weights associated with all vehicles at time \(t\) are computed as follows:  
\[B_{t}=softmax\big{(}tanh\big{(}W_{\beta}G_{t}\big{)}\big{)},W_{\beta}\in \mathbb{R}^{1\times d} \tag{15}\]  
where \(W_{\beta}\) represents the learnable weights.  
\[\beta_{t}=\beta_{t}^{1},\ldots,\beta_{t}^{n},\ldots,\beta_{t}^{N},\beta_{t} \in\mathbb{R}^{d\times N},\beta_{t}^{n}\in\mathbb{R}^{d\times 1} \tag{16}\]Here, \(\beta_{t}\) represents the spatial attention weights corresponding to each cell at time \(t\). Finally, the historical information of the target vehicle and its neighboring vehicles is aggregated as follows:  
\[V_{t}=G_{t}(B_{t})^{T}=\sum\beta_{t}^{n=1}G_{t}^{n} \tag{17}\]  
Subsequently, the spatiotemporal interaction information \(V_{t}\) is coupled with the driving style information.  
In the HTSA-LSTM model, there is a strong interdependence between the temporal attention mechanism and the spatial attention mechanism. This relationship is manifested primarily in the following two aspects:  
Temporal Attention Mechanism: The model applies weighted processing to the T-step historical trajectory data of the target vehicle and its neighboring vehicles, generating a weighted hidden state \(H_{t}^{v}\) for each time step. This process focuses on assigning appropriate weights based on the significance of historical data, thereby highlighting the most critical moments for future trajectory prediction. These weighted hidden states are subsequently integrated into the spatial attention mechanism, forming the tensor cell values \(G_{t}^{n}\). Specifically, if vehicle \(v\) is within network cell \(n\), \(H_{t}^{v}\) is included as part of \(G_{t}^{n}\); otherwise, \(G_{t}^{n}\) is set to zero. This demonstrates that the output of the temporal attention directly influences the input structure of the spatial attention mechanism.  
Spatial Attention Mechanism: This mechanism leverages the tensor cell values \(G_{t}^{n}\) of all vehicles, including the output from the temporal attention mechanism, to calculate the spatial attention weights \(B_{t}\). This step emphasizes identifying the regions within the various spatial cells (or network cells) that have the most significant impact on predicting the target vehicle's trajectory. The model then performs a weighted sum of all \(G_{t}^{n}\) using the weights \(B_{t}\), generating the final representation \(V_{t}\) that integrates both temporal and spatial information, as described in Equation (17).