Advanced · Generative AI
Generative Adversarial Network (GAN)
Visual diagram · Math · Worked example · 3 difficulty levels.
TL;DR. GANs are a pair of competing neural networks — one generates fake data, the other tries to detect fakes — driving each other to improve until the fakes are indistinguishable from real data.
Intuitive Explanation
A GAN is like a counterfeiter and a detective in an arms race. The counterfeiter (generator) creates fake paintings, and the detective (discriminator) tries to spot them. Over time, the counterfeiter gets so good that even the detective can't tell the fakes from the originals.
Technical Definition
A Generative Adversarial Network consists of two networks trained simultaneously in a minimax game: a generator G that maps random noise z to data space, and a discriminator D that classifies inputs as real or generated. Training objective: min_G max_D E[log D(x)] + E[log(1 - D(G(z)))].
How it works
GANs were one of the most exciting developments in generative AI before diffusion models took over. **Training Process:** 1. Generator creates fake samples from random noise 2. Discriminator evaluates both real and fake samples 3. Both networks update: Generator learns to fool the discriminator, discriminator learns to be a better judge 4. At equilibrium (ideally), G produces indistinguishable samples and D outputs 0.5 for everything **Key Variants:** - **DCGAN:** Uses convolutional layers for image generation - **StyleGAN:** Produces photorealistic faces with style control - **CycleGAN:** Unpaired image-to-image translation (horse → zebra) - **Pix2Pix:** Paired image-to-image translation (sketch → photo) - **WGAN:** Uses Wasserstein distance for more stable training **Challenges:** - **Mode Collapse:** Generator produces limited variety of outputs - **Training Instability:** Delicate balance between G and D - **Vanishing Gradients:** If D becomes too good, G gets no useful gradient signal **GANs vs Diffusion Models:** Diffusion models (DALL-E, Stable Diffusion) have largely replaced GANs for image generation because they're more stable to train and produce higher diversity. However, GANs remain faster at inference (single forward pass vs iterative denoising).
Mathematical Notation
min_G max_D E[log D(x)] + E[log(1 − D(G(z)))]The discriminator D maximizes its ability to classify real data x as real and generated data G(z) as fake. The generator G minimizes the discriminator's success — a minimax game.
Visual Explanation (flowchart)
Random Noise z → Generator → Fake Data → Discriminator ← Real Data → Real or Fake?
Real-World Use Cases
- NVIDIA: StyleGAN generates photorealistic human faces and enables style editing
- Adobe: GAN-based tools for image inpainting, super-resolution, and style transfer
- DeepFake Detection: Both creating and detecting synthetic media relies on GAN technology
Related Concepts
- Neural Network — A computing system inspired by biological neural networks that learns patterns from data through interconnected layers of nodes.
- Deep Learning — A subset of machine learning using neural networks with many layers to learn hierarchical representations from large datasets.
- Diffusion Model — A generative model that learns to create data by reversing a gradual noising process, producing high-quality images and audio.