# Prediction of Cotton Yarn Quality Based on Attention-GRU
## 3 Case Analysis
### Model Testing and Comparison
The formula is represented as follows:  
\[R^{2}=1-\frac{\sum_{i=1}^{n}\left(\overline{y}_{i}-y_{i}\right)^{2}}{\sum_{i=1} ^{n}\left(y-y_{i}\right)^{2}} \tag{9}\]  
where: \(y\) represents the average value of \(n\) instances of \(y_{i}\). As shown in Figure 9, the coefficients of determination \(R^{2}\) for the four models are 0.825, 0.903, 0.905, and 0.954, respectively. The experimental results indicate that the attention-GRU model exhibits a higher training fit between predicted and actual values in the context of yarn quality prediction, compared to the GRU, LSTM, and BP neural network prediction models without the incorporation of the attention mechanism. To further evaluate the predictive performance of the proposed models, each trained model is used to predict the outcomes of 10 test samples. The predictive feedback times for each model on the test samples are presented in Table 4. \begin{table}
\begin{tabular}{c c c c c} \hline \hline \multirow{2}{*}{**Test Sample No.**} & \multicolumn{4}{c}{**Predictive Model**} \\ \cline{2-5}  & **BP** & **LSTM** & **GRU** & **Attention-GRU** \\ \hline
1 & 1.03 & 2.17 & 2.04 & 3.81 \\
2 & 1.49 & 2.34 & 2.06 & 3.62 \\
3 & 0.98 & 1.99 & 2.17 & 4.17 \\
4 & 1.32 & 2.03 & 1.88 & 3.73 \\
5 & 1.21 & 2.15 & 2.00 & 3.89 \\
6 & 0.86 & 2.45 & 2.30 & 4.61 \\
7 & 1.08 & 1.76 & 1.83 & 3.29 \\
8 & 1.14 & 2.22 & 2.01 & 3.76 \\
9 & 1.03 & 2.03 & 2.14 & 2.48 \\
10 & 1.09 & 2.42 & 1.62 & 3.61 \\ Total Time(ms) & 11.23 & 21.56 & 20.05 & 36.97 \\ \hline \hline \end{tabular}
\end{table} 
Table 4: Comparison of model prediction feedback time. Figure 8: Adaptive partial loss curve before and after iteration number. As evident from Table 4, the predictive feedback times for the test data among the four models range from 11.23 ms to 36.97 ms. Among these, the BP neural network demonstrates the shortest predictive feedback time. While the attention-GRU prediction model proposed in this study does not achieve the optimal prediction feedback time, its millisecond-level predictive feedback time is of minimal consequence for textile production characterized by long production cycles. This slight variation in feedback time is not expected to significantly impact the value of the model's predictive results. In addition, the models were quantitatively evaluated using two loss functions that represent regression errors: mean absolute percentage error (MAPE) and root mean square error (RMSE). Smaller values of these metrics indicate more accurate quality prediction results. The formulas are as follows:  
\[Emape=\frac{100\%}{n}{\sum_{i=1}^{n}{(\overline{y}i-yi)}^{2}} \tag{10}\]  
\[Emse=\sqrt{\frac{1}{n}{\sum_{i=1}^{n}{(\overline{y}i-yi)}^{2}}} \tag{11}\]  
The predicted outcomes and the predictive performance of each model are presented in Figure 10 and Table 5, respectively. Figure 10 illustrates that the prediction curve generated by the attention-GRU model closely approximates the curve of actual quality values. Figure 9: Correlation analysis comparison chart: (**a**) correlation analysis chart of BP neural network prediction results; (**b**) correlation analysis chart of LSTM neural network prediction result; (**c**) correlation analysis chart of GRU neural network prediction results; (**d**) attention-GRU prediction results correlation analysis chart. As shown in Table 5, the attention-GRU model demonstrates superior performance compared to the other three methods in terms of root mean square error (RMSE). It showcases substantial reductions of 56.3%, 38.5%, and 36.4% when compared to the other methods, respectively. Moreover, the mean absolute percentage error (MAPE) values for the attention-GRU model decreased by 0.367%, 0.158%, and 0.190% when contrasted with the other methods. In addition, Table 4 provides insight into the goodness of fit of the models on the test dataset. Considering the earlier discussion on training fit, it is evident that the attention-GRU model exhibits the least degree of overfitting.