Jaber Notes · 16 of 16
Generative Models
VAE (ELBO), GAN (WGAN, mode collapse), normalizing flows, diffusion.
How models generate: the VAE ELBO and reparameterization trick, the GAN minimax objective and its instabilities (WGAN, gradient penalty), normalizing flows, and diffusion models through to Stable Diffusion.
Models that learn to generate new data — not just classify existing data. Full derivations of the ELBO, GAN minimax, and the diffusion forward/reverse process.
Table of contents
- Taxonomy of generative models
- Variational Autoencoders (VAE)
- Generative Adversarial Networks (GAN)
- Normalizing flows (overview)
- Diffusion models
- Evaluation of generative models
- Comparison and when to use what
1. Taxonomy of generative models
A generative model learns the data distribution and can sample from it.
| Model | Learns | Sampling | Likelihood |
|---|---|---|---|
| Autoregressive (GPT, PixelCNN) | Sequential, slow | Exact | |
| VAE | Latent variable | One decoder pass, fast | Lower bound (ELBO) |
| GAN | Implicit (generator + discriminator) | One generator pass, fast | Not tractable |
| Normalizing flow | Bijective transformation | One pass + Jacobian | Exact |
| Diffusion | Iterative denoising | Many denoising steps | Lower bound (VLB) |
2. Variational Autoencoders (VAE)
2.1 Latent variable model
The VAE (Kingma & Welling, 2013) defines a joint model:
with prior .
Marginal likelihood (what we want to maximize):
This integral is intractable for deep decoder networks.
2.2 Variational inference and ELBO derivation
Introduce an encoder (recognition model) to approximate the intractable posterior .
KL decomposition:
Since :
ELBO = Evidence Lower BOund. Maximizing ELBO ≡ maximizing marginal likelihood while minimizing KL to prior.
2.3 Decomposition of the ELBO
- Reconstruction: how well the decoder reconstructs from the latent code .
- KL regularization: how close the encoder distribution is to the prior. Prevents the encoder from using a wildly non-standard latent space. Encourages a smooth, regular latent space useful for generation.
2.4 Gaussian encoder and closed-form KL
Encoder: .
Prior: .
Closed-form KL (for Gaussian):
Only the reconstruction term requires sampling; KL is computed analytically.
2.5 Reparameterization trick
The reconstruction term requires taking gradients through a sampling operation , which is non-differentiable.
Trick: reparameterize as:
Now is a deterministic function of , and independent noise . Gradients w.r.t. flow through and .
2.6 VAE architecture and training
import torch
import torch.nn as nn
import torch.nn.functional as F
class VAE(nn.Module):
def __init__(self, input_dim=784, latent_dim=20):
super().__init__()
self.enc_fc1 = nn.Linear(input_dim, 400)
self.enc_mu = nn.Linear(400, latent_dim)
self.enc_logvar = nn.Linear(400, latent_dim)
self.dec_fc1 = nn.Linear(latent_dim, 400)
self.dec_out = nn.Linear(400, input_dim)
def encode(self, x):
h = F.relu(self.enc_fc1(x))
return self.enc_mu(h), self.enc_logvar(h)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def decode(self, z):
h = F.relu(self.dec_fc1(z))
return torch.sigmoid(self.dec_out(h))
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar)
return self.decode(z), mu, logvar
def vae_loss(recon_x, x, mu, logvar, beta=1.0):
recon = F.binary_cross_entropy(recon_x, x, reduction='sum')
kl = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return recon + beta * kl2.7 -VAE and disentanglement
Higher forces more independent, disentangled latent dimensions. Each latent dimension captures one factor of variation.
2.8 VAE for generation
Sample , then decode . The smooth latent space enables interpolation between data points.
3. Generative Adversarial Networks (GAN)
3.1 The idea (Goodfellow et al., 2014)
Frame generation as a two-player minimax game:
- Generator : maps noise to fake samples .
- Discriminator : outputs probability that input is real vs. fake.
Each tries to beat the other:
- tries to distinguish real from fake.
- tries to fool .
3.2 Minimax objective (derivation)
Optimal discriminator (for fixed ): minimize over :
Substitute back into :
This equals , where is the Jensen-Shannon divergence.
Global minimum: when (generator perfectly matches data).
3.3 Training algorithm
for each training iteration:
1. Sample real batch: x ~ p_data
2. Sample noise: z ~ p(z), generate fakes: x_fake = G(z)
3. Update D: maximize log D(x) + log(1 - D(G(z)))
(or: minimize BCE on real=1, fake=0)
4. Update G: minimize log(1 - D(G(z)))
(in practice: maximize log D(G(z)) — avoids vanishing gradients early)Non-saturating GAN loss for generator:
3.4 Training instabilities
Mode collapse: maps all inputs to a few modes (ignores noise). Discriminator learns to reject those modes → shifts to other modes. Never covers full distribution.
Training instability (oscillation): and take turns outcompeting each other without converging.
Gradient vanishing in : if is too good, → no gradient for .
3.5 Wasserstein GAN (WGAN)
Replace JS divergence with Earth Mover's (Wasserstein-1) distance:
Critic (no longer a probabilistic discriminator) approximates this distance with Lipschitz constraint.
WGAN objective:
Benefits:
- Meaningful loss (correlates with sample quality, unlike JS divergence).
- More stable training; mode collapse less severe.
Enforcing Lipschitz: weight clipping (original WGAN) or gradient penalty (WGAN-GP): penalize at interpolated samples.
3.6 Conditional GAN (cGAN)
Condition both and on class label :
Enables class-conditional generation, image-to-image translation (pix2pix), text-to-image (early methods).
3.7 StyleGAN (brief)
Progressive growing: train at low resolution first, gradually increase. Stable training of high-resolution generators.
Style-based generator: separate style (appearance) from content. Inject style via AdaIN (Adaptive Instance Normalization) at each resolution level.
4. Normalizing flows (overview)
4.1 Idea
Learn a bijective, differentiable function between data space and a simple latent space ().
Exact likelihood via change-of-variables:
The log-determinant of the Jacobian accounts for volume change.
Challenge: the Jacobian determinant is in general. Flows design architectures where the Jacobian is triangular or block-diagonal → determinant.
4.2 Coupling layers (RealNVP, Glow)
Split into . Coupling layer:
where are arbitrary networks. Jacobian is triangular → . Invertible: .
Glow (Kingma & Dhariwal, 2018) applies flows to image generation with learnable permutations and achieved high-quality faces.
5. Diffusion models
5.1 Overview
Diffusion models (DDPM, Ho et al., 2020; score matching, Song & Ermon, 2019) are currently the dominant generative model for images, audio, and video.
Idea: learn to reverse a gradual noising process. Add noise to data step by step until it becomes pure Gaussian noise; train a neural network to reverse each noising step.
5.2 Forward process (adding noise)
Define a Markov chain that gradually adds Gaussian noise over steps:
where is the noise schedule (small at first, increases).
Define , .
Key property — forward in one step:
So:
As and : (pure noise).
5.3 Reverse process (denoising)
The reverse process is also Gaussian (for small ):
A neural network predicts the noise that was added at step . Then:
5.4 Training objective (simplified)
The variational lower bound (VLB) simplifies to:
Training algorithm:
for each training step:
1. Sample x_0 ~ q(x_0) (real data)
2. Sample t ~ Uniform({1,...,T})
3. Sample ε ~ N(0, I)
4. x_t = sqrt(ᾱ_t) * x_0 + sqrt(1-ᾱ_t) * ε
5. Loss = ||ε - ε_θ(x_t, t)||²
6. Gradient step on θRemarkably simple: the model just learns to predict the noise given a noisy image and a time step.
5.5 Sampling (reverse diffusion)
Start from and iteratively denoise:
for t = T, T-1, ..., 1:
z ~ N(0,I) if t > 1, else z = 0
x_{t-1} = (1/sqrt(α_t)) * (x_t - β_t/sqrt(1-ᾱ_t) * ε_θ(x_t, t)) + σ_t * zThis requires (typically 1000) forward passes through → slow generation.
5.6 DDIM (Denoising Diffusion Implicit Models)
Song et al. (2020): derive a non-Markovian sampling process with the same marginals as DDPM but with fewer steps (10–100 instead of 1000):
With : deterministic sampling (DDIM). Enables 10–50 step generation.
5.7 Latent diffusion models (LDM / Stable Diffusion)
Problem: running diffusion in pixel space is expensive (high-dimensional ).
Rombach et al. (2022): encode images into a low-dimensional latent space with a pretrained VAE, run diffusion there:
- Encode: ( spatial compression).
- Add noise to in forward process.
- Train diffusion model on latent space.
- Decode: .
Much cheaper (smaller latent space) while maintaining image quality.
5.8 Classifier-free guidance (CFG)
Condition the diffusion model on a text prompt (or class) . Train with and without conditioning (randomly drop with probability 0.1):
Guidance scale amplifies the conditional signal. Higher → more prompt-aligned but less diverse.
5.9 Score matching connection
The noise-prediction model is related to the score function (gradient of log density):
Score-based generative models (Song & Ermon) and DDPM are equivalent formulations.
6. Evaluation of generative models
6.1 Fréchet Inception Distance (FID)
Compute Inception-v3 features of real () and generated () samples:
Lower FID = more realistic and diverse samples. Standard metric for image generation.
6.2 Inception Score (IS)
High IS = samples are clearly classifiable AND diverse. Doesn't compare to real data distribution (FID is preferred).
6.3 Precision and Recall
Precision: fraction of generated samples that are realistic (near real data manifold). Recall: fraction of real data manifold covered by generated samples.
High precision + low recall: mode coverage poor (generated images look real but limited variety). High recall + low precision: diverse but unrealistic samples.
7. Comparison and when to use what
| Model | Quality | Diversity | Speed (sampling) | Likelihood | Training |
|---|---|---|---|---|---|
| Autoregressive | High | High | Slow (sequential) | Exact | Stable |
| VAE | Medium | High | Very fast | Lower bound | Stable |
| GAN | Very high | Varies (mode collapse) | Very fast | None | Unstable |
| Normalizing flow | High | High | Fast (1 pass) | Exact | Moderate |
| Diffusion | SOTA | High | Slow (many steps) | Lower bound | Very stable |
Current state (2026):
- Images: Diffusion (Stable Diffusion, DALL-E 3, Imagen).
- Text: Autoregressive (GPT-4, LLaMA, Gemini).
- Audio: Diffusion + autoregressive (AudioLM, MusicGen).
- Video: Diffusion (Sora, Stable Video Diffusion).
- Fast generation needed: GAN still competitive. Flow Matching (continuous normalizing flows) emerging as faster alternative to diffusion.
*File: notes/16_generative_models.md — all DL notes complete.*