๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #295

GAN

The Generative Adversarial Network (GAN) takes a radically different approach to generative modeling than autoencoders: two networks โ€” a Generator and a Discriminator โ€” trained simultaneously against each other in a competitive game, each pushing the other to improve.

The Two Players

NetworkJob
Generator (\(G\))Takes random noise \(\mathbf{z}\) and tries to produce a fake sample realistic enough to fool the Discriminator
Discriminator (\(D\))Takes a sample (real or generated) and tries to correctly classify it as real or fake

The Minimax Formula

\[ \min_G \max_D \ \mathbb{E}_{x\sim p_{\text{data}}}[\log D(x)] + \mathbb{E}_{z\sim p_z}[\log(1-D(G(z)))] \]

\(D\) wants to maximize this โ€” correctly assigning high probability to real data (\(D(x)\) near 1) and low probability to generated fakes (\(D(G(z))\) near 0). \(G\) wants to minimize the same expression โ€” producing fakes convincing enough that \(D(G(z))\) is close to 1, fooling the discriminator into thinking they're real.

The Adversarial Training Loop

  1. Sample a batch of real data and a batch of generated fakes (from current \(G\)).
  2. Update \(D\) to better distinguish real from fake (a standard binary classification training step โ€” see Binary Cross-Entropy).
  3. Update \(G\) to produce fakes that fool the now-updated \(D\) more effectively.
  4. Repeat โ€” each network continuously adapts to the other's latest improvements.

Diagram โ€” The Adversarial Loop

Generator noise z fake sample Discriminator real or fake? also sees real data samples

The generator tries to fool the discriminator; the discriminator tries not to be fooled โ€” both continuously improving against a moving target.

Code

import torch
import torch.nn as nn

generator = nn.Sequential(nn.Linear(100, 256), nn.ReLU(), nn.Linear(256, 784), nn.Tanh())
discriminator = nn.Sequential(nn.Linear(784, 256), nn.LeakyReLU(0.2), nn.Linear(256, 1), nn.Sigmoid())

loss_fn = nn.BCELoss()
g_optimizer = torch.optim.Adam(generator.parameters(), lr=0.0002)
d_optimizer = torch.optim.Adam(discriminator.parameters(), lr=0.0002)

real_images = torch.rand(32, 784)
z = torch.randn(32, 100)
fake_images = generator(z)

# Discriminator step
d_optimizer.zero_grad()
real_loss = loss_fn(discriminator(real_images), torch.ones(32, 1))
fake_loss = loss_fn(discriminator(fake_images.detach()), torch.zeros(32, 1))
(real_loss + fake_loss).backward()
d_optimizer.step()

# Generator step -- wants the discriminator to output 1 (real) for its fakes
g_optimizer.zero_grad()
g_loss = loss_fn(discriminator(fake_images), torch.ones(32, 1))
g_loss.backward()
g_optimizer.step()

Why GAN Training Is Notoriously Unstable

Unlike ordinary supervised training toward a fixed target, both networks are chasing a constantly moving target (each other) โ€” this can lead to real practical problems: mode collapse (the generator finds a small set of outputs that reliably fool the discriminator and stops producing genuine variety), or oscillating, non-converging training dynamics where neither network settles into a stable equilibrium.

Common Mistakes

  • Training the discriminator far more than the generator (or vice versa) without balance โ€” a discriminator that becomes too strong too quickly provides the generator with an unhelpfully weak, uninformative gradient signal (since \(D\) confidently rejects everything the generator produces).
  • Forgetting to .detach() the generated fakes when computing the discriminator's loss โ€” without it, the discriminator's backward pass would also compute (unwanted, wasted) gradients through the generator.

Interview Relevance

Q: "What is 'mode collapse' in GAN training, and why does it happen?" Mode collapse is when the generator learns to produce only a narrow range of outputs (sometimes nearly identical ones) that reliably fool the current discriminator, rather than capturing the full diversity of the real data distribution. It happens because the generator's objective is purely to fool the discriminator โ€” if a small set of convincing fakes achieves that goal, there's no direct pressure in the adversarial objective forcing genuine output diversity.

Practice Question

In the GAN minimax formula, what does it mean, in terms of \(D(G(z))\), for the generator to be "successfully fooling" the discriminator?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

GAN โ€“ FAQs

Quick answers about learning GAN in Deep Learning.

This free note from CodingNow 2.0 explains GAN in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including GAN, is 100% free with no signup required.
With focused practice, most students grasp GAN in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now