Topic 59 of 62 · Wider ML toolkit
Diffusion vs GAN
MediumHow do diffusion models and GANs learn to generate samples, and what are their core trade-offs?
1 · Start with the intuition
A GAN frames generation as a two-player minimax game: a generator maps random noise to an image in a single forward pass, while a discriminator tries to tell real images from generated ones. A diffusion model removes the adversarial game entirely: it defines a fixed forward process that gradually corrupts training images with Gaussian noise, and trains a single denoiser network with a simple supervised regression loss to predict the added noise at any corruption level t. At generation time, starting from pure Gaussian noise, a sampler calls that denoiser repeatedly to step back toward a clean sample.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Corrupt in closed form
Sample a timestep t and noise ε ~ N(0, I), and form xₜ = √ᾱₜ x₀ + √(1 − ᾱₜ) ε in one step.
Next, we will look at: Train by noise regression. Move on when this operation makes sense.
Visualizing the behavior
Two different training signals
DDPM linear schedule (β: 1e-4 → 0.02, T = 1000)
Derived (approx.)- signal √ᾱₜ
- noise √(1 − ᾱₜ)
3 · The governing equation
Forward closed form: xₜ = √ᾱₜ x₀ + √(1 − ᾱₜ) ε, ε ~ N(0, I)
Diffusion training: L_simple(θ) = E_{t, x₀, ε} [ ‖ε − εθ(xₜ, t)‖² ]
GAN minimax game: min_G max_D E_x[log D(x)] + E_z[log(1 − D(G(z)))]Notation: ᾱₜ = Π_{s≤t} (1 − βₛ) is the cumulative signal fraction remaining at step t; εθ is the denoiser network; G and D are the GAN generator and discriminator.
Notice the asymmetry between training and inference in diffusion: because the sum of independent Gaussians has a closed form, training does NOT run a 1,000-step loop per image—it picks one random t per example and runs a single forward/backward pass. That stable supervised objective avoids the mode collapse and discriminator-balancing failures of GANs and covers diverse conditional distributions well. The price arrives at inference: whereas a GAN runs G(z) once, standard diffusion runs εθ across N sequential steps (often doubled by classifier-free guidance), motivating latent diffusion (compressing pixels via a VAE), DiT, flow matching, and step distillation.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Thinking diffusion training has to step through all T = 1,000 noise levels sequentially for every training image, or claiming diffusion can never run in 1–4 steps (ignoring consistency models, rectified flows, and adversarial/progressive distillation).
In practice · implementation & engineering rule of thumb
When serving diffusion or flow models with classifier-free guidance (CFG), batch the conditional and unconditional inputs along the batch dimension (2B) so both run in a single fused denoiser forward pass.
# Batch conditional and unconditional passes together for Classifier-Free Guidance: latent_in = torch.cat([x_t, x_t], dim=0) cond_in = torch.cat([uncond_emb, text_emb], dim=0) eps_uncond, eps_cond = denoiser(latent_in, t, cond_in).chunk(2, dim=0) eps_guided = eps_uncond + guidance_scale * (eps_cond - eps_uncond)
Go one level deeper
Follow-ups that often come after a correct first answer.
- Classifier-free guidance
- Train one denoiser both with and without the condition (by randomly dropping it). At sampling time, use ε̂ = ε(x, ∅) + w·(ε(x, c) − ε(x, ∅)). A larger w follows the prompt more closely at the cost of diversity.
- Latent diffusion
- Run diffusion in the latent space of a pretrained autoencoder (for example 8× spatial downsampling) rather than on pixels. Each denoiser call becomes far cheaper, which is the core idea behind Stable Diffusion.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy doesn’t a diffusion training step need to unroll all t corruption steps from x₀ to xₜ?
Show follow-up reasoning
Because each forward step adds independent Gaussian noise, and the sum of independent Gaussians is another Gaussian whose mean and variance have an exact closed form: q(xₜ | x₀) = N(√ᾱₜ x₀, (1 − ᾱₜ)I). You can jump directly from x₀ to any timestep t in a single line of arithmetic.