Unit 11, lesson 77

Generative Models and Their Evaluation

What a generative model is, how autoregressive factorization gives an exact likelihood, and what latent-variable models, variational autoencoders and their evidence lower bound, adversarial networks and diffusion models optimize.

  • Video 77
  • Generative Models

A model that produces pairs of numbers, sentences or images can make every single sample look right and still be a poor model of the data. It may never produce whole regions of the distribution, it may copy training examples, and its fluent output may be false. This lesson defines a generative model, derives the autoregressive likelihood exactly, and explains at an introductory level what variational autoencoders, generative adversarial networks and diffusion models optimize. Their training objectives measure different things and are not interchangeable likelihood scores. A constructed distribution on two binary coordinates serves as the running example.

Key terms

Generative model

Definition A generative model specifies a probability distribution over observations, a probability mass function or density pθ(x)p_{\boldsymbol{\theta}}(\mathbf{x}) with parameters θ\boldsymbol{\theta}, or pθ(x∣c)p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{c}) when it is conditioned on a prompt, a class or another input c\mathbf{c}. It is fitted to training observations so that it approximates the unknown data distribution pdatap_{\text{data}}. Some generative models can evaluate pθ(x)p_{\boldsymbol{\theta}}(\mathbf{x}) for a given x\mathbf{x}, some can only draw samples, and some can do both. A discriminative classifier, by contrast, models only the label given the input, P(Y=y∣x)P(Y = y \mid \mathbf{x}), and says nothing about how the inputs themselves are distributed.

In plain words A generative model describes how observations vary, so it can produce new ones. Whether a sample is plausible is one question. Whether it is true, useful, original or permitted for a particular use are further questions, and the model’s probability calculations answer none of them.

Example The constructed target distribution of this lesson has two binary coordinates xx and yy with

p(0,0)=p(1,1)=0.4,p(0,1)=p(1,0)=0.1.\begin{gathered} p(0, 0) = p(1, 1) = 0.4, \\[0.4em] p(0, 1) = p(1, 0) = 0.1 . \end{gathered}

The four probabilities add to 11. Each coordinate on its own is balanced, P(x=1)=P(y=1)=0.5P(x = 1) = P(y = 1) = 0.5, but the two coordinates agree with probability 0.80.8. A sampler for this table produces frequencies that fluctuate around these probabilities. In 10001000 independent draws the frequency of (0,0)(0, 0) has standard deviation 0.4⋅0.6/1000≈0.0155\sqrt{0.4 \cdot 0.6 / 1000} \approx 0.0155, so a simulation checks the sampler but never replaces the declared table.

Autoregressive factorization

Definition Order the coordinates of an observation as x1,…,xTx_1, \dots, x_T and write x<t=(x1,…,xt−1)x_{<t} = (x_1, \dots, x_{t-1}). Every outcome with positive probability satisfies

p(x1,…,xT)=∏t=1Tp(xt∣x<t),\begin{aligned} & p(x_1, \dots, x_T) \\[0.4em] &\quad = \prod_{t=1}^{T} p(x_t \mid x_{<t}), \end{aligned}

where the factor for t=1t = 1 is p(x1)p(x_1). The derivation is the multiplication rule P(A∩B)=P(A)P(B∣A)P(A \cap B) = P(A)P(B \mid A) applied repeatedly. It gives p(x1,…,xT)=p(x1,…,xT−1) p(xT∣x<T)p(x_1, \dots, x_T) = p(x_1, \dots, x_{T-1})\, p(x_T \mid x_{<T}), and induction on TT gives the product. The identity holds for every distribution and every order of the coordinates. An autoregressive model parameterizes each conditional pθ(xt∣x<t)p_{\boldsymbol{\theta}}(x_t \mid x_{<t}) directly, for example with a decoder-only transformer. Because each conditional adds to 11 over xtx_t, the product is then automatically a valid joint distribution. For independent observations x(1),…,x(n)\mathbf{x}^{(1)}, \dots, \mathbf{x}^{(n)} the likelihood is the product of their probabilities, so the log likelihood is the sum of ln⁡pθ(xt(i)∣x<t(i))\ln p_{\boldsymbol{\theta}}(x^{(i)}_t \mid x^{(i)}_{<t}) over all observations ii and positions tt. Maximizing it is the same as minimizing the negative log likelihood. A reported average must say whether it is per observation, per token or per coordinate, because these denominators give different numbers. For continuous coordinates the factors are densities, and a density value is not the probability of one exact point.

