Before this lesson:lesson 06
(the score, denoising score matching, Tweedie's formula at one noise level) and
lesson 07 (the forward process q(xt∣x0), the ELBO and the
ε-loss, noise schedules). You also need conditional expectation and the Gaussian
density (probability and statistics) and the
change of variables in an integral.
Time: about 100 minutes, plus the notebook.
Lesson 07 trained a network to predict the noise ε
(DDPM, Stable Diffusion 1). Other models predict other things. EDM (Karras et al. 2022) predicts the
clean sample through a preconditioned network. Progressive distillation, Imagen Video and the
768-pixel Stable Diffusion 2 model predict the velocity v. Score-based SDE models predict the score.
Flow matching and rectified flow (lesson 10,
Stable Diffusion 3) predict a flow velocity. These look like different methods. They are not:
at the optimum they are one function, seen through different linear maps.
What really differs is (1) how the loss weights the noise levels, (2) the scale of the target the
network must output, and (3) how much a network error grows when it is converted to a clean-sample
estimate. These three points explain most training choices: cosine vs linear schedules,
min-SNR weighting, v-prediction, EDM preconditioning, zero terminal SNR, and schedule shifts for
large images. When you choose a target and a weighting for a new model (for example a latent
diffusion model of weather fields or seismic waveforms), you choose a point in this design space.
with signal scaleat and noise scaleσt. In DDPM, at=αˉt and
σt=1−αˉt (lesson 07). The network sees xt.
It does not see x0 or ε. But the equation links them: a guess of x0 fixes the guess
of ε, and the other way round:
ε=σtxt−atx0,x0=atxt−σtε.
So "predict the noise" and "predict the clean sample" are two ways to write the same answer.
Look at one coordinate. Then (x0,ε) is a point in a plane. In the
variance-preserving (VP) case at2+σt2=1, so we can write
at=cosϕ and σt=sinϕ for an angle ϕ between 0° (clean) and 90°
(pure noise). Then xt=cosϕx0+sinϕε is the coordinate of the point along
the unit direction (cosϕ,sinϕ).
Define the velocity as the coordinate along the perpendicular direction (−sinϕ,cosϕ):
v=atε−σtx0=−sinϕx0+cosϕε=dϕdxt.
It is called the velocity because it is the rate at which xt moves as the angle ϕ grows.
The pair (xt,v) is the pair (x0,ε) in a frame rotated by ϕ. A rotation is easy
to undo, so
x0=atxt−σtv,ε=σtxt+atv.
Check the first one: atxt−σtv=at2x0+atσtε−atσtε+σt2x0=x0, because at2+σt2=1.
Given xt, the unknown point (x0,ε) lies on one line: the line
atx0+σtε=xt, perpendicular to the direction of xt. Any prediction picks one
point on this line. An ε-network reports its vertical coordinate, an x0-network its
horizontal coordinate, a v-network its position along the line.
Interactive figure: loading…
Drag the orange prediction along the dashed line. All points on that line
have the same xt. The blue point is the true pair (x0,ε); the
violet arrow is v. The distance between the two points along the line is the
error Δv. Its horizontal part is the x0-error and its vertical part the ε-error.
Move the angle ϕ to high noise: the line becomes almost horizontal, so a small ε-error
is a large x0-error (the orange bar σt/at grows). The two v bars never exceed 1.
A real network makes errors. The figure shows what happens to them:
From ε to x0: an error δ in ε^ becomes an error
(σt/at)δ in x^0. At high noise at→0, so this blows up.
From x0 to ε: an error δ in x^0 becomes (at/σt)δ in
ε^. At low noise σt→0, so this blows up.
From v:x^0=atxt−σtv^ and ε^=σtxt+atv^, so a
v-error is multiplied by σt≤1 or by at≤1. It never grows.
This is the main practical reason to prefer v (or the EDM preconditioning of section 3) over
ε when the model must work at very high noise.
Quick check
At a VP noise level with at=0.6 and σt=0.8, an ε-network makes an error of 0.3. What is the error of the x^0 computed from it?
Why.
x^0=(xt−σtε^)/at, so an error δ in ε^ gives an error (σt/at)δ=(0.8/0.6)⋅0.3=0.4. The value 0.225 uses the inverse factor at/σt, which is the factor for the opposite conversion (x0→ε). The value 0.5 divides by at but forgets the factor σt.
Many pairs (x0,ε) give the same xt. A network that only sees xt cannot know which
pair was used. So what is the best it can do? For a squared-error loss the answer is always the same:
the minimizer of E∥target−f(xt)∥2 over all functions f is the conditional
mean E[target∣xt] (proof in section 3). The best x0-prediction is the
average of all clean samples that could have produced xt, weighted by how likely they are.
This optimal x0-prediction is called the denoiserD(xt)=E[x0∣xt]. Every other
target is a linear function of (x0,ε) for a fixed xt, and the conditional mean is
linear. So the optimal ε,v and score predictions are the same linear maps applied to
D. One function determines all of them, and Tweedie's formula says which one: the score of
the noisy density. In the simplest variance-exploding (VE) form x=x0+σε
(no scaling, a=1), it reads
D(x)=E[x0∣x]=x+σ2∇xlogpσ(x).
The denoiser is one step from x along the score, with step size σ2. You met this form in
lesson 06.
Interactive figure: loading…
Drag the yellow observationx and change the noise level σ. The
magenta curve is the posterior p(x0∣x) over the clean value, computed
on a grid with Bayes' rule. Its mean is the orange line. The
violet arrow is the Tweedie step σ2∇logpσ(x), computed
from the exact score. It ends exactly at the posterior mean (compare the two bold numbers). At high
noise the posterior covers both modes, and D(x) lies between them: the best guess in squared
error is an average, not a plausible sample. The lower plot shows the whole denoiser for this σ.
Data: 0.3N(−1,0.22)+0.7N(1.5,0.52); the score is exact, not learned.
Quick check
Data x0∼N(0,σdata2) with σdata=0.5. You observe x=x0+σε=1.0 with σ=0.5 (VE form). What is the optimal denoiser output D(x)?
Why.
The noisy density is pσ=N(0,σdata2+σ2)=N(0,0.5), so its score is −x/0.5=−2. Tweedie gives D=x+σ2⋅(−2)=1−0.25⋅2=0.5. In general D(x)=σdata2+σ2σdata2x: the observation is shrunk toward the data mean. 1.0 ignores the noise; 0 is the data mean, the answer only for σ→∞.
From xt and one prediction you get all the others (VP case):
given
x^0
ε^
v^
score s^
ε^
(xt−σtε^)/at
—
(ε^−σtxt)/at
−ε^/σt
x^0
—
(xt−atx^0)/σt
(atxt−x^0)/σt
−(xt−atx^0)/σt2
v^
atxt−σtv^
σtxt+atv^
—
−(σtxt+atv^)/σt
The first two columns use only xt=atx0+σtε. The v entries also use
at2+σt2=1; for example, v=atε−σt(xt−σtε)/at=(ε−σtxt)/at.
The score column is exact for the optimal predictions (Theorem 1 below); for a trained network it
defines the score model.
The squared loss is minimized by the conditional mean #
Lemma. Let Y be a target and X an input with E∥Y∥2<∞. Over all functions
f,E∥Y−f(X)∥2 is smallest for f∗(x)=E[Y∣X=x], and the minimum is
EtrCov(Y∣X).
Proof.
Write Y−f(X)=(Y−f∗(X))+(f∗(X)−f(X)).
Expand the square. The cross term is 2E[(f∗(X)−f(X))⊤(Y−f∗(X))].
Condition on X: the first factor is fixed, and E[Y−f∗(X)∣X]=0. So the cross term is 0.
Hence E∥Y−f(X)∥2=E∥Y−f∗(X)∥2+E∥f∗(X)−f(X)∥2. The second term is
≥0 and is 0 only for f=f∗. The first term is EtrCov(Y∣X).■
So with unlimited capacity, the ε-network learns E[ε∣xt], the
x0-network learns E[x0∣xt], and so on, at every noise level.
Why this is true (intuition). The noisy density pt is the data density blurred by a Gaussian.
Its score at xt points toward where the clean data that could explain xt is concentrated.
Moving along the score by σt2 (the noise variance) moves you to the center of that mass.
Proof.
xt given x0 is N(atx0,σt2I), so
pt(x)=∫N(x;atx0,σt2I)pdata(x0)dx0.
The gradient of a Gaussian density in x is
∇xN(x;atx0,σt2I)=−σt2x−atx0N(x;atx0,σt2I).
Differentiate step 1 under the integral (allowed for a Gaussian kernel):
∇pt(x)=−σt21∫(x−atx0)N(x;atx0,σt2I)pdata(x0)dx0.
Bayes' rule: p(x0∣xt=x)=N(x;atx0,σt2I)pdata(x0)/pt(x).
Divide step 3 by pt(x); the integral becomes an expectation over this posterior:
∇logpt(x)=pt(x)∇pt(x)=−σt2x−atE[x0∣xt=x].
Solve step 4 for E[x0∣xt]: this is the first formula.
ε=(xt−atx0)/σt is linear in x0 for fixed xt, so
E[ε∣xt]=(xt−atE[x0∣xt])/σt=−σt∇logpt(xt) by step 4. ■
With at=1,σt=σ this is the VE form of section 2.
Consequences.
All optima are one function.ε∗=−σt∇logpt,x0∗=(xt+σt2∇logpt)/at,v∗=atε∗−σtx0∗. With unlimited
capacity, the choice of target does not change the learned model at any single noise level. It
changes how the levels are weighted and how training behaves (Theorem 2).
Denoising score matching is the same thing. Regressing s^ on −ε/σt is the
ε-loss divided by σt2; its optimum is −E[ε∣xt]/σt=∇logpt.
The loss has a floor. By the lemma, the minimum of E∥ε−ε^∥2 is
EtrCov(ε∣xt)>0. A loss value alone does not say how good a model is.
Second-order Tweedie: the posterior covariance
Write m(x)=E[x0∣xt=x]. Two facts give the covariance of the posterior.
The posterior is p(x0∣x)∝pdata(x0)exp(atx0⊤x/σt2−at2∥x0∥2/(2σt2)).
As a function of x,x enters only through η=atx/σt2, like the natural parameter
of an exponential family. For such a family the derivative of the mean with respect to η is
the covariance. So ∂m/∂x=(at/σt2)Cov(x0∣x).
The true pair and the predicted pair give the same xt:atx0+σtε=atx^0+σtε^.
So at(x0−x^0)=−σt(ε−ε^).
Take squared norms: at2∥x0−x^0∥2=σt2∥ε−ε^∥2. Divide by at2:
this is the first identity, since σt2/at2=1/SNR. (No VP assumption so far.)
For v:v−v^=at(ε−ε^)−σt(x0−x^0). Insert step 1,
x0−x^0=−(σt/at)(ε−ε^):v−v^=(at+σt2/at)(ε−ε^)=atat2+σt2(ε−ε^)=at1(ε−ε^), using VP.
Square: ∥v−v^∥2=∥ε−ε^∥2/at2, and
1/at2=(at2+σt2)/at2=1+1/SNR. The last form follows from step 2. ■
Read it in "x0 units" (the factor in front of ∥x0−x^0∥2): the x0-loss has weight
1, the ε-loss has weight SNR, the v-loss has weight 1+SNR, and the
plain score loss ∥s^+ε/σt∥2 has weight SNR/σt2. So, compared with
the x0-loss, the ε-loss puts much more weight on low noise (high SNR), and much less on
high noise.
Quick check
A model must give good x^0 estimates at very high noise (at≈0.01). Which output is the safest choice for the network?
Why.
The conversions are exact for exact outputs, but a network has errors. From ε^,x^0=(xt−σtε^)/at multiplies an error by σt/at≈100. From v^,x^0=atxt−σtv^ multiplies it by σt≤1. The score is −ε^/σt, so it has the same problem as ε^. Equal optima do not mean equal errors.
All losses as one weighted integral over λ (Kingma and Gao 2023) #
Theorem 2 lets us write every loss in ε units. Write xλ for the noisy sample at
log-SNR λ. Every common objective is a special case of
Lw(x0)=21∫λminλmaxw(λ)Eε∥ε−ε^(xλ;λ)∥2dλ
for some weight functionw(λ)≥0.
How a schedule turns into a weight, step by step. Training draws t∼U(0,1) and
computes λ=λt. Because λt decreases in t, this gives λ a density
p(λ)=−dt/dλ (change of variables for densities). Then
So a loss with weight w can be trained with any schedule, if each sample is multiplied by
w(λ)/p(λ). In the other direction, the plain loss Lsimple=Et∥ε−ε^∥2
has w(λ)=p(λ):for Lsimple the schedule is the weight.
Example: the cosine schedule. Without the small offset, at=cos(πt/2) and
σt=sin(πt/2).
So p(λ)=2π1sech(λ/2), a bell shape centered at λ=0.
Implied weights (up to a constant factor), using Theorem 2 to move each loss to ε units:
objective
w(λ)
decreasing in λ?
continuous-time ELBO
1
yes (constant)
ε-loss, cosine schedule (Lsimple)
p(λ)∝sech(λ/2)
no
x0-loss, cosine schedule
p(λ)e−λ
yes
v-loss, cosine schedule
p(λ)(1+e−λ)=e−λ/2/π
yes
flow matching, t∼U(0,1)
21e−λ/2
yes
EDM
N(λ;2.4,2.42)(e−λ+σdata2)/σdata2
no
min-SNR-γ,ε-loss, cosine
p(λ)min(1,γe−λ)
no
Min-SNR-γ (Hang et al. 2023, usually γ=5) caps the weight in x0 units at
min(SNR,γ). In ε units that is min(SNR,γ)/SNR=min(1,γ/SNR).
It lowers the weight of the easy, low-noise levels.
Monotone weights are ELBOs (Kingma and Gao 2023, Theorem 1). If w(λ) decreases as
λ grows (it increases with t), then Lw equals, up to a constant, an average over
noise levels of the negative ELBO of noise-perturbed data. That is maximum likelihood with
Gaussian noise as data augmentation. The v-loss with the cosine schedule and flow matching have
monotone weights; Lsimple with the cosine schedule and EDM do not.
Interactive figure: loading…
Pick a target and the way λ is sampled. The orange curve is the
weight w(λ) of the resulting loss. It is the product of the aqua densityp(λ) (how often training sees each noise level) and the violet target factor
(Theorem 2: 1 for ε,e−λ for x0,1+e−λ for v). The grey line
is the ELBO, w=1. Notice that x0 and v tilt the weight toward high noise (left), and that
min-SNR-5 cuts the weight at low noise (right). The badge says whether the weight decreases in
λ. The linear schedule is drawn in its continuous form, β(t) from 0.1 to 20.
Theorem 3 (the continuous-time ELBO depends on the schedule only through its end points) #
The ELBO of a diffusion model with T steps contains a sum of terms Lt−1, one per step
(lesson 07). As T→∞ with a fixed function SNR(t) on
t∈[0,1], this sum becomes (Kingma et al. 2021, VDM, eq. 15)
L∞=−21Ex0,ε∫01SNR′(t)∥x0−x^0(xt;t)∥2dt.
SNR′(t)<0, so L∞≥0.Claim. Suppose the network uses its input only through
the rescaled sample xt/at and the value SNR(t). Then
L∞=21∫SNRminSNRmaxEx0−x~(x0+ε/u,u)2du,
where SNRmax=SNR(0),SNRmin=SNR(1) and x~(z,u) is the
network written as a function of z=xt/at and u=SNR. Only the two end points of the
schedule enter. The other two ELBO terms (the prior term at t=1 and the reconstruction term at
t=0) also depend only on the end points.
Why this is true (intuition). The integrand is "the denoising error at signal-to-noise ratio
u". The schedule only says when (at which t) each u is visited and how fast. Integrating
over u instead of t removes the "when".
Proof (from the discrete ELBO to the end points)
One step of the discrete ELBO. Ho et al. (2020, eq. 12) give
Lt−1=2β~tαt(1−αˉt)βt2E∥ε−ε^∥2+Ct for the
reverse variance β~t=1−αˉt1−αˉt−1βt. Insert β~t:
the weight is 2αt(1−αˉt−1)βt.
In SNR form. With SNR(t)=αˉt/(1−αˉt) and αˉt=αtαˉt−1:SNR(t)SNR(t−1)=αt(1−αˉt−1)1−αˉt, so
SNR(t)SNR(t−1)−1=αt(1−αˉt−1)1−αˉt−αt+αˉt=αt(1−αˉt−1)βt.
So the weight is 21(SNR(t−1)/SNR(t)−1).
In x0 units. Theorem 2, step 2 (no VP needed) gives
∥ε−ε^∥2=SNR(t)∥x0−x^0∥2. Hence
Lt−1=21(SNR(t−1)−SNR(t))E∥x0−x^0∥2+Ct.
The limit. Put the steps on a grid ti=i/T. Then SNR(ti−1)−SNR(ti)≈−SNR′(ti)/T,
and the sum over i is a Riemann sum of the integral L∞.
Change of variables.SNR(t) is strictly decreasing, so it is invertible. Set
u=SNR(t),du=SNR′(t)dt;t=0↦SNRmax and t=1↦SNRmin:L∞=−21∫SNRmaxSNRminE∥x0−x^0∥2du=21∫SNRminSNRmaxE∥x0−x^0∥2du.
The integrand depends only on u.xt/at=x0+(σt/at)ε=x0+ε/u. The
law of this rescaled input depends only on u. By assumption the network output is
x~(xt/at,u). So the integrand is a function of u alone, and the schedule enters only
through the limits. ■
The same in λ. With du=eλdλ and eλ∥x0−x^0∥2=∥ε−ε^∥2
(Theorem 2), L∞=21∫E∥ε−ε^∥2dλ: the weight w(λ)=1
of the table. The same change of variables shows that every Lw with a fixed w(λ)
does not depend on the schedule between the end points (Kingma and Gao 2023, section 3.2).
What the schedule still changes.
The variance of training. The schedule is the importance-sampling distribution p(λ) of
the Monte Carlo estimate. It sets the variance of the loss and gradient estimates, and with it
the training speed.
The weight, for losses defined in t. For Lsimple,w(λ)=p(λ), so a new
schedule is a new objective.
The end points.SNRmin must be close to 0 so that x1 matches the Gaussian prior
(see zero terminal SNR below), and SNRmax sets how clean the last step is.
Resolution. More pixels average the noise, so the coarse structure survives longer at the
same SNR. The shifted cosine schedule (Hoogeboom et al. 2023) uses λ(t)+2log(64/d) for
resolution d; for d=256 the shift is 2log(1/4)=−2.77.
Quick check
You replace the noise schedule of a continuous-time diffusion model by another one with the same SNRmin and SNRmax, and keep the continuous-time ELBO as the loss. What changes?
Why.
By Theorem 3 the continuous-time ELBO is an integral over u=SNR between the two end points, so its value does not change. But training estimates it by sampling t, and the schedule decides which λ values are sampled often. That changes the variance of the estimate. Option one forgets the change of variables; option three forgets that we never compute the integral exactly.
EDM preconditioning (Karras et al. 2022, section 5) #
EDM works in the VE form x=y+σn: clean data y with standard deviation σdata,
noise n∼N(0,I), noise level σ. (The link to VP: y=x0,σ=σt/at,x=xt/at.) The network Fθ is wrapped in a denoiser
The four coefficients are chosen so that the network always sees inputs and targets of unit size.
The loss is Eσ,y,nω(σ)∥Dθ(y+σn;σ)−y∥2. (Karras et al. call the
loss weight λ(σ); we write ω to avoid a clash with the log-SNR.) Since
Dθ−y=cout(Fθ−Ftarget) with the effective target
Ftarget=couty−cskipx,
the loss is Eωcout2∥Fθ−Ftarget∥2. The coefficients follow in four steps.
Unit input variance.Var(cinx)=cin2(σdata2+σ2)=1, so
cin=1/σ2+σdata2.
Unit target variance.y−cskipx=(1−cskip)y−cskipσn has variance
(1−cskip)2σdata2+cskip2σ2 (per coordinate; y and n are independent).
For Var(Ftarget)=1,cout2 must equal this variance.
Smallest error amplification. An error of Fθ reaches Dθ multiplied by cout.
So choose cskip to make the variance of step 2 as small as possible. Set its derivative
to zero: −2(1−cskip)σdata2+2cskipσ2=0, so
cskip=σ2+σdata2σdata2. Insert it into step 2:
cout=σ2+σdata2σσdata.
Equal effective weight. Choose ω(σ)=1/cout2=(σσdata)2σ2+σdata2, so
every noise level has weight 1 on the loss of Fθ.
Empirical choices (not derived): cnoise=41lnσ; training levels
lnσ∼N(−1.2,1.22);σdata=0.5 for images; sampling from
σmax=80 down to σmin=0.002.
What Fθ learns. Insert x=y+σn and the coefficients:
Ftarget=σdataσ2+σdata2σy−σdata2n.
For σ→0 this tends to −n: the network predicts the noise, like ε-prediction.
For σ→∞ it tends to y/σdata: the network predicts the signal, like
x0-prediction. In between it mixes the two, and its variance is always
σ2+σdata2σ2+σ2+σdata2σdata2=1.
Interactive figure: loading…
Move the noise level σ from left to right. Top: cskip falls
from 1 to 0 around σ=σdata,cout rises like σ and
then stops at σdata, while ε-prediction (grey dashed, cout=σ)
keeps growing to 80. Bottom: the effective target has a noise part and a
signal part; their shares always add up to 1. The Monte Carlo readout
checks Var(Ftarget)≈1 with 2000 samples. Switch the data to two points:
the variance is still 1, because only Var(y)=σdata2 matters.
The skip path is the best linear denoiser. For Gaussian data y∼N(0,σdata2),E[y∣x]=σdata2+σ2σdata2x=cskipx (the Wiener
filter) and Var(y∣x)=cout2. So cskipx is already the optimal
denoiser for Gaussian data, and Fθ learns only the correction for the non-Gaussian structure.
Older choices as special cases of the EDM form
(Karras et al., Table 1; Kingma and Gao, Appendix E.2.)
ε-prediction: cskip=1,cout=−σ. Network errors are multiplied by
σ, by 80 at EDM's σmax.
x0-prediction: cskip=0,cout=1.
v-prediction is EDM with σdata=1. With at=1/1+σ2,σt=σ/1+σ2
and xt=atx:x^0=atxt−σtv^=1+σ21x−1+σ2σv^.
So cskip=1/(1+σ2) and cout=−σ/1+σ2 (the minus sign only
flips the sign of F), and the network input xt=atx is exactly cinx.
In λ form, EDM's weight in ε units is ω(σ)σ2=(e−λ+σdata2)/σdata2,
and its training density is λ=−2lnσ∼N(2.4,2.42): the EDM row of the table.
So in training, xT=αˉTx0+1−αˉTε still contains some signal.
At inference, sampling starts from xT∼N(0,I), which contains none. This is a
train–test mismatch.
The leaked signal is mostly the lowest frequency, such as the mean of each channel. Averaging D
values divides the noise by D but keeps the signal. The model learns to read the mean
brightness from xT. At inference it reads a mean near 0, so Stable Diffusion made only images
of medium brightness: it could not make a very dark or very bright image.
The fix (Lin et al.): (1) shift and rescale αˉt so that αˉT=0
while αˉ1 stays the same; (2) train with v-prediction; (3) start sampling at
t=T; (4) rescale classifier-free guidance (lesson 11) to avoid
over-exposure.
Why not ε at t=T. With aT=0,xT=ε exactly. The ε-target equals
the input, so it is trivial, and x^0=(xT−σTε^)/aT is 0/0: the network
gives no information about x0. The v-target there is v=−x0, so a v-network predicts
minus the conditional data mean, which is well defined.
EDM avoids the problem differently: it starts from N(0,σmax2I) with
σmax=80≫σdata, so the remaining mismatch is tiny.
Interactive figure: loading…
Compare the schedules and change the latent size. Blue and magenta: the channel mean of xT
in training, for a bright image (clean mean +0.5) and a dark one (−0.5). Aqua dashed: the channel
mean of pure noise, which is where sampling starts. With the Stable Diffusion schedule at 64×64
the two training curves are clearly apart, so the network learns to read the brightness from xT.
At inference the start is always near 0. Increase n: the leak gets worse, because the noise
average shrinks like 1/n but the signal does not. Zero terminal SNR makes all three curves equal.
Quick check
A text-to-image model with the "scaled linear" schedule cannot generate a fully black image. What does zero terminal SNR (with v-prediction) fix?
Why.
With SNR(T)>0, training inputs at t=T still carry the image mean, and the model learns to use it. At inference the start has mean 0, so the model "sees" a medium-brightness image. With αˉT=0 there is no signal at t=T in training either. ε-prediction is then undefined at t=T(x^0=0/0), so v-prediction is used.
Answers to section 1. (1) Yes, at every noise level. Both optima are conditional means, linked by
the conversions (Theorem 1). But the two averaged losses weight the levels differently, by a factor
SNR (Theorem 2). So with finite capacity and finite training they spend their effort
differently. (2) The factor is σt/at≈0.995/0.1≈10, so the x^0-error is
about 0.5: ten times larger. (3) It must output xT itself, because xT=ε when aT=0.
The implied x^0=(xT−σTε^)/0 is undefined, so an ε-network carries
no information about x0 at that step; use v (or x0).
Theorem 2 says the second is the first divided by SNR:0.3077⋅9/16=0.1731 ✓.
Second-order Tweedie gives the same: 0.640.36(1+0.36⋅(−0.521))=0.5625⋅0.3077=0.1731 ✓.
(c) Error conversion. Suppose the network outputs ε^=1.6: an error of 0.1 against
the optimum 1.5. (Theorem 2 holds for any two pairs on the line atx0+σtε=xt.)
NumPy and SciPy check the worked example, Tweedie's formula for non-Gaussian data, Theorem 2, the EDM
target and Theorem 3. The notebook has these cells and the exercises.
importnumpyasnpfromscipy.integrateimportquadfromscipy.statsimportnorma,s,sd=0.8,0.6,0.5# VP level (alpha_bar = 0.64) and sigma_dataxt=1.3# (a) Tweedie for Gaussian data: p_t = N(0, a^2 sd^2 + s^2)score=-xt/(a**2*sd**2+s**2)x0_hat=(xt+s**2*score)/aeps_hat=-s*scorev_hat=a*eps_hat-s*x0_hatprint(round(score,4),round(x0_hat,4),round(eps_hat,4),round(v_hat,4))# -2.5 0.5 1.5 0.9# (b) Tweedie for a two-component mixture, against Bayes' rule by quadraturew,mu,sk=np.array([0.3,0.7]),np.array([-1.0,1.5]),np.array([0.2,0.5])xm=0.7var=a**2*sk**2+s**2# each component of p_tr=w*norm.pdf(xm,a*mu,np.sqrt(var));r/=r.sum()score_m=-np.sum(r*(xm-a*mu)/var)# exact score of p_tp0=lambdax0:np.sum(w*norm.pdf(x0,mu,sk))joint=lambdax0:p0(x0)*norm.pdf(xm,a*x0,s)post_mean=quad(lambdax0:x0*joint(x0),-10,10)[0]/quad(joint,-10,10)[0]print(round(post_mean,6),round((xm+s**2*score_m)/a,6))# 1.235454 1.235454# (c) Theorem 2 on random outputs: x0- and v-errors implied by an eps-errorrng=np.random.default_rng(0)x0,eps,eps_n=rng.normal(size=(3,100_000))x_t=a*x0+s*epsx0_n=(x_t-s*eps_n)/a# the same output, in x0 formv,v_n=a*eps-s*x0,(eps_n-s*x_t)/a# ... and in v formL_eps,L_x0,L_v=[np.mean((p-q)**2)forp,qin[(eps,eps_n),(x0,x0_n),(v,v_n)]]snr=a**2/s**2print(np.isclose(L_x0,L_eps/snr),np.isclose(L_v,L_eps*(1+1/snr)))# True True# (d) EDM at the same point: sigma = s/a, x = x_t/asig=s/ac_skip,c_out=sd**2/(sig**2+sd**2),sig*sd/np.sqrt(sig**2+sd**2)print(round(c_skip,4),round(c_out,4),round(c_skip*xt/a,4))# 0.3077 0.416 0.5y,n=sd*rng.normal(size=100_000),rng.normal(size=100_000)F=(y-c_skip*(y+sig*n))/c_out# effective targetprint(round(F.var(),2))# 1.0# (e) Theorem 3: two schedules with the same end points (lambda from 10 to -10)mmse=lambdau:sd**2/(1+u*sd**2)# error of E[x0 | x_t] at SNR uforlam,dlamin[(lambdat:10-20*t,lambdat:-20.0),(lambdat:10-20*t**3,lambdat:-60*t**2)]:f=lambdat:-0.5*np.exp(lam(t))*dlam(t)*mmse(np.exp(lam(t)))print(round(quad(f,0,1,limit=200)[0],6))# 4.306938 (both)print(round(0.5*np.log((1+np.exp(10)*sd**2)/(1+np.exp(-10)*sd**2)),6))# 4.306938
In (e), for Gaussian data the best x0-error at SNR u is mmse(u)=σdata2/(1+uσdata2),
so L∞=21ln1+SNRminσdata21+SNRmaxσdata2 in closed form.
The linear and the cubic schedule give the same value. Zero terminal SNR for the Stable Diffusion
schedule and the rescaling of Lin et al.:
importnumpyasnpbetas=np.linspace(0.00085**0.5,0.012**0.5,1000)**2# Stable Diffusion "scaled linear"sab=np.sqrt(np.cumprod(1-betas))# sqrt(alpha_bar_t), t = 1..Tprint(round(sab[-1],4),round(sab[-1]**2/(1-sab[-1]**2),4))# 0.0683 0.0047# The leak: channel mean of x_T for a 64 x 64 latent channel with clean mean 0.5signal=sab[-1]*0.5noise_std=np.sqrt(1-sab[-1]**2)/64# mean of 4096 noise valuesprint(round(signal,4),round(noise_std,4),round(signal/noise_std,1))# 0.0341 0.0156 2.2# Lin et al., Algorithm 1: shift so the last value is 0, scale so the first is keptnew=(sab-sab[-1])*sab[0]/(sab[0]-sab[-1])print(np.isclose(new[0],sab[0]),new[-1])# True 0.0
A clean channel mean of 0.5 is 2.2 noise standard deviations away from 0 at t=T: easy for a
network to detect.
Image and video models. Stable Diffusion 1.x uses ε-prediction. The 768-pixel Stable
Diffusion 2 model and Imagen Video use v-prediction. Progressive distillation (Salimans and Ho
2022) needs v or x0, because ε-prediction breaks down at high noise when only a few
sampling steps are left (lesson 12).
Flow matching and rectified flow (lesson 10).
With xt=(1−t)x0+tε and the velocity target u=ε−x0, the conversions are
x^0=xt−tu^ and ε^=xt+(1−t)u^: one more linear target. With
t∼U(0,1) its weight is w(λ)=21e−λ/2, the same shape as v with
the cosine schedule (Kingma and Gao 2023, Appendix D). Stable Diffusion 3 trains rectified flow with
a logit-normal distribution of t and shifts it for higher resolutions (Esser et al. 2024).
Weather and climate. GenCast and NVIDIA's CorrDiff build on the EDM formulation: a denoiser
Dθ(x;σ) trained over noise levels σ, and samplers on a σ grid.
Inverse problems and data assimilation. Diffusion posterior sampling uses the Tweedie
estimate x^0(xt) to approximate the likelihood of observations; second-order Tweedie gives
the posterior covariance (lesson 15).
Scientific data in practice. Standardize the data and set σdata from it; do not
copy 0.5 from image models. For long records or large grids, think about the resolution shift:
more values per sample need lower SNR to destroy the coarse structure.
Mixing VP and VE variables.σ=σt/at and x=xt/at. A VP-trained network inside an
EDM-style sampler needs this rescaling of its input.
Sign conventions. Here v=atε−σtx0 (Salimans and Ho). The rectified-flow
velocity ε−x0 is a different target with a different time variable.
Converting ε to x0 at high noise.x^0=(xt−σtε^)/at divides by a
small at. Clip x^0 to the data range, or use v or x0.
Comparing loss values across parameterizations. They are in different units. Compare in one
unit, for example the x0-error at each noise level.
"The schedule does not matter." True only for the continuous-time loss with a fixed
w(λ). It always matters for the variance of training and for sampling, and for
Lsimple it is the weight.
ε-prediction with a zero-terminal-SNR schedule. The last step is undefined.
Wrong noise conditioning. Feeding a network a t or cnoise from a different schedule
than in training gives wrong denoisers at every level.
Reading D(x) as a sample. The optimal denoiser is a posterior mean. At high noise it is a
blurry average between modes, not a realistic clean sample.
VP level with αˉ=0.36(at=0.6,σt=0.8). Given xt=(0.5,−1.0) and
v^=(0.2,0.4), find x^0 and ε^.Answerx^0=atxt−σtv^=(0.3−0.16,−0.6−0.32)=(0.14,−0.92).ε^=σtxt+atv^=(0.4+0.12,−0.8+0.24)=(0.52,−0.56). Check: 0.6x^0+0.8ε^=(0.084+0.416,−0.552−0.448)=(0.5,−1.0) ✓.
At SNR=0.25, an ε-prediction has squared error 0.04. What are the squared
errors of the implied x^0 and v^ (VP)?Answerx^0:0.04/SNR=0.04/0.25=0.16.v^:0.04(1+1/0.25)=0.04⋅5=0.2.
Which w(λ) does the continuous-time ELBO use, and which does the ε-loss with
t∼U(0,1) use?AnswerThe ELBO: w(λ)=1. The ε-loss: w(λ)=p(λ)=−dt/dλ, the density of log-SNR values that the schedule produces (sech(λ/2)/(2π) for the cosine schedule).
Why does min-SNR-γ use the weight min(1,γ/SNR) on the ε-loss?AnswerIt caps the weight in x0 units at min(SNR,γ). The ε-loss equals SNR times the x0-loss (Theorem 2), so the ε-weight is min(SNR,γ)/SNR=min(1,γ/SNR). It lowers the weight of the low-noise levels.
In step 3 of the EDM derivation, why minimize the target variance over cskip?Answercout scales the target to unit variance, so cout2 equals that variance. An error of Fθ reaches Dθ multiplied by cout. The smallest variance gives the smallest cout, so network errors are amplified as little as possible.
Explain why the channel mean leaks through xT when SNR(T)>0, and why it leaks more at
higher resolution.AnswerThe channel mean of xT is αˉTxˉ0 plus the mean of D noise values, whose standard deviation is 1−αˉT/D. The signal part does not shrink with D, but the noise part does. So with more values per channel the mean is easier to read. For Stable Diffusion's schedule and 64×64 latents, a clean mean of 0.5 is 2.2 noise standard deviations from 0.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
Write x^0 in terms of xt and ε^.
x^0=(xt−σtε^)/at, from xt=atx0+σtε.
Define the v-target and recover x0 from it (VP).
v=atε−σtx0; then x0=atxt−σtv and ε=σtxt+atv.
How is the optimal ε-prediction related to the score?
E[ε∣xt]=−σt∇logpt(xt), so score =−ε^/σt.
State Tweedie's formula for xt=atx0+σtε.
E[x0∣xt]=(xt+σt2∇logpt(xt))/at.
Why do the ε-, x0- and v-networks have the same optimum?
The squared-loss optimum is the conditional mean; for fixed xt all targets are linear in (x0,ε), so their conditional means are linked by the conversions.
An ε^ has error δ. What is the error of the implied x^0?
(σt/at)δ. It blows up at high noise, where at→0.
For one output, how is ∥x0−x^0∥2 related to ∥ε−ε^∥2?
∥x0−x^0∥2=∥ε−ε^∥2/SNR. So the ε-loss is the x0-loss weighted by SNR.
Which weight w(λ) does the continuous-time ELBO put on the ε-loss, and which does Lsimple use?
The ELBO: w(λ)=1.Lsimple:w(λ)=p(λ)=−dt/dλ, the density set by the schedule.
Give EDM's cskip and cout.
cskip=σdata2/(σ2+σdata2) and cout=σσdata/σ2+σdata2.
What goes wrong when SNR(T)>0, and what is the fix?
Training inputs at t=T still carry the image mean, but sampling starts from pure noise. Fix: rescale to αˉT=0, train with v-prediction, start sampling at t=T.
"ε,x0 or v: which and why?" — The same optimum (Tweedie). Different weights over
SNR (SNR,1,1+SNR in x0 units) and different error amplification.
ε is bad at high noise, x0 at low noise; v (or EDM preconditioning) is safe at both ends.
"State and prove Tweedie's formula." — E[x0∣xt]=(xt+σt2∇logpt)/at.
Differentiate the Gaussian convolution under the integral and use Bayes' rule.
"Does the noise schedule matter?" — Not for the continuous-time ELBO, except its end points. Yes for
the variance of training (importance sampling), for losses weighted in t, and for sampling.
"Why could Stable Diffusion not make very dark images?" — SNR(T)>0 leaks the image mean
in training; at inference the start has mean 0. Fix: zero terminal SNR, v-prediction, start at t=T.
"What do cskip and cout do in EDM?" — They give a unit-variance target with the
smallest error amplification. The skip path is the Wiener filter, and the network predicts noise at
low σ and signal at high σ.
Write the six conversions (ε↔x0,v from and to both, score from ε)
and pass round-trip tests.
Tweedie for a mixture: compute D(x)=x+σ2∇logpσ(x) from the exact score and compare it with a
Monte Carlo estimate of E[x0∣x] by binning noisy samples.
Check the loss-weight identities of Theorem 2 on random predictions at several noise levels.
Write the EDM coefficients and check by Monte Carlo that the effective target has unit variance
for two different data distributions.
Compute the continuous-time ELBO of the Gaussian toy for two schedules with the same end points
(equal) and for one with a different end point (not equal).
The chapters "How diffusion models work" and "Training and score matching" cover prediction targets and noise levels; "Frequency perspective" explains why resolution changes the schedule.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Start at the chapter "Sampling and training"; the chapter "Image spectrum and tuning" connects noise schedules to image frequencies.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The chapters "Diffusion modeling" and "Training techniques" show how these choices are made in large latent diffusion models.
Open on YouTube · the player loads from youtube-nocookie.com when you press play