{'fraction_non_alphanumeric': 0.03073463268365817, 'fraction_numerical': 0.005922038980509745, 'mean_word_length': 4.9188110026619345, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Autoencoding Generative Adversarial Networks

In the years since Goodfellow et al. introduced Generative Adversarial Networks (GANs), there has been an explosion in the breadth and quality of generative model applications. Despite this work, GANs still have a long way to go before they see mainstream adoption, owing largely to their infamous training instability. Here I propose the Autoencoding Generative Adversarial Network (AEGAN), a four-network model which learns a bijective mapping between a specified latent space and a given sample space by applying an adversarial loss and a reconstruction loss to both the generated images and the generated latent vectors. The AEGAN technique offers several improvements to typical GAN training, including training stabilization, mode-collapse prevention, and permitting the direct interpolation between real samples. The effectiveness of the technique is illustrated using an anime face dataset.


Introduction
For as long as humans have drawn breath we have been captivated by the arts. Cave paintings and jewelry are among the oldest artifacts of our species. Equally ancient and lasting is our chronic laziness and derision of work. In recent years, these two passions have been combined in the field of generative modelling, which allows for the effortless generation of works in a multitude of domains. At long last, we can create art without the drudgery of getting out of bed (assuming one owns a laptop). However, this field is still in its infancy. GANs [1], which have demonstrated photo-realistic results in image generation [2,3,4], are notoriously difficult to train [5,6]. Because they learn arbitrary, surjective mappings from a latent space (the random noise used in sample generation) to a sample space (the dataset from which we are trying to generate new samples), they are prone to mode collapse [7] and do not lend well to analysis of structures within that latent space.
In this paper, I outline the Autoencoding Generative Adversarial Network (AEGAN), which can be seen as a combination of two GANs and two autoencoders (AEs), similar to the CycleGAN formulation [2]. The AEGAN technique is a scaffolding for a GAN which learns a bijective mapping between the sample space and the specified latent space. The benefits of this technique are the primary contributions of this paper, and they are threefold: • The AEGAN technique stabilizes GAN training by directing the generator towards the low-dimensional manifold in high-dimensional pixel space.
• The AEGAN technique mitigates mode collapse by requiring that the generator be able to reproduce the training dataset.
• The AEGAN technique allows for direct interpolation between real samples.

Autoencoders
Autoencoders are a class of self-supervised neural networks with an hourglass shape which learn an efficient, domainspecific encoding of a given dataset drawn from some sample space X [8]. Autoencoders consist of two networks: an encoder, E, which learns a mapping E : X → Z, where Z is some lower-dimensional latent space; and a decoder, G, which learns a mapping G : Z → X. The autoencoder is tasked with encoding then reconstructing each sample from the dataset such that G(E(x)) = x ≈ x, and is penalized with a reconstruction loss. This reconstruction loss is typically some function of the pixel-wise difference between the input x and output x. After fitting, the encoder can be separated, frozen, and used for other purposes, such as in feature extraction for downstream applications [9]. Unfortunately, the decoder can't be used as a generative model [10]. This is because the encodings E(X) are not constrained to a smooth, well-behaved distribution, and as such most points in Z do not correspond to a realistic sample in X [10] and because pixel-wise reconstruction loss is known to result in blurry samples [11]. The first problem has been addressed by others, namely through Variational Autoencoders (VAEs) [12] and Adversarial Autoencoders (AAEs) [13], and the latter has been addressed by the VAE/GAN technique [11]. The AEGAN technique proposed in this paper can be considered a combination of the AAE and the VAE/GAN techniques, and as such addresses both of these problems.

Generative Adversarial Networks
GANs are a class of generative, unsupervised neural networks which approximate the data generating distribution p data used to produce a given dataset X [1]. They consist of two networks, a generator G and discriminator D x . The generator learns a mapping G : Z → X, from some latent space Z to the desired sample space X, where latent samples are drawn from Z with the distribution p(z) (typically a spherical, 100-dimensional normal distribution). The discriminator is tasked with discerning which samples are drawn from the true distribution (ie, the dataset) and which were generated by G. The generator and discriminator take turns updating in a minimax game, with the generator attempting to minimize Equation 1 while the discriminator attempts to maximize it.
This is equivalent to tasking the generator with minimizing the Jensen-Shannon divergence between X and G(Z) [1]. However, the sample space is typically a small manifold in a much larger pixel space; this results in disjoint support between G(Z) and X, which results in a perfect (ie, useless) discriminator which provides unhelpful feedback to the generator, leading to lack of convergence and mode collapse [6]. The AEGAN technique addresses this issue.

CycleGAN
CycleGAN is a technique for unpaired image-to-image translation whereby a bijective function is learned, mapping two image sample spaces, X and Y , to each other [2]. This is accomplished through the use of two GANs, making use of both the adversarial loss (equation 1) and a cycle-consistency loss (ie, a reconstruction loss). The proposed AEGAN technique is a generalization of CycleGAN, wherein one of the sample spaces is the latent space Z and the other is the desired sample space (not necessarily images, although incidentally an image dataset was used for this paper).

Autoencoding Generative Adversarial Networks
AEGAN is a technique for learning a bijective mapping between some sample space X and a latent space Z. An encoder network E is trained to learn a function E : X → Z, mapping each real sample to a point in the latent space. The generator network G is trained to learn a function G : Z → X mapping each point in the latent space to a sample in the sample space. These networks are trained in tandem with two discriminator networks, D x and D z , which learn to discriminate between real and generated samples and latent points, respectively. The high-level network is illustrated in Figure 2.

Adversarial Loss
The adversarial loss has four components: we consider generated samples G(z) =x, encoded samples E(x) =ẑ, autoencoded samples G(E(x)) = x, and autoencoded latent vectors E(G(z)) = z, where x are real samples drawn from the dataset and z are latent vectors drawn randomly from some distribution. Thus, the four components to the adversarial loss are: Which, summed, form the adversarial loss L GAN (G, E, D x , D z ).

Reconstruction Loss
Because the goal is to learn a bijective mapping, it is important that E undoes G and vice-versa. Thus, a reconstruction loss is used: where λ rx and λ rz are hyperparamters for controlling the weight of the sample reconstruction and latent vector reconstruction, respectively. The L 1 loss is used for sample reconstruction because it is known to result in less bluriness than L 2 in image reconstruction.

AEGAN Loss
The full AEGAN objective is thus: G and E attempt to minimize L AEGAN while D x and D z attempt to maximize it.

Results
A set of 43104 anime face images (21552 unique faces, augmented by mirroring horizontally), each with a resolution of 64x64 pixels, was used as the sample dataset X [14], while random vectors Z were drawn from the N 32 (0, I) distribution. An AEGAN was trained using this data. For comparison, a GAN (comprising of networks G and D x in Figure 2) and an AAE (comprising of networks G, E, and D z in Figure 2), both subsets of the AEGAN, were built and trained with the exact same applicable architectures and hyperparameters. Each was trained for 300k training steps (11134 epochs with a minibatch size of 16). Code and hyperparameters used for building and training the models are available at https://github.com/ConorLazarou/AEGAN-keras.

Generation
Random samples generated by the GAN and by the AEGAN are illustrated in Figure 3. Not only did the AEGAN's generator avoid mode collapse, unlike the GAN, but it also resulted in substantially higher-quality images, many of The bottom right appears to be a werewolf's neck; understandably, the reconstruction is terrifying.
which could easily be mistaken for hand-drawn faces. There is substantial diversity in the generated samples, with the exception of eye colour which is overwhelmingly green. Visualization of generated samples during the training process revealed that the AEGAN cycled through various eye colours during training, a common GAN oscillation behaviour. To verify that the stark differences in results were not a fluke, the AEGAN and the GAN were both trained from random initialization for 300k training steps five additional times; in each case, the GAN underwent total mode collapse while the AEGAN did not.

Reconstruction
Samples from the original dataset and their reconstructions are illustrated in Figure 4. The AEGAN was able to effectively reconstruct many of the original images without the telltale bluriness that autoencoders are known for, despite the pixel-wise reconstruction loss. Hair colour, face shape, eye shape, and pose are reproduced reasonably well in most cases. Some shortcomings are obvious, however: there is little variety in eye colour; the AEGAN was unable to generate glasses, and seems to have been confused by them; male-presenting faces are of poorer quality, likely due to their apparent under-representation in the dataset.

Interpolation
GANs famously allow for interpolation between generated samples by interpolating between the latent vectors used to generate those samples. Because AEGAN is bijective, it also allows for the interpolation between real samples by encoding those samples as latent vectors, interpolating between the vectors, and generating samples from those interpolated vectors. This process is illustrated in Figure 1. The AEGAN is able to smoothly interpolate between hair colours, poses, and face and eye shapes. Some issues are plainly visible, however. Eye colour is uniformly green, and several samples were reconstructed with open mouths instead of closed mouths.

Bijective mapping
Training a GAN to learn a bijective mapping is an elegant solution to the problems of training instability and mode collapse. The Generator in a traditional GAN is never directly exposed to the dataset; indeed, it's remarkable that they work at all. By including a reconstruction component in training, the AEGAN's generator can learn directly from the dataset instead of aimlessly feeling about its way in the dark that is pixel-space, making training much more stable. The reverse mapping, from images to latent space, forces the AEGAN to avoid mode collapse without relying on batch-independence-breaking techniques such as batch normalization or minibatch discrimination.

Limitations
The primary limitation of the AEGAN technique is that it effectively doubles the size of the network, from two sub-networks to four, requiring more time and resources to train. The primary limitation of this particular experiment is that the GAN implementation (https://github.com/ConorLazarou/AEGAN-keras) is fairly simplistic due to time and resource constraints. Hyperparamers, such as number and depth of layers, values of the reconstruction weights λ rx and λ rz , and curriculum methods that would decrease those weights over time were not extensively explored. Further, batch normalization, a staple of GAN implementation, was not used due to an issue in the TensorFlow.Keras implementation, and more sophisticated techniques such as the Wasserstein loss [15], the progressive growing technique [16], and conditionality [17] were not applied.

Applications and Further Work
Despite the above shortcomings, there is nothing in the AEGAN formulation that prevents it from being used in conjunction with those techniques, each of which may be an exciting excursion in the GAN landscape. Indeed, I expect combining the AEGAN technique with leading GAN techniques such as StyleGAN would prove very fruitful. Another avenue of exploration is supplementing the multivariate normal distribution used for the latent space with binomial and multinomial elements, as was done in [13]. Such a conditional training scheme may result in an interpretable latent space. One can imagine commercial applications where a potential customer's face is mapped to a vector in latent space, the vector is altered to include sunglasses or some other product, and the new vector is used to produce an image of the customer's face with the product. Obviously this is a silly example, but the ability to map a bijective function between a sample domain and its latent space is a powerful technique, the applications of which are ripe for exploration.