{'fraction_non_alphanumeric': 0.03262816847722508, 'fraction_numerical': 0.017571945873832668, 'mean_word_length': 4.588072417465389, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Semi-Supervised Adversarial Variational Autoencoder

We present a method to improve the reconstruction and generation performance of a variational autoencoder (VAE) by injecting an adversarial learning. Instead of comparing the reconstructed with the original data to calculate the reconstruction loss, we use a consistency principle for deep features. The main contributions are threefold. Firstly, our approach perfectly combines the two models, i.e., GAN and VAE, and thus improves the generation and reconstruction performance of the VAE. Secondly, the VAE training is done in two steps, which allows to dissociate the constraints used for the construction of the latent space on the one hand, and those used for the training of the decoder. By using this two-step learning process, our method can be more widely used in applications other than image processing. While training the encoder, the label information is integrated to better structure the latent space in a supervised way. The third contribution is to use the trained encoder for the consistency principle for deep features extracted from the hidden layers. We present experimental results to show that our method gives better performance than the original VAE. The results demonstrate that the adversarial constraints allow the decoder to generate images that are more authentic and realistic than the conventional VAE.


Introduction
Deep generative models (DGMs) are part of the deep models family and are a powerful way to learn any distribution of observed data through unsupervised learning. The DGMs are composed mainly by variational autoencoders (VAEs) [1] [2] [3], [4], and generative adversarial networks (GANs) [5]. The VAEs are mainly used to extract features from the input vector in an unsupervised way while the GANs are used to generate synthetic samples through an adversarial learning by achieving an equilibrium between a Generator and a Discriminator. The strength of VAE comes from an extra layer used for sampling the latent vector z and an additional term in the loss function that makes the generation of a more continuous latent space than standard autoencoders.
VAEs are very efficient for reconstructing input data through a continuous and an optimized latent space. Indeed, one of the terms of the VAE loss function is the reconstruction term which is suitable for reconstructing the input data, but with less realism than GANs. For example, an image reconstructed by a VAE is generally blurred and less realistic than the original one. However, the VAEs are completely inefficient for generating new synthetic data from the latent space comparing to the GANs. Upon an adversarial learning of the GANs, a discriminator is employed to evaluate the probability that a sample created by the generator is real or false. Most of the applications of GANs are focused in the field of image processing such as biomedical images [26] [27], but recently some papers in the industrial field have been published, mainly focused on data generation to solve the problem of the imbalanced training data [28] [29] [30] [31] [32] [33]. Through continuous improvement of GAN, 2 of 17 its performance has been increasingly enhanced and several variants have recently been developed [34] [35] [36] [37] [38] [39] [40] [41] [42] [43] [44] [45] [46] [47] [48] [49] [50] [51].
The weakness of GANs lies in their latent space which is not structured similarly to VAEs due to the lack of an encoder which can further supports the inverse mapping. With an additional encoding function, GANs can explore and use a structured latent space to discover "concept vectors" that represent high-level attributes [26]. To this purpose, some research has been recently developed such as [48] [52] [53] [54] [55].
In this paper, we propose a method to inject an adversarial learning into a VAE to improve its reconstruction and generation performance. Instead of comparing the reconstructed data with the original data to calculate the reconstruction loss, we use a consistency principle for deep features such as style reconstruction loss (Gatys [56]). To do this, the training process of the VAE is divided into two steps, training the encoder and then training the decoder. While training the encoder, we integrate the label information to structure the latent space in a supervised way, in a manner similar to [55] [53]. For the consistency principle for deep features, contrary to [56] and Hou [54] who used the pretrained deep convolutional neural network VGGNet [57], we use the features extracted from the hidden layers of the encoder. Thus, our method can be applied to areas other than image processing such as industrial nonlinear monitoring or biomedical applications. We present experimental results to show that our method gives better performance than the original VAE. In brief, our main contributions are threefold.
• Our approach perfectly combines the two models, i.e., GAN and VAE and thus improves the generation and reconstruction performance of the VAE. • The VAE training is done in two steps, which allows to dissociate the constraints used for the construction of the latent space on the one hand, and those used for the training of the decoder. • The encoder is used for the consistency principle for deep features extracted from the hidden layers.
The rest of the paper is organized as follows. We first briefly review the generative models in Section 2. After that, in Section 3, we describe the proposed method. Then, in Section 4, we present experimental results which show that the proposed method improves the performance of VAE. The last section of the paper summarizes the conclusions.

Autoencoder and the Variational form
Autoencoder (AE) represents one of the first generative model trained to recreate or reproduce the input vector x [58], [59], [60], [61]. The AE is composed by two main structures: an encoder and a decoder ( Fig. 1) which are multilayered neural networks (NNs) parameterized by φ and θ, respectively. The first part encodes the input data x into a latent representation z by the encoder function z = f φ (x), whereas the second NN decodes this latent representation ontox = h θ (z) which is an approximation or a reconstruction of the original data. In an AE, an equal number of units are used in the input/output layers while less units are used in the latent space (Fig. 1).
The variational form of the AE (Fig. 1) becomes a popular generative model by combining Bayesian inference and the efficiency of the NNs to obtain a nonlinear low-dimensional latent space [1], [2], [3], [4]. The Bayesian inference is obtained by an additional layer used for sampling the latent vector z with a prior specified distribution p(z), usually assumed to be a standard Gaussian N (0, I), where I is the identity matrix. Each element z i of the latent layer is obtained as follow: where µ i and σ i are the i th components of the mean and standard deviation vectors, is a random variable following a standard Normal distribution ( ∼ N (0, 1)). Unlike the AE which generates the latent vector z, the VAE generates vector of means µ i and standard deviations σ i . This allows to have more continuity in the latent space than the original AE. The VAE loss function given by the equation 2 has two terms. The first term L rec is the reconstruction loss function (equ. 3) which allow to minimize the difference between the input and output instances.
Both the negative expected log-likelihood (e.g., the cross-entropy function) or the mean squared error (MSE) can be used. When the sigmoid function is used in the output layer, the derivatives of MSE and cross-entropy can have similar forms.
The second term L kl (equ. 4) corresponds to the Kullback-Liebler (D kl ) divergence loss term that forces the generation of a latent vector with the specified Normal distribution [62], [63]. The D kl divergence is a theoretical measure of proximity between two densities q(x) and p(x). It is asymmetric (D kl (q p) = D kl (p q)) and nonnegative. It is minimized when q(x) = p(x) [64]. Thus, the D kl divergence term measures how close is the conditional distribution density q φ (z | x) of the encoded latent vectors from the desired Normal distribution p(z). The value of D kl is zero when two probability distributions are the same, which forces the encoder of VAE q φ (z | x) to learn the latent variables that follow a multivariate normal distribution over a k-dimensional latent space.

Adversarial learning
One of the major advantages of the Variational form compared to the classic version of the AE is achieved using the Kullback-Liebler divergence loss term (D kl ) which ensure that the encoder will organize the latent space with good properties for the generative process. Instead of encoding an input as a single point, a Normal distribution is associated to encode each input instance. This continuity of latent space allows the decoder not only to be able to reproduce an input vector, but also to generate new data from the latent space. However, the reconstruction loss function L rec is not effective for data Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 2 August 2020 doi:10.20944/preprints202008.0051.v1 generation comparing to the adversarial learning technics [5]. The main idea of these methods is to create an additional NN, called a discriminator Dis(x), that will learn to distinguish between the real data and the data generated by the generator Gen(z).
The learning process then consists in successively training the generator to generate new data and the discriminator to dissociate between real and generated data (Fig. 2). The learning process converges when the generator reaches the point of luring the discriminator. The discriminator Dis(x) is optimized by maximizing the probability of distinguishing between real and generated data while the generator Gen(z) is trained simultaneously to minimize log(1 − Dis(Gen(z))). Thus, the goal of the whole adversarial training can be summarized as a two-player minmax game with the value function V(Gen, Dis) [5]: where Φ x = log(Dis(x)) and Ψ z = log(1 − Dis(Gen(z))). These techniques are much more efficient than the reconstruction loss function L rec for generating data from a data space, .e.g. the latent space of VAE. Unlike the latent space of the VAE where the data are structured according to the Kullback-Liebler divergence loss term that forces the generation of a latent vector with the specified Normal distribution, the data space of the GAN is structured in a random way, making it less efficient in managing the data to be generated.

Method Overview
The method proposed in this paper, which is presented in Figure 3, consists in separating the learning of the VAE into two successive parts: the learning of the encoder Enc(x) followed by the learning of the decoder Dec(z). This decoupling between encoder and decoder during the learning phase allows a better structuration of the latent encoder space to focus in a second step on the optimization of the decoder. In order to structure the latent space taking into account the label information, a classifier Class(z) is trained at the same time as the encoder Enc(x). Two optimization functions are then used during the first phase: the Kullback-Liebler divergence loss function L kl and the categorical cross-entropy L cat .
As the convergence of the first phase, the two models thus obtained, i.e. the encoder Enc(x) and the classifier Class(z), will be used to train the decoder Dec(z). Three optimization functions are then used for this second step: • a reconstruction loss function (L rec ) • an adversarial loss function (L adv ) • a latent space loss function (L latent ) As previously used by Hou [54], instead of comparing the reconstructed dataX with the original one X to calculate the reconstruction loss, we compare some features extracted from the encoder as a  Figure 3. The framework of the proposed method.
style reconstruction loss (Gatys [56]). A feature reconstruction loss function is then used instead of a data reconstruction loss function. To calculate these features, we use the encoder thus obtained by the first learning phase instead of using a pretrained deep convolutional neural network such as the VGGNet network [57] used by Gatys [56] and Hou [54].
In order to facilitate the convergence of the decoder, two further optimization terms are included in the whole loss function: an adversarial loss function L adv obtained with the help of a discriminator Dis(x), trained at the same time as the decoder Dec(z), and a latent space loss function L latent . This supplementary term will compare the original latent space Z with the reconstructed oneẐ thanks to the classifier Class(z) obtained during the first learning phase.

First step: training the Variational Encoder Classifier
In this first step, the encoder and classifier are stacked together in order to form the Variational Encoder Classifier (VEC). The whole structure is trained at the same time using the following loss function L VEC : where L kl corresponds to the Kullback-Liebler (D kl ) divergence loss term given by the equation 4, while the second terme L cat represents the categorical cross-entropy obtained by the following expression: where y is the target label and E[.] is the statistical expectation operator. The algorithm 1 presents the complete procedural steps of this first learning phase.

Second step: training the Decoder
The encoder as well as the classifier obtained from the previous phase will therefore be frozen and used to train the decoder. The global optimization function is given by the following expression where β and γ are fitting parameters: The first term L rec allows the decoder to enhance its input reconstruction properties, while the second term L adv allows it to increase its ability to generate artificial data from latent space. These two terms will be put in competition and it will be necessary to find the best compromise thanks to the fitting parameters. A third term L latent will ensure that the virtual data generation is done respecting the distribution of classes in the latent space. The whole training process of the decoder is presented by the algorithm 2.

Feature reconstruction loss
For the reconstruction optimization function, we adopt the method proposed by Gatys [56] which consists in extracting the style of an image from the first hidden layers of a pre-trained network. It is also the same principle used by Lin [65] as Feature Pyramid Networks for Object Detection. Thus, instead of calculating the reconstruction error between the input X and the outputX of the decoder, the reconstruction error is calculated from the features of the image. As an alternative to a generic pre-trained network, such as the VGGNet network [57] used by [56] and Hou [54], we will exploit the features extracted from the previously trained encoder. The advantage of our method is to adapt the extracted features to the type of application being studied, which may be outside the scope of the images learned by the VGGNet network. Thus, our approach can be applied to biomedical images such as histopathological images ( [66], [27]) or vibration signals from a mechatronic system for industrial monitoring ( [20]).
As shown in Figure 4, the first three layers of the encoder will be used to extract features from the input image X. The characteristics F i of X will then be compared to the characteristicsF i of the output X generated by the decoder. The optimization function is then given by the following expression: where F i = ∅ i (X) andF i = ∅ i (X) are respectevely the features of X andX extracted from the layer i and α i is a fitting parameter.

Adversarial loss
In addition to the reconstruction function, a second term L adv allows the decoder to reconstitute the input data more realistically. A discriminator trained with the adversarial learning techniques presented in the previous section will feed a cost function that will penalize the decoder if the reconstructed image is not authentic. As in the works of [47], the output of the discriminator is linear with +1 label for real images and -1 for artificial images. However, to enhance the data generation effect, larger output values are possible, such as the ±10 output used by [54]. For this purpose, we prefer until Convergence of parameters; to adjust this generation effect with a fitting variable β (equation 8). The update of the discriminator is done before each decoder training cycle. In order to increase this adversarial learning, we use the Wasserstein GAN method [47] which consists in increasing the number of learning cycles of the discriminator with respect to the generator. The algorithm 2 presents the detail of the discriminator training where the variable n dis defines the number of training cycles of the discriminator. The optimization function L adv is then given by the following expression:

Latent loss
A last term L latent prevents that the generation effect will be stronger than the reconstitution effect. The classifier obtained in the previous learning phase will make sure that the latent spaceẐ obtained by the generated data has the same distribution as the original latent space Z. The label information of the input data is thus integrated in the reconstitution / generation process. The output of the classifier will penalize the decoder if the generated data does not belong to the same class as the original data. The expression of L latent is defined by:

Dataset description
We experimented our method on four databases: The MNIST [67], The Omniglot dataset [68], The Caltech 101 Silhouettes dataset [69] and Fashion-MNIST [70] which are briefly described below. Figure  5 illustrates some of the images taken randomly from the four databases.
• The MNIST [67] is a standard database which contains 28 × 28 images of ten handwritten digits (0 to 9) and is split in 60,000 samples for training and 10,000 for the test. • The Omniglot dataset [68] contains 28 × 28 images of handwritten characters from many world alphabets representing 50 classes which is split in 24,345 training and 8070 test image. • The Caltech 101 Silhouettes dataset [69] is composed of 28 × 28 images representing object silhouettes of 101 classes and is split in 6364 samples for training and 2307 for the test. • Fashion-MNIST [70] contains 28 × 28 images of fashion products from 10 categories and is split in 60,000 samples for training and 10,000 for the test.

Neural network architecture
As shown in the figure 6, both of the encoder, decoder are deep residual convolutional neural networks. The encoder is composed of 6 convolutional layers with 3 × 3 kernel and 1 × 1 stride. To achieve spatial downsampling we use 2 maxpooling layers with 2 × 2 kernel and 2 × 2 stride. Each convolutional layer is followed by a residual block and a LeakyReLU activation layer. We use the same residual block as used by [54]. At the end of the encoder, three fully connected layers with a LeakyReLU activation are followed by two linear fully connected layers for mean and variance. The output of the encoder is given by the sampling layer. For the decoder, we almost use the inverse encoder structure. The input of the decoder is the sampling layer followed by three fully connected layers with batch normalization and a LeakyReLU activation. 7 convolutional layers with 3 × 3 kernel and 1 × 1 stride followed by batch normalization, a LeakyReLU activation and residual block and 2 upsampling layers.
For the discriminator, we use 3 convolutional layers with 3 × 3 kernel and 1 × 1 stride LeakyReLU activation, 2 maxpooling layers with 2 × 2 kernel and 2 × 2 stride and 4 fully connected layers with a LeakyReLU activation. Lastly, the output of the discriminator is formed by one linear unit.
The classifier is formed by 3 fully connected layers with batch normalization and a LeakyReLU activation. Each fully connected layer is followed by a residual block. The output of the classifier is obtained by a softmax layer.

Impact of the dimension size of the latent space
It is obvious that the dimension of the latent space plays an active influence on the quality of reconstruction and data generation. The quality of VAE decoding is highly dependent on the K z size of the latent space. We then compared two configurations: a two-dimensional latent space (K z = 2) with a (K z = 100) dimensional space.

Impact of weighting parameters β and γ
We also investigated the impact of the β and γ weighting parameters of the equation 8. We set the parameters α i of the feature reconstruction loss (equ. 3) to 1, 0.1 and 0.01 for i=1, 2 and 3 respectively. To judge the influence of each parameter of adversarial loss β and latent loss γ, we have varied the β parameter in an increasing way according to two configurations of γ: γ = 0 and γ = 0.1. In order to visually analyze the latent space obtained by the encoder-classifier, we set the dimension of this space to 2 (K z = 2). The interest of such a space is to be able to make a projection of the initial input data space to a space visually accessible to the human brain. We can thus visually analyze the quality of the encoder. Figure 7 shows the 2D latent space obtained for the four databases. We can then see the distribution of data in the latent space as well as the interaction between the different classes. We can thus analyze the overlaps between the classes. One can then choose the area of this latent space in order to generate artificial data. Figure 8 shows examples of images generated from the latent space for Mnist and Fashion Mnist respectively. It is interesting to note that even in a space of reduced size, the data are organized according to a logic of likelihood between classes. This is very well illustrated by the latent space of Fashion Mnist where we can see that the three classes #5, 7, and9 which represent the shoes are grouped together. The handbag cluster is also isolated from the rest of the clusters.

Image reconstruction and generation
The figure 9 gives all the results obtained for the four databases with different values of β and γ for each two latent spaces (K z = 2 and K z = 100). For every database, we randomly generated 40 images as input of the VAE. The resulting output image is given according to the experimental parameters.
A. Impact of the latent space: The first observation that can be extracted from these tests concerns the influence of the latent space size on the quality of the reconstructed image. We can see that for the Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 2 August 2020 doi:10.20944/preprints202008.0051.v1 two databases Mnist and Fashion-MNIST, the results achieved on the two-dimensional latent space are almost as good as for K z = 100. Nevertheless the quality of the reconstruction alone, obtained by setting the adversary parameter β = 0, is better for K z = 100: the images are less blurred and close to the input image than on the 2D latent space. On the other hand, the image reconstruction performance obtained on the 2D space is less convincing for the Caltech database. Indeed, we can see that the reconstructed images are very blurred but for the most part close to the original image. Note that the reconstruction was completely impossible for the Omniglot database in 2D space. The only result in this space was obtained by forcing the weight of the adversary loss β to 0.5 which allowed to generate new world alphabets.
B. Impact of the parameter β: In order to measure the effect of adversarial loss term L adv on the feature reconstruction loss term L rec , we varied the adversarial β parameter during each test sequence in a progressive way. This test allows to evaluate how the data generation effect can affect the reconstruction effect. Generally speaking, we obtain three steps which are described as follows: • β < ε 1 =⇒ dominant reconstruction effect with blurred images • ε 1 ≤ β ≤ ε 2 =⇒ reconstruction effect with clearer and more realistic images • β > ε 2 =⇒ generative effect is dominant with ε 1 and ε 2 are two thresholds to define. This is clearly illustrated by the MNIST database. Let's take the case of K z = 2: • up to β = 0.003, the reconstruction effect is dominant with blurred images.
• For 0.005 ≤ β ≤ 0.01 the images are clearer and more realistic.
• The generative effect becomes dominant for β = 0.1 where most of the resulting images do not match the original ones. These cases are highlighted in red.
C. Impact of the parameter γ: In order to avoid that the generative effect becomes stronger than the reconstruction one and thus to limit the dominant effect of adversarial loss term L adv , we have Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 2 August 2020 doi:10.20944/preprints202008.0051.v1 varied the β parameter in an increasing way, under two different configurations of γ (γ = 0 and γ = 0.1). This measures the effect of the latent loss term L latent . We can see that it had the expected effect. The red-framed images have been corrected and are now compatible with the original images again. These corrected images are framed in green. Figure 9. The results obtained for the four databases with different values of β and γ for each of the two latent spaces (K z = 2 and K z = 100).

Test on Cifar 10
To conclude this experimental part, we did a final test on the reconstruction of images taken from the Cifar10 database [71]. The size of the latent space is fixed at K z = 100, with the parameters α i = 1, β = 0.001 and γ = 0. Figure 10 shows several pairs of random images with their reconstructed output.

Conclusion
In this article, we propose a new technique for the training of the varitional autoencoder by incorporating several efficient constraints. Particularly, we use the principle of consistency of deep characteristics to allow the output of the decoder to have a better reconstruction quality. The adversarial constraints allow the decoder to generate data with better authenticity and more realistic than the Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 2 August 2020 doi:10.20944/preprints202008.0051.v1