## 3 FDIA Detection Model Based on GAN-GRU and Transformer
### GAN-GRU-Based Data-Balancing Process
#### 3.1.2 Loss Function  
For the generator, we define the mean square error (MSE) as the loss function \(L_{\text{G}}\):  
\[L_{\text{G}}=\frac{1}{n}\sum_{i=1}^{n}\left[y_{i}-p(y_{i})\right]^{2}, \tag{12}\]  
where \(n\) represents the number of data samples, \(y_{i}\) represents the actual value, and \(p(y_{i})\) indicates the predicted value. The MSE function serves as a metric for quantifying the extent of separation between two vectors. This function enables a more efficient assessment of the dissimilarity between the generated data and the authentic data, thereby enhancing the proximity of the attack vectors produced by the generator to the original attack vectors. Shifting the focus to the discriminator, the initial step encompasses the introduction of the authentic signal into the discriminator, enabling forward propagation. Subsequently, the computation of the true loss is computed, which is subsequently juxtaposed with the reference value of 1. Following this, the introduction of random noise culminates in the generation of a spurious signal through the generator. This synthetic signal is then presented to the discriminator, subjected to forward propagation, and culminates in the computation of the false loss. The latter is subsequently juxtaposed with the reference value of 0. In this context, the true loss delineates the discriminator's quantification of loss between the actual signal output and the target label. Conversely, the false loss encapsulates the discriminator's quantification of loss between the synthetic signal output generated by the generator and the target label. To accomplish this objective, the loss function employed by the discriminator utilizes a binary cross-entropy loss function \(L_{D}\):  
\[L_{D}=-\frac{1}{n}\sum_{i=1}^{n}\left\{y_{i}\times log[1-p(y_{i})]+(1-y_{i}) \times log[1-p(y_{i})]\right\}, \tag{13}\]  
where \(y_{i}\) is the binary label 0 or 1, and \(p(y_{i})\) is the sigmoid function representing the probability that the output belongs to the label \(y_{i}\):  
\[p(y_{i})=\frac{1}{1+exp(-y_{i})}, \tag{14}\]  
The binary cross-entropy loss function is aptly employed to normalize the discriminator's output, treating it as a probability distribution. This encourages the discriminator to acquire the skill of allocating real data to the high-probability realm (proximate to 1) and attributing generated data to the low-probability realm (proximate to 0). Consequently, enhanced discrimination between these data types is facilitated by the discriminator. The primary objective of categorizing the input data into two classes, namely, real data and generated data, is fulfilled using the discriminator. The utilization of a binary cross-entropy loss function is instrumental in securing the stability of gradients during the training process. This stability assumes paramount importance in the context of GAN training, where the adversarial interplay between the discriminator and the generator can lead to training instability. Notably, binary cross-entropy typically yields gradients that exhibit a relatively smooth profile, thereby streamlining the training process. Hence, the adoption of binary cross-entropy emerges as a natural choice, given its widespread application as a loss function in binary classification scenarios. The loss function assumes a pivotal role within the training procedure of both the generator and the discriminator. The magnitude of the loss value is computed, thereby facilitating the adoption of corresponding optimization strategies to modify the gradient of the discriminator and generator, subsequently updating the network parameters. Thisconfiguration and training regimen within GAN-GRU engenders the optimization of the generator and discriminator performance, culminating in the ultimate achievement of generating balanced datasets. Through the amalgamation of the mechanisms outlined above, the enactment of a competitive interplay between the generator and the discriminator is facilitated.