In plain words A joint distribution can always be generated one coordinate at a time, each choice conditioned on the ones already made. That part is exact. The modeling work is in representing each conditional, and the chosen order changes how hard each conditional is to learn, even though a perfect model could use any order.

Example For the constructed table, draw xx with P(x=1)=0.5P(x = 1) = 0.5 and then let yy equal xx with probability 0.80.8. This gives p(0,0)=0.5⋅0.8=0.4p(0, 0) = 0.5 \cdot 0.8 = 0.4 and p(0,1)=0.5⋅0.2=0.1p(0, 1) = 0.5 \cdot 0.2 = 0.1, and the row x=1x = 1 follows the same way. Three independent constructed observations (0,0)(0, 0), (1,1)(1, 1) and (1,0)(1, 0) have probabilities 0.40.4, 0.40.4 and 0.10.1, so

L=0.4⋅0.4⋅0.1=0.016,−13ln⁡L≈1.3784\begin{gathered} \mathcal{L} = 0.4 \cdot 0.4 \cdot 0.1 = 0.016, \\[0.4em] -\tfrac13 \ln \mathcal{L} \approx 1.3784 \end{gathered}

nats per two-coordinate observation, or about 0.68920.6892 nats per coordinate. The rare observation contributes −ln⁡0.1≈2.3026-\ln 0.1 \approx 2.3026, more than twice the −ln⁡0.4≈0.9163-\ln 0.4 \approx 0.9163 of each common one. A model that treats the coordinates as independent balanced bits gives every pair 0.250.25. Averaged over the target table, the expected negative log probability is about 1.19351.1935 for the correct model and ln⁡4≈1.3863\ln 4 \approx 1.3863 for the independent one. The independent model matches both single-coordinate distributions exactly and still misses the dependence between them.

Latent-variable model and variational autoencoder

Definition A latent-variable model introduces an unobserved variable z\mathbf{z} with a prior distribution p(z)p(\mathbf{z}) and a conditional distribution pθ(x∣z)p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}) of the observation. Its distribution of observations is the marginal

pθ(x)=∑zp(z) pθ(x∣z),p_{\boldsymbol{\theta}}(\mathbf{x}) = \sum_{\mathbf{z}} p(\mathbf{z})\, p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}),

with an integral in place of the sum when z\mathbf{z} is continuous. Sampling draws z\mathbf{z} from the prior and then x\mathbf{x} from pθ(x∣z)p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}). A Gaussian mixture is an example with a discrete z\mathbf{z}. A variational autoencoder, or VAE, is a latent-variable model with a continuous latent vector, usually with the standard normal prior N(0,I)N(\mathbf{0}, I), whose decoder is a neural network that outputs the parameters of pθ(x∣z)p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}). With such a decoder the marginal and the posterior pθ(z∣x)p_{\boldsymbol{\theta}}(\mathbf{z} \mid \mathbf{x}) generally have no closed form, so a second network, the encoder, outputs the parameters of an approximate posterior qϕ(z∣x)q_{\boldsymbol{\phi}}(\mathbf{z} \mid \mathbf{x}). Encoder and decoder are trained together by maximizing the evidence lower bound of the next card. Unlike an ordinary autoencoder, which maps each input to one code and back, a VAE defines a probability model, and it generates new observations by decoding draws from the prior.

In plain words The model explains the variation in the data through a hidden variable that it introduces itself. That variable is part of the model’s construction, not evidence of a real hidden cause in the system that produced the data. The encoder answers which latent values could have produced a given observation, and the decoder answers which observations a given latent value tends to produce.

