15/2075
Generative Adversarial Networks (GAN) · Page 1 of 2

Generator vs Discriminator

32 min Advanced

Generative Adversarial Networks (GAN)

The Idea: A Game Between Two Networks

Generator: "I'll create fake images" Discriminator: "I'll catch your fakes"

Real images: [dog, dog, dog, dog, dog]
      ↓ (mix)
Fake images: [fake, fake, fake, fake]
      ↓
Discriminator: "Is this real or fake?"

If Discriminator says "real" on fake: Discriminator loses
If Discriminator says "fake" on real: Discriminator loses
If Generator makes fake that fools Discriminator: Generator wins

Both networks improve iteratively!

The Two Networks

Generator

Random noise (100D): z ~ Normal(0, 1)
        ↓
Dense layer (512 neurons)
        ↓
Dense layer (1024 neurons)
        ↓
Dense layer (784 neurons)
        ↓
Output: Fake image (28×28)

Training: Fool the discriminator!
Goal: Minimize log(1 - D(G(z)))

Key insight: Starts with random noise, learns to create realistic images!

Discriminator

Input: Image (real or fake)
        ↓
Conv layer (32 filters)
        ↓
Conv layer (64 filters)
        ↓
Dense layer (128 neurons)
        ↓
Output: Real or Fake? (sigmoid)

Training: Classify correctly!
Goal: Minimize -log(D(real)) - log(1 - D(G(z)))

The Training Loop

for epoch in range(100):
    for batch in dataset:
        # Train Discriminator
        real_output = Discriminator(real_images)      # Should be ~1
        fake_output = Discriminator(Generator(noise))  # Should be ~0
        
        D_loss = -log(real_output) - log(1 - fake_output)
        Discriminator.update(D_loss)
        
        # Train Generator
        fake_images = Generator(noise)
        fake_output = Discriminator(fake_images)
        
        G_loss = -log(fake_output)  # Fool discriminator
        Generator.update(G_loss)

Nash Equilibrium (Perfect Balance)

When both networks are perfectly balanced:

Generator: Creates perfect fakes (indistinguishable from real)
Discriminator: Guesses 50-50 (can't tell)

Result: P(real) = P(fake) = 0.5

This is the goal! Both networks reach equilibrium.

Applications

ApplicationUse
Image GenerationCreate new faces, art, objects
Image-to-ImageStyle transfer, photo enhancement
Text GenerationGenerate realistic text
Data AugmentationGenerate more training data
Super-resolutionUpscale low-res images
Anomaly DetectionDetect unusual images

Challenges

  • Mode collapse: Generator only creates same image
  • Instability: Training oscillates, doesn't converge
  • Slow convergence: Takes many iterations to balance

Solutions (Modern GANs)

TechniqueFixes
WGANWasserstein loss (more stable)
Spectral NormNormalize discriminator
Progressive GrowingStart small, grow gradually
StyleGANSeparate style and content
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…