TL;DR
A generative adversarial network, or GAN, is a pair of neural networks trained against each other. The generator takes random noise and turns it into a fake sample, such as an image. The discriminator looks at a sample and guesses whether it came from the real data or from the generator. Each network improves by beating the other, and when training works, the generator ends up producing samples the discriminator cannot tell apart from real ones.
Where you'll see it: early photorealistic face generation, image-to-image translation such as turning sketches into photos, and data augmentation for rare medical or industrial images.
Intuition
A counterfeiter and a detective
Picture a counterfeiter who wants to pass forged banknotes, and a detective who wants to catch them. At the start the forgeries are crude and the detective catches every one. Each time a forgery is caught, the counterfeiter studies what gave it away and improves. The detective, in turn, has to get sharper, because the forgeries are better now. Neither side is ever told exactly what a real note looks like. Each only knows whether it fooled the other.
A GAN is this contest written as a training loop. The generator is the counterfeiter, the discriminator is the detective, and the real data is the genuine currency. The generator never sees a real example directly. Its only feedback comes through the discriminator's judgement.
Why two networks instead of one
The obvious way to learn a data distribution is to maximise the likelihood of the training data, as in latent variable models and autoregressive models. That needs a density you can compute exactly or at least bound tightly, which is hard for images. A GAN sidesteps the problem. It never writes down the density of its output. It only needs to produce samples, and it trains by asking whether a sample looks real. The discriminator is a learned, flexible judge of realism, so the generator is not forced to fit a formula.
The plot below shows the goal in one dimension. The real data follows one curve, and the generator starts with a different one. Training pushes the generator's curve on top of the real one. When the two curves coincide, the discriminator is reduced to guessing.
- real data p_data
- generator p_g, early
- generator p_g, late
The math
Setup
Let be the distribution of real samples , such as images. The generator takes a noise vector , drawn from a simple distribution such as a standard normal, and outputs a sample . The distribution of those outputs is written . The discriminator takes a sample and returns a number between 0 and 1, read as the probability that is real.
The value function
Goodfellow and co-authors wrote the training problem as a single objective:
Each term does one job. The first term rewards for scoring real samples close to 1, since is largest when is largest. The second term rewards for scoring fake samples close to 0, since is largest when is smallest. The discriminator maximises . The generator minimises it, which means making large, so the fakes look real. Expectations here are averages over the sampling distribution written under .
The discriminator's best response
Fix the generator, so is a known fixed distribution. For each point , the discriminator's best score is the one that maximises the integrand. Writing the integrand as and setting its derivative in to zero gives
Intuitively, says: at a point where real data is common and generated data is rare, the answer is close to 1, and the reverse gives a value close to 0. Where the two densities are equal, the answer is exactly one half.
What the generator is really minimising
Substitute back into . Write for the average of the two distributions. Each fraction can be rewritten as times a ratio against :
The last line uses the definition of the Jensen-Shannon divergence: an average of two KL divergences, each taken against the mixture . This is the key result of the paper. If the discriminator is perfect, the generator's objective is a constant plus twice a divergence between the real and generated distributions. The JSD is zero only when , so that is the global minimum, with value .
Training in practice
Real training cannot compute at every step. Instead, both networks are neural networks, typically feedforward networks or convolutional ones, and they are trained by gradient descent with mini-batches. Each iteration does one or more gradient steps for that increase , then one step for that decreases . The discriminator's loss is a binary cross-entropy between its guesses and the labels real and fake.
For the generator, the literal objective has a known weakness, discussed in the worked examples. The standard fix is the non-saturating loss, which minimises instead of . The two have the same fixed points, but the second gives much stronger gradients early in training.
Worked examples
Let the sample space be just two values, and . Let the real distribution be and the generator's distribution be .
- The best discriminator scores are and .
- Compute the value directly. The real term is .
- The fake term is .
- Add them: nats.
- Check with the formula. The mixture is . The two KL terms are and , so . Then , which matches up to rounding.
Interpretation: the value sits between , the best the generator can achieve, and , the worst case, reached when the two distributions are disjoint (JSD gives ). The generator would improve by moving toward .
Let the discriminator's output on a fake sample be . Suppose the discriminator's raw score (its logit) is , with and the logistic function. Then , and .
- Saturating loss: the generator's gradient is about , a small signal.
- Non-saturating loss: the gradient is about , nine times larger.
- Numerically, and . The non-saturating loss is steep where the generator is losing, and flat where it is already winning.
Interpretation: early in training, the discriminator easily spots fakes, so is near 0. The literal objective gives the generator almost no gradient exactly when it needs one most. The non-saturating form fixes this and is what most implementations use.
Tradeoffs and failure modes
GANs produce sharp samples and are fast to sample from, since one forward pass through gives one sample. They have no tractable likelihood, so they cannot tell you how probable a given image is, and they do not cover the whole data distribution by default.
| Situation | Problem | Better choice |
|---|---|---|
| Discriminator wins too early | Vanishing gradient for | Non-saturating loss, or Wasserstein loss |
| Generator produces a few good samples repeatedly | Mode collapse: low diversity | Minibatch discrimination, unrolled updates, or a different objective |
| Need exact likelihoods or latent inference | GAN gives samples only | [[latent-variable-model |
| Need stable training at scale | Oscillating, non-convergent updates | Diffusion models, which train by a simple regression loss |
| Disjoint supports between and | JSD is flat at , so no signal | Wasserstein distance, which keeps changing as the distributions move |
The training dynamics are the hard part. The game is not a simple minimisation, and the two networks can chase each other in circles, see minimax games. Evaluation is also difficult. There is no single loss curve that says the samples are good, so practitioners inspect samples or use proxy scores such as the Frechet Inception Distance.
History
Ian Goodfellow and collaborators at the Université de Montréal published the GAN framework at NeurIPS in 2014. The problem they were solving was generative modelling of high-dimensional data such as images, where maximum likelihood methods of the time either needed intractable densities or relied on approximations that produced blurry samples. Their idea was to replace the likelihood with a learned adversary.
Early GAN images were small and blurry. Progress came from architectural work, most notably DCGAN in 2015 by Radford, Metz and Chintala, which established convolutional designs and training recipes that became the default. The Wasserstein GAN of 2017 by Arjovsky, Chintala and Bottou addressed the instability of the JSD objective. By the late 2010s, GANs were producing high-resolution photorealistic faces and became the standard reference point for image generation research. Later, diffusion models overtook them on many benchmarks, but the adversarial idea remains widely used.
Check yourself
QuestionIn one sentence, what does the discriminator learn to do, and what does the generator learn to do?Show answer
The discriminator learns to output a high probability for real samples and a low probability for generated ones, so it acts as a learned realism judge. The generator learns to transform noise into samples that the discriminator scores as real, so it moves its output distribution toward the real data.
QuestionIf the generator's distribution exactly equals the real distribution, what is the value of , and why does the discriminator get stuck?Show answer
When , the JSD is zero, so . The optimal discriminator is everywhere. It cannot tell real from fake, so it guesses at random.
QuestionA discriminator gives a fake sample the score . Compute the value of the non-saturating generator loss , and say whether the generator should be pleased with this score.Show answer
. A high loss means the generator is not fooling the discriminator, so it should be unhappy and will get a strong gradient to improve. The score is not pleasing, since the discriminator still identifies this fake with probability 0.8.
QuestionWhy does a GAN's objective tie the generator to the JSD, and what goes wrong when the real and generated distributions do not overlap?Show answer
Substituting the optimal discriminator turns the value into a constant plus twice the JSD between the two distributions, so the generator is effectively minimising JSD. When the supports are disjoint, the JSD is at its maximum of no matter how far apart the distributions are, so small moves give no gradient. This is the flat-region problem that motivated the Wasserstein distance.