c22 · Section 5
Sampling from a generative model
How do you draw a new sample?
L1Core
The idea, one concrete case and enough to use it. Every simplification is marked and linked to the precise statement.
Ask the same model for the same picture twice and you get two different pictures, because the sampler draws a fresh random number each time and the weights never see that draw; the draw is an input, not a parameter. One model, one prompt, two outputs: the difference sits in the drawing procedure, not in the weights.
How do you draw a new sample? You draw a number between zero and one, find the interval where it lands in the model's list of probabilities, and return the outcome attached to that interval. A diffusion model repeats the same kind of draw at every denoising step [S:74:371]. !Three outcomes with probabilities 0.5, 0.3 and 0.2, laid along the unit interval. A draw of 0.7 lands in the second block and returns outcome two; a draw of 0.1 returns outcome one. Every sampler in the chapter is this construction with more intervals. Status: synthetic.
The mechanical version is ancestral sampling: draw the top-level variable from its distribution, then draw each lower variable from its conditional distribution given what you have [S:74:343]. For a latent variable model the top variable is the code and the lower one is the picture, so you draw a code and then draw a picture from the decoder [S:74:343]. !The guidance weight as a dial. The combined estimate is one plus the weight times the conditional estimate, minus the weight times the unconditional one, so the line leaves the conditional value at zero and reaches -1.4 at a weight of two. Each increment pushes further into the condition and costs variety. Status: synthetic.
The interval construction is worth doing once by hand. Suppose a model gives three outcomes probabilities 0.5, 0.3 and 0.2, in that order. Lay them along the unit interval: outcome one owns 0 to 0.5, outcome two owns 0.5 to 0.8, outcome three owns 0.8 to 1. A uniform draw of 0.7 lands in the second interval, so the sample is outcome two. A draw of 0.1 gives outcome one.
Nothing in that paragraph is specific to pictures, because the recipe applies wherever a model assigns probabilities to outcomes. A diffusion model applies it once per pixel or once per step [S:74:371]. The sampler is the part that turns a table of numbers into an outcome.
Conditioning changes which distribution you draw from. A conditional model draws a picture together with its label, and at sampling time you fix the label and draw the picture [S:211:3]. A text prompt works the same way: the caption is the condition, and the sampler draws from the pictures the model considers compatible with it [S:74:382].
Many draws turn a sampler into a measuring instrument. Average any quantity over a thousand samples and you estimate its expectation, and the estimate improves as the count grows [S:74:351]. That is how a sampler is used to inspect a distribution that nobody can write down.
The construction generalises without changing its character. A Gaussian sampler keeps no table of intervals, because the intervals are infinitely many and infinitesimally wide, so it uses the inverse of the Gaussian cumulative curve to map a uniform draw to a value. A categorical sampler keeps the table, and a pixel with 256 grey levels keeps 256 intervals.
Diffusion adds a second layer. Instead of one draw, the reverse chain makes a draw at every step, and each draw is a Gaussian centred on the denoised estimate [S:74:371]. Remove the fresh noise entirely and the chain becomes deterministic, which is the idea behind denoising diffusion implicit models [S:74:379]. The same trained network then supports both a stochastic and a deterministic sampler, and good samples can be created with 50 time steps when the chain is deterministic [S:74:379].
Guidance changes the draw rather than the model. In a conditional diffusion model, the network can be asked for an estimate with the condition and without it, and the sampler combines the two. Multiply the conditional estimate by one plus the weight, multiply the unconditional estimate by the weight itself, and subtract the second from the first [S:211:5]. The result leans away from the unconditional estimate and toward the condition. That combination is classifier-free guidance, because no separate classifier is trained [S:211:4-5].
Worked example. A conditional estimate of -0.2 and an unconditional estimate of 0.4, with three settings of the weight.
| weight | conditional | unconditional | combined estimate |
|---|---|---|---|
| 0 | -0.2 | 0.4 | -0.2 |
| 1 | -0.2 | 0.4 | -0.8 |
| 2 | -0.2 | 0.4 | -1.4 |
At weight zero the combined estimate equals the conditional one, so guidance is switched off. At weight one it is twice the conditional estimate minus the unconditional one, which is -0.8. At weight two it is three times -0.2 minus two times 0.4, which is -1.4. Each increment pushes the sample further in the direction the condition asks for.
The push has a price. Guidance trades mode coverage against sample fidelity, in the same spirit as low temperature sampling or truncation in other model families [S:211:1]. Over-weight the condition and the model produces very high quality but slightly stereotypical examples [S:74:381]. Under-weight it and the samples become varied and vague [S:211:1].
The same trade appears under other names. Truncation in a GAN narrows the noise input, and low temperature sharpens a categorical draw, and both buy fidelity with variety [S:211:1]. Diffusion needed a new mechanism because the obvious versions of those tricks do not work on a denoising chain [S:211:1].
Stop and try
a model gives outcomes A, B and C probabilities 0.2, 0.5 and 0.3. Condition on "not C", renormalise the remaining two, and say which outcome a draw of 0.8 returns. Then say what the same draw returns without the condition.
Key idea
A sample is a random draw pushed through the model, not a stored picture. The sampler walks the cumulative probabilities, and a diffusion model repeats that walk at every denoising step. Guidance reweights the draw, buying fidelity with diversity.
The cost is that one sample tells you almost nothing about the distribution behind it. Two pictures from the same prompt look different, and neither shows you the region the model cannot reach. That is why the next section stops looking at pictures and starts measuring distributions.
L2Deepen
The formal statement, the derivation and the edge cases. It refines L1 and never contradicts it.
The formal statement. Draw a uniform random number between zero and one. The value at which the cumulative distribution curve reaches that number has the distribution you want. For a finite set of outcomes, the curve is a staircase, and the inverse maps each interval to its outcome. A sampler for a directed model draws the variables in an order that respects the arrows: each variable comes from its conditional given the values already drawn [S:74:343]. A diffusion sampler applies that rule at every step of the reverse chain [S:74:371].
The derivation. Take any value on the line. The chance that the inverse lands at or below that value equals the chance that the uniform draw lands at or below the curve's height there. A uniform number falls below a threshold with probability equal to the threshold, so the chance equals the curve's height, which is the definition of the distribution. This account omits the computation of the inverse for a Gaussian. No closed form exists, so a Gaussian sampler calls a numerical inverse of the error function, and the cost of that call is the price of the continuous case.
The edge case. The core says two calls give two pictures because the sampler draws a fresh number each time. That holds for a stochastic chain. Remove the fresh noise and the chain becomes deterministic, so the same initial draw gives the same picture every time [S:74:379]. The randomness then sits in one place, the starting noise, and not in every step. Two samplers built from one trained network can therefore differ in reproducibility without differing in the model, and a reported sample count means little unless it names the sampler.
What the core hid. The core averaged over a thousand draws and called the result an estimate of an expectation. The sample mean is unbiased whenever each draw has the target distribution as its marginal, whatever the dependence between draws; dependence changes the variance of the estimate, not its mean. A draw from a sampler whose law is not the model distribution makes the estimate biased, and no number of chains removes that bias. The error falls at the stated rate only when the draws are independent, and separate diffusion chains are independent because their starting noise is. Each chain is one sample, and the cost of a sample is the whole chain. The error falls with the number of chains, not with the number of network calls. A long chain buys a better picture and does not buy a better estimate.
Where the analogy breaks. The core says guidance leans the draw toward the condition. The formula mixes two estimates, and the result need not be the score of any distribution. Ho and Salimans state that the combined estimate is not in general the gradient of any scalar potential, because both inputs come from unconstrained networks [S:211:5]. Leaning toward the condition describes the direction of the change, not a probability that has been reweighted. The trade of coverage for fidelity is real; the picture of a tilted distribution is not.
L3Research
Full rigour, the original sources and the open questions. This is the research frontier.
Original sources. Ancestral sampling is the textbook construction for a directed latent variable model [S:74:343]. Classifier-free guidance is Ho and Salimans, who replace the classifier gradient with a linear combination of a conditional and an unconditional estimate from one network [S:211:4-5]. Classifier guidance, the version that needs the extra network, is Dhariwal and Nichol, as the same paper reports [S:211:1].
An open question. The field has not settled how to choose the guidance weight. The weight is swept, and the sweep traces a trade-off between sample quality and diversity, measured by Inception score and FID [S:211:2]. Nothing in the training objective fixes the value for a new dataset or a new prompt distribution. The paper reports the curve rather than a rule, and the curve belongs to its own model and data [S:211:2]. Evidence that would settle it: fix a target trade-off in advance, then show that one rule for the weight meets it across datasets, models and prompt sets, with failures reported rather than excluded.
A frontier result. The sampler became a separate object from the model. Denoising diffusion implicit models keep the training objective and change the sampler, reporting generation ten to fifty times faster than the stochastic chain [S:207:1]. The same trained network then supports a stochastic and a deterministic sampler, and good samples appear with fifty steps [S:74:379]. Because the sampler is a separate choice from the weights, one trained model can be sampled in more than one way [S:74:379].
What to read next. Read Ho and Salimans sections 3 and 4 for both forms of guidance and the trade-off [S:211:3-5]. Read Prince 18.6.2 for the sampler that skips steps [S:74:379]. Then read Song, Meng and Ermon for why a deterministic chain is still a generative model and not a lookup table [S:207:1].