Quiz 2

Generative Models Overview: Taxonomy, Likelihood, and Fundamentals

2888 words
14 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Generative Models Overview: Taxonomy, Likelihood, and Fundamentals ## 🎯 Learning Objectives - Understand what generative models are and how they differ from discriminative models - Classify generative models into likelihood-based, implicit, and score-based categories - Derive and interpret maximum likelihood esti...

Generative Models Overview: Taxonomy, Likelihood, and Fundamentals

🎯 Learning Objectives

  • Understand what generative models are and how they differ from discriminative models
  • Classify generative models into likelihood-based, implicit, and score-based categories
  • Derive and interpret maximum likelihood estimation for generative modeling
  • Explain the i.i.d. assumption and its implications for learning data distributions
  • Compare the trade-offs between different generative modeling paradigms

📋 Prerequisites

  • Probability distributions (BSMA1004): Random variables, PDF/PMF, conditional probability
  • Basic calculus: Partial derivatives, chain rule
  • Neural network basics: Forward pass, backpropagation

1. 📖 Core Content

1.1 Intuition: What is a Generative Model?

Imagine you've never seen a cat before. I show you 10,000 photos of cats. After studying them, you can:
  1. Recognize whether a new photo contains a cat (discrimination)
  2. Draw a brand-new cat that looks realistic (generation) A generative model learns the second skill. It captures the underlying distribution of the training data so it can produce new samples that resemble the original data. Formally, if training data comes from an unknown true distribution pdata(x)p_{\text{data}}(x), the generative model learns a distribution pθ(x)p_\theta(x) that approximates pdata(x)p_{\text{data}}(x). Why does this matter? Generative models power:
  • Image generation: DALL·E, Midjourney, Stable Diffusion
  • Text generation: GPT-4, Claude, Llama
  • Drug discovery: Generating new molecular structures
  • Data augmentation: Creating synthetic training examples
  • Anomaly detection: Flagging inputs with low likelihood under the learned distribution

1.2 Discriminative vs Generative: The Fork in the Road

AspectDiscriminative ModelGenerative Model
LearnsDecision boundary $p(y\x)$
OutputClass label or probabilityNew data samples + probabilities
ExampleLogistic regression, SVM, ResNetGAN, VAE, GPT, Diffusion Model
Training signalPairs (x, y)Unlabeled x (or paired)
Can create data?NoYes
A discriminative model answers: "What's the probability this image is a cat?" A generative model answers: "Here's a new image that looks like a cat."

1.3 Taxonomy of Generative Models

(Diagram)

Likelihood-based Models

These explicitly model the probability density pθ(x)p_\theta(x) and train by maximizing the likelihood of training data.
  • Autoregressive models (PixelCNN, GPT): Decompose p(x)=tp(xtx<t)p(x) = \prod_t p(x_t | x_{<t}). Easy to train, slow to sample.
  • VAEs (Variational Autoencoders): Learn a latent variable model with an approximate posterior. Fast sampling, but blurry outputs.
  • Normalizing Flows: Invertible transformations that map a simple base distribution to complex data. Exact likelihood, but computationally expensive.

Implicit Models

These define a sampling process without explicitly specifying the full probability distribution.
  • GANs (Generative Adversarial Networks): Generator produces samples; discriminator judges realism. No explicit likelihood, sharp images, but training can be unstable.

Score-based / Diffusion Models

These learn the gradient of the log-density (the "score") rather than the density itself.
  • Diffusion Models: Gradually add noise to data, then learn to reverse the process. State-of-the-art quality, slower sampling.
  • Score Matching: Directly estimate xlogp(x)\nabla_x \log p(x).

1.4 Maximum Likelihood Estimation (MLE) for Generative Models

MLE is the workhorse training principle for likelihood-based generative models.

Intuition

We want to find parameters θ\theta such that the model assigns high probability to our observed data. MLE says: choose θ\theta that maximizes the probability of observing the training data under the model.

Formal Definition

Given i.i.d. dataset D={x(1),x(2),...,x(N)}\mathcal{D} = \{x^{(1)}, x^{(2)}, ..., x^{(N)}\}, the likelihood is:
L(θ)=i=1Npθ(x(i))L(\theta) = \prod_{i=1}^N p_\theta(x^{(i)})
Since products are numerically unstable (many tiny numbers multiplied), we work with the log-likelihood:
(θ)=logL(θ)=i=1Nlogpθ(x(i))\ell(\theta) = \log L(\theta) = \sum_{i=1}^N \log p_\theta(x^{(i)})
The MLE estimate is:
θ^MLE=argmaxθi=1Nlogpθ(x(i))\hat{\theta}_{\text{MLE}} = \arg\max_\theta \sum_{i=1}^N \log p_\theta(x^{(i)})

