GANs — Generator vs Discriminator
Two networks in adversarial competition. Mode collapse, training instability, Wasserstein distance — the honest account of what makes GANs hard to train.
A forger tries to create fake currency that fools the bank. The bank trains detectors to catch fakes. The forger studies the detector's failures and improves. Both get better in lockstep. That is a GAN.
Ian Goodfellow invented GANs in 2014 — the idea came to him at a bar in Montreal after a friend suggested using a neural network to generate images. The insight: instead of hand-crafting a loss function that measures image quality (which is impossible to define), learn the loss function itself using a second neural network. Let the Discriminator define what "real" looks like, and let the Generator learn to fool it.
The Generator takes random noise as input and produces an image. It never sees real images directly. The Discriminator takes an image — either real or generated — and outputs a probability that it is real. The two networks are trained simultaneously with opposing objectives: the Generator wants to maximise the Discriminator's error, the Discriminator wants to minimise it. At equilibrium — the Nash equilibrium — the Generator produces images indistinguishable from real ones.
A counterfeiter (Generator) and a detective (Discriminator) playing an arms race. The counterfeiter starts producing terrible fakes — the detective catches them all. The counterfeiter studies which fake features gave them away and improves. The detective trains on the new fakes and gets better. After 100,000 rounds, the counterfeiter produces fakes so good even experts cannot tell them apart. Neither was told what perfect currency looks like — they learned from each other.
The catch: if the detective gets too good too fast, the counterfeiter receives no useful signal — all attempts score equally bad. If the counterfeiter gets too good too fast, the detective gives up and labels everything as fake. Balance is everything — and maintaining balance is why GAN training is notoriously hard.
Minimax game — what each network optimises and why
The GAN objective is a minimax game. The Discriminator D maximises its ability to distinguish real from fake. The Generator G minimises D's ability — equivalently, maximises the probability that D mistakes its outputs for real.
DCGAN — Deep Convolutional GAN — the architecture that made GANs work for images
The original GAN used fully connected layers and only worked on tiny 28×28 images. DCGAN (Radford et al., 2015) replaced them with convolutional layers and introduced a set of architectural guidelines that made GAN training dramatically more stable. These guidelines are still followed in modern GANs.
Mode collapse, vanishing gradients, and the Wasserstein fix
Two failure modes plague vanilla GAN training. Mode collapse: the Generator finds a single image (or a small set) that always fools the Discriminator and stops exploring. You get 1,000 generated images that all look nearly identical. Vanishing gradients: when the Discriminator becomes too good, it outputs probabilities near 0 for all fakes — the gradient of log(1 − D(G(z))) saturates and the Generator receives no signal.
Wasserstein GAN (WGAN, 2017) addresses both by replacing the Jensen-Shannon divergence objective with the Wasserstein distance — a metric that provides meaningful gradients even when the generated and real distributions do not overlap. WGAN removes the Sigmoid from the Discriminator (now called Critic), clips weights to enforce a Lipschitz constraint, and trains the Critic more steps than the Generator.
Conditional GAN — generate specific classes on demand
Vanilla GANs generate random samples from the full data distribution. Conditional GANs (cGAN) condition generation on a label — generate a kurta specifically, not a random fashion item. Both Generator and Discriminator receive the class label as additional input. The Generator learns to produce images for each class. The Discriminator learns to judge whether an image matches its label — not just whether it looks real.
Every common GAN failure — explained and fixed
Where GANs still win in production — and where diffusion has taken over
GANs lost the "best image quality" crown to diffusion models around 2021–2022, and most new text-to-image products are built on diffusion. That does not mean GANs disappeared from production — single-forward-pass generation is a genuine advantage that diffusion's iterative denoising cannot match, and entire categories of shipped products still run on GAN architectures for exactly that reason.
The GAN-vs-diffusion split is already blurring. Consistency models and adversarial diffusion distillation (the technique behind SDXL Turbo) train a fast, few-step or single-step generator to match a slow diffusion model's output — borrowing GAN-style single-pass speed while keeping diffusion-level training stability and quality. If you are asked in an interview whether GANs are "dead," the accurate answer is that their core insight — a fast single-pass generator — got absorbed into the diffusion toolchain rather than discarded.
Five things people get wrong about GANs
Mode collapse is a diversity failure, not a convergence failure — the Generator's weights are still updating, and it can even be fooling the Discriminator quite successfully. What has happened is that it found a narrow region of the output space that reliably scores well and stopped exploring the rest of the data distribution. The loss curves can look completely healthy while every generated sample looks nearly identical, which is exactly why loss values alone cannot diagnose GAN health — you have to directly inspect a batch of generated samples for diversity.
Much of GAN's reputation for instability traces back to the original 2014 formulation with naive hyperparameters, not to anything unavoidable about adversarial training itself. DCGAN's architectural rules, WGAN-GP's Wasserstein objective, spectral normalisation, and label smoothing collectively make training dramatically more reliable — this module exists specifically because the field spent years engineering these stabilisation techniques and they work. Instability was a largely solvable problem, not a permanent property of the minimax game.
As the production landscape above shows, GANs remain the practical choice anywhere single-forward-pass speed matters — real-time filters, upscaling, narrow-domain face generation. It is also worth knowing that modern fast diffusion variants (consistency models, adversarial diffusion distillation) borrow GAN-style objectives to get GAN-like speed out of a diffusion-trained model — the two approaches have converged rather than one cleanly replacing the other.
It is close to the opposite. A Discriminator that classifies everything correctly gives the Generator essentially zero useful gradient — the vanishing-gradient failure mode covered earlier in this module. Healthy GAN training deliberately keeps the Discriminator somewhat "confused," typically D(x) around 0.6–0.8 rather than 1.0. This runs against the instinct that a more accurate classifier is always a better sign, which is exactly why it trips people up the first time they read GAN training logs.
The objective function is adversarial — one network's gain is written as the other's loss — but in practice healthy training behaves more like a cooperative arms race than a zero-sum fight. Each network needs the other to keep improving in order to keep receiving useful learning signal itself: if either one "wins" outright and stops providing a meaningful gradient to the other, both stop improving. Balance, not victory, is the actual target — the forger-and-detective analogy earlier in this module is making exactly this point.
GANs — 5 questions interviewers actually ask
The Discriminator tries to maximise its ability to tell real data from the Generator's fakes; the Generator tries to minimise that same quantity — it wants the Discriminator's job to be as hard as possible. Training alternates between a step that moves the Discriminator toward its maximum and a step that moves the Generator toward its minimum of the same expression, which is exactly the "min over G, max over D" formulation the name describes. At the theoretical equilibrium, the Generator's distribution matches the real data distribution closely enough that the Discriminator cannot do better than random guessing.
Two networks are each other's moving target — unlike standard supervised learning where the loss surface is fixed, here the Generator's loss landscape shifts every time the Discriminator updates and vice versa, so there is no static optimum either one is climbing toward. On top of that, if the Discriminator gets too strong, the Generator's gradient vanishes entirely. Two concrete fixes: switch to a Wasserstein objective with gradient penalty (WGAN-GP), which provides meaningful gradients even when the real and fake distributions barely overlap, and follow the DCGAN architectural guidelines (strided convolutions, batch norm placement, matched learning rates and momentum) that were specifically found to reduce oscillation.
The original GAN objective is based on Jensen-Shannon divergence, which saturates and provides essentially no usable gradient when the real and generated distributions do not meaningfully overlap — a common situation early in training, exactly when the Generator most needs signal. The Wasserstein (Earth Mover's) distance stays smooth and informative even when two distributions are disjoint, because it measures the cost of literally moving probability mass from one distribution to the other rather than a divergence that collapses to a constant. WGAN implements this by removing the Discriminator's Sigmoid (making it a Critic outputting an unbounded score) and enforcing a Lipschitz constraint, originally via weight clipping and later, more stably, via a gradient penalty on interpolated points.
Mode collapse is when the Generator converges to producing a narrow set of outputs — sometimes nearly identical images — regardless of the input noise, because it found a small region that reliably fools the current Discriminator. In practice, loss curves alone will not reveal this, since both networks can look like they are training normally. What actually catches it: sample a large batch from the Generator and check output diversity directly, or compute FID against a diverse validation set — a Generator producing low-variance, repetitive samples will show a poor FID despite individual samples looking sharp, because FID specifically penalises a collapsed distribution shape, not just per-image quality.
Yes, and the reasoning is entirely about latency. A GAN generates an image in a single forward pass — on the order of 10ms on a mobile GPU — while a standard diffusion model needs tens to hundreds of denoising steps to produce comparable quality, which is far too slow for a live video frame rate. If diffusion-level quality is genuinely required, distilled or consistency-model variants of diffusion can get closer to single-step speed, but for a hard real-time constraint like a live filter, a GAN remains the reasonable default rather than a legacy choice.
You understand adversarial training. Next: a smoother path to generation via structured latent spaces.
GANs generate sharp images but training is unstable and mode collapse is a constant risk. Variational Autoencoders take a different path — instead of adversarial competition, they use a principled probabilistic framework that guarantees a smooth, structured latent space. Module 62 builds a VAE from scratch, derives the ELBO loss, and shows the reparameterisation trick that makes it trainable.
The reparameterisation trick, KL divergence loss, and why VAEs enable controllable generation through structured latent spaces.
🎯 Key Takeaways
- ✓A GAN pits two networks against each other: the Generator maps random noise to fake data, the Discriminator classifies real vs fake. The Generator is trained to fool the Discriminator; the Discriminator is trained to catch fakes. At Nash equilibrium the Generator produces data indistinguishable from real.
- ✓The training loop alternates: train Discriminator on real (label 1) and fake (label 0), then train Generator to make Discriminator output 1 on fakes. Use detach() when training D to prevent gradients flowing back through G. Use betas=(0.5, 0.999) for Adam — lower momentum reduces oscillation.
- ✓DCGAN architectural rules that stabilise training: strided convolutions instead of pooling, BatchNorm everywhere except first D layer and last G layer, LeakyReLU(0.2) in D, ReLU in G, Tanh output. Weight initialisation: normal distribution with mean=0, std=0.02.
- ✓Two main failure modes: mode collapse (Generator outputs same image repeatedly — fix with minibatch discrimination, WGAN-GP, or larger latent dim) and vanishing gradients (Discriminator wins too easily — fix with non-saturating G loss, label smoothing, or WGAN-GP).
- ✓WGAN-GP replaces the Discriminator with a Critic (no Sigmoid), uses Wasserstein distance instead of BCE loss, and enforces the Lipschitz constraint with gradient penalty (LAMBDA_GP=10) instead of weight clipping. Train critic 5 steps per generator step. Use betas=(0.0, 0.9) instead of (0.5, 0.999).
- ✓Conditional GANs add class labels as input to both G and D — the Generator learns to produce images of a specific class, the Discriminator judges both realism and label consistency. Implement via nn.Embedding: embed integer class label to a dense vector and concatenate with noise (G) or image (D).
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.