Example Draw a balanced binary zz, set x=zx = z, and set y=zy = z with probability 0.80.8. Summing over zz gives p(0,0)=0.5⋅1⋅0.8=0.4p(0, 0) = 0.5 \cdot 1 \cdot 0.8 = 0.4 and p(0,1)=0.5⋅1⋅0.2=0.1p(0, 1) = 0.5 \cdot 1 \cdot 0.2 = 0.1, and the other two pairs follow the same way, so this latent-variable model reproduces the constructed table exactly. The autoregressive construction of the previous card produces the same table without any latent variable, so fitting the data well does not identify the mechanism behind it.

Evidence lower bound

Definition For discrete distributions qq and rr on the same set, the Kullback-Leibler divergence is

DKL(q ∥ r)=∑zq(z)ln⁡q(z)r(z),D_{\text{KL}}(q \,\Vert\, r) = \sum_{z} q(z) \ln \frac{q(z)}{r(z)} ,

with the sum over the values zz where q(z)>0q(z) > 0. It is finite when r(z)>0r(z) > 0 wherever q(z)>0q(z) > 0, it is never negative, it is 00 exactly when q=rq = r, and it is not symmetric in qq and rr. Now take a latent-variable model with prior p(z)p(\mathbf{z}) and likelihood pθ(x∣z)p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}), an observation x\mathbf{x} with evidence pθ(x)>0p_{\boldsymbol{\theta}}(\mathbf{x}) > 0, and posterior pθ(z∣x)=p(z) pθ(x∣z)/pθ(x)p_{\boldsymbol{\theta}}(\mathbf{z} \mid \mathbf{x}) = p(\mathbf{z})\,p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z}) / p_{\boldsymbol{\theta}}(\mathbf{x}). For a distribution qq over the latent values that is positive only where the posterior is positive, the evidence lower bound is

ELBO⁡(q)=Eq[ln⁡pθ(x∣z)]−DKL(q(z) ∥ p(z)),\begin{aligned} & \operatorname{ELBO}(q) \\[0.4em] &\quad = E_{q}\bigl[\ln p_{\boldsymbol{\theta}}(\mathbf{x} \mid \mathbf{z})\bigr] \\[0.4em] &\quad - D_{\text{KL}}\bigl(q(\mathbf{z}) \,\Vert\, p(\mathbf{z})\bigr), \end{aligned}

and it satisfies the identity

ln⁡pθ(x)=ELBO⁡(q)+DKL(q(z) ∥ pθ(z∣x)),\begin{aligned} & \ln p_{\boldsymbol{\theta}}(\mathbf{x}) = \operatorname{ELBO}(q) \\[0.4em] &\quad + D_{\text{KL}}\bigl(q(\mathbf{z}) \,\Vert\, p_{\boldsymbol{\theta}}(\mathbf{z} \mid \mathbf{x})\bigr), \end{aligned}

with integrals and densities in place of sums when z\mathbf{z} is continuous. The last divergence is never negative, so ELBO⁡(q)≤ln⁡pθ(x)\operatorname{ELBO}(q) \le \ln p_{\boldsymbol{\theta}}(\mathbf{x}), with equality exactly when qq is the posterior. A VAE uses q=qϕ(z∣x)q = q_{\boldsymbol{\phi}}(\mathbf{z} \mid \mathbf{x}) for each training observation and maximizes the sum of the ELBOs over θ\boldsymbol{\theta} and ϕ\boldsymbol{\phi}. The first term rewards reconstructions that the stated observation model finds probable, and the second keeps the approximate posterior close to the prior.

In plain words The log likelihood of a VAE is hard to compute, but the ELBO can be estimated from draws of qq, and it is never larger than the log likelihood. Raising it either improves the model or tightens the bound by bringing qq closer to the true posterior, and the gap is exactly the divergence between the two. An ELBO is a bound on a log likelihood, so its value cannot be ranked against a GAN loss or a diffusion training loss.

