# Adaptive Semi-Supervised Algorithm for Intrusion Detection and Unknown Attack Identification
## 3 Methodology
### Algorithm Design
#### 3.2.3 Loss Function  
We introduce an improved loss calculation method that enhances the training stability of the model through a gradient penalty mechanism. Traditional GANs may encounter issues such as vanishing or exploding gradients during training, which can hinder model convergence or result in suboptimal generation performance. To mitigate this issue, we generate new samples by interpolating between real and synthetic samples, and then calculate the gradient of the discriminator with respect to these interpolated samples, thereby introducing a gradient penalty term.  
**GAN Loss:** Let \(x_{\text{real}}\) be the real samples and \(x_{\text{fake}}\) be the generated samples. First, generate a random weight \(\alpha\) that matches the dimensions of the samples. This weight is used to perform linear interpolation between the real and generated samples, resulting in an interpolated sample \(\hat{\pi}\):  
\[\hat{x}=\alpha\cdot\mathbf{x}_{\text{real}}+(1-\alpha)\cdot\mathbf{x}_{\text {fake}} \tag{2}\]  
Next, the interpolated sample \(\hat{x}\) is input into the discriminator \(D\), yielding the discriminator output \(D(\hat{x})\). The gradient of the discriminator output with respect to the interpolated sample \(\
abla_{\hat{x}}D(\hat{x})\) is then computed, and the \(L_{2}\) norm of this gradient is calculated.  
The design goal of the gradient penalty term (GP) is to ensure that the norm of the discriminator's gradient is close to 1, thereby ensuring the smoothness of the gradients [24].  
The final discriminator loss \(L_{D}\) is given by the following:  
\[L_{D}=-\frac{1}{N}\sum_{i=1}^{N}D(\mathbf{x}_{\text{real},i})+\frac{1}{N}\sum _{i=1}^{N}D(\mathbf{x}_{\text{fake},i})+\lambda\cdot gp \tag{3}\]  
where \(\lambda\) is a hyperparameter used to adjust the weight of the gradient penalty term; in this study, \(\lambda\) is set to 10.  
The loss for the generator is computed as follows:  
\[L_{D}=-\frac{1}{N}\sum_{i=1}^{N}D(G(z_{i})) \tag{4}\]  
where \(z\) represents the random noise input to the generator.  
**Encoder Loss:** Using pre-trained generator and discriminator models, train the encoder model with preprocessed two-dimensional feature matrix data from normal traffic conditions.  
Feed the real feature matrix into the encoder network to obtain the generated noise \(z\). Then, input the noise \(z\) into the trained generator model to produce the generated \"fake\" feature matrix fake_matrix.  
Finally, input both the generated feature matrix and the real data into the trained discriminator to obtain fake_feature and real_feature, respectively. The encoder loss is calculated as follows:  
\[L_{\text{mat}}=\text{MSE}(G(E(x_{\text{real}})),x_{\text{real}}) \tag{5}\]  
\[L_{\text{feat}}=\text{MSE}(D(G(E(x_{\text{real}}))),D(x_{\text{real}})) \tag{6}\]  
\[L_{E}=L_{\text{mat}}+kappa*L_{\text{feat}} \tag{7}\]  
where \(L_{\text{mat}}\) is the loss between the real data and the generated data, \(L_{\text{feat}}\) represents the loss between the discriminator's evaluation results of the real data and the generated data, and \(L_{E}\) is the total loss of the encoder, which also serves as the final detection score of the entire algorithm model. \(kappa\) is a hyperparameter controlling the importance of \(L_{\text{feat}}\); in this study, it is set to 0.9.