Why the Log?

  • Converts product to sum (easier to differentiate)
  • Prevents numerical underflow
  • Monotonic transformation: maximizing log-likelihood is equivalent to maximizing likelihood

Worked Example 1: MLE for a Gaussian

Suppose we want to model 1-D data {1.2,0.8,1.1,0.9,1.0}\{1.2, 0.8, 1.1, 0.9, 1.0\} as N(μ,σ2=0.25)\mathcal{N}(\mu, \sigma^2=0.25). Find the MLE for μ\mu. Step 1: Write the log-likelihood. For a single point x(i)x^{(i)}: pμ(x(i))=12πσ2exp((x(i)μ)22σ2)p_\mu(x^{(i)}) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x^{(i)}-\mu)^2}{2\sigma^2}\right)
(μ)=i=15[12log(2πσ2)(x(i)μ)22σ2]\ell(\mu) = \sum_{i=1}^5 \left[-\frac{1}{2}\log(2\pi\sigma^2) - \frac{(x^{(i)}-\mu)^2}{2\sigma^2}\right]
Step 2: Drop constant terms (they don't affect the argmax).
(μ)=12σ2i=15(x(i)μ)2+const\ell(\mu) = -\frac{1}{2\sigma^2} \sum_{i=1}^5 (x^{(i)}-\mu)^2 + \text{const}
Step 3: Differentiate and set to zero.
ddμ=1σ2i=15(x(i)μ)=0\frac{d\ell}{d\mu} = \frac{1}{\sigma^2} \sum_{i=1}^5 (x^{(i)}-\mu) = 0 i=15(x(i)μ)=0\sum_{i=1}^5 (x^{(i)}-\mu) = 0 (1.2+0.8+1.1+0.9+1.0)5μ=0(1.2+0.8+1.1+0.9+1.0) - 5\mu = 0 5.05μ=05.0 - 5\mu = 0 μ=1.0\mu = 1.0
Step 4: Interpret. The MLE for the mean is the sample mean: μ^=1.0\hat{\mu} = 1.0. This matches our intuition.

Worked Example 2: MLE for Bernoulli

Suppose we flip a coin 10 times and get: H, H, T, H, T, H, H, T, H, H (7 heads, 3 tails). Model as Bernoulli(θ)\text{Bernoulli}(\theta) where θ=P(heads)\theta = P(\text{heads}). Step 1: Write the likelihood.
L(θ)=i=110θyi(1θ)1yi=θ7(1θ)3L(\theta) = \prod_{i=1}^{10} \theta^{y_i} (1-\theta)^{1-y_i} = \theta^7 (1-\theta)^3
Step 2: Log-likelihood.
(θ)=7logθ+3log(1θ)\ell(\theta) = 7\log\theta + 3\log(1-\theta)
Step 3: Differentiate and set to zero.
ddθ=7θ31θ=0\frac{d\ell}{d\theta} = \frac{7}{\theta} - \frac{3}{1-\theta} = 0 7θ=31θ\frac{7}{\theta} = \frac{3}{1-\theta} 7(1θ)=3θ7(1-\theta) = 3\theta 77θ=3θ7 - 7\theta = 3\theta 7=10θ7 = 10\theta θ=0.7\theta = 0.7
The MLE is 7/10 = 0.7, which matches the empirical frequency.

Worked Example 3: MLE for a Simple Generative Model

Consider a dataset of 2D points that clearly cluster into two groups. A Gaussian Mixture Model with 2 components has parameters θ={π1,μ1,Σ1,π2,μ2,Σ2}\theta = \{\pi_1, \mu_1, \Sigma_1, \pi_2, \mu_2, \Sigma_2\}. The likelihood for a point x is:
pθ(x)=π1N(xμ1,Σ1)+π2N(xμ2,Σ2)p_\theta(x) = \pi_1 \mathcal{N}(x|\mu_1, \Sigma_1) + \pi_2 \mathcal{N}(x|\mu_2, \Sigma_2)
Unlike the previous examples, there's no closed-form solution for MLE because the latent variable (which component generated x) is unknown. This is why we need the EM algorithm or gradient-based optimization for complex generative models.

1.5 The i.i.d. Assumption

What It Says

Training examples {x(1),...,x(N)}\{x^{(1)}, ..., x^{(N)}\} are:
  1. Independent: The value of x(i)x^{(i)} doesn't influence x(j)x^{(j)} for iji \neq j
  2. Identically Distributed: Each x(i)x^{(i)} comes from the same probability distribution pdatap_{\text{data}}

Why We Need It

Without the i.i.d. assumption, the likelihood factorizes differently:
  • With i.i.d.: p(Dθ)=ipθ(x(i))p(\mathcal{D}|\theta) = \prod_i p_\theta(x^{(i)}) — simple product
  • Without i.i.d.: p(Dθ)=pθ(x(1))pθ(x(2)x(1))pθ(x(3)x(1),x(2))...p(\mathcal{D}|\theta) = p_\theta(x^{(1)}) \cdot p_\theta(x^{(2)}|x^{(1)}) \cdot p_\theta(x^{(3)}|x^{(1)}, x^{(2)}) \cdot ... — much more complex

When i.i.d. Fails

  • Time series: Stock prices today depend on yesterday's prices
  • Spatial data: Adjacent pixels in an image are highly correlated
  • Duplicate data: If the same data point appears multiple times, it's not independent

Edge Cases & Gotchas

  • Small datasets: With few samples, the empirical distribution is a poor approximation of the true distribution
  • Mode collapse: If the model memorizes training examples (especially common with small data), it fails to generalize
  • Distribution shift: If test data comes from a different distribution than training data, the generative model will produce poor samples

1.6 Why This Matters

Understanding the taxonomy of generative models and the MLE principle is foundational for the rest of this course. In subsequent weeks:
  • Week 2 (GANs): Implicit models that avoid explicit likelihood
  • Week 3 (VAEs): Likelihood-based models with tractable lower bounds
  • Week 4 (Diffusion): Score-based models that learn gradients of log-density
  • Week 7 (Conditional Generation): Controlling the output of any generative model The choice of which paradigm to use depends on your application: need fast sampling and don't care about exact likelihood? GANs. Need exact likelihood and have compute budget? Normalizing flows. Best quality? Diffusion models.

2. 📐 Key Formulas / Concepts

ConceptFormulaInterpretation
MLE objectiveθ^=argmaxθipθ(x(i))\hat{\theta} = \arg\max_\theta \prod_i p_\theta(x^{(i)})Maximizes probability of observed data
Log-likelihood(θ)=ilogpθ(x(i))\ell(\theta) = \sum_i \log p_\theta(x^{(i)})Numerically stable MLE equivalent
Likelihood-basedpθ(x)p_\theta(x) explicitly modeledModels like VAE, autoregressive
ImplicitSampling process zG(z)xz \to G(z) \to xModels like GAN, no explicit density
Score-basedsθ(x)=xlogp(x)s_\theta(x) = \nabla_x \log p(x)Models like diffusion
i.i.d. assumptionp(D)=ip(x(i))p(\mathcal{D}) = \prod_i p(x^{(i)})Enables tractable likelihood computation

3. ⚠️ Common Pitfalls

Pitfall 1: Confusing Generative and Discriminative

Mistake: Treating a generative model as a classifier (using it only for prediction, not generation). Why: Students often come from a supervised learning background and default to thinking in terms of prediction. How to catch: If you're not generating new data samples, you might be using the wrong type of model. Correct approach: A generative model's primary purpose is to model p(x)p(x) so you can sample from it. Classification is a secondary capability (e.g., using Bayes rule for p(yx)p(xy)p(y)p(y|x) \propto p(x|y)p(y)).

Pitfall 2: Believing Higher Likelihood Always Means Better Samples

Mistake: Thinking the model with the highest test likelihood will produce the best-looking samples. Why: Likelihood measures density estimation quality, not perceptual quality. Some models (like diffusion) can have slightly lower likelihood than VAEs on certain datasets but produce far better-looking images. How to catch: Always evaluate generative models on both likelihood AND sample quality (FID, human evaluation). Correct approach: Use likelihood for model selection within the same family, but don't compare likelihood across different model families (VAE vs GAN). Each has different inductive biases.

Pitfall 3: Ignoring the i.i.d. Assumption When Processing Sequential Data

Mistake: Shuffling a time series and treating it as i.i.d. data. Why: Standard ML pipelines shuffle data by default, which destroys temporal dependencies. How to catch: If adjacent samples in the dataset have temporal, spatial, or structural relationships, they are not independent. Correct approach: Use models that respect data structure (RNNs, Transformers with positional encoding, or structured prediction) rather than treating every point as independent.

Pitfall 4: Maximizing Likelihood Without Regularization

Mistake: Fitting a very flexible generative model (like a mixture with many components) via MLE without any regularization. Why: MLE on finite data can overfit — the model assigns all probability mass to observed data points and zero everywhere else. How to catch: If your model assigns near-zero likelihood to any slightly novel input, it's overfit. Correct approach: Use Bayesian priors (MAP estimation), add regularization (KL term in VAE), or use architectural inductive biases (locality in CNNs).

4. 📝 Practice Questions

Q1: Is logistic regression a generative or discriminative model? Why?
Answer: Logistic regression is a discriminative model. It directly models p(yx)p(y|x) without modeling the distribution of x. It learns a decision boundary between classes, not the underlying data distribution. You cannot use logistic regression to generate new data points. Q2: Suppose you have a dataset of 5 numbers: [2, 3, 5, 7, 11]. Model it as N(μ,σ2=4)\mathcal{N}(\mu, \sigma^2=4). Find the MLE for μ\mu.
Step 1: Log-likelihood (dropping constant terms):
(μ)=12σ2i=15(x(i)μ)2=18i=15(x(i)μ)2\ell(\mu) = -\frac{1}{2\sigma^2} \sum_{i=1}^5 (x^{(i)}-\mu)^2 = -\frac{1}{8} \sum_{i=1}^5 (x^{(i)}-\mu)^2
Step 2: Differentiate:
ddμ=14i=15(x(i)μ)=0\frac{d\ell}{d\mu} = \frac{1}{4} \sum_{i=1}^5 (x^{(i)}-\mu) = 0
Step 3: Solve:
(2+3+5+7+11)5μ=0(2+3+5+7+11) - 5\mu = 0
285μ=028 - 5\mu = 0
μ^=285=5.6\hat{\mu} = \frac{28}{5} = 5.6
The MLE is the sample mean: 5.6. Q3: Compare VAEs and GANs in terms of (a) likelihood computation, (b) sample quality, (c) training stability.
CriterionVAEGAN
LikelihoodLower bound (ELBO) computedNot available (implicit model)
Sample qualityOften blurry, less sharpCan be photorealistic
Training stabilityStable (optimize ELBO)Unstable (min-max game, mode collapse)
VAEs provide a tractable lower bound on likelihood but tend to produce blurry images due to the Gaussian decoder assumption and the KL regularizer. GANs can produce sharper images through adversarial training but don't provide likelihood estimates and are notoriously difficult to train due to the delicate balance between generator and discriminator. Q4: Why can't we directly compute the likelihood for a GAN?
GANs are implicit generative models. They define a sampling path: sample noise zp(z)z \sim p(z), then transform it through the generator Gθ(z)G_\theta(z) to produce a sample xx. However, the generator defines a complex, high-dimensional transformation that makes it infeasible to compute pθ(x)p_\theta(x) explicitly. The probability density would require integrating over all zz that map to xx, which is intractable for neural network generators. Q5: A dataset has 1000 images, but 100 of them are exact duplicates. How does this violate the i.i.d. assumption?
Independence violation: Duplicates are not independent of each other — knowing one exists tells you the other exists. This artificially inflates the weight of those specific data points in the likelihood, causing the model to over-emphasize the duplicated content. The model effectively sees 900 unique images + 100 copies of the same image, making it believe that image is more important than it should be. Q6: Derive the MLE for the variance of a Gaussian (assuming known mean μ=0\mu=0) given data {x1,...,xN}\{x_1, ..., x_N\}.
Step 1: Log-likelihood.
(σ2)=i=1N[12log(2πσ2)xi22σ2]\ell(\sigma^2) = \sum_{i=1}^N \left[-\frac{1}{2}\log(2\pi\sigma^2) - \frac{x_i^2}{2\sigma^2}\right]
=N2log(2π)N2log(σ2)12σ2i=1Nxi2= -\frac{N}{2}\log(2\pi) - \frac{N}{2}\log(\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^N x_i^2
Step 2: Differentiate w.r.t. σ2\sigma^2:
dd(σ2)=N2σ2+12σ4i=1Nxi2=0\frac{d\ell}{d(\sigma^2)} = -\frac{N}{2\sigma^2} + \frac{1}{2\sigma^4}\sum_{i=1}^N x_i^2 = 0
Step 3: Solve:
N2σ2+xi22σ4=0-\frac{N}{2\sigma^2} + \frac{\sum x_i^2}{2\sigma^4} = 0
Multiply by 2σ42\sigma^4: Nσ2+xi2=0-N\sigma^2 + \sum x_i^2 = 0
σ^2=1Ni=1Nxi2\hat{\sigma}^2 = \frac{1}{N}\sum_{i=1}^N x_i^2
Note: This is a biased estimator (doesn't use Bessel's correction N1N-1). For small N, consider the unbiased version. Q7: Explain the "likelihood-free" training objective for GANs at a high level.
Instead of maximizing pθ(x)p_\theta(x), GANs play a two-player game:
  1. Discriminator DD: Learns to distinguish real vs fake. Maximizes Expdata[logD(x)]+Ezpz[log(1D(G(z)))]\mathbb{E}_{x \sim p_{\text{data}}}[\log D(x)] + \mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))].
  2. Generator GG: Learns to fool the discriminator. Minimizes Ezpz[log(1D(G(z)))]\mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))] (or equivalently maximizes Ez[logD(G(z))]\mathbb{E}_{z}[\log D(G(z))]).