Example A separate constructed model has two latent states with prior probabilities 1/21/2 each, and the observed event x=1x = 1 has probability 0.20.2 in state 11 and 0.80.8 in state 22. The evidence is 0.5⋅0.2+0.5⋅0.8=0.50.5 \cdot 0.2 + 0.5 \cdot 0.8 = 0.5, and Bayes’ rule gives the posterior (0.2,0.8)(0.2, 0.8). Take the approximate posterior q=(0.25,0.75)q = (0.25, 0.75). The reconstruction term is 0.25ln⁡0.2+0.75ln⁡0.8≈−0.56970.25 \ln 0.2 + 0.75 \ln 0.8 \approx -0.5697 and the prior divergence is 0.25ln⁡0.5+0.75ln⁡1.5≈0.13080.25 \ln 0.5 + 0.75 \ln 1.5 \approx 0.1308, so

ELBO⁡≈−0.5697−0.1308≈−0.7005,\begin{aligned} \operatorname{ELBO} &\approx -0.5697 - 0.1308 \\[0.4em] &\approx -0.7005 , \end{aligned}

below the log evidence ln⁡0.5≈−0.6931\ln 0.5 \approx -0.6931. The gap of about 0.00740.0074 equals DKL(q ∥ posterior)=0.25ln⁡1.25+0.75ln⁡0.9375D_{\text{KL}}(q \,\Vert\, \text{posterior}) = 0.25 \ln 1.25 + 0.75 \ln 0.9375. Reversing the first divergence gives DKL(prior ∥ q)≈0.1438D_{\text{KL}}(\text{prior} \,\Vert\, q) \approx 0.1438 instead of 0.13080.1308, which shows the asymmetry.

Generative adversarial network and mode collapse

Definition A generative adversarial network, or GAN, pairs a generator GθG_{\boldsymbol{\theta}}, which maps a noise vector z\mathbf{z} drawn from a fixed distribution p(z)p(\mathbf{z}) to a synthetic observation Gθ(z)G_{\boldsymbol{\theta}}(\mathbf{z}), with a discriminator DϕD_{\boldsymbol{\phi}}, which outputs a number in (0,1)(0, 1) read as the probability that its input came from the training data. The original objective is the game min⁡θmax⁡ϕV(θ,ϕ)\min_{\boldsymbol{\theta}} \max_{\boldsymbol{\phi}} V(\boldsymbol{\theta}, \boldsymbol{\phi}) with the value function

V(θ,ϕ)=Ex[ln⁡Dϕ(x)]+Ez[ln⁡(1−Dϕ(Gθ(z)))],\begin{aligned} & V(\boldsymbol{\theta}, \boldsymbol{\phi}) = E_{\mathbf{x}}\bigl[\ln D_{\boldsymbol{\phi}}(\mathbf{x})\bigr] \\[0.4em] & + E_{\mathbf{z}}\bigl[\ln\bigl(1 - D_{\boldsymbol{\phi}}(G_{\boldsymbol{\theta}}(\mathbf{z}))\bigr)\bigr], \end{aligned}

where the first expectation is over training observations and the second over the noise. It is trained by alternating gradient steps, the discriminator ascending and the generator descending, and in practice the generator often maximizes ln⁡Dϕ(Gθ(z))\ln D_{\boldsymbol{\phi}}(G_{\boldsymbol{\theta}}(\mathbf{z})) instead because that gives stronger gradients early in training. A standard GAN provides no normalized likelihood pθ(x)p_{\boldsymbol{\theta}}(\mathbf{x}), and the value of its loss depends on the current discriminator. In an idealized analysis with unlimited capacity and exact optimization, the game is solved when the generator’s distribution equals pdatap_{\text{data}}, but finite networks trained by alternating steps need not reach that point. Mode collapse is the failure in which the generator produces only a limited part of the data distribution and misses other regions that carry substantial probability.

In plain words The generator learns by trying to fool a critic that is learning at the same time. A collapsed generator can produce samples that each look perfectly valid, so a few appealing samples cannot reveal the failure. Coverage has to be measured by comparing the distribution of many generated outputs with held-out data.

