Lesson 8.9 · 9 of 9 · 6 min read · Reviewed 24 Nov 2026
GANs and VAEs: the generative families before diffusion
By the end, you can
- Explain how a GAN and a VAE each learn to generate images.
- Spot mode collapse and blurriness and name the family each belongs to.
- Compare GANs, VAEs and diffusion on quality, variety and speed.
Before diffusion (8.4, 8.7) took over image generation, two families did most of the work: generative adversarial networks (GANs) and variational autoencoders (VAEs). You still meet both: GANs in fast super-resolution and face tools, VAEs inside every latent diffusion model. Knowing how they fail tells you why diffusion won.
GANs: a forger and a detective
A GAN trains two networks against each other:
generator: random noise z -> fake image
discriminator: image -> "real" or "fake"?
Round after round:
the discriminator learns to tell real from fake
the generator learns to fool the discriminator
Neither network is told what a good image looks like. The generator only gets a signal through the discriminator’s judgement. When the two stay balanced, the generator ends up making sharp, realistic images in a single pass, which is very fast.
Mode collapse is the classic failure. The generator finds a few outputs that fool the discriminator and keeps making them. Train on ten kinds of shoe and you may get crisp white trainers, over and over. Training can also be unstable: if the discriminator gets too good too quickly, the generator receives no useful signal. Many later tricks (feature matching, minibatch discrimination, better losses) exist to fight these two problems.
VAEs: squeeze, then rebuild
A VAE has three parts:
- An encoder turns an image into a small latent code, described as a range of likely values rather than one point.
- The latent space is pushed to be smooth and centred, so nearby codes mean similar images.
- A decoder turns a code back into an image.
Training rewards two things at once: rebuild the input well, and keep the latent space tidy. To generate, you sample a fresh code and decode it. Because the space is smooth, you can also walk between two codes and watch one face turn gradually into another.
The weakness is blurriness. If two sharp images are equally likely for the same code, a decoder trained to minimise average error outputs their average, which is soft. Fine textures such as hair and grass smear out.
Side by side
| GAN | VAE | Diffusion (8.7) | |
|---|---|---|---|
| How it learns | Generator vs discriminator | Encode, then decode, with a tidy latent space | Predict and remove noise |
| Sample quality | Sharp | Often blurry | Sharp |
| Variety | Risk of mode collapse | Good coverage | Good coverage |
| Training | Unstable, needs care | Stable | Stable |
| Speed per image | One pass | One pass | Many steps |
| Still used for | Fast upscaling, image-to-image | Compressing images for latent diffusion | Most text-to-image and video |
Diffusion won because it combines stable training with sharp, varied output. Its cost is speed, which is why so much recent work goes into fewer steps.
How to spot which failure you have
- Crisp but repetitive: suspect mode collapse. Count how many distinct kinds appear in a few hundred samples.
- Varied but soft: suspect VAE-style averaging. Compare fine detail with real images.
- Noisy or broken: in diffusion, often too few steps.
Common mistakes
- Judging a generator on a handful of samples. You need many to see variety, or the lack of it.
- Calling the VAE in latent diffusion “the model”. It only compresses and decodes; the denoiser does the generating.
Key points
- A GAN trains a generator to fool a discriminator. Samples are sharp and fast, but training is unstable and can collapse onto a few outputs.
- A VAE encodes an image to a small latent code and decodes it back. Its latent space is smooth, but averaging over likely outputs makes images blurry.
- Diffusion trades speed for stability and variety: many steps per image, but steady training and good coverage (8.7).
- VAEs live on inside diffusion: latent diffusion uses an autoencoder to compress images before denoising.
Measure mode collapse and blur in toy generators
Score three simulated generators on variety (modes covered) and sharpness, and show why a decoder trained on average error blurs.
You will have: A script that prints modes covered and sharpness for a healthy GAN, a collapsed GAN and a VAE, plus the averaged decoder output.
Steps
- Run the starter: every generator scores 0, because coverage() and sharpness() are stubs.
- Fill in coverage(): count the modes that at least 5% of samples land near.
- Fill in sharpness(): the share of samples close to any real mode.
- Replace best with the per-pixel mean of the two target images.
- Match each result to a row of the lesson's comparison table.
Done when
- The collapsed GAN covers only 2 of 4 modes while staying fully sharp.
- The VAE covers all 4 modes but only about a third of its samples are sharp.
- The decoder output is [0.5, 0.5, 0.5, 0.5]: the average of two sharp images, which is blur.
Open in the Playground No API key needed.
Starter code (Python)
import random
# Toy 1D "images": each kind of shoe is a point on a line. Simulated generators, seeded.
MODES = [0.0, 2.0, 4.0, 6.0]
def gan_ok(rng): return rng.choice(MODES) + rng.gauss(0, 0.1)
def gan_collapsed(rng): return (MODES[1] if rng.random() < 0.9 else MODES[2]) + rng.gauss(0, 0.1)
def vae(rng): return rng.choice(MODES) + rng.gauss(0, 0.8)
def coverage(samples, near=0.3, min_share=0.05):
# TODO: count the modes that at least min_share of samples land near
return 0
def sharpness(samples, near=0.3):
# TODO: share of samples within `near` of any mode
return 0.0
for name, gen in [("GAN", gan_ok), ("GAN, collapsed", gan_collapsed), ("VAE", vae)]:
rng = random.Random(7)
xs = [gen(rng) for _ in range(1000)]
print(f"{name:<15} modes covered {coverage(xs)} of 4, sharp {sharpness(xs):.0%}")
# Why VAEs blur: two equally likely sharp images for the same code.
targets = [[0, 1, 0, 1], [1, 0, 1, 0]]
# TODO: the output with the lowest average squared error is the per-pixel mean
best = targets[0]
print("decoder output that minimises squared error:", best)
Show a solution
import random
# Toy 1D "images": each kind of shoe is a point on a line. Simulated generators, seeded.
MODES = [0.0, 2.0, 4.0, 6.0]
def gan_ok(rng): return rng.choice(MODES) + rng.gauss(0, 0.1)
def gan_collapsed(rng): return (MODES[1] if rng.random() < 0.9 else MODES[2]) + rng.gauss(0, 0.1)
def vae(rng): return rng.choice(MODES) + rng.gauss(0, 0.8)
def coverage(samples, near=0.3, min_share=0.05):
hits = [sum(abs(s - m) < near for s in samples) for m in MODES]
return sum(h >= min_share * len(samples) for h in hits)
def sharpness(samples, near=0.3):
return sum(min(abs(s - m) for m in MODES) < near for s in samples) / len(samples)
for name, gen in [("GAN", gan_ok), ("GAN, collapsed", gan_collapsed), ("VAE", vae)]:
rng = random.Random(7)
xs = [gen(rng) for _ in range(1000)]
print(f"{name:<15} modes covered {coverage(xs)} of 4, sharp {sharpness(xs):.0%}")
# Why VAEs blur: two equally likely sharp images for the same code.
targets = [[0, 1, 0, 1], [1, 0, 1, 0]]
best = [sum(t[i] for t in targets) / len(targets) for i in range(4)]
print("decoder output that minimises squared error:", best)
Finished the chapter? Take the chapter check.