Generative adversarial network
![]()
A generative adversarial network (GAN) is a class of machine learning frameworks and a prominent framework for approaching generative artificial intelligence. The concept was initially developed by Ian Goodfellow and his colleagues in June 2014.[1] In a GAN, two neural networks compete with each other in the form of a zero-sum game, where one agent's gain is another agent's loss. Given a training set, this technique learns to generate new data with the same statistics as the training set. For example, a GAN trained on photographs can generate new photographs that look at least superficially authentic to human observers, having many realistic characteristics. Though originally proposed as a form of generative model for unsupervised learning, GANs have also proved useful for semi-supervised learning,[2] fully supervised learning,[3] and reinforcement learning.[4] The core idea of a GAN is based on the "indirect" training through the discriminator, another neural network that can tell how "realistic" the input seems, which itself is also being updated dynamically.[5] This means that the generator is not trained to minimize the distance to a specific image, but rather to fool the discriminator. This enables the model to learn in an unsupervised manner. GANs are similar to mimicry in evolutionary biology, with an evolutionary arms race between both networks. DefinitionMathematicalThe original GAN is defined as the following game:[1]
The generator's task is to approach , that is, to match its own output distribution as closely as possible to the reference distribution. The discriminator's task is to output a value close to 1 when the input appears to be from the reference distribution, and to output a value close to 0 when the input looks like it came from the generator distribution. In practiceThe generative network generates candidates while the discriminative network evaluates them.[1] This creates a contest based on data distributions, where the generator learns to map from a latent space to the true data distribution, aiming to produce candidates that the discriminator cannot distinguish from real data. The discriminator's goal is to correctly identify these candidates, but as the generator improves, its task becomes more challenging, increasing the discriminator's error rate.[1][6] A known dataset serves as the initial training data for the discriminator. Training involves presenting it with samples from the training dataset until it achieves acceptable accuracy. The generator is trained based on whether it succeeds in fooling the discriminator. Typically, the generator is seeded with randomized input that is sampled from a predefined latent space (e.g. a multivariate normal distribution). Thereafter, candidates synthesized by the generator are evaluated by the discriminator. Independent backpropagation procedures are applied to both networks so that the generator produces better samples, while the discriminator becomes more skilled at flagging synthetic samples.[7] When used for image generation, the generator is typically a deconvolutional neural network, and the discriminator is a convolutional neural network. Relation to other statistical machine learning methodsGANs are implicit generative models,[8] which means that they do not explicitly model the likelihood function nor provide a means for finding the latent variable corresponding to a given sample, unlike alternatives such as flow-based generative model. ![]() Compared to fully visible belief networks such as WaveNet and PixelRNN and autoregressive models in general, GANs can generate one complete sample in one pass, rather than multiple passes through the network. Compared to Boltzmann machines and linear ICA, there is no restriction on the type of function used by the network. Since neural networks are universal approximators, GANs are asymptotically consistent. Variational autoencoders might be universal approximators, but it is not proven as of 2017.[9] Mathematical propertiesMeasure-theoretic considerationsThis section provides some of the mathematical theory behind these methods. In modern probability theory based on measure theory, a probability space also needs to be equipped with a σ-algebra. As a result, a more rigorous definition of the GAN game would make the following changes:
Since issues of measurability never arise in practice, these will not concern us further. Choice of the strategy setIn the most generic version of the GAN game described above, the strategy set for the discriminator contains all Markov kernels , and the strategy set for the generator contains arbitrary probability distributions on . However, as shown below, the optimal discriminator strategy against any is deterministic, so there is no loss of generality in restricting the discriminator's strategies to deterministic functions . In most applications, is a deep neural network function. As for the generator, while could theoretically be any computable probability distribution, in practice, it is usually implemented as a pushforward: . That is, start with a random variable , where is a probability distribution that is easy to compute (such as the uniform distribution, or the Gaussian distribution), then define a function . Then the distribution is the distribution of . Consequently, the generator's strategy is usually defined as just , leaving implicit. In this formalism, the GAN game objective is Generative reparametrizationThe GAN architecture has two main components. One is casting optimization into a game, of form , which is different from the usual kind of optimization, of form . The other is the decomposition of into , which can be understood as a reparametrization trick. To see its significance, one must compare GAN with previous methods for learning generative models, which were plagued with "intractable probabilistic computations that arise in maximum likelihood estimation and related strategies".[1] At the same time, Kingma and Welling[10] and Rezende et al.[11] developed the same idea of reparametrization into a general stochastic backpropagation method. Among its first applications was the variational autoencoder. Move order and strategic equilibriaIn the original paper, as well as most subsequent papers, it is usually assumed that the generator moves first, and the discriminator moves second, thus giving the following minimax game: If both the generator's and the discriminator's strategy sets are spanned by a finite number of strategies, then by the minimax theorem,that is, the move order does not matter. However, since the strategy sets are both not finitely spanned, the minimax theorem does not apply, and the idea of an "equilibrium" becomes delicate. To wit, there are the following different concepts of equilibrium:
For general games, these equilibria do not have to agree, or even to exist. For the original GAN game, these equilibria all exist, and are all equal. However, for more general GAN games, these do not necessarily exist, or agree.[12] Main theorems for GAN gameThe original GAN paper proved the following two theorems:[1] Theorem (the optimal discriminator computes the Jensen–Shannon divergence)—For any fixed generator strategy , let the optimal reply be , then
where the derivative is the Radon–Nikodym derivative, and is the Jensen–Shannon divergence. Proof
By Jensen's inequality, and similarly for the other term. Therefore, the optimal reply can be deterministic, i.e. for some function , in which case
To define suitable density functions, we define a base measure , which allows us to take the Radon–Nikodym derivatives with . We then have
The integrand is just the negative cross-entropy between two Bernoulli random variables with parameters and . We can write this as , where is the binary entropy function, so
This means that the optimal strategy for the discriminator is , with after routine calculation. Interpretation: For any fixed generator strategy , the optimal discriminator keeps track of the likelihood ratio between the reference distribution and the generator distribution:where is the logistic function. In particular, if the prior probability for an image to come from the reference distribution is equal to , then is just the posterior probability that came from the reference distribution: Theorem (the unique equilibrium point)—For any GAN game, there exists a pair that is both a sequential equilibrium and a Nash equilibrium:
That is, the generator perfectly mimics the reference, and the discriminator outputs deterministically on all inputs. Proof
From the previous proposition,
For any fixed discriminator strategy , any concentrated on the set is an optimal strategy for the generator. Thus,
By Jensen's inequality, the discriminator can only improve by adopting the deterministic strategy of always playing . Therefore,
By Jensen's inequality,
with equality if , so
Finally, to check that this is a Nash equilibrium, note that when , we have which is always maximized by . When , any strategy is optimal for the generator. Training and evaluating GANTrainingUnstable convergenceWhile the GAN game has a unique global equilibrium point when both the generator and discriminator have access to their entire strategy sets, the equilibrium is no longer guaranteed when they have a restricted strategy set.[12] In practice, the generator has access only to measures of form , where is a function computed by a neural network with parameters , and is an easily sampled distribution, such as the uniform or normal distribution. Similarly, the discriminator has access only to functions of form , a function computed by a neural network with parameters . These restricted strategy sets take up a vanishingly small proportion of their entire strategy sets.[13] Further, even if an equilibrium still exists, it can only be found by searching in the high-dimensional space of all possible neural network functions. The standard strategy of using gradient descent to find the equilibrium often does not work for GAN, and often the game "collapses" into one of several failure modes. To improve the convergence stability, some training strategies start with an easier task, such as generating low-resolution images[14] or simple images (one object with uniform background),[15] and gradually increase the difficulty of the task during training. This essentially translates to applying a curriculum learning scheme.[16] Mode collapseGANs often suffer from mode collapse where they fail to generalize properly, missing entire modes from the input data. For example, a GAN trained on the MNIST dataset containing many samples of each digit might only generate pictures of digit 0. This was termed "the Helvetica scenario".[1] One way this can happen is if the generator learns too fast compared to the discriminator. If the discriminator is held constant, then the optimal generator would only output elements of .[citation needed] So for example, if during GAN training for generating MNIST dataset, for a few epochs, the discriminator somehow prefers the digit 0 slightly more than other digits, the generator may seize the opportunity to generate only digit 0, then be unable to escape the local minimum after the discriminator improves. Some researchers perceive the root problem to be a weak discriminative network that fails to notice the pattern of omission, while others assign blame to a bad choice of objective function. Many solutions have been proposed, but it is still an open problem.[17][18] Even the state-of-the-art architecture, BigGAN (2019), could not avoid mode collapse. The authors resorted to "allowing collapse to occur at the later stages of training, by which time a model is sufficiently trained to achieve good results".[19] Two time-scale update ruleThe two time-scale update rule (TTUR) is proposed to make GAN convergence more stable by making the learning rate of the generator lower than that of the discriminator. The authors argued that the generator should move slower than the discriminator, so that it does not "drive the discriminator steadily into new regions without capturing its gathered information". They proved that a general class of games that included the GAN game, when trained under TTUR, "converges under mild assumptions to a stationary local Nash equilibrium".[20] They also proposed using the Adam stochastic optimization[21] to avoid mode collapse, as well as the Fréchet inception distance for evaluating GAN performances. Vanishing gradientConversely, if the discriminator learns too fast compared to the generator, then the discriminator could almost perfectly distinguish . In such case, the generator could be stuck with a very high loss no matter which direction it changes its , meaning that the gradient would be close to zero. In such case, the generator cannot learn, a case of the vanishing gradient problem.[13] Intuitively speaking, the discriminator is too good, and since the generator cannot take any small step (only small steps are considered in gradient descent) to improve its payoff, it does not even try. One important method for solving this problem is the Wasserstein GAN. EvaluationGANs are usually evaluated by Inception score (IS), which measures how varied the generator's outputs are (as classified by an image classifier, usually Inception-v3), or Fréchet inception distance (FID), which measures how similar the generator's outputs are to a reference set (as classified by a learned image featurizer, such as Inception-v3 without its final layer). Many papers that propose new GAN architectures for image generation report how their architectures break the state of the art on FID or IS. Another evaluation method is the Learned Perceptual Image Patch Similarity (LPIPS), which starts with a learned image featurizer , and finetunes it by supervised learning on a set of , where is an image, is a perturbed version of it, and is how much they differ, as reported by human subjects. The model is finetuned so that it can approximate . This finetuned model is then used to define .[22] Other evaluation methods are reviewed in.[23] VariantsThere is a veritable zoo of GAN variants.[24] Some of the most prominent are as follows: Conditional GANConditional GANs are similar to standard GANs except they allow the model to conditionally generate samples based on additional information. For example, if we want to generate a cat face given a dog picture, we could use a conditional GAN. The generator in a GAN game generates , a probability distribution on the probability space . This leads to the idea of a conditional GAN, where instead of generating one probability distribution on , the generator generates a different probability distribution on , for each given class label . For example, for generating images that look like ImageNet, the generator should be able to generate a picture of cat when given the class label "cat". In the original paper,[1] the authors noted that GAN can be trivially extended to conditional GAN by providing the labels to both the generator and the discriminator. Concretely, the conditional GAN game is just the GAN game with class labels provided:where is a probability distribution over classes, is the probability distribution of real images of class , and the probability distribution of images generated by the generator when given class label . In 2017, a conditional GAN learned to generate 1000 image classes of ImageNet.[25] GANs with alternative architecturesThe GAN game is a general framework and can be run with any reasonable parametrization of the generator and discriminator . In the original paper, the authors demonstrated it using multilayer perceptron networks and convolutional neural networks. Many alternative architectures have been tried. Deep convolutional GAN (DCGAN):[26] For both generator and discriminator, uses only deep networks consisting entirely of convolution-deconvolution layers, that is, fully convolutional networks.[27] Self-attention GAN (SAGAN):[28] Starts with the DCGAN, then adds residually-connected standard self-attention modules to the generator and discriminator. Variational autoencoder GAN (VAEGAN):[29] Uses a variational autoencoder (VAE) for the generator. Transformer GAN (TransGAN):[30] Uses the pure transformer architecture for both the generator and discriminator, entirely devoid of convolution-deconvolution layers. Flow-GAN:[31] Uses flow-based generative model for the generator, allowing efficient computation of the likelihood function. GANs with alternative objectivesMany GAN variants are merely obtained by changing the loss functions for the generator and discriminator. Original GAN: We recast the original GAN objective into a form more convenient for comparison: Original GAN, non-saturating loss: This objective for generator was recommended in the original paper for faster convergence.[1]The effect of using this objective is analyzed in Section 2.2.2 of Arjovsky et al.[32] Original GAN, maximum likelihood: where is the logistic function. When the discriminator is optimal, the generator gradient is the same as in maximum likelihood estimation, even though GAN cannot perform maximum likelihood estimation itself.[33][34] Hinge loss GAN:[35]Least squares GAN:[36]where are parameters to be chosen. The authors recommended . Wasserstein GAN (WGAN)The Wasserstein GAN modifies the GAN game at two points:
One of its purposes is to solve the problem of mode collapse (see above).[13] The authors claim "In no experiment did we see evidence of mode collapse for the WGAN algorithm". GANs with more than two playersAdversarial autoencoderAn adversarial autoencoder (AAE)[37] is more autoencoder than GAN. The idea is to start with a plain autoencoder, but train a discriminator to discriminate the latent vectors from a reference distribution (often the normal distribution). InfoGANIn conditional GAN, the generator receives both a noise vector and a label , and produces an image . The discriminator receives image-label pairs , and computes . When the training dataset is unlabeled, conditional GAN does not work directly. The idea of InfoGAN is to decree that every latent vector in the latent space can be decomposed as : an incompressible noise part , and an informative label part , and encourage the generator to comply with the decree, by encouraging it to maximize , the mutual information between and , while making no demands on the mutual information between . Unfortunately, is intractable in general, The key idea of InfoGAN is Variational Mutual Information Maximization:[38] indirectly maximize it by maximizing a lower boundwhere |