Lesson 23 • Advanced
Generative Models: Autoencoders, VAEs & GANs
Build models that create entirely new data — from compressing images with autoencoders to generating photorealistic faces with GANs.
✅ What You'll Learn
- • Autoencoders: encode, compress, and reconstruct data
- • GANs: adversarial training between generator and discriminator
- • VAEs: structured latent spaces and smooth interpolation
- • When to use each generative approach
🎨 Creating New Data
🎯 Real-World Analogy: An autoencoder is like summarising a novel in 3 sentences (encoding) then trying to rewrite the full novel from just those sentences (decoding). A GAN is like a forger (generator) trying to fool an art critic (discriminator) — both improve through competition. A VAE is like organising a library where similar books are shelved nearby — you can walk between genres smoothly.
Generative models learn the underlying data distribution and can sample new examples from it. Unlike discriminative models (which classify), generative models answer: "What does this type of data look like?"
Try It: Autoencoder
Compress 3×3 patterns into 3 values and reconstruct them
import numpy as np
# Autoencoder: Compress → Reconstruct
# Learn a compressed representation of data
np.random.seed(42)
class SimpleAutoencoder:
def __init__(self, input_dim, latent_dim):
self.W_enc = np.random.randn(latent_dim, input_dim) * 0.3
self.W_dec = np.random.randn(input_dim, latent_dim) * 0.3
self.input_dim = input_dim
self.latent_dim = latent_dim
def encode(self, x):
return np.tanh(self.W_enc @ x)
def decode(self, z):
...Try It: GAN Training
Watch the generator learn to fool the discriminator in real-time
import numpy as np
# GAN: Generator vs Discriminator — A Minimax Game
# Generator creates fakes, Discriminator detects them
np.random.seed(42)
class Generator:
"""Creates fake data from random noise"""
def __init__(self):
self.W = np.random.randn(1) * 0.5
self.b = np.random.randn(1) * 0.5
def generate(self, noise):
return self.W * noise + self.b
class Discriminator:
"""Classifies data as real or fake"""
def __init__(self):
self.W = np.
...⚠️ Common Mistake: GAN mode collapse — the generator produces the same output regardless of input noise. Fix with spectral normalization, progressive growing, or switch to a Wasserstein GAN (WGAN) which has a more stable training signal.
Try It: Variational Autoencoder
Explore structured latent spaces and smooth interpolation between data points
import numpy as np
# VAE: Variational Autoencoder — Generative + Structured Latent Space
# Unlike GANs, VAEs learn a smooth, continuous latent space
np.random.seed(42)
def reparameterize(mu, log_var):
"""The reparameterization trick: z = mu + sigma * epsilon"""
std = np.exp(0.5 * log_var)
eps = np.random.randn(*mu.shape)
return mu + std * eps
def kl_divergence(mu, log_var):
"""KL divergence from learned distribution to N(0,1)"""
return -0.5 * np.sum(1 + log_var - mu**
...💡 Pro Tip: For practical image generation today, skip GANs and VAEs — use Diffusion Models (next lesson). They produce higher quality results with more stable training. GANs remain useful for real-time generation and super-resolution.
📋 Quick Reference
| Model | Strength | Weakness | Use Case |
|---|---|---|---|
| Autoencoder | Simple, fast | Blurry outputs | Compression, denoising |
| VAE | Smooth latent space | Blurry outputs | Interpolation, exploration |
| GAN | Sharp, realistic | Unstable training | Image synthesis, SR |
| Diffusion | Best quality | Slow generation | DALL-E, Stable Diffusion |
🎉 Lesson Complete!
You now understand the three foundational generative architectures. Next, explore Diffusion Models — the technology behind DALL-E and Stable Diffusion!
Sign up for free to track which lessons you've completed and get learning reminders.