Random Noise (Latent Vector)
100-dim random vector
Generator → Generated Image
Fake sample
Real Training Image
Real sample
Generation
Training
Latent Vector Control
Training Progress
Epoch: 0
G Loss: -
D Loss: -
D(real): -
D(fake): -
Understanding GANs
Generative Adversarial Networks (GANs) are a revolutionary approach to generative modeling. Two neural networks compete: a generator creates fake data, while a discriminator tries to distinguish fake from real. This adversarial training produces remarkably realistic generated samples.
The Two-Player Game
- Generator (G):
- Input: Random noise vector z (latent space)
- Output: Fake image G(z)
- Goal: Fool the discriminator
- Learns mapping from noise to realistic images
- Discriminator (D):
- Input: Image (real or fake)
- Output: Probability that image is real
- Goal: Correctly classify real vs fake
- Acts as learned loss function for generator
Training Dynamics
GANs train through minimax game:
- Discriminator Training:
- Maximize D(real) - encourage high probability for real images
- Minimize D(fake) - encourage low probability for fake images
- Standard binary classification
- Generator Training:
- Maximize D(G(z)) - fool discriminator
- No access to real data
- Backprop through discriminator (frozen weights)
- Equilibrium:
- Ideally: D(x) = 0.5 for all x
- Generator produces perfect fakes
- Discriminator can't tell difference
Training Challenges
- Mode Collapse:
- Generator produces limited variety
- Ignores parts of data distribution
- Solutions: Minibatch discrimination, unrolled GANs
- Training Instability:
- Generator and discriminator must stay balanced
- If D too strong, G gets no useful gradient
- If G too strong, D fails to provide signal
- Solutions: Careful learning rates, architectural tricks
- Vanishing Gradients:
- When D is perfect, G gradients vanish
- Solution: Non-saturating loss, Wasserstein loss
- Evaluation:
- No single metric for quality
- Inception Score, FID popular
- Often requires human evaluation
GAN Architectures
- DCGAN (Deep Convolutional GAN):
- First stable architecture (2015)
- All-convolutional (no fully connected)
- Batch normalization
- ReLU (G), LeakyReLU (D)
- Foundation for many variants
- WGAN (Wasserstein GAN):
- Uses Wasserstein distance
- More stable training
- Meaningful loss metric
- Weight clipping or gradient penalty
- StyleGAN:
- NVIDIA's state-of-the-art
- Style-based generator
- Incredibly realistic faces
- Controllable generation
- Conditional GAN (cGAN):
- Condition on class labels
- Controlled generation
- Both G and D see condition
- CycleGAN:
- Unpaired image-to-image translation
- Horses ↔ Zebras, Photos ↔ Paintings
- Cycle consistency loss
- Pix2Pix:
- Paired image-to-image translation
- Sketches → Photos
- U-Net generator
Applications
- Image Generation: Create realistic faces, art, designs
- Data Augmentation: Generate training examples
- Image-to-Image Translation: Day→Night, Summer→Winter
- Super-Resolution: Enhance low-res images
- Text-to-Image: DALL-E, Stable Diffusion
- Video Generation: Create video frames
- 3D Object Generation: Create 3D models
- Drug Discovery: Generate molecular structures
- Music Generation: Compose audio
Training Tips
- Use DCGAN architecture as starting point
- Batch normalization except in D's output and G's input
- LeakyReLU (α=0.2) in discriminator
- ReLU in generator (except output: tanh)
- Adam optimizer (β1=0.5 for stability)
- Different learning rates for G and D (often D lower)
- Train D more steps per G step initially
- Monitor generated samples visually
- Use label smoothing (0.9 instead of 1.0)
- Add noise to discriminator inputs
Evaluation Metrics
- Inception Score (IS):
- Uses pre-trained Inception network
- Measures quality and diversity
- Higher is better
- Can be gamed
- Fréchet Inception Distance (FID):
- Compare distributions of real and generated
- Lower is better
- More reliable than IS
- Current standard
- Precision & Recall:
- Precision: Generated samples look real
- Recall: Covers all modes of real data
GAN Variants
- Progressive GAN: Grow resolution during training
- BigGAN: Large-scale, high-fidelity generation
- Self-Attention GAN (SAGAN): Attention for long-range dependencies
- Spectral Normalization GAN: Stabilize discriminator
Latent Space Exploration
- Interpolation: Smooth transitions between samples
- Arithmetic: Smiling woman - neutral woman + neutral man = smiling man
- Disentanglement: Control specific attributes independently
Beyond Images
- MusicVAE/GANSynth: Music generation
- MolGAN: Molecular generation
- TimeGAN: Time series generation
- StackGAN: Text-to-image
Experiment with the Playground
Use the interactive tool above to:
- Generate samples from random noise
- Watch generator and discriminator compete
- Explore latent space with sliders
- See training dynamics over time
- Understand adversarial training
GANs opened entirely new possibilities in AI - creating rather than just recognizing. They're behind many of the most impressive AI demos you've seen!