GANs: Generator and Discriminator
Min-max game theory, GAN training instability, mode collapse, DCGAN implementation.
GANs: Generator and Discriminator is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a GAN?
A Generative Adversarial Network pits two networks against each other: a Generator that creates fake data and a Discriminator that judges real vs fake. They compete, and the generator learns to produce convincing samples.
The Adversarial Game
Think of a forger (generator) and a detective (discriminator). The forger improves fakes to fool the detective; the detective sharpens its eye. This arms race drives both to get better, until fakes look real.
The Generator
The generator maps a random noise vector to a fake image. It learns to turn meaningless random numbers into structured, realistic-looking data.
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(100, 256), nn.ReLU(),
nn.Linear(256, 784), nn.Tanh()
)
def forward(self, z):
return self.net(z)The Noise Vector
The input z is sampled from a standard normal. Different noise vectors produce different images, so the noise is the "seed" of generation. Its dimension (here 100) is the latent size.
z = torch.randn(batch_size, 100)
fake_images = generator(z)The Discriminator
The discriminator takes an image and outputs a single probability: how likely it is real. A Sigmoid gives a value in [0,1], where 1 means real and 0 means fake.
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(784, 256), nn.LeakyReLU(0.2),
nn.Linear(256, 1), nn.Sigmoid()
)Training the Discriminator
Train D on a batch of real images labeled 1 and fake images labeled 0. It learns to push real scores up and fake scores down using binary cross-entropy.
d_real = criterion(D(real), ones)
d_fake = criterion(D(fake.detach()), zeros)
d_loss = d_real + d_fake
d_loss.backward()Why detach() the Fakes?
When training D we use fake.detach() so gradients do not flow into the generator. We are only updating the discriminator in this step; the generator gets its turn separately.
Training the Generator
Now train G to fool D: feed fakes through D and reward the generator when D outputs real (label 1). The generator improves by making D believe its fakes.
g_loss = criterion(D(fake), ones) # want D to say "real"
g_loss.backward()
g_optimizer.step()Alternating Training
Each iteration alternates: first update D, then update G. Keeping them roughly balanced is key, if D gets too strong, G receives no useful gradient; if too weak, G has no challenge.
# per step
# 1) update Discriminator
# 2) update GeneratorMode Collapse
A classic failure is mode collapse: the generator finds one or a few outputs that fool D and produces only those, ignoring the data's variety. Symptoms: every generated sample looks nearly identical.
Training Instability
GANs are notoriously unstable: losses oscillate, one network can overpower the other, and convergence is fragile. Tricks like LeakyReLU, careful learning rates, and label smoothing help stabilize training.
Quick Check
Test your GAN understanding.
Recap: GANs
You learned the adversarial setup: a generator turns a noise vector into fakes, a discriminator outputs a real/fake probability, and they train by alternating D then G. You also learned to spot mode collapse and the general instability that makes GANs tricky to train.
Frequently asked questions
Is the “GANs: Generator and Discriminator” lesson free?
Yes — the full text of “GANs: Generator and Discriminator” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “GANs: Generator and Discriminator”?
Min-max game theory, GAN training instability, mode collapse, DCGAN implementation. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “GANs: Generator and Discriminator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Autoencoders for Representation Learning
- Variational Autoencoders (VAE)
- GANs: Generator and Discriminator
- Conditional GANs and Style Transfer