## 3 Methodology
### TRFM Trajectory Prediction Model
#### 3.3.5 Loss Function
Finally, uniform time interval resampling is performed to maintain equal time intervals between trajectory points. The processed trajectories fit well with the original trajectories. The AIS data used in this study consist of a total of 6,057,648 AIS records, with 1578 valid ship trajectories. Relative data are used as model input, using 61 historical trajectory points to predict the latitude and longitude positions of future trajectory points. After differencing to obtain relative values, the input sequence length is set to an integer of 60. In the constructed dataset, there are 40,556 training datasets, 10,140 validation datasets, and 12,674 test datasets. ### Trajectory Prediction

To validate the effectiveness of the proposed TRFM model for trajectory prediction, experiments are conducted on the constructed AIS ship trajectory prediction dataset in this study. The model's performance is compared with state-of-the-art trajectory prediction models, including the LSTM model [26], ATT-LSTM model [7], CNN-LSTM model [27], and Bi-LSTM model [28]. Additionally, to validate the advantage of TRFM's encoder-decoder (ENC-DEC) structure, a comparison is made with a model using only the decoder (DEC) structure, denoted as TRFM(DEC). The experimental environment parameters are shown in Table 2, and the model parameters for TRFM(ENC-DEC) are presented in Table 3. In the TRFM model parameters, \"head\" indicates the number of attention heads, \"d_model\" denotes the sequence feature dimensionality in the Transformer, \"enc_layer\" and \"dec_layer\" indicate the number of stacked Transformer units in the encoder and decoder, respectively, \"diff\" represents the feature dimensionality in the Feed Forward layer, and \"label_len\" is the length of prior trajectory input to the decoder. _MAE_ is used as the training loss during the training process. Due to the use of latitude and longitude relative values for prediction, the output prediction values are scaled up by a factor of 10,000 to enhance model convergence. The initial learning rate is set to 0.0001, and optimization is performed using the Adam stochastic gradient descent algorithm. The training process consists of 100 epochs, with a batch size of 128, totaling 40,000 training steps. Figure 7 shows the training loss curves for different models. From the figure, it can \begin{table}
\begin{tabular}{c c} \hline \hline
**Environment** & **Configuration** \\ \hline Operating System & Ubuntu 20.0 \\ RAM & 32 G \\ Python & 3.8 \\ Tensorflow & 2.15 \\ GPU & RTX3080 (12 G RAM) \\ Cpu & i7-11700k \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Experimental Environment Parameters. \begin{table}
\begin{tabular}{c c} \hline \hline
**Parameter** & **Value** \\ \hline head & 8 \\ d\_model & 512 \\ enc\_layer & 2 \\ dec\_layer & 2 \\ diff & 2048 \\ label\_len & 15 \\ \hline \hline \end{tabular}
\end{table} 
Table 3: TRFM (ENC-DEC) Model Parameters. be observed that the TRFM model converges faster and achieves a lower loss compared to other models. Within the TRFM model, the ENC-DEC structure exhibits a lower loss than using the DEC structure alone, indicating superior performance. To measure the error between the model's predictions and the actual values, _MSE_ and _RMSE_ are used. _MSE_ represents the error as the squared difference between predicted and actual values. A smaller _MSE_ indicates better predictive performance. _RMSE_, on the other hand, also measures the error between predictions and actual values but on a smaller scale, making it more sensitive to outliers in the data. Figure 8 displays box plots showing the statistical distribution of prediction errors for different models using _MSE_ and _RMSE_. From the figure, it is evident that the ENC-DEC-structured TRFM model has the smallest trajectory prediction errors compared to other models, indicating its superior accuracy and stability in trajectory prediction. Figure 7: The data processing effects at each step: (**a**) Trajectories after segmentation. (**b**) Trajectories after segmentation.