← Back to Tutorials

6. Generative Adversarial Networks

Generative Networks

What is a GAN?

Introduced by Ian Goodfellow in 2014, GANs consist of two neural networks competing in a minimax game:

The generator tries to fool the discriminator, while the discriminator tries to avoid being fooled. Both improve through this adversarial process.

GAN architecture showing generator and discriminator interaction

Figure: The GAN training loop — generator creates fakes, discriminator evaluates them.

Training Objective

The minimax objective: min_G max_D V(D,G) = E[log D(x)] + E[log(1 - D(G(z)))]

# Simplified GAN training loop
for epoch in range(epochs):
    # Train discriminator
    real = sample_real_data(batch_size)
    fake = generator(noise(batch_size))
    d_loss = -mean(log(D(real)) + log(1 - D(fake)))
    d_optimizer.step(d_loss)

    # Train generator
    fake = generator(noise(batch_size))
    g_loss = -mean(log(D(fake)))  # -log(D) trick
    g_optimizer.step(g_loss)

Important GAN Variants

Challenges in Training GANs

Practice Task: Implement a simple GAN in PyTorch or TensorFlow to generate MNIST digits. Use a DCGAN architecture. Train for 100 epochs and generate sample images every 10 epochs. Observe how the quality improves (or fails to improve). Experiment with different latent dimensions (z). Write a paragraph explaining why mode collapse might occur.