# Unsupervised Learning-Based Spectrum Sensing Algorithm with Defending Adversarial Attacks
## 4 Implementation of USDAA Defense Algorithm
### Adversarial Pre-Training  
Adversarial pre-training uses two encoders and two decoders with the same structure and shared parameters. The encoder is composed of six convolution layers and one maximum pooling layer, and each convolution layer is followed by a batch normalization layer (\(BN\)) and a nonlinear activation layer (Relu). The decoder is composed of six deconvolution networks and a maximum pooling layer. Similarly, behind each deconvolution layer, there are a \(BN\) layer and a Relu layer. The deconvolution layer is the inverse operation of the convolutional layer, which inversely maps the feature potential representation \(Z_{i}(i=1,2)\) to the original signal space. A feature extractor encoder is obtained by the adversarial pre-training, which can extract rich features of adversarial samples and clean samples. To enhance the effectiveness of adversarial pre-training and achieve the goal of defending the primary user adversarial attacks in spectrum sensing, the loss function in the adversarial pre-training shown in Equation (5) is designed:  
\[loss=\text{loss}_{\text{c}}+\text{loss}_{\text{Id}}+\text{loss}_{\text{re}}, \tag{5}\]  
where \(\text{loss}_{\text{c}}\) represents contrast loss, \(\text{loss}_{kl}\) represents KL divergence and \(\text{loss}_{\text{re}}\) represents sample reconstruction loss. The labels of adversarial samples and clean samples are the same, and the adversarial samples and clean samples are taken as a pair of positive samples. Encoders extract the clean sample features \(Q_{1}\) and the adversarial sample features \(Q_{2}\), respectively, and the Euclidean distance between \(Q_{1}\) and \(Q_{2}\) is calculated using Equation (6) as the contrast loss value \(loss_{\text{c}}\),  
\[\text{loss}_{\text{c}}=\left\|\,Q_{1}-Q_{2}\right\|_{2}=(\,\sum_{i=1}^{\text {v}}|q_{1i}-q_{2i}|^{2})^{1/2}, \tag{6}\]  
where the characteristic dimension of \(Q_{i}(i=1,2)\) is \(v\times 1\). The contrast loss between adversarial samples and clean samples is calculated, guiding the model to gather the same sample features together according to the extracted features, improving the classification and clustering ability of the encoder, and enhancing the robustness of the model to the adversarial samples and clean samples. According to Equation (7), the potential representation of features \(Z_{1}\) and \(Z_{2}\) is obtained by using the reset parameters [30]  
\[Z_{1}=\mu_{1}+e_{1}*\sigma_{1}^{2}\qquad Z_{2}=\mu_{2}+e_{2}*\sigma_{2}^{2}, \tag{7}\]  
Figure 1: USDAA algorithm framework. where \(e_{1}\) and \(e_{2}\) are samples from a matrix following the \(N(0,1)\) distribution, and \(\mu_{i},\sigma_{1}^{2}(i=1,2)\) are the mean and variance from the encoder output. In order to enhance the diversity of the reconstructed samples \(D^{re}\) and \(D^{re\_adv}\), forcing the potential feature representation \(Z_{1}\) and \(Z_{2}\) obtained from the encoder to approximate a normal distribution, the KL divergence [31]\(loss_{kl}\) between \(N(\mu_{1},\sigma_{1}^{2}I)\), \(N(\mu_{2},\sigma_{2}^{2}I)\) and the standard normal distribution \(N(0,I)\) are calculated, respectively, and their sum is shown in Equation (8). \[loss_{kl}=KL\{N(\mu_{1},\sigma_{1}^{2}I)||N(0,I)\}+KL\{N(\mu_{2},\sigma_{2}^{2} I)||N(0,I)\}. \tag{8}\]  
To make the model display better generalization performance for adversarial samples with different perturbation values, the reconstruction loss is designed as shown in Equation (9), where \(D^{re}\) and \(D^{re\_adv}\) represent the clean samples and adversarial samples reconstructed by the decoder from \(Z_{1}\) and \(Z_{2}\), respectively, and \(t\) represents the sample dimension. \[ \begin{split}& loss_{re}=\left\|D-D^{re}\right\|_{2}+\left\|D^{ adv}-D^{re\_adv}\right\|_{2}\\ &=(\sum\limits_{i=1}^{t}\left|d_{i}-\mathrm{d}_{i}^{re}\right|^ {2})^{1/2}+(\sum\limits_{i=1}^{t}\left|d_{i}^{adv}-\mathrm{d}_{i}^{re\_adv} \right|^{2})^{1/2}\.\end{split}