# Stacked-GRU Based Power System Transient Stability Assessment Method
## 2 Methodology  
### Gated Recurrent Unit  
As shown in Figure 1[20], GRU (Gated Recurrent Unit) [21] is a kind of recurrent neural network (RNN) used to solve the problem of gradient explosion and gradient disappearance [22]. Traditional neural networks such as the BP neural network [23] and convolutional neural networks [19] are not good at processing time series information, and GRU, as a variant of RNN, can combine historical information with current time to predict further information, so it has been widely used in speech recognition, language modeling, translation, picture description, and other issues [24].  
Figure 1: Gated Recurrent Unit.  
The forward propagation of the GRU unit is shown in the Equations (1)-(5), where \".\" is matrix multiplication and \"\(\odot\)\" represents matrix wise element multiplication:  
The GRU's update gate \(z_{t}\) and reset gate \(\mathbf{r}_{t}\) are used to control the direction of the data stream at time t. The calculation method is as follows:  
\[z_{t}=\sigma(\mathbf{W}_{z}\cdot[\mathbf{h}_{t-1},\mathbf{x}_{t}] \tag{1}\]  
\[\mathbf{r}_{t}=\sigma(\mathbf{W}_{r}\cdot[\mathbf{h}_{t-1},\mathbf{x}_{t}]) \tag{2}\]  
where \(\mathbf{x}_{t}\) is the input at time t, \(\mathbf{W}_{z}\) is the weight of update gate, \(\mathbf{W}_{r}\) is the weight of reset gate, and \(\mathbf{h}_{t-1}\) is the output of hidden layer.  
When calculating \(\widetilde{\mathbf{h}}_{t}\) which is the output of candidate hidden layer at the time t, the information of the historical time which means the output of hidden layer is retained, and the historical time information is controlled by adjusting the value of \(\mathbf{r}_{t}\), as shown by:  
\[\widetilde{\mathbf{h}}_{t}=tanh(\mathbf{W}\cdot[\mathbf{r}_{t}\odot\mathbf{h}_{t-1},\ \mathbf{x}_{t}] \tag{3}\]  
where \(\tan\mathrm{h(x)}=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}\).  
Finally, by using \(z_{t}\) to control the information of the hidden layer that how much is forgotten and the how much information in the candidate hidden layer is retained, the final calculated output of hidden layer is as shown in the equation:  
\[\mathbf{h}_{t}=\mathbf{z}_{t}\odot\mathbf{h}_{t-1}+(1-\mathbf{z}_{t})\odot\widetilde{\mathbf{h}} _{t} \tag{4}\]  
Then the output of GRU is:  
\[\widetilde{y}_{last}=\sigma(\mathbf{W}_{o}\mathbf{h}_{last}+\mathbf{b}_{o}) \tag{5}\]  
where \(\widetilde{y}_{last}\) is the output of last output layer in the time series, \(\mathbf{W}_{o}\) is weight of output layer, and \(\mathbf{b}_{o}\) is bias of output layer.  
If the reset gate is close to 0, then the output of hidden layer at the moment will not be preserved, and the GRU unit will discard some historical information unrelated to the future. The update gate determines how much information the hidden layer at time \(t-1\) needs to be saved in the output of hidden layer at time \(t\). If the element is close to 1, the corresponding information of the hidden layer at the moment will be copied to the time \(t\). At this time, the update gate unit with long distance dependence is active, therefore long-distance information can be learned; if the element is close to 0, it is equivalent to the standard RNN, and the short-distance information can be processed. At this time, the update gate unit with short-distance dependence is active. Under this mechanism, historical information can flexibly help predict future information.  
When training the model, the GRU uses the backpropagation through time (BPTT) [25] for network optimization. The target loss function to optimize is:  
\[L=\sum_{i}^{N}y^{(i)}ln(\widetilde{y}_{last}^{(i)}) \tag{6}\]  
In the formula, \(y^{(i)}\) is the real label of \(i\)-th sample, and \(\widetilde{y}_{last}^{(i)}\) is predicted label at the last moment of \(i\)-th sample, and \(N\) is the total number of samples.