A chapter that uses mathematics has to teach that mathematics by making the reader compute. A chapter that only displays equations has failed, however correct the equations are.
M3Load-bearing
5/5 problems5/4 variants10/10 exercisesquota met, and enforced
The contract
5 worked problems, minimum.
4 distinct variants, and no variant more than half of them.
10 exercises, every one with a published solution.
At least one numeric problem — present.
At least one symbolic problem — present.
At least one limit or counterexample problem — present.
At least one complexity or shape problem — present.
Exact values where possible; six decimal places otherwise.
STATEMENT
Compute the log-evidence of one point under probabilistic PCA, then the
lower bound that four different encoder distributions give it, and confirm
that each gap is a KL divergence.
GIVEN
The four points of I.12 have covariance eigenvalues 4 and 1 along
u1=(0.8,0.6) and u2=(−0.6,0.8). The model has one latent
dimension, z∼N(0,1) and
x∣z∼N(wz,σ2I), at its maximum
likelihood σ2=1 and w=3u1. Take
x=(1,2). The four candidate encoder distributions are
N(0,1), N(0,41), N(23,1)
and N(23,41).
FIND
logp(x); the posterior of z; the ELBO for each candidate; the
gaps; and the two terms of the ELBO at the posterior.
STRATEGY
Use (I.13.2) for the evidence and the posterior, written in the
eigenbasis. For the ELBO, both terms of (I.13.4) have closed forms for a
Gaussian q with mean m and variance v.
SOLUTION
The evidence. The point has coordinates u1Tx=2
and u2Tx=1. The model’s covariance
wwT+I has eigenvalues 4 along u1 and
1 along u2, so its determinant is 4 and
and the reconstruction term is −log2π minus half of that. The price
of the code is 21(m2+v−1−logv) by (I.13.5). For the prior,
m=0 and v=1: the squared error averages to 8, the reconstruction
term is −log2π−4, the price is 0, and
ELBO=−5.837877.
The other three, computed the same way, are −5.031024, −4.337877 and
−3.531024.
The gaps. Subtracting from logp(x) gives 2.306853, 1.5,
0.806853 and 0. By (I.13.4) each must equal the KL divergence from
N(m,v) to the posterior N(0.866025,0.25). For the
prior,
At the posterior. The squared error averages to
5−6+3=2, so the reconstruction term is −log2π−1=−2.837877. The price
is 21(0.75+0.25−1−log0.25)=log2=0.693147. Their difference is
the evidence.
Answer
logp(x)=−3.531024; posterior N(0.866025,0.25); ELBOs
−5.837877, −5.031024, −4.337877, −3.531024; gaps 2.306853, 1.5,
0.806853, 0; at the posterior, reconstruction −2.837877 and price
0.693147.
Check — sanity
Every bound is below the evidence and the gaps are non-negative. The
correct width with mean 0 costs 1.5, which is
21(0.8660252)/0.25: with equal variances, the KL is the squared
distance between the means over twice the variance.
It checks all fifteen problems of the chapter and the numbers quoted in its
four propositions. It confirms the evidence by integrating
p(z)p(x∣z) numerically, and each gap against a separately
computed KL divergence.
Executed in CI. The digits above are the digits it printed.
Where this breaks
With a nonlinear decoder neither the evidence nor the posterior has a
closed form, and only the ELBO can be computed. The gaps above can be
seen only because this model is linear.
Variation
Every corner of the rectangle has ∣u1Tx∣=2 and
∣u2Tx∣=1, so all four have the same evidence, and
the posterior means are ±0.866025. The average log-likelihood over the
dataset is therefore also −3.531024.
Exact values where possible; six decimal places otherwise.
STATEMENT
Derive the ELBO identity, derive the KL divergence of a Gaussian code from
the standard normal and its derivatives, and show that the encoder that
maximises the ELBO is the posterior.
GIVEN
A model p(z)p(x∣z) with evidence p(x)>0, and a
distribution q(z∣x) that is positive exactly where the
posterior is. For the last part, the linear model of
I.13.B01.
FIND
(a) logp(x)−ELBO(q) as a single divergence.
(b) KL(N(μ,s2)∥N(0,1)) and its derivatives with
respect to μ and logs2.
(c) The q=N(m,v) that maximises the ELBO of the linear model, and
the ELBO there.
STRATEGY
For (a), write logp(x) as an average under q of something that
does not depend on z. For (b), average the log-ratio of densities.
For (c), differentiate the closed-form ELBO.
SOLUTION
(a) Bayes’ rule gives
p(x)=p(x,z)/p(z∣x) for every z with
positive posterior. The left side does not depend on z, so it
equals its average under q:
The first average is the ELBO and the second is
KL(q(z∣x)∥p(z∣x)). Splitting
p(x,z)=p(x∣z)p(z) in the first gives the
form in (I.13.4), with −KL(q∥p(z)) as its second term.
Since a KL divergence is non-negative and zero only for equal
distributions, the ELBO is a lower bound, tight exactly at the posterior.
Under q, (z−μ)2 averages to s2 and z2 to μ2+s2.
Therefore
KL=−21logs2−21+21(μ2+s2)=21(μ2+s2−1−logs2).
This is (I.13.5) for one coordinate. With ℓ=logs2, so that
s2=eℓ,
∂μ∂KL∂ℓ∂KL=μ,=21(s2−1).
Both vanish only at μ=0, s2=1. For example
KL(N(1,0.25)∥N(0,1))=21(0.25+1.386294)=0.818147.
(c) From B01, with a=wTx and r=∥w∥2,
the ELBO of N(m,v) is, up to a constant,
−2σ2−2am+(m2+v)r−21(m2+v−1−logv).
Setting the derivative in m to zero gives a/σ2=m(r/σ2+1),
so m=a/(r+σ2). In v it gives −r/2σ2−21+1/(2v)=0,
so v=σ2/(r+σ2). These are the posterior’s mean and variance
in (I.13.2). For the point (1,2) they are 0.866025 and 0.25, and by
(a) the ELBO there equals logp(x)=−3.531024.
Answer
(a) logp(x)−ELBO(q)=KL(q(z∣x)∥p(z∣x)).
(b) 21(μ2+s2−1−logs2), with derivatives μ and
21(s2−1). (c) m=a/(r+σ2) and v=σ2/(r+σ2),
the exact posterior, where the ELBO is −3.531024.
Check — sanity
The KL in (b) is zero at the prior and positive elsewhere: for fixed s
it grows with μ2, and s2−1−logs2≥0 with equality only at
s2=1, by logt≤t−1.
Where this breaks
If q is positive somewhere the posterior is zero, the KL in (a) is
infinite and the ELBO is minus infinity. If q is zero somewhere the
posterior is not, (a) still holds but q cannot be the posterior, so the
gap never closes. A Gaussian prior and decoder make the posterior positive
everywhere, and neither case arises. If the decoder variance also depends
on z, (c) has no closed form.
Variation
For a code with k independent coordinates the KL is a sum of k copies
of (b). At the posterior of the linear model the price is
21log((r+σ2)/σ2)+21(m2+v−1). At the
maximum-likelihood w the second part averages to zero over the
data, and at every corner of the rectangle it is exactly zero, leaving
21log4=log2.
Exact values where possible; six decimal places otherwise.
STATEMENT
Solve the linear β-VAE exactly, find its rate and distortion, and follow
them as β goes to zero and as β grows past the point where the code is
abandoned.
GIVEN
Centred data whose covariance has eigenvalues
λ1>λ2≥⋯, the top one along u1. One latent
dimension with prior N(0,1), a decoder
x∣z∼N(wz,σ2I) with σ2 fixed, and
an encoder N(m,v) chosen freely for each input. The objective is
the average of Eqlogp(x∣z)−βKL(q∥N(0,1)).
For numbers use the four points of I.12: λ1=4, λ2=1,
σ2=1.
FIND
(a) The best encoder for a given w. (b) The best ∥w∥2.
(c) The rate and the mean squared distortion there. (d) The limits
β→0 and βσ2≥λ1. (e) A table for the four
points at β=0.25, 1, 2 and 4.
STRATEGY
Write a=wTx, r=∥w∥2 and c=r+βσ2.
Optimise the encoder first; what is left depends on w only through
r and its direction.
SOLUTION
(a) For one input, up to terms that do not involve m or v, the
objective is
2σ22am−(m2+v)r−2β(m2+v−1−logv).
Setting the derivatives in m and v to zero,
m=ca,v=cβσ2.
(b) Substituting, the terms in a2 combine to a2/(2σ2c) and
the terms in v to 2βlog(βσ2/c), up to constants.
Averaged over the data, E[a2]=wTSw, which for
a given r is largest, rλ1, when w lies along u1.
What remains is
J(r)=2σ2crλ1−2βlogc+const,
with derivative
J′(r)=2cβ(cλ1−1).
It is positive while c<λ1 and negative after, so
r=max(λ1−βσ2,0).
(c) While the code is in use, c=λ1. Then E[m2]=r/λ1
and v=βσ2/λ1, and the average KL is
R=21(λ1r+βσ2−1−logλ1βσ2)=21logβσ2λ1.
The squared error averages to
trS−2r+(E[m2]+v)r=trS−r, so
D=i>1∑λi+βσ2.
(d) As β→0, r→λ1, the encoder’s variance
βσ2/λ1 goes to zero, the rate grows like
−21logβ without bound, and the distortion falls to
∑i>1λi, the loss of the principal-axis autoencoder of
I.12.T1. The code tends to u1Tx/λ1:
the principal score, scaled to the prior’s unit variance.
As βσ2 rises to λ1, r and R fall to zero and D
rises to trS. For any larger β, J′(r)<0 for
all r≥0: the optimum is w=0, the encoder returns
N(0,1) for every input, and the code is ignored.
(e) With λ1=4, λ2=1 and σ2=1, R=21log(4/β)
and D=1+β:
β
r
v
R
D
0.25
3.75
0.0625
1.386294
1.25
1
3
0.25
0.693147
2
2
2
0.5
0.346574
3
4
0
1
0
5
Answer
m=a/(r+βσ2) and v=βσ2/(r+βσ2);
∥w∥2=max(λ1−βσ2,0) along u1; while in use,
R=21log(λ1/βσ2) and
D=∑i>1λi+βσ2. As β→0, D→∑i>1λi
and R→∞; from βσ2=λ1 on, the code is unused, with
R=0 and D=trS.
Check — sanity
At β=1 the encoder is the exact posterior of (I.13.2), r=3 is
probabilistic PCA’s ∥w∥2, and the objective equals the
log-evidence −3.531024 of every corner. The curve is continuous at the
threshold: 1+4=5=trS.
Where this breaks
The solution needs σ2 fixed. If it is learned, at β=1 it
settles at the average discarded eigenvalue, and a direction collapses
when its eigenvalue does not exceed that. With several latent dimensions
the same threshold applies to each eigen-direction separately.
Variation
At β=8 the code is unused, as at β=4. At β=0.5 the rate
is 21log8=1.039721 and the distortion 1.5. Halving β
always adds 21log2=0.346574 nats to the rate.
Exact values where possible; six decimal places otherwise.
STATEMENT
Show that the pathwise and score-function estimators of one gradient are
both unbiased, compute their variances, and follow them as the encoder’s
distribution narrows.
GIVEN
q=N(μ,s2), z=μ+sε with
ε∼N(0,1), and f(z)=z2. The target is
∂Eq[f(z)]/∂μ. The moments
E[ε2]=1, E[ε4]=3 and E[ε6]=15,
and odd moments vanish.
FIND
Both one-draw estimates, their means, their variances in general and at
μ=1, s=0.5, and their behaviour as s→0.
STRATEGY
Write each estimate as a polynomial in ε and take moments term
by term.
SOLUTION
The target.Eq[z2]=μ2+s2, so the derivative is 2μ.
Pathwise. By (I.13.6) the estimate is
gp=f′(z)=2(μ+sε). Its mean is 2μ and its
variance is
Vargp=4s2.
Score function. By (I.13.7), with
∂logq/∂μ=(z−μ)/s2=ε/s,
gs=s(μ+sε)2ε.
Expanding, (μ+sε)2ε=μ2ε+2μsε2+s2ε3, whose mean is
2μs. So Egs=2μ: unbiased. For the second moment,
(μ+sε)4ε2=μ4ε2+4μ3sε3+6μ2s2ε4+4μs3ε5+s4ε6,
which averages to μ4+18μ2s2+15s4. Dividing by s2 and
subtracting the squared mean,
Vargs=s2μ4+18μ2s2+15s4−4μ2.
The numbers. At μ=1, s=0.5 the pathwise variance is 1 and the
score-function variance is (1+4.5+0.9375)/0.25−4=21.75.
As s→0. The pathwise variance 4s2 goes to zero. The
score-function variance behaves like μ4/s2 and grows without bound:
s
pathwise
score function
0.5
1
21.75
0.1
0.04
114.15
0.01
0.0004
10,014
Answer
Both have mean 2μ. Variances 4s2 and
(μ4+18μ2s2+15s4)/s2−4μ2: at μ=1, s=0.5, 1 against
21.75. As s→0 the first vanishes and the second grows like
μ4/s2.
Check — sanity
At μ=0 the score-function variance is 15s2, finite and small, while
the pathwise variance is 4s2. The score function is worst when f is
large where q sits, because it multiplies f itself by
ε/s.
Where this breaks
The comparison depends on f. For an integrand that oscillates quickly,
such as sin(10z) in
I.13.X10, the order
reverses. A baseline subtracted from f can reduce the score function’s
variance a great deal without biasing it.
Variation
Subtract the baseline b=Eqf=μ2+s2 from f before multiplying by
ε/s. The estimate stays unbiased because
E[ε/s]=0. The estimate becomes
2με2+s(ε3−ε), with variance
8μ2+10s2. At μ=1, s=0.5 that is 10.5: half of 21.75, and
still ten times the pathwise 1.
Three significant figures for sample counts; six decimal places otherwise.
STATEMENT
Count how many prior samples an unbiased estimate of the evidence needs,
in one dimension and in ten, and set that against the cost of one ELBO
step.
GIVEN
The estimate is the average of p(x∣zs) over n draws zs
from the prior. By (I.13.3) one draw has relative variance
r=∫p(z∣x)2/p(z)dz−1, and for a posterior
N(μ,s2) with s2<2 the integral is
eμ2/(2−s2)/(s2−s2). Use the point (1,2) under the model
of I.13.B01, and again with
noise variance 0.01, where ∥w∥2=3.99. The target is a relative
standard error of ten per cent, so n=r/0.12. For the encoder, take a
hidden layer of 400 units and a code of k=20 dimensions.
FIND
r and n for both noise levels in one dimension; n in ten independent
latent dimensions; the cost of one ELBO step; the size of the encoder’s
output layer.
STRATEGY
Get the posterior from (I.13.2), apply the formula, and use the fact that
for independent dimensions the integral in (I.13.3) factorises.
SOLUTION
Noise variance 1. The posterior is N(0.866025,0.25), so
μ2=0.75 and 2−s2=1.75:
Noise variance 0.01. The posterior variance is
0.01/(3.99+0.01)=0.0025, a standard deviation of 0.05, and its mean is
23.99/4=0.998749. The same formula gives r=22.316431 and
n=2,230. Narrowing the posterior by a factor of ten multiplied the
cost by about seventeen.
Ten dimensions. For a factorised prior and posterior the integral is
a product, one factor 1+r per dimension:
1+r10=(1+r)10.
At noise variance 1 that is 2.32079710=4,530, so
n=4.53×105. At 0.01 it is 23.31643110=4.75×1013,
so n=4.75×1015. The cost is exponential in the number of latent
dimensions.
One ELBO step. The bound needs one draw from the encoder’s
distribution per example: one encoder pass, one draw of
ε, one decoder pass, and the closed form (I.13.5) for the
KL. Its cost does not grow with k except through the width of two
layers.
The encoder’s output layer. It returns μ and logσ2,
2k=40 numbers, from 400 hidden units: 400⋅40+40=16,040
parameters, twice what a deterministic code of the same width would need.
Answer
r=1.320797 and n≈132 at noise variance 1; r=22.316431 and
n≈2,230 at 0.01. In ten dimensions, n≈4.53×105
and 4.75×1015. One ELBO step costs one encoder and one decoder
pass per example. The output layer has 16,040 parameters.
Check — sanity
If the posterior were the prior, μ=0 and s=1, the formula gives
1+r=1/(1⋅1)=1 and r=0: every draw would return p(x) exactly.
The cost comes entirely from the posterior differing from the prior.
Where this breaks
The closed form needs s2<2; a posterior wider than twice the prior’s
variance makes the integral infinite. Real posteriors are not factorised,
and the exponent of the dimension count then depends on their shape, but
the growth remains exponential in general.
Variation
Draw from the posterior instead and weight each draw by
p(z)/p(z∣x). Every draw then returns p(x) exactly and the
variance is zero. An encoder close to the posterior is a cheap
approximation to that ideal proposal.
Exercises
Every one has a published solution. A hidden solution is a solution; a missing one is an abandonment.
I.13.X01A Gaussian KL by hand, both ways roundnumeric▲△△
Compute KL(N(1,0.25)∥N(0,1)) with (I.13.5). Then
compute KL(N(0,1)∥N(1,0.25)) with the general
formula
21(v1/v2+(m1−m2)2/v2−1+log(v2/v1)).
Which direction does a VAE’s loss use, and what does the asymmetry mean?
Hint
The first is a narrow distribution measured against a wide one, the second
a wide one against a narrow one.
The VAE’s loss uses the first direction: the encoder’s distribution
measured against the prior, KL(q∥p). It is cheap for q to be
narrower than the prior and costly to put mass where the prior has
little. The reverse direction punishes the narrow distribution for every
place the wide one reaches and it does not, so it is more than three times
larger here.
The same direction appears in the gap of (I.13.4), KL(q∥p(z∣x)).
That is why an encoder fitted by the ELBO tends to sit inside the
posterior rather than cover it, the effect
I.13.X03 measures.
I.13.X02One reparameterised gradientgradient▲△△
Use the model of I.13.B01:
w=3u1, σ2=1, x=(1,2), so
wTx=23 and ∥w∥2=3. The encoder returns
m=0.5 and s=0.5, and the draw is ε=0.6. Compute the
one-draw gradient of the ELBO with respect to m and s, then the exact
gradient with respect to m.
Hint
The reconstruction term is −∥x−wz∥2/2 plus a constant, with
z=m+sε. The KL term is 21(m2+s2−1−logs2).
Solution
The code is z=0.5+0.5⋅0.6=0.8. The derivative of the reconstruction
term with respect to z is
wT(x−wz)=23−3⋅0.8=1.064102.
Since ∂z/∂m=1 and ∂z/∂s=ε,
and the KL has derivatives m and s−1/s,
The exact gradient with respect to m averages over ε. The
reconstruction derivative is linear in z, so its average is its value at
z=m: 23−1.5=1.964102. Subtracting the KL’s 0.5 gives
1.464102. The single draw underestimated it by 0.9, because
ε=0.6 moved the code above its mean; an ε of
−0.6 would have overestimated it by the same amount.
Both one-draw derivatives point uphill. An unbiased estimate need not be
close; it needs only to be right on average, which is all stochastic
gradient ascent requires.
I.13.X03A factorised encoder misreports its uncertaintycounterexample▲▲△
A two-dimensional posterior is N(0,Σ) with unit
variances and correlation ρ=0.9. The encoder’s family is factorised:
q=N(m,diag(v1,v2)). Find the member of the
family that minimises KL(q∥p), the variance it reports for each
coordinate, and the gap that remains.
The mean term is minimised at m=0. With Σq
diagonal, only the diagonal of Σ−1 enters the trace:
KL=21(1−ρ2v1+v2−2+log(1−ρ2)−logv1−logv2).
Each vi appears in its own term. Setting
1/(1−ρ2)−1/vi=0 gives vi=1−ρ2=0.19. Substituting, the trace term becomes 2 and cancels
the −2:
KL=21(log0.19−2log0.19)=−21log0.19=0.830366.
So the best factorised encoder reports a variance of 0.19 for each
coordinate, whose true posterior variance is 1. It matches the
conditional variance of one coordinate given the other, not the marginal
one, and so understates the uncertainty more than fivefold.
The claim this breaks is that a trained encoder’s variance measures how
uncertain the code is. The gap of 0.830366 nats is the
approximation gap: it does not shrink with training, because no member of
the family is closer. Only a richer family, one with correlations, can
remove it.
I.13.X04A point code has unbounded ratelimit▲▲△
Keep the encoder’s mean at 0.866025, the posterior mean of the point
(1,2), and shrink its standard deviation s. Compute the price of the
code, KL(N(0.866025,s2)∥N(0,1)), at s=0.1,
0.01 and 0.001. How fast does it grow, and what does the limit say
about the autoencoder of Chapter I.12?
Hint
Only the −logs2 term of (I.13.5) grows as s→0.
Solution
With μ2=0.75, (I.13.5) gives 21(s2−0.25−logs2):
s
price of the code
0.1
2.182585
0.01
4.480220
0.001
6.782756
Each tenfold narrowing adds log10=2.302585 nats, apart from the vanishing
s2 term: the price grows like −logs without limit.
A deterministic encoder is the limit s→0, so its rate is infinite. The
autoencoder of I.12 is a VAE whose code costs nothing because its price is
never charged. Proposition 4 reaches the same corner from the other side:
in the linear model the encoder’s variance is βσ2/λ1,
which goes to zero only as β→0, and the rate
21log(λ1/βσ2) diverges with it.
The noise in the code is what makes the space between codes trained. A
point code puts the decoder back where it was in I.12.
I.13.X05Where Jensen's inequality is tightsymbolic▲▲△
Derive the ELBO from Jensen’s inequality, and state exactly when the
inequality is an equality. Then check the condition for the model of
I.13.B01: show that
logp(x,z)−logp(z∣x) takes the same value at every z,
and find it.
Hint
The logarithm is strictly concave, so logE[Y]=E[logY] only when Y
is constant.
Solution
Write Y=p(x,z)/q(z∣x) with z∼q. Its mean
is ∫p(x,z)dz=p(x), so by Jensen’s inequality
(Jensen's inequality· 0.IT.05)
logp(x)=logEq[Y]≥Eq[logY]=ELBO(q).
Because log is strictly concave, equality holds exactly when Y is the
same number for every z that q can draw. Then
q(z∣x)∝p(x,z), and normalising gives
q=p(x,z)/p(x), the posterior. The constant is p(x).
For the linear model, with q the posterior, Y is
p(x,z)/p(z∣x), which by Bayes’ rule is p(x) at every
z. The script evaluates
logp(z)+logp(x∣z)−logN(z;0.866025,0.25) at
z=−1, 0, 0.5 and 2 and gets −3.531024 each time, the
log-evidence.
For any other q the ratio varies with z, and the spread of
logY is what Jensen’s inequality charges. The first derivation in
proposition 2 shows that the charge is exactly
KL(q∥p(z∣x)).
I.13.X06The rate splits into information and a mismatchprobability▲▲△
At β=1 the four points have encoder distributions
N(±0.866025,0.25), two corners at each sign. Find the
average KL to the prior, the mutual information between the example and
its code, and the KL from the aggregate posterior to the prior, and check
that (I.13.9) holds. Compare the aggregate posterior with the prior at
z=0.
Hint
Two corners share each distribution, so the information between the
example and the code equals the information between the sign and the code.
That is H(q(z))−H(q(z∣sign))
(Mutual information· 0.IT.04). The first entropy has no closed form;
integrate it numerically.
Solution
Average KL. Every corner has m2=0.75 and v=0.25, so each KL is
21(0.75+0.25−1−log0.25)=log2=0.693147, and so is the average.
Information. The aggregate posterior is the mixture
q(z)=21N(0.866025,0.25)+21N(−0.866025,0.25).
Each component has entropy
21log(2πe⋅0.25)=0.725791. Numerical integration of
−qlogq gives the mixture’s entropy, and the difference is
Iq(n;z)=0.585939.
It is below log2=0.693147, the most a binary sign could carry, because
the two components overlap.
Mismatch.KL(q(z)∥N(0,1))=−H(q)+21log2π+21Eq[z2], and
Eq[z2]=0.75+0.25=1. Numerically it is 0.107208.
The identity.0.585939+0.107208=0.693147, the average KL. Most of
the rate pays for information about the example; about a sixth pays for
the aggregate posterior not being the prior.
At z=0. The mixture’s density is
2⋅21N(0;0.866025,0.25)=0.178032, and the prior’s is
1/2π=0.398942. The mixture has the prior’s variance but not its
shape. Codes drawn from the prior near zero, which are the commonest, are
codes the encoder produced less than half as often. That is a hole, and
the second term of (I.13.9) is its price.
I.13.X07A mean reduction weights the KL by the input widthshape▲▲△
A VAE for 28×28 images in [0,1] has a batch of B inputs of
shape (B,1,28,28), a code of width k=32, and a Gaussian decoder with
σ2=21, so that −logp(x∣z) is the summed
squared error plus a constant. Give the shape of every tensor from the
encoder’s output to the loss. Then compare the correct loss with the one
written as the mean squared error over all elements plus the KL summed
over the code and averaged over the batch. What β does the second
one train with, and by I.13.T3 what happens to the code?
Hint
A mean over all elements divides by B⋅784. The KL was divided only
by B.
Solution
Shapes. The encoder’s last layer returns (B,64), split into
μ and logσ2, each (B,32). The noise
ε and the code z are (B,32). The decoder returns
(B,784), compared with the input flattened to (B,784). The KL by
(I.13.5) is summed over the 32 coordinates to give (B,), as is the
squared error over the 784 pixels, and the loss averages both over the
batch.
The two losses. The correct negative ELBO, up to a constant, is
B1b∑(i=1∑784(xbi−x^bi)2+j=1∑32KLbj).
The second divides the squared error by B⋅784 and the KL by B.
Multiplying it by 784 gives the correct reconstruction term plus 784
times the KL. The two have the same minimisers only if β=784.
What happens. In the linear model of I.13.T3 a direction survives only
if its variance exceeds βσ2=392. A pixel in [0,1] has
variance at most 41, so the total variance of all 784 pixels is
at most 196, and no direction exceeds that. Every direction collapses:
the encoder returns the prior and the decoder learns the mean image.
A nonlinear VAE is not bound by this exactly, but the pressure is the
same, and blurred reconstructions with a KL near zero are the usual
symptom. The shapes were all correct. Only the reductions disagreed.
I.13.X08A discrete code has no pathwise derivativecounterexample▲▲△
A one-bit code is z∼Bernoulli(π), and
f(z)=(z−0.3)2. Find the true derivative of E[f(z)] with respect to
π. Write the draw as z=1[u<π] with u uniform on
(0,1), and find what differentiating through it returns. Then find the
score-function estimate at π=0.5, its mean and its variance.
Hint
E[f(z)]=πf(1)+(1−π)f(0). The function 1[u<π] is a step
in π.
Solution
The truth.f(1)=0.49 and f(0)=0.09, so the derivative is
f(1)−f(0)=0.4.
Through the draw. For fixed u, 1[u<π] is constant in
π except at π=u, where it jumps. Its derivative is zero with
probability one, so backpropagating through the draw returns 0. The
claim broken is that writing a draw as a function of noise always gives a
usable pathwise gradient: the function must be differentiable, and a step
is not.
Score function.logq(z)=zlogπ+(1−z)log(1−π), so its
derivative is z/π−(1−z)/(1−π). At π=0.5 the estimate is
f(1)/0.5=0.98 when z=1 and −f(0)/0.5=−0.18 when z=0. Its mean is
21(0.98−0.18)=0.4, correct, and its variance is
21(0.98−0.4)2+21(−0.18−0.4)2=0.3364.
The standard deviation 0.58 exceeds the gradient itself, which is
typical of the score function. Relaxations such as the Gumbel-softmax
replace the step by a smooth function of the noise, accepting bias for a
pathwise gradient.
I.13.X10The pathwise estimator is not always the quieter onecounterexample▲▲△
Let q=N(μ,1) at μ=0 and f(z)=sin(10z). Find the true
derivative of Eq[f(z)] with respect to μ, and the variances of the
pathwise estimate f′(z) and the score-function estimate
f(z)(z−μ).
Hint
For z∼N(μ,1), E[cos(ωz)]=cos(ωμ)e−ω2/2.
Use cos2t=21(1+cos2t).
Solution
The truth.Eq[sin(10z)]=sin(10μ)e−50, so the derivative at
μ=0 is 10e−50=1.9×10−21: zero to any printed precision.
Pathwise. The estimate is 10cos(10z). Its mean is the derivative,
and its second moment is
100E[cos2(10z)]=50(1+e−200). Its variance is therefore
50 to every printed digit.
Score function. The estimate is sin(10z)z. Its mean is also the
derivative. Its second moment is
E[sin2(10z)z2]=21E[z2]−21E[z2cos(20z)], and the
second average is −399e−200, so the variance is 0.5 to every
printed digit.
So the pathwise estimate is a hundred times noisier here. It reads the
slope of f at the draw, and a function that oscillates ten times per
unit has slopes of size ten everywhere, which cancel only on average. The
score function reads the value of f, which never exceeds one. The claim
broken is that reparameterisation always lowers the variance. It does for
the smooth, slowly varying log-likelihoods of a VAE decoder; it does not
in general.
I.13.X09The rate is information plus mismatch, for any encoderproof▲▲▲
Let the training set have N examples, chosen with equal probability
q(n)=1/N, and let the encoder give q(z∣n). Prove (I.13.9):
the average of KL(q(z∣n)∥p(z)) equals
Iq(n;z)+KL(q(z)∥p(z)), with
q(z)=N1∑nq(z∣n). Deduce that a zero average KL
means a code that carries no information, and that the rate bounds the
information from above.
Hint
Insert q(z) into the log-ratio:
logp(z)q(z∣n)=logq(z)q(z∣n)+logp(z)q(z).
Solution
The identity. Average the split in the hint over q(n,z)=q(n)q(z∣n):
In the first term,
q(z∣n)/q(z)=q(n,z)/(q(n)q(z)), so it is
KL(q(n,z)∥q(n)q(z))=Iq(n;z)
(Mutual information· 0.IT.04). The second term depends on z
alone, and the marginal of q(n,z) over z is q(z), so
it is KL(q(z)∥p(z)). ■
Zero rate means no information. Both terms on the right are
non-negative. If the left side is zero, both are zero. Then
Iq(n;z)=0: the code is independent of the example, so it tells the
decoder nothing about which input it came from. Equivalently, each
KL(q(z∣n)∥p) is non-negative and their average is zero, so
each is zero and every q(z∣n) is the prior. That is posterior
collapse.
The rate bounds the information. Dropping the non-negative second term
gives Iq(n;z)≤N1∑nKL(q(z∣n)∥p). The rate
is an upper bound on what the code says about the example. Since n takes
N values, the information is also at most logN; any rate above that
is spent on the mismatch.
The identity holds for every encoder, not only at an optimum. The script
checks it for three encoders on the four points, including the collapsed
one, where both sides are zero.