# 4-Class MI-EEG Signal Generation and Recognition with CVAE-GAN
## 3 Methods
### Optimization of Training Process  
The encoder E maps the input data \(x\) to a latent representation \(z\) through a learned distribution \(P(z|x_{r},c)\), where \(s\) represents the category of the data. The generative network \(G\) generates EEG data from a learned distribution \(P(x_{f}\big{|}z,c)\). The function of the generator and discriminator is similar to that in the generative adversarial network (GAN). The generator tries to model the real data distribution according to the gradients given by the discriminator result learning to distinguish between real and fake samples. In the encoder of the CVAE framework, \(L_{KL}\) is employed in the encoding network, indicating whether the distribution of latent variable is similar to the expected distribution, as shown:  
\[L_{KL}=\frac{1}{2}\{\mu^{T}\mu+\text{sum}[\exp(\sigma)-\sigma-k]\}, \tag{6}\]  
where the loss function consists of the reconstruction loss of the decoder and the variational posterior encoder loss, this framework was suggested to enforces representation for \(z\) with respect to \(s\). However, as is universally known to be an unperfect achievement in generation practice, which we applied GAN to tackle this problem. The GAN consists of a generator \(G\) (the CVAE) and a discriminator \(D\), where the generator loss and the discriminator loss can be illustrated respectively as:  
\[L_{G}\leftarrow\frac{1}{2}||\ x_{r}-x_{f}||_{2}^{2}, \tag{7}\]  
\[L_{D}\leftarrow-\log(d_{r})+\log(1-d_{g}). \tag{8}\] \begin{table}
\begin{tabular}{c c c c c} \hline \hline  & **Layers** & **Input** & **Output** & **Processing** \\ \hline Pre-processing & & T \(\times\) E & 32 \(\times\) 32 \(\times\) 3 & STFT \\ \hline \multirow{8}{*}{Encoder} & E1 & 32 \(\times\) 32 \(\times\) 3 & 15 \(\times\) 32 \(\times\) 1 & \begin{tabular}{c} Convolution kernel 20@ 3 \(\times\) 1 \(\times\) 3 \\ Batch Norm and MaxPool2D (20 \(\times\) 2) \& \\ Dropout (0.5) \\ Convolution kernel 40@1 \(\times\) 3 \(\times\) 1 \\ Batch Norm and MaxPool2D (1 \(\times\) 15) \& \\ Dropout (0.5) \\ \end{tabular} \\  & E2 & 15 \(\times\) 32 \(\times\) 1 & 15 \(\times\) 1 \(\times\) 40 & \begin{tabular}{c} Convolution kernel 40@1 \(\times\) 3 \(\times\) 1 \\ Batch Norm and MaxPool2D (1 \(\times\) 15) \& \\ Dropout (0.5) \\ \end{tabular} \\  & E3 & 15 \(\times\) 1 \(\times\) 40 & 600 & \begin{tabular}{c} Flatten \\ mapping into two feature \\ compression layers \\ \end{tabular} \\ \hline Latent Representation & R & \(\mu\), \(\sigma\) & \(z\) & Map representation \(z\) \\ \hline \multirow{8}{*}{Generator} & G1 & \(z\) & 600 & Fully-Connected Layer \\  & G2 & 600 & \(15\times 1\times 40\) & Reshape \\  & G3 & \(15\times 1\times 40\) & \(15\times 32\times 1\) & De-convolution and \\  & G4 & \(15\times 32\times 1\) & \(32\times 32\times 3\) & bilinear interpolation \\ \hline \multirow{8}{*}{Recognizer} & F1 & 32 \(\times\) 32 \(\times\) 3 & 15 \(\times\) 32 \(\times\) 1 & \
\begin{tabular}{c} Convolution kernel 20@ 3 \(\times\) 1 \(\times\) 3 \\ Batch Norm and MaxPool2D (20 \(\times\) 2) \& \\ Dropout (0.5) \\ \end{tabular} \\  & Feature extractor & F2 & 15 \(\times\) 32 \(\times\) 1 & 15 \(\times\) 1 \(\times\) 40 &
\begin{tabular}{c} Convolution kernel 40@ 1 \(\times\) 3 \(\times\) 1 \\ Batch Norm and MaxPool2D (1 \(\times\) 15) \& \\ Dropout (0.5) \\ \end{tabular} \\ \hline \multirow{8}{*}{Discriminator} & D3 & 15 \(\times\) 1 \(\times\) 40 & 60 & Flatten and Fully-Connected Layer \\  & softmax & 60 & 2 & Recognition for real or fake signal \\ \hline \multirow{2}{*}{Classifier} & C1 & 15 \(\times\) 1 \(\times\) 40 & 100 & Flatten and Fully-Connected Layer \\  & softmax & 100 & 4 & Classification with softmax \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Implementation details for the proposed CVAE-GAN architecture.