AI
Generative Adversarial Network (GAN), Explained
Generative adversarial network explained: how a GAN's generator and discriminator compete, a worked loss example, GAN types, uses and GAN vs diffusion.
A generative adversarial network (GAN) is a deep learning model made of two neural networks that compete: a generator creates fake data, such as images, from random noise, and a discriminator tries to tell those fakes from real examples. Each improves by beating the other, until the generator produces outputs realistic enough to fool the discriminator. GANs, introduced in 2014, were the leading way to generate realistic images for several years, and they're still used for fast image synthesis, super-resolution and editing, though diffusion models now dominate text-to-image generation.
This guide explains what is GAN in machine learning, the GAN deep learning architecture, how training works with a worked loss example, the main GAN types, generative adversarial networks examples in practice, GAN vs diffusion model, common problems and how they're fixed. It goes deeper than the GAN section in our diffusion model explainer.
What Is GAN in Machine Learning?
In machine learning, a GAN is a framework for training a generative model, one that produces new data resembling its training data. Ian Goodfellow and colleagues introduced it in the 2014 paper "Generative Adversarial Nets." Its abstract describes two models trained together: "a generative model G that captures the data distribution, and a discriminative model D that estimates the probability that a sample came from the training data rather than G."
A common analogy is a counterfeiter and a detective. The counterfeiter (generator) prints fake banknotes; the detective (discriminator) inspects notes and says real or fake. Every time the detective catches a fake, the counterfeiter learns what gave it away; every time a fake slips through, the detective learns to look closer. Over many rounds, the fakes become very hard to tell from real notes.
The paper frames this as "a minimax two-player game" and shows that in the ideal case a unique solution exists, with the generator recovering the training data distribution and the discriminator outputting one half everywhere: it can no longer tell real from fake.
The GAN Deep Learning Architecture
A GAN has two networks with opposite jobs:
| Part | Input | Output | Goal |
|---|---|---|---|
| Generator (G) | A random noise vector, often 100 to 512 numbers; optionally a label or image | A fake sample, such as an image | Make samples the discriminator classifies as real |
| Discriminator (D) | A real sample or a generated one | A probability that the input is real | Correctly label real as real and fake as fake |
For images, the generator is usually a stack of layers that upsample noise into a picture, often transposed convolutions or upsampling plus convolutions; the discriminator is a convolutional classifier that downsamples an image to a single score. The noise vector, the latent vector, acts like a set of dials: moving through this "latent space" changes the generated image smoothly, which is what made GAN face editing possible.
How Generative Adversarial Network Training Works
Training alternates between the two networks:
- Train the discriminator. Take a batch of real images and a batch of images from the generator. Update the discriminator to output a high probability for the real ones and a low probability for the fakes.
- Train the generator. Generate a new batch, pass it through the discriminator, and update only the generator so that the discriminator's output on its fakes goes up.
- Repeat for many thousands of steps.
The generator never sees real images directly. It learns entirely from the discriminator's feedback: the gradient of the discriminator's judgment tells it which direction makes the fake look more real.
Worked example: reading the losses
The discriminator uses a standard classification loss: −[log D(real) + log(1 − D(fake))]. In practice, the generator usually minimizes −log D(fake), a "non-saturating" version the original paper suggested because it gives stronger gradients early in training.
| Situation | D(real) | D(fake) | Discriminator loss | Generator loss |
|---|---|---|---|---|
| Discriminator winning | 0.9 | 0.2 | 0.329 | 1.609 |
| Closer contest | 0.6 | 0.5 | 1.204 | 0.693 |
| Theoretical equilibrium | 0.5 | 0.5 | 1.386 | 0.693 |
When the discriminator is confident (it rates real images 0.9 and fakes 0.2), its loss is low and the generator's loss is high, so the generator gets a strong signal to improve. At the theoretical equilibrium, the discriminator outputs 0.5 for everything: it's guessing. Unlike most models, a GAN's losses don't steadily fall to zero; they bounce as the two networks trade advantages, which is why practitioners judge progress by looking at samples and using quality metrics rather than loss curves alone.
Types of Generative Adversarial Networks
| GAN type | Key idea | What it enabled |
|---|---|---|
| Original GAN (2014) | Generator and discriminator as simple networks | Proof of concept on small images |
| DCGAN | Convolutional generator and discriminator with design rules for stable training | The first reliably good GAN images; became the standard starting point |
| Conditional GAN | Feed a label or other input to both networks | Generate a specific class, such as "a digit 7" or "a dog" |
| pix2pix | Conditional GAN that maps one image to another using paired examples | Sketch to photo, map to satellite, day to night |
| CycleGAN | Image-to-image translation without paired examples, using cycle consistency | Horses to zebras, photos to paintings |
| Wasserstein GAN (WGAN) | A different loss based on the Wasserstein distance | More stable training and more meaningful losses |
| StyleGAN family (NVIDIA) | Style-based generator that controls features at different scales | Photorealistic faces with controllable attributes |
| Super-resolution GANs (such as SRGAN, ESRGAN) | Adversarial loss for sharper upscaling | Detailed image upscaling and restoration |
NVIDIA's StyleGAN paper described "an alternative generator architecture" borrowed from style transfer that led to "an automatically learned, unsupervised separation of high-level attributes," such as pose and identity in faces, and "intuitive, scale-specific control of the synthesis." It was behind the realistic fake portraits that became famous online around 2019.
Generative Adversarial Networks Examples
| Area | Generative adversarial networks examples |
|---|---|
| Image synthesis | Realistic faces, products and scenes from random noise |
| Image editing | Changing age, expression or lighting in a photo by moving through latent space |
| Image-to-image translation | Sketches to photos, summer to winter scenes, maps to satellite imagery |
| Super-resolution and restoration | Upscaling low-resolution images, restoring old photos and video |
| Data augmentation | Creating extra training images, including synthetic medical images for research |
| Video and animation | Face reenactment and some deepfake techniques |
| Audio | Fast speech synthesis vocoders and sound effects |
| Science | Simulating particle physics events and other expensive simulations |
| Anomaly detection | Learning what normal data looks like and flagging what the generator can't reproduce |
The realism that makes GANs useful also enables deepfakes; our guide on how to spot a deepfake covers practical checks.
GAN vs Diffusion Model
Diffusion models learn to remove noise step by step, starting from pure noise and refining it into an image over many steps. They overtook GANs for text-to-image generation in the early 2020s.
| GAN | Diffusion model | |
|---|---|---|
| How it generates | One pass through the generator | Many denoising steps (tens to hundreds, fewer with distillation) |
| Generation speed | Very fast | Slower, though distilled models narrow the gap |
| Training stability | Tricky: adversarial balance, mode collapse | Stable: a simple denoising objective |
| Output diversity | Can collapse to a few modes | High diversity and coverage |
| Image quality | Excellent in narrow domains (faces, specific objects) | Excellent across very broad domains |
| Text conditioning | Harder to scale to open-ended prompts | Scales well with text encoders; powers most text-to-image tools |
| Typical use today | Real-time synthesis, super-resolution, specific domains, as a component in other systems | Text-to-image, video, image editing, audio |
The two also mix: some fast image generators distill a diffusion model into a few-step generator using an adversarial loss, combining diffusion's quality with GAN-like speed. Our explainer on AI image generators covers the tools built on these models.
Common GAN Problems and Fixes
Google's machine learning course on GANs lists the classic training problems:
| Problem | What happens | Common fixes |
|---|---|---|
| Vanishing gradients | If the discriminator becomes too good, the generator gets almost no useful gradient and stops learning | Wasserstein loss, the non-saturating generator loss |
| Mode collapse | The generator finds a few outputs that fool the discriminator and keeps producing them | Wasserstein loss, unrolled GANs, minibatch discrimination |
| Failure to converge | The two networks oscillate instead of reaching a balance | Regularization: adding noise to discriminator inputs, penalizing discriminator weights, gradient penalties |
| Evaluation difficulty | Losses don't show quality | Look at samples; use metrics such as Fréchet Inception Distance (FID) |
A Short History of GANs
Ian Goodfellow and colleagues at the University of Montreal published the GAN framework in 2014, and early results were blurry, low-resolution images. Progress came quickly: DCGAN in 2015 showed convolutional GANs could train reliably; conditional GANs, pix2pix and CycleGAN between 2014 and 2017 turned them into practical image-translation tools; the Wasserstein GAN in 2017 improved stability; and NVIDIA's progressive and style-based GANs in 2017 to 2019 reached photorealistic, high-resolution faces. From around 2021, diffusion models overtook GANs on broad image generation, but adversarial training lives on inside many modern systems, from super-resolution to fast distilled image generators.
How GANs Are Evaluated
Because GAN losses don't track quality, researchers use separate measures:
- Fréchet Inception Distance (FID): compares statistics of generated and real images, computed with a pretrained image network. Lower is better, and it rewards both quality and diversity.
- Inception Score (IS): an earlier metric that rewards images a classifier recognizes confidently and a variety of classes; it ignores how close images are to the real data.
- Precision and recall for generative models: measure realism and coverage separately, which helps diagnose mode collapse.
- Human evaluation: still the final check for perceived realism and usefulness.
GANs, Deepfakes and Responsible Use
The same techniques that create convincing faces can create convincing fakes of real people. GAN-generated profile photos have been used for fake social media accounts, and face-swapping tools have drawn on adversarial training. Detection tools look for statistical artifacts, but they lag behind generators. Responsible use means consent for any real person's likeness, labeling synthetic media, and following platform rules and laws on deepfakes, which are expanding in many countries.
How to Build a Simple GAN
A first GAN is a classic learning project:
- Pick a small dataset, such as handwritten digits or small clothing images.
- Build a small generator that maps a noise vector to an image, and a discriminator that classifies images as real or fake.
- Alternate training steps as described above, using the binary cross-entropy loss available in PyTorch or Keras.
- Save sample images every few hundred steps to watch progress.
- Try DCGAN design rules (strided convolutions, batch normalization, specific activations) if training is unstable.
PyTorch and TensorFlow both publish official DCGAN tutorials that walk through this exact setup.
FAQ
What is a GAN in simple terms?
It's an AI model made of two networks that compete: one creates fake data, the other tries to spot the fakes. Their competition teaches the creator to produce very realistic results.
Are GANs still used?
Yes, though less for text-to-image generation, where diffusion models lead. GANs remain common for fast image synthesis, super-resolution, restoration, data augmentation and as a component that speeds up other generators.
Is a GAN supervised or unsupervised learning?
Usually considered unsupervised or self-supervised, because it learns from unlabeled data. Conditional GANs add labels or paired data, which makes them partly supervised.
What is mode collapse in GANs?
It's when the generator produces only a few kinds of outputs because they reliably fool the discriminator, instead of covering the full variety of the training data.
What is the difference between a GAN and a diffusion model?
A GAN generates an image in one pass, trained through competition with a discriminator. A diffusion model generates by removing noise over many steps, trained with a simpler and more stable objective, and it handles broad text prompts better.