At equilibrium, GG has learned pdatap_{\text{data}} without ever computing a likelihood. This is the key innovation: training a generative model through an adversarial discrimination game. Q8: What is the "curse of dimensionality" in the context of generative models?
In high-dimensional spaces (e.g., 256×256×3 images = 196,608 dimensions), the volume of space grows exponentially. This means:
  1. Data sparsity: Fixed dataset covers an infinitesimally tiny fraction of the space
  2. Density estimation failure: MLE pushes mass to observed points, leaving sparse regions with near-zero density
  3. Sample quality degradation: The model may generate implausible samples in regions it never observed
Generative models combat this through:
  • Manifold assumption: Real data lies on a low-dimensional manifold within the high-dimensional space
  • Inductive biases: Convolutions (locality), recurrence (sequentiality), attention (long-range dependencies)
  • Latent variable models: Learning compact representations (VAEs, GANs) Q9: For an autoregressive model modeling text as p(x)=t=1Tp(xtx<t)p(x) = \prod_{t=1}^T p(x_t | x_{<t}), is the i.i.d. assumption satisfied?
The data points within a single sequence (individual tokens xtx_t) are NOT independent — each token depends on all previous tokens. That's the point of the autoregressive decomposition.
However, different sequences in the dataset are assumed i.i.d. For example, each document in a text corpus is treated as independent and identically distributed.
The autoregressive factorization respects the within-sequence dependence while leveraging the between-sequence independence assumption for the likelihood computation. Q10: Give an example where a generative model is preferred over a discriminative model even though both can solve the task.
Anomaly Detection in Manufacturing: You have images of normal products (only one class). A generative model learns p(x)p(x) for normal products. During testing, any product with low p(x)p(x) is flagged as anomalous. A discriminative model would require examples of both normal AND anomalous products for training, which is often impossible because anomalies are rare and unpredictable.
Another example: Data Augmentation. When you have limited training data for a classification task, you can train a generative model (e.g., diffusion model) to create synthetic training examples, then use a discriminative model for the actual classification. The generative model enriches the training set. Q11: If a model's log-likelihood on test data is -1500 nats and another model's is -1400 nats, which is better and by how much?
The model with -1400 nats is better because it's a higher log-likelihood (less negative).
The difference of 100 nats means the better model assigns e1002.7×1043e^{100} \approx 2.7 \times 10^{43} times higher probability to the test data. However, this comparison is only valid if both models are in the same family (both likelihood-based). You cannot directly compare a VAE's ELBO (-1400) with a normalizing flow's exact log-likelihood (-1500) because the VAE provides a lower bound, not the true likelihood. Q12: Design a simple experiment to test whether your generative model has memorized training data vs. learned to generalize.
Hold-out method: Split your dataset into training and held-out sets. Train on the training set. Now:
  1. Compute likelihood: If test-set likelihood is close to training-set likelihood, the model generalizes well
  2. Nearest neighbor test: For each generated sample, find its nearest neighbor in the training set. If most generated samples are nearly identical to training examples (e.g., pixel-wise MSE < threshold), the model is memorizing
  3. Interpolation test: Generate samples that should interpolate between training examples. If they look like unrealistic blends, the model has memorized specific points rather than learned the manifold
Ideally, a good generative model produces novel samples that resemble training data without being copies.

5. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.