Example A generator for the constructed table that outputs (0,0)(0, 0) and (1,1)(1, 1) half the time each produces only valid, common pairs, yet never produces (0,1)(0, 1) or (1,0)(1, 0), which together carry probability 0.20.2 under the target. Five independent draws from the target itself contain no such pair with probability 0.85≈0.330.8^5 \approx 0.33, so five samples often cannot tell the two apart. For 100100 draws that probability is 0.8100≈2×10−100.8^{100} \approx 2 \times 10^{-10}, so counting over a large sample exposes the collapse. Read as a distribution, the collapsed generator gives probability 00 to a held-out pair (0,1)(0, 1), so its log likelihood on data containing that pair is −∞-\infty.

Diffusion model

Definition A diffusion model defines a forward process that adds Gaussian noise to a training observation x0\mathbf{x}_0 over many steps and learns a reverse process that turns noise back into observations. In a common formulation the noisy version at step tt can be drawn directly as

xt=αˉt x0+1−αˉt ε,\mathbf{x}_t = \sqrt{\bar{\alpha}_t}\, \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\, \boldsymbol{\varepsilon},

where ε\boldsymbol{\varepsilon} is drawn from N(0,I)N(\mathbf{0}, I) independently of x0\mathbf{x}_0 and the schedule αˉt\bar{\alpha}_t decreases from near 11 at the first step to near 00 at the last step TT. A network εθ(xt,t)\boldsymbol{\varepsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) is trained to predict the added noise by minimizing the average of ∥ε−εθ(xt,t)∥22\lVert \boldsymbol{\varepsilon} - \boldsymbol{\varepsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) \rVert_2^2 over training observations, steps and noise draws. Sampling starts from pure noise xT\mathbf{x}_T drawn from N(0,I)N(\mathbf{0}, I) and applies learned reverse steps one after another. The objective, schedule and sampler differ between formulations. This noise-prediction loss is related to a variational bound, but with its usual weighting it is not the model’s log likelihood, and its value depends on the schedule and the weighting.

In plain words Destroying data with noise is easy and needs no learning. The learned part runs the other way. From a noisy input and its noise level the network estimates what noise was added, and the sampler uses that estimate to take a step toward cleaner data. During generation neither the clean point nor the true noise is known, so repeatedly applying some smoothing filter is not a diffusion sampler.

Example Take the constructed clean point x0=(2,−1)\mathbf{x}_0 = (2, -1), the schedule value αˉt=0.64\bar{\alpha}_t = 0.64 and the noise vector ε=(0.5,−0.5)\boldsymbol{\varepsilon} = (0.5, -0.5). Then 0.64=0.8\sqrt{0.64} = 0.8 and 0.36=0.6\sqrt{0.36} = 0.6, so

xt=0.8 (2,−1)+0.6 (0.5,−0.5)=(1.9,−1.1).\begin{aligned} \mathbf{x}_t &= 0.8\,(2, -1) \\[0.4em] &\quad + 0.6\,(0.5, -0.5) \\[0.4em] &= (1.9, -1.1) . \end{aligned}

If an oracle supplied the true noise, (xt−0.6 ε)/0.8(\mathbf{x}_t - 0.6\,\boldsymbol{\varepsilon})/0.8 would recover (2,−1)(2, -1) exactly. A trained network only estimates the noise. The estimate (0.4,−0.4)(0.4, -0.4) gives (2.075,−1.075)(2.075, -1.075), which misses the clean point by 0.6/0.8=0.750.6/0.8 = 0.75 times the noise error (0.1,−0.1)(0.1, -0.1). This is one forward noising step and its inversion, not the output of a trained diffusion model.

Check yourself

  1. A generator of binary pairs draws x=1x = 1 with probability 0.60.6. Given x=1x = 1 it sets y=1y = 1 with probability 0.70.7, and given x=0x = 0 it sets y=1y = 1 with probability 0.20.2. Find p(1,1)p(1, 1) and p(0,1)p(0, 1) and the likelihood of the two independent observations (1,1)(1, 1) and (0,1)(0, 1). What is wrong with a generator that always emits the most probable pair?
Show the answer

