## 3 Method
### CNN-Based Adversarial Training  
GAN-style adversarial training methods have been shown to perform well in anomaly detection on time series data [24]. To this end, we also constructed two CNN decoders in the CAT-IADEF model, as shown in Figure 1, and we treat the training of the entire model as a two-stage training (the actual two-stage training happens at the same time).  
As shown in Figure 1, \(ED_{1}\) consists of \(Encoder\) and \(Decoder1\), and \(ED_{2}\) consists of \(Encoder\) and \(Decoder2\). First, the two EDs reconstruct the normal input window sequence \(W\), and second, the two EDs are trained adversarially. \(ED_{1}\) tries to fool \(ED_{2}\), and \(ED_{2}\) aims to know the truth of the data. Below are the details of the two-stage training.  
**Phase 1: Minimize rebuild.** Figure 1 also directly presents the first stage outputs of \(ED_{1}\) and \(ED_{2}\), and, to minimize the reconstruction loss of the input data [19], we utilize the \(L_{2}\) norm to define the reconstruction losses of \(ED_{1}\) and \(ED_{2}\) during Phase 1:  
\[L_{1} =||W-ED_{1}(W)||_{2} \tag{1}\] \[L_{2} =||W-ED_{2}(W)||_{2} \tag{2}\]  
Their goal is to minimize these losses, that is:  
\[\min||W-ED_{1}(W)||_{2} \tag{3}\]  
\[\min||W-ED_{2}(W)||_{2} \tag{4}\]  
**Phase 2: Adversarial training amplifies errors.** The \(ED_{2}\) training goal is to distinguish actual data. \(ED_{1}\) is trained to fool \(ED_{2}\), while \(ED_{2}\) reconstructs the data output by \(ED_{1}\) and outputs the reconstruction error with the actual data \(W\). So, the reconstruction loss here is:  
\[L_{3} =||W-ED_{2}(ED_{1}(W))||_{2} \tag{5}\]  
At this point, the goal of \(ED_{1}\) is to minimize the difference between the outputs of \(W\) and \(ED_{2}\). In contrast, \(ED_{2}\) in the second stage aims to maximize the difference \(||W-ED_{2}(ED_{1}(W))||_{2}\) to distinguish the data \(ED_{1}(W)\) generated by \(ED_{1}\) from the real data \(W\), that is, the reconstruction loss needs to be amplified. This means that the training target at this time is:  
\[\min_{ED_{1}}\max_{ED_{2}}||W-ED_{2}(ED_{1}(W))||_{2} \tag{6}\]  
\(ED_{1}\) needs to minimize this error, and \(ED_{2}\) needs to maximize this error (amplify it). Therefore, we adjust by adding a sign to the reconstruction loss.  
\[L^{*}_{1} =+||W-ED_{2}(ED_{1}(W))||_{2} \tag{7}\]  
Figure 1: The overall architecture of CAT-IADEF.  
\[L^{*}{}_{2}=-||W-ED_{2}(ED_{1}(W))||_{2} \tag{8}\]  
**Two-stage training.** We find the corresponding reconstruction loss functions of \(ED_{1}\) and \(ED_{2}\) at different stages and now combine them to obtain the training targets of \(ED_{1}\) and \(ED_{2}\) for the entire training stage:  
\[L_{ED_{1}}{=}\frac{1}{n}||W-ED_{1}(W)||_{2}{+}(1{-}\frac{1}{n})||W-ED_{2}(ED_{1} (W))||_{2} \tag{9}\]  
\[L_{ED_{2}}{=}\frac{1}{n}||W-ED_{2}(W)||_{2}{-}(1{-}\frac{1}{n})||W-ED_{2}(ED_{1} (W))||_{2} \tag{10}\]  
Here \(n\) is the training period, and phase 1 and adversarial training are generally reflected in Algorithm 1.  
```
Initialization: \(T\) used for \(W\)  Encoder, Decoder1, and Decoder2 Iteration limit N
1. Initialize weights
2. \(n\gets 0\)
3.do
4.for\(t=1\) to \(T\)
5. \(R_{1}\gets ED_{1}(W_{t})\), \(R_{2}\gets ED_{2}(W_{t})\)
6. \(R_{2}^{\prime}\gets ED_{2}(R_{1})\)
7. \(L_{ED_{1}}{=}\frac{1}{q}||W_{t}-R_{1}||_{2}{+}(1{-}\frac{1}{q})||W_{t}-R_{2}^{ \prime}||_{2}\)
8. \(L_{ED_{2}}{=}\frac{1}{n}||W_{t}-R_{2}||_{2}{-}(1{-}\frac{1}{n})||W_{t}-R_{2}^{ \prime}||_{2}\)
9. Encoder, Decoder1, and Decoder2\(\leftarrow\) update weights using \(L_{ED_{1}},L_{ED_{2}}\)
10. \(n\gets n+1\)
11.while\(n<N\)
```  
**Algorithm 1** CAT-IADEF training algorithm.  
The first line in Algorithm 1 is to initialize the weights of _Encoder_, _Decoder_1, _Decoder_2, and lines 2 to 4 iterate according to the given \(N\). Lines 4 to 11 use \(L_{ED_{1}},L_{ED_{2}}\) to update the weights of _Encoder_, _Decoder_1, and _Decoder_2 in each iteration.