# Prediction of Cotton Yarn Quality Based on Attention-GRU
## 2 Cotton Yarn Quality Prediction Model
### Attention-GRU Prediction Model  
Drawing upon the aforementioned analysis, this paper introduces an attention-enhanced GRU-based model for predicting yarn quality in spinning (illustrated in Figure 5). The model's detailed description is provided below:  
Figure 4: Attention mechanism structure. Figure 5: Structure of spinning quality prediction model based on attention-GRU. (1) The raw cotton performance indicators and process parameters of each processing stage are essentially independent time series. In order to integrate these influential features impacting cotton yarn quality, this study draws inspiration from word-embedding techniques employed in natural language processing [26]. Assuming there are \(N\) raw cotton performance indicators, these are transformed into N-dimensional feature vectors, denoted as \((x_{1}^{1},x_{1}^{2},\dots,x_{1}^{N})\), and utilized as the initial input. The formula for calculating the input dataset of the model for raw cotton is given by Equation (2). \[X=\left[ \begin{array}{ccc}x_{1}^{1}\,,x_{1}^{2}\,,x_{1}^{3}\,,\dots\,,x_{1}^{ l}\,,x_{1}^{l+1}\\ x_{2}^{1}\,,x_{2}^{2}\,,x_{2}^{3}\,,\dots\,,x_{1}^{l}\,,x_{2}^{l+1}\\ \dots\\ x_{N}^{1},x_{N}^{2},x_{N}^{3}\,,\dots\,,x_{1}^{l}\,,x_{N}^{l+1}\end{array} \right] \tag{2}\]  
Furthermore, for the ease of model training, the original input data is normalized using the min-max normalization method to fall within the range of (\(-1\),1), as expressed by Equation (3). \[x^{\prime}=\frac{x-x_{min}}{x_{max}-x_{min}} \tag{3}\]  
where: \(x\) represents the original input data, \(x_{max}\) and \(x_{min}\) denote the maximum and minimum values of the input data, respectively, and \(x^{\prime}\) signifies the normalized input data after the min-max normalization process. (2) Constructing a single-layer GRU neural network architecture facilitates a comprehensive learning of the input feature information, enabling the capture of temporal dependencies within the sequential data. This architecture aims to predict yarn quality by employing the data from the past \(l+1\) time steps to forecast the data at the subsequent time step, necessitating only a single decoding step. The output of the GRU layer comprises two components: the hidden state sequence \(H\) (\(h_{1},h_{2},\dots,h_{n}\)) from the encoder and the first hidden state parameter \({h_{1}}^{\prime}\) from the decoder. The computation formula for the GRU output vector is depicted by Equation (4). \[H_{GRU}=(h_{1},h_{2},\dots,h_{n}),{h_{1}}^{\prime} \tag{4}\]  
(3) The attention layer analyzes the significance of feature information at different time steps based on the magnitude of weights, continuously iterating to update and optimize the optimal weight parameters. The calculation formula for the attention mechanism is presented in Equation (5). \[\left\{ \begin{array}{l}e_{t}=\text{score}\big{(}h_{t},{h_{1}}^{\prime}\big{)} \\ a_{t}=\text{softmax}(e_{t})\\ C=\sum_{t=1}^{n}a_{t}\cdot h_{t}\\ s=\text{contact}(C,h_{1}^{\prime})\end{array} \right. \tag{5}\]  
where: \(h_{t}\) corresponds to the hidden state at time \(t\) within the set \(H\); \(t=1,2,\dots,n\) score denote the similarity function utilized for calculating the cosine similarity score \(e_{t}\) between \(h_{t}\) and \({h_{1}}^{\prime}\); softmax stands for the normalized exponential function, transforming \(e_{t}\) into weight values \(a_{t}\) for each hidden state; contact signifies the concatenation function; and \(s\) signifies the prediction output with the attention mechanism incorporated. (4) The output from the attention layer is connected to a fully connected neural network where the output end of the fully connected network aggregates information, yielding the predicted value of yarn quality. This process is formulated in Equation (6). \[\overline{y}=L_{11}\cdot s_{1}+L_{12}\cdot s_{2}+\cdots+L_{1n}\cdot s_{n} \tag{6}\]  
where: \(\overline{y}\) represents the predicted value of yarn quality by the model, and \(L\) denotes the aggregation coefficients of the various units within the fully connected layer.