By the autoregressive factorization, p(1,1)=0.6⋅0.7=0.42p(1, 1) = 0.6 \cdot 0.7 = 0.42 and p(0,1)=0.4⋅0.2=0.08p(0, 1) = 0.4 \cdot 0.2 = 0.08. The likelihood of the two independent observations is

L=0.42⋅0.08=0.0336.\mathcal{L} = 0.42 \cdot 0.08 = 0.0336 .

The other two pairs have p(1,0)=0.6⋅0.3=0.18p(1, 0) = 0.6 \cdot 0.3 = 0.18 and p(0,0)=0.4⋅0.8=0.32p(0, 0) = 0.4 \cdot 0.8 = 0.32, and the four probabilities add to 11.

The most probable pair is (1,1)(1, 1). A generator that always emits it produces a valid and fairly common outcome every time, but it never produces the other three pairs, which carry probability 1−0.42=0.581 - 0.42 = 0.58. This is complete mode collapse. Read as a distribution it gives probability 00 to the observation (0,1)(0, 1), so the likelihood of the two observations above under it is 00.

  1. In the two-state model of the ELBO card, take the approximate posterior equal to the prior, q=(0.5,0.5)q = (0.5, 0.5). Compute the ELBO and its gap to the log evidence. For which qq is the bound tight?
Show the answer

The prior divergence is 00 because qq equals the prior. The reconstruction term is

0.5ln⁡0.2+0.5ln⁡0.8=ln⁡0.4≈−0.9163,\begin{aligned} & 0.5 \ln 0.2 + 0.5 \ln 0.8 \\[0.4em] &\quad = \ln 0.4 \approx -0.9163 , \end{aligned}

so the ELBO is about −0.9163-0.9163. The log evidence is ln⁡0.5≈−0.6931\ln 0.5 \approx -0.6931, so the gap is about 0.22310.2231. It equals the divergence from qq to the posterior, 0.5ln⁡2.5+0.5ln⁡0.625≈0.22310.5 \ln 2.5 + 0.5 \ln 0.625 \approx 0.2231. The approximate posterior (0.25,0.75)(0.25, 0.75) of the card gave the tighter value −0.7005-0.7005 because it is closer to the posterior.

The bound is tight exactly when qq is the posterior (0.2,0.8)(0.2, 0.8). Then the reconstruction term is 0.2ln⁡0.2+0.8ln⁡0.8≈−0.50040.2 \ln 0.2 + 0.8 \ln 0.8 \approx -0.5004, the prior divergence is about 0.19270.1927, and the ELBO is ln⁡0.5\ln 0.5, with gap 00.

  1. A team reports that viewers rated a gallery of samples from its image GAN as realistic, and that its training loss is lower than the training loss of a competing diffusion model. Which evaluation questions remain open, and is the loss comparison meaningful?
Show the answer

Realism ratings address sample fidelity only, and only for the samples shown. A gallery chosen by hand is not representative, so the samples should be drawn by a stated procedure and rated with a stated comparison set and uncertainty. Several separate questions remain open.

  • Coverage. Does the generator produce the full variety of the data in the right proportions? Compare the distribution of many generated outputs with held-out data and look for missing regions, as with the diagonal-only generator, which never produces pairs that carry probability 0.20.2.
  • Memorization. Does it reproduce particular training images? Search the training set for exact or near copies of generated images and compare with how close held-out images come. In a space with four possible outcomes repetition proves nothing, but for high-dimensional images a near copy of a training image is real evidence.
  • Task usefulness. Do the outputs help the intended task, judged by a protocol fixed in advance? A realistic image can still carry a wrong label or unsafe details.
  • Accuracy and permission. Whether generated content is factually correct, and whether the training data and outputs may be used, needs separate evidence that no generation metric supplies.

The loss comparison is not meaningful. A GAN loss depends on its own discriminator, and a diffusion loss is a weighted noise-prediction error under a particular schedule. The two numbers measure different quantities, so ranking them says nothing about which model is better. Both models have to be compared on a common, justified evaluation question.

How this lesson connects