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
Run a width-one linear autoencoder by hand on four points. Then improve the
decoder alone, and after it the encoder alone, using their closed forms.
GIVEN
The data are (1,2), (2.2,0.4), (−1,−2) and (−2.2,−0.4). They are
centred, with
StrS=[2.921.441.442.08],=5.
The autoencoder starts at E=(1,0) and D=(1,0)T:
it keeps the first coordinate and writes it back.
FIND
The four codes, the reconstructions and the loss. The best decoder for this
encoder and its loss. The best encoder for that decoder and its loss. Where
the sequence goes if the two repairs are alternated.
STRATEGY
Evaluate (I.12.1) directly, then apply the two lines of (I.12.3) in turn.
After a decoder repair the loss is
trS−tr(DES).
SOLUTION
The starting point. The codes are the first coordinates:
1, 2.2, −1 and −2.2. Each reconstruction is (z,0), so each error
is the square of the second coordinate: 4, 0.16, 4 and 0.16. The
loss is their mean, 2.08. That is S22, the variance of the
coordinate that was dropped.
Repair the decoder. With E=(1,0) the code variance is
ESET=2.92 and
SET=(2.92,1.44)T. The first line of
(I.12.3) gives
D=2.921[2.921.44]=[10.493151].
The decoder now predicts the second coordinate from the first by
regression. The loss falls to
5−2.922.922+1.442=5−2.9210.6=1.369863.
Repair the encoder. For this decoder
DTD=1.243198, and the second line of (I.12.3) gives
E=1.243198DT=(0.804377,0.396679).
The autoencoder is now the projection onto the direction of D. By
(I.12.4) its loss is 5 minus the variance along that direction, which is
1.101887.
Alternate. Two more repairs give 1.026137 and 1.006577, and the
sequence converges to 1.
The reason is visible in the two formulas. The decoder repair sets
D proportional to SET, and the encoder repair
sets E proportional to DT. One full sweep
therefore multiplies the direction by S, which is the power method.
The starting direction (1,0) equals 0.8u1−0.6u2 for the
eigenvectors u1=(0.8,0.6) and u2=(−0.6,0.8) of S,
with eigenvalues 4 and 1. Each sweep divides the tangent of the angle
to u1 by 4: it is 0.75, then 0.1875, then 0.046875. A
projection at angle φ to u1 has loss 1+3sin2φ,
which reproduces 1.101887 and 1.006577.
Answer
Codes (1,2.2,−1,−2.2) and loss 2.08. Best decoder
(1,0.493151)T with loss 1.369863. Best encoder
(0.804377,0.396679) with loss 1.101887. Then 1.026137, 1.006577,
and the limit 1, the smaller eigenvalue of S.
Check — sanity
The losses decrease at every step, because each repair is an exact
minimisation over one matrix with the other held fixed. None falls below
1, the bound of I.12.T1.
It checks all fifteen problems of the chapter and the numbers quoted in its
four propositions. Its first line reports the losses above. Before that it
confirms that S has eigenvalues 4 and 1 with the stated
eigenvectors, and later that the gradients of (I.12.2) match central finite
differences for untied and for tied weights.
Executed in CI. The digits above are the digits it printed.
Where this breaks
Start the encoder at u2T=(−0.6,0.8). Then
SET is a multiple of u2, the repaired decoder
is u2 again, and the alternation never moves. The loss stays at
4. This is the saddle of I.12.T2, and an exact start on it is the only
way to remain there.
Variation
Keep the second coordinate instead, E=(0,1) and
D=(0,1)T. The loss is S11=2.92, worse than before
because the first coordinate has the larger variance. One decoder repair
gives D=(0.692308,1)T and a loss of
5−6.4/2.08=1.923077.
Exact values where possible; six decimal places otherwise.
STATEMENT
Prove that the smallest loss of a linear autoencoder is the sum of the
discarded eigenvalues, find every pair of matrices that attains it, and
evaluate the result on four points.
GIVEN
Centred data with covariance
S=∑i=1dλiuiuiT, where
λ1≥⋯≥λd>0 and the ui are orthonormal. The
loss is the first line of (I.12.2), with
E∈Rk×d, D∈Rd×k and k<d. Assume
λk>λk+1. For the numbers, use the four points of
I.12.B01 with k=1.
FIND
(a) The best encoder for a fixed decoder, and the end-to-end map it gives.
(b) The loss of that map in terms of an orthonormal basis.
(c) The lower bound and the condition for equality.
(d) All minimising pairs.
(e) The eigenvalues, the optimal map, its loss and two different
factorisations for the four points.
(f) The other stationary projection and a path that leaves it downhill.
STRATEGY
Reduce pairs of matrices to subspaces by solving for the encoder. Then
write the retained variance as a weighted sum of eigenvalues and bound the
weights.
SOLUTION
(a) Fix D with independent columns. For one input, the
derivative of ∥x−Dz∥2 with respect to z is
−2DT(x−Dz). It vanishes at
z=(DTD)−1DTx,
which is the unique minimum because DTD is positive
definite. The same matrix serves every input, so
E=(DTD)−1DT and
DE=D(DTD)−1DT=P.
Direct multiplication gives P2=P and
PT=P: the orthogonal projection onto the columns of
D. No other encoder gives any input a better code, so no encoder
has a smaller loss than P. Because λd>0 the data span every
direction, and a different encoder gives some of them a strictly worse one.
(b) Choose Q∈Rd×k with orthonormal columns spanning
the same subspace. Then P=QQT, and
I−P is symmetric with (I−P)2=I−P.
Moving one factor around the trace,
L(P)=tr((I−P)S(I−P))=tr(S(I−P))=trS−tr(QTSQ).
This is (I.12.4).
(c) Substitute the eigendecomposition:
tr(QTSQ)ci=i=1∑dλici,=∥QTui∥2.
Since QQT is a projection and ui a unit
vector, 0≤ci≤1. Since the ui are an orthonormal basis,
∑ici=tr(QTQ)=k. Therefore
∑i≤k(1−ci)=∑i>kci, and
So the retained variance is at most λ1+⋯+λk, and
L≥i>k∑λi.
Because λk>λk+1, equality requires ci=0 for every
i>k, hence ci=1 for every i≤k. A unit vector whose projection
has length one lies in the subspace. The subspace is therefore the span of
u1,…,uk, and P=UkUkT.
(d) A decoder attains the bound exactly when its columns span that
subspace, that is, D=UkG with
G=UkTD invertible. By (a) the encoder must then
be
E=(GTG)−1GTUkT=G−1UkT.
Every invertible G gives a minimiser, and these are all of them.
This is (I.12.5).
A decoder of rank below k spans fewer than k dimensions. Its retained
variance is at most λ1+⋯+λk−1, which is strictly
less because λk>0.
(e) For the four points, S has trace 5 and determinant
2.92⋅2.08−1.442=4, so its eigenvalues solve
λ2−5λ+4=0: they are 4 and 1. Solving
(S−4I)u=0 gives u1=(0.8,0.6), and then
u2=(−0.6,0.8). With k=1,
DEL=u1u1T=[0.640.480.480.36],=λ2=1.
For the point (1,2) the code under G=1 is 0.8+1.2=2, the
reconstruction is (1.6,1.2) and the residual is (−0.6,0.8), of squared
length 1. The same holds at every corner.
Two factorisations of the same map: G=1 gives
D=(0.8,0.6)T and E=(0.8,0.6). G=1/5
gives D=(0.16,0.12)T and E=(4,3).
(f) Setting D=u2 and E=u2T makes
both gradients of (I.12.2) vanish, with loss λ1=4. Rotate the
direction to cosθu2+sinθu1. By (b) the loss
is 5−(cos2θ+4sin2θ)=4−3sin2θ. At θ=0.1 it is
3.970100. The point is stationary and is not a minimum.
Answer
The minimum is ∑i>kλi, attained exactly by
D=UkG, E=G−1UkT for
invertible G. For the four points the eigenvalues are 4 and 1,
the optimal map is [0.640.480.480.36] and
the loss is 1. The projection onto u2 is a saddle with loss 4.
Check — sanity
The optimal map has trace 1, the dimension of the subspace, and squares
to itself: its first entry is 0.642+0.482=0.64. The retained variance
4 and the loss 1 add to the total 5.
Where this breaks
With λk=λk+1 the last step fails: weight can move between
the tied directions without changing the sum, and the optimal subspace is
not unique
(I.12.X05). With a
zero eigenvalue the encoder is determined only on the directions in which
the data vary. With uncentred data S is not a covariance and the
subspace found is a different one
(I.12.X03).
Variation
The script scans 3600 directions and confirms that the loss of the
projection onto each lies between 1 and 4, the two eigenvalues. For
k=d the bound is an empty sum: the loss is 0 and the map is the
identity
(I.12.X04).
Exact values where possible; six decimal places otherwise.
STATEMENT
Find the linear map that best removes noise of variance σ2 from four
points, with and without a bottleneck, and follow it as the noise goes to
zero and to infinity.
GIVEN
The four points of
I.12.B01, with
eigenvalues λ=(4,1) and eigenvectors u1=(0.8,0.6),
u2=(−0.6,0.8). The input is corrupted as
x~=x+ε, where ε is
independent of x with mean zero and covariance σ2I. The
loss is E∥Mx~−x∥2.
FIND
(a) The optimal gain on each principal direction and the loss it leaves.
(b) The optimal map and its loss at σ2=1.
(c) The same when M must have rank one.
(d) The limits σ2→0 and σ2→∞, with values at
σ2=0.01 and 100.
(e) A check of (I.12.8) for Gaussian data.
STRATEGY
Use the decomposition in (I.12.7). The optimal map shares the eigenvectors
of S, so the problem separates into one scalar problem per direction.
SOLUTION
(a) By (I.12.7) the loss is the clean reconstruction loss of
M plus σ2∥M∥F2, and its minimiser
S(S+σ2I)−1 has the eigenvectors of S. Write
M=∑igiuiuiT. The loss becomes
∑i[(1−gi)2λi+σ2gi2]. Each term is a parabola
in its own gi, with derivative −2(1−gi)λi+2σ2gi:
gilossi=λi+σ2λi,=λi+σ2λiσ2.
A direction is kept at exactly one half when the noise variance equals its
own.
(b) At σ2=1 the gains are 4/5 and 1/2. The two
projections are
(c) A rank-one map keeps one direction. Keeping direction i costs
λiσ2/(λi+σ2) there and the full λj on
the other. Keeping u1 costs 0.8+1=1.8; keeping u2 costs
0.5+4=4.5. So the best rank-one map is
0.8u1u1T, with loss 1.8.
The bottleneck chooses the direction, as in I.12.T1. The noise then shrinks
what is kept.
(d) As σ2→0 each gain tends to 1, the map tends to the
identity and the loss to 0. Without noise, nothing stops the copy. As
σ2→∞ each gain behaves like λi/σ2 and tends
to 0; the map outputs the mean, and the loss tends to
trS=5.
σ2
0.01
1
100
g1
0.997506
0.8
0.038462
g2
0.990099
0.5
0.009901
loss
0.019876
1.3
4.836253
(e) If x is Gaussian with mean zero and covariance S, then
x~ is Gaussian with covariance S+σ2I and
score −(S+σ2I)−1x~. Then (I.12.8) gives
x~−σ2(S+σ2I)−1x~=S(S+σ2I)−1x~,
which is the map of (a). For Gaussian data the best denoiser is linear, and
the two results are one.
Answer
Gains λi/(λi+σ2) with loss
∑iλiσ2/(λi+σ2). At σ2=1:
M⋆=[0.6920.1440.1440.608] with
loss 1.3, or 0.8u1u1T with loss 1.8 at rank
one. The map tends to the identity as σ2→0 and to zero as
σ2→∞.
Check — sanity
At σ2=1, copying the corrupted input costs
σ2∥I∥F2=2 and outputting the mean costs
trS=5. The optimum, 1.3, is below both. Every gain
lies strictly between 0 and 1.
Where this breaks
Noise with a general covariance N gives
S(S+N)−1, which shares eigenvectors with S only
if the two matrices commute. And for data that are not Gaussian the best
denoiser is not linear at all:
I.12.X08 finds a
tanh.
Variation
At σ2=4 the gains are (0.5,0.2) and the loss is 2+0.8=2.8.
The script also confirms the decomposition of (I.12.7) exactly, by
averaging over the four points and four noise vectors
(±σ2,0), (0,±σ2), whose covariance is
σ2I.
Exact values where possible; six decimal places otherwise.
STATEMENT
Show that a narrow code does not, by itself, stop an autoencoder from
memorising its training set.
GIVEN
The training set is the four points (1,2), (2.2,0.4), (−1,−2) and
(−2.2,−0.4), the corners of a rectangle. The encoder keeps the first
coordinate, z=x1. The decoder returns g(z)=(z,g2(z)). Its second
output is piecewise linear, with relu(a)=max(a,0):
g2(z)=−34z−310+310relu(z+1)−310relu(z−1).
For comparison, the principal-axis autoencoder of
I.12.B02 projects
onto u1=(0.8,0.6). The held-out set is the midpoints of the
rectangle’s four sides: (−0.6,0.8), (0.6,−0.8), (1.6,1.2) and
(−1.6,−1.2).
FIND
The training loss of both autoencoders, their mean error on the held-out
points, the decoding of the code z=5, and what went wrong.
STRATEGY
Write the decoder’s second coordinate on each of its three linear pieces.
Then evaluate both models point by point.
SOLUTION
The decoder, piece by piece. The two ReLU terms switch on at z=−1
and z=1, so the second output is
Training points. The codes are 1, 2.2, −1 and −2.2, and
g2 returns 2, 0.4, −2 and −0.4. Every training point is
reconstructed exactly, so the training loss is 0. The principal axis
gives each corner an error of 1, so its training loss is 1.
Held-out points. At (−0.6,0.8) the code is −0.6 and the decoder
returns (−0.6,−1.2): squared error (0.8+1.2)2=4. At (0.6,−0.8) it
returns (0.6,1.2): error 4 again. At (1.6,1.2) and (−1.6,−1.2) it
is exact. The mean is 2.
The principal axis gives the first two points the code 0 and the
reconstruction (0,0), with error 1 each. The other two lie on the axis,
with error 0. The mean is 0.5.
training loss
held-out mean
width-one code, two ReLU kinks
0
2
principal axis
1
0.5
Away from the data. The code 5 decodes to
(5,−320+310)=(5,−3.333333). The training codes lie
between −2.2 and 2.2; nothing constrained the decoder beyond them.
What went wrong. The reconstruction surface is a zigzag through the
corners in order of their first coordinate:
(−2.2,−0.4)→(−1,−2)→(1,2)→(2.2,0.4). Its middle piece crosses the
rectangle from one corner to the opposite one. The code is one number, as
narrow as a code can be, and the surface is one-dimensional as it must be.
But a curve with two kinks has three straight pieces, enough to meet four
points. The bottleneck bounded the dimension of the surface and left its
shape free.
Answer
Training loss 0 against 1. Held-out mean error 2 against 0.5, with
4 against 1 at (−0.6,0.8). The code 5 decodes to (5,−3.333333).
The decoder had enough freedom to pass through every training point.
Check — sanity
The decoder is continuous: at z=−1 both neighbouring pieces give −2,
and at z=1 both give 2. Its first output is the code itself, so only
the second coordinate can be wrong.
Where this breaks
The construction needs the training points to have distinct codes. Two
inputs that share a code share a reconstruction, whatever the decoder, so
they cannot both be memorised. A curve with n−2 kinks passes through n
points with distinct codes; with more training points than that, the same
decoder can no longer fit them all.
Variation
Add the four midpoints to the training set. The zigzag now has a loss of
1 on the eight points, since it errs by 4 on two of them, while the
principal axis has 0.75. More data repaired what a narrower code could
not.
Track shapes and parameters through a dense and a convolutional
autoencoder for the same image, and find the two places where the
convolutional one goes wrong.
GIVEN
One input is a 1×28×28 image, 784 numbers.
The dense autoencoder has layer widths 784, 128, 32, 128, 784,
with a bias on every layer.
The convolutional encoder is two layers with 3×3 kernels, stride
2 and padding 1, taking 1 channel to 8 and then 8 to 16. The
decoder is two transposed convolutions with the same kernel, stride and
padding, taking 16 channels to 8 and then 8 to 1. All four layers
have biases.
FIND
(a) The dense parameter count, untied and with tied weights.
(b) The spatial sizes through the convolutional encoder, and how many
numbers its code holds.
(c) The decoder’s output size with output padding 0 and with 1.
(d) The convolutional parameter count.
(e) The code channel counts for which the convolutional code is narrower
than its input.
STRATEGY
Count each dense layer as inputs times outputs plus outputs. Use (I.11.4)
for the encoder’s sizes and (I.11.5) for the decoder’s.
for a total of 209,968. With tied weights the two decoder matrices are
transposes of the encoder’s. The weights are then
100,352+4,096=104,448 and the four bias vectors add 1,072, for
105,520. The code is 32 numbers, 24.5 times fewer than the input.
(b) By (I.11.4), with no dilation,
⌊228+2−3⌋+1⌊214+2−3⌋+1=14,=7.
The code has shape 16×7×7 and holds 784 numbers, exactly as
many as the input. This is the first fault. Each layer halved the height
and the width and the picture looks like an hourglass, but the channel
count grew as fast as the grid shrank. By count, nothing forces this code
to discard anything.
(c) By (I.11.5) a transposed layer returns
(n−1)⋅2−2+2+o+1=2n−1+o for output padding o. With o=0 the sizes
run 7→13→25. The reconstruction is 25×25, with 625 numbers,
and cannot be compared with a 28×28 target. This is the second
fault. With o=1 they run 7→14→28.
The ambiguity is real. Under the encoder’s stride-two layer an input of
size 27 and one of size 28 both give 14. The transposed layer cannot
know which it is undoing, and output padding tells it.
(d) A transposed convolution stores its weight with shape
(Cin,Cout,kh,kw) and one bias per output
channel. The four layers hold
a total of 2,481, against 209,968 for the dense stack.
(e) A code of C channels on the 7×7 grid holds 49C numbers.
It is narrower than the input when 49C<784, that is, C<16. With C=4
the code holds 196 numbers, a quarter of the input.
Answer
(a) 209,968 untied and 105,520 tied. (b) 28→14→7; the code
holds 784 numbers. (c) 7→13→25 without output padding and
7→14→28 with it. (d) 2,481. (e) Fewer than 16 code channels.
Check — sanity
The tied count is half the untied weights plus all the biases:
208,896/2+1,072=105,520. The transposed sizes 14 and 28 are the
encoder’s sizes read backwards.
Where this breaks
A count of numbers is a necessary check and not a sufficient one. A code
with fewer numbers than the input can still memorise
(I.12.B04), and
shared, local weights restrict a convolutional autoencoder in ways the
count does not show. The count catches only the case where no restriction
was imposed at all.
Variation
With 4×4 kernels, stride 2 and padding 1, the encoder still
gives ⌊(28+2−4)/2⌋+1=14, and the transposed layer returns
(7−1)⋅2−2+3+0+1=14 with no output padding. An even input size is
then recovered without the extra argument.
Exercises
Every one has a published solution. A hidden solution is a solution; a missing one is an abandonment.
I.12.X01A code has no unitsnumeric▲△△
A width-one linear autoencoder has E=(0.8,0.6) and
D=(0.8,0.6)T. Encode and reconstruct the new input
(2,−1), and find its squared error. Repeat with E′=(4,3) and
D′=(0.16,0.12)T. What do the two codes have in common,
and what do they not?
Hint
Compute DE and D′E′ before computing anything else.
Solution
The code is 0.8⋅2+0.6⋅(−1)=1. The reconstruction is
(0.8,0.6) and the residual is (1.2,−1.6), with squared length
1.44+2.56=4.
With the second pair the code is 4⋅2+3⋅(−1)=5, and the
reconstruction is (0.16⋅5,0.12⋅5)=(0.8,0.6) again. Both
products equal
[0.640.480.480.36],
so the two autoencoders are the same function of the input and have the
same loss on every dataset. They are the cases G=1 and
G=1/5 of (I.12.5).
The codes share their ordering and their ratios: one is five times the
other for every input. They do not share a scale. A code’s magnitude, and
with more than one code unit its axes and the angles between them, are
set by the factorisation that training happened to reach and not by the
data.
The input (2,−1) is u1−2u2 in the eigenvectors
u1=(0.8,0.6) and u2=(−0.6,0.8). The error 4 is the square
of its discarded component.
I.12.X02The encoder waits for the decodergradient▲△△
For the four points of
I.12.B01,
S=[2.921.441.442.08]. At
E=(1,0) and D=(1,0)T, evaluate both gradients
of (I.12.2). Take one step of gradient descent with step size 0.1 and
find the new loss. Then evaluate both gradients at E=0,
D=0.
Hint
Form (I−DE)S once and use it in both gradients.
Solution
Here I−DE=[0001], so
(I−DE)S=[01.4402.08].
Multiplying on the right by ET=(1,0)T and on
the left by DT=(1,0):
∇DL∇EL=−2[01.44]=[0−2.88],=−2(0,0)=(0,0).
The decoder is told to raise its second weight: the residual, which lives
in the second coordinate, is correlated with the code. The encoder is told
nothing. Its gradient passes through DT, and the decoder
does not yet write to the coordinate where the error is.
After the step, D=(1,0.288)T and E is unchanged.
The residual map is [0−0.28801] and the loss
is
0.2882⋅2.92−2⋅0.288⋅1.44+2.08=1.492756,
down from 2.08. On the next step the encoder’s gradient is no longer
zero.
At E=0 and D=0, each gradient has a zero matrix as
a factor, so both vanish. The loss is trS=5 and
gradient descent never leaves. An autoencoder needs a nonzero
initialisation in both halves.
I.12.X04As wide as the input, and nothing learnedcounterexample▲△△
A linear autoencoder with d=k=2 has
E=[2111],D=[1−1−12].
Find its loss on the four points of
I.12.B01, the code
of (1,2), and its reconstruction of (100,−7), a point nowhere near the
data. What has it learned about the data?
Hint
Multiply DE.
Solution
The product is
DE=[2−1−2+21−1−1+2]=[1001].
Every input is reconstructed exactly, so the loss is 0 on the four points
and on any other dataset. The code of (1,2) is (2+2,1+2)=(4,3), and
(100,−7) is returned as (100,−7).
It has learned nothing about the data. The loss is at its smallest
possible value, yet the same pair of matrices would have been optimal for
any four points. Any invertible E with D=E−1 does the
same, a family with four free numbers.
The code is the input in another basis. A linear classifier on the code
can do exactly what it could do on the input, no more and no less.
This is the case k=d of (I.12.5): the sum of discarded eigenvalues is
empty. A reconstruction loss of zero is evidence that an autoencoder
compresses only when something prevents this solution.
I.12.X03Centre the data, or the code spends itself on the meannumeric▲▲△
Three inputs are (10,−1), (10,0) and (10,1). A width-one linear
autoencoder without biases is trained on them as they are. Find the
direction it keeps, the three codes and the loss. Then subtract the mean
first and repeat. Which result is the principal subspace?
Hint
Without centring, the matrix in (I.12.2) is the second-moment matrix
n1XTX and not the covariance. Apply I.12.T1 to
whichever matrix the loss contains.
Solution
As they are. The second-moment matrix is
31XTX=[100002/3].
The algebra of I.12.T1 applies to this matrix. Its largest eigenvalue is
100, with eigenvector (1,0), so the autoencoder keeps the first
coordinate. All three codes are 10. Every input is reconstructed as
(10,0) and the loss is the other eigenvalue, 2/3=0.666667.
The code is the same number for every example. It carries no information
about which example it came from.
Centred. The mean is (10,0), and the centred inputs are (0,−1),
(0,0) and (0,1). The covariance is
[0002/3]. Its largest eigenvalue is 2/3,
with eigenvector (0,1). The codes are −1, 0 and 1, and the loss is
the other eigenvalue, 0.
The second result is the principal subspace: the direction along which the
data vary. The first is the direction in which the data are large. The two
matrices differ by μμT for the mean μ, and
when the mean is long compared with the spread, that term dominates and
the leading direction is close to μ itself.
An autoencoder with a bias in the decoder can learn the mean and escape
this. Without one, centre the data first.
I.12.X05A flat spectrum cannot be compressedlimit▲▲△
A covariance in eight dimensions has eigenvalues λi=28−i, that
is, 128,64,…,2,1.
(a) Find the loss of the best width-k linear autoencoder for
k=1,2,4,7, and the fraction of the total variance each leaves
unexplained.
(b) Find the smallest k that leaves under one per cent.
(c) Repeat (b) for the flat spectrum λi=1.
(d) For λi=ρi−1 with 0<ρ<1 and d→∞, find the
unexplained fraction as a function of k. What happens as ρ→1?
(e) In the flat case, which subspace is optimal?
Hint
By (I.12.5) the loss is the sum of the eigenvalues after the k-th. Both
spectra are geometric series.
Solution
(a) The total is 28−1=255, and
∑i>k28−i=28−k−1.
k
loss
fraction unexplained
1
127
0.498039
2
63
0.247059
4
15
0.058824
7
1
0.003922
Each extra code unit roughly halves what is left.
(b) At k=6 the loss is 3 and the fraction is 3/255=0.011765,
just over. At k=7 it is 0.003922. So k=7.
(c) With every eigenvalue equal to 1 the loss is 8−k and the
fraction is 1−k/8. It is under one per cent only at k=8, where the code
is as wide as the input. Nothing can be compressed.
(d) The tail is ∑i>kρi−1=ρk/(1−ρ) and the total
is 1/(1−ρ), so the unexplained fraction is exactly ρk. Each code
unit removes the fraction 1−ρ of what remains. To reach a fraction
ϵ needs k=lnϵ/lnρ, which grows like
ln(1/ϵ)/(1−ρ) as ρ→1. A spectrum that decays slowly
needs a wide code, and in the limit of no decay no finite width suffices.
(e) Every one. In the proof of I.12.T1 the retained variance is
∑iλici with weights that total k. When all λi are
equal this is k whatever the weights, so every k-dimensional subspace
has the same loss. The uniqueness in I.12.T1 required
λk>λk+1, and here that fails at every k. Training will
return some subspace, fixed by the initialisation and not by the data.
A linear code compresses exactly to the extent that the spectrum is
uneven. Isotropic noise has a flat spectrum, and no linear code compresses
it.
I.12.X07A penalty on the code can be scaled awaycounterexample▲▲△
On the four points of
I.12.B01, a
width-one linear autoencoder has E=(0.8,0.6)/c and
D=c(0.8,0.6)T for a constant c>0. Its objective is
the reconstruction loss plus β times the mean of ∣z∣ over the
data, with β=0.5.
(a) Evaluate the objective at c=1, 10 and 1000. What is its infimum,
and is it attained?
(b) Now hold the decoder at (0.8,0.6)T and let each input
take whichever code is best for it. Find that code for a corner of the
rectangle, and the objective.
(c) For which β is the best code in (b) zero?
Hint
Each corner has a score of ±2 along (0.8,0.6) and ±1 along
(−0.6,0.8). Use (I.12.9) for (b).
Solution
(a) The product DE does not depend on c. It is the
projection onto (0.8,0.6), with reconstruction loss 1. The codes are
±2/c, with mean absolute value 2/c. So the objective is
1+c0.5⋅2=1+c1,
which is 2, 1.1 and 1.001 at the three values of c.
Its infimum is 1, the unpenalised loss, and no finite c attains it.
Along the way the encoder shrinks towards zero and the decoder grows
without bound. Nothing about the function computed has changed. The
penalty has selected for large decoder weights and done nothing else.
(b) With a unit-norm decoder the error of one input splits along the
two eigenvectors. For a corner with scores a=±2 and b=±1, a code
z gives
(a−z)2+b2+β∣z∣.
By (I.12.9) the best code is
sign(a)(∣a∣−β/2)=±1.75. The objective is
0.252+1+0.5⋅1.75=1.9375,
against 2 for the unshortened code ±2. The penalty now trades
something real: codes an eighth shorter, and a reconstruction error of
1.0625 where it was 1.
(c) The code is zero when β/2≥∣a∣=2, that is, β≥4.
The unit is then never active, the reconstruction is the origin for every
input, and the reconstruction loss is the total variance, 5.
The fix. Hold each decoder column at unit norm, or weight ∣zj∣ by
the norm of its column, so that the product of the two scales is what is
penalised. A penalty on the encoder’s Jacobian has the same weakness, and
tied weights remove it there.
I.12.X08The best denoiser returns a conditional meanprobability▲▲△
A scalar input is +1 or −1 with probability one half each. It is
corrupted to x~=x+ε with ε drawn from
N(0,σ2).
(a) Find the best denoiser under squared error,
E[x∣x~].
(b) Evaluate it at x~=0.5 for σ2=1 and for
σ2=0.25, and at x~=0.
(c) Verify (I.12.8) for this distribution.
(d) The output at x~=0 is not a possible input. What does that say
about reconstructions trained with squared error?
Hint
Use Bayes’ rule (Bayes' rule· 0.PR.04) for the two posterior
probabilities. The Gaussian normalising constants cancel in their ratio.
Solution
(a) The two likelihoods are proportional to
e−(x~−1)2/2σ2 and e−(x~+1)2/2σ2, and
the priors are equal. Their ratio is e2x~/σ2, so the
posterior probabilities are
p±=ex~/σ2+e−x~/σ2e±x~/σ2.
The conditional mean is p+−p−:
E[x∣x~]=tanh(x~/σ2).
(b) At x~=0.5: tanh(0.5)=0.462117 for σ2=1 and
tanh(2)=0.964028 for σ2=0.25. At x~=0 it is 0 for any
σ2.
With less noise the same observation is stronger evidence, and the
denoiser commits to the nearer input. As σ2→0 it becomes the
sign of x~. As σ2→∞ it flattens to the prior mean,
0.
(c) The noised density is proportional to the sum of the two
likelihoods. Differentiating its logarithm,
So x~+σ2 times the score is tanh(x~/σ2), as
(I.12.8) requires. The displacement of the denoiser points towards the
nearer of the two inputs, where the noised density is higher. The script
checks this with a numerical derivative at all three points of (b).
(d) At x~=0 the two inputs are equally likely. The value that
minimises the expected squared error is their mean, 0, even though the
input is never 0. Squared error rewards the average of the plausible
answers and not any one of them.
For images, the average of several sharp reconstructions that differ
slightly is a blurred one. An autoencoder trained with squared error on
inputs it cannot resolve returns that average. More training does not
sharpen it, because the average is the optimum.
I.12.X10An anomaly on the surface scores zerocounterexample▲▲△
The best width-one linear autoencoder for the four points (1,2),
(2.2,0.4), (−1,−2) and (−2.2,−0.4) projects onto
u1=(0.8,0.6). Its squared reconstruction error is used as an
anomaly score, and any input scoring above 1.5 is flagged.
(a) Find the scores of the four training points.
(b) Find the scores of (8,6) and of (−1.2,1.6), and the distance from
each to its nearest training point.
(c) Which of the two is flagged?
(d) What does the score measure, and what would be needed to flag the
other one?
Hint
Write each new point in the eigenvectors u1=(0.8,0.6) and
u2=(−0.6,0.8). The score is the square of the u2 component.
Solution
(a) Each training point has a component of ±1 along u2, so
every score is 1. The threshold of 1.5 passes all of them.
(b) The point (8,6) is 10u1. It lies on the principal axis
and its score is 0. Its nearest training points are (1,2) and
(2.2,0.4), each at distance 65=8.062258.
The point (−1.2,1.6) is 2u2. Its score is 4. Its nearest
training points are (1,2) and (−2.2,−0.4), each at distance
5=2.236068.
(c) Only (−1.2,1.6) is flagged. The point (8,6) passes with the
lowest score of any input seen so far, lower than every training point,
though it is more than three times as far from the data as the point that
was flagged.
(d) The score is the squared distance from the reconstruction surface,
here a line. It says nothing about where along the line an input falls.
The line extends without end, and the data occupy a short stretch of it.
To flag (8,6) one must know which codes are usual. Its code is 10,
where the training codes are ±2. Reconstruction error does not look at
the code, and (I.12.1) defines no distribution over codes against which
10 could be judged improbable. A threshold on the size of the code would
catch this case and would be a second detector, added by hand.
The failure grows with the quality of the model. A decoder that
generalises well reconstructs inputs it was never shown, and each of them
scores as normal.
I.12.X06Weight decay fixes the axessymbolic▲▲▲
Add the penalty γ(∥E∥F2+∥D∥F2), with γ>0, to
the loss of (I.12.2).
(a) Write the two stationarity conditions and prove that
DTD=EET at every stationary
point.
(b) Let D=UkC and E=DT with
C=diag(c1,…,ck). Show that this pair is
stationary when ci2=1−γ/λi, and find the end-to-end map.
(c) For the four points, with k=2 and γ=0.5, find the gains, the
decoder and the value of the objective. Compare it with the identity.
(d) Explain why the principal axes can be read from this decoder and not
from an unpenalised one.
(e) What happens to a direction with λi≤γ?
Hint
For (b), use SUk=UkΛk and
UkTUk=I, where
Λk=diag(λ1,…,λk).
Solution
(a) The penalty adds 2γD and 2γE to the
gradients of (I.12.2). Setting both to zero:
(I−DE)SETDT(I−DE)S=γD,=γE.
Multiply the first on the left by DT and the second on
the right by ET. Both left-hand sides become
DT(I−DE)SET, so
γDTD=γEET. Divide by
γ.
(b) Here DE=UkC2UkT. Using the
hint,
(I−DE)SET=(I−UkC2UkT)UkΛkC=Uk(I−C2)ΛkC.
This equals γD=γUkC when
(1−ci2)λi=γ for every i, that is,
ci2=1−γ/λi. Since DE and S are both
symmetric and share eigenvectors, they commute, and the second condition
is the transpose of the first. The end-to-end map is
DE=Ukdiag(1−γ/λi)UkT.
Replacing D by DOT and E by
OE for an orthogonal O changes neither condition nor
either norm. That gives the family (I.12.6).
(c) The gains are 1−0.5/4=0.875 and 1−0.5/1=0.5, so
c=(0.935414,0.707107). The decoder’s columns are
The reconstruction term is
∑i(1−gi)2λi=0.0625+0.25=0.3125. The penalty is
γ⋅2∑ici2=0.5⋅2⋅1.375=1.375. The objective is
1.6875. The identity, E=D=I, reconstructs exactly and
pays 0.5⋅4=2. With weight decay, copying is no longer the best
answer even at full width.
(d) The expression D=UkCOT is a
singular value decomposition of the decoder: left singular vectors
ui, singular values ci. When the λi are distinct, so
are the ci, and the left singular vectors are determined up to sign and
come out in order of λi. The unknown rotation O sits on the
right and does not disturb them.
Without the penalty the decoder is UkG for an arbitrary
invertible G. Its left singular vectors are Uk times those
of G, which can be any orthonormal basis of the subspace.
(e) In this family the contribution of one direction to the objective
is (1−c2)2λ+2γc2. Its derivative with respect to c2 is
−2(1−c2)λ+2γ, which is non-negative for all c2≥0 when
λ≤γ. The best choice is then c=0: the direction is not
learned at all. Weight decay sets a floor on the variance a direction
needs before it is worth representing.
For the four points with γ=1, the second direction has
λ2=γ and is dropped. The first has gain 0.75, the decoder
is 0.75u1=(0.692820,0.519615), and the objective is
0.25+1+1.5=2.75.
The script checks the stationarity of (c) to machine precision, confirms
that sixteen small perturbations all raise the objective, and confirms
that rotating the code leaves it unchanged.
I.12.X09A circle does not pass through a width-one codeproof▲▲▲
The data are the unit circle in the plane, and the code has width one.
(a) Prove that every continuous encoder gives some pair of antipodal
points the same code.
(b) Deduce that, whatever the decoder, some point of the circle is
reconstructed at distance at least 1 from itself.
(c) Encoding a point by its angle in (−π,π] and decoding z to
(cosz,sinz) reconstructs every point exactly. Why does this not
contradict (b)? For the inputs at +175∘ and −175∘, find the
distance between them, the distance between their codes, and the distance
from either to the decoding of the code halfway between.
(d) How wide a code allows an autoencoder for the circle that is both
continuous and exact?
Hint
Write the encoder as a function F of the angle and study
h(θ)=F(θ)−F(θ+π).
Solution
(a) Let F(θ) be the code of the point at angle θ. It is
continuous, and F(θ+2π)=F(θ) because the two angles name one
point. Define h(θ)=F(θ)−F(θ+π). Then
h(θ+π)=F(θ+π)−F(θ+2π)=−h(θ).
If h(0)=0 the points at angles 0 and π share a code. Otherwise
h(0) and h(π) have opposite signs, and by the intermediate value
theorem h(θ⋆)=0 for some θ⋆ between them. The
points at θ⋆ and θ⋆+π are antipodal and share a
code. ■
(b) Call those points p and −p. A decoder sees one code,
so it returns one point a for both. By the triangle inequality,
∥a−p∥+∥a+p∥≥∥(a+p)−(a−p)∥=2∥p∥=2.
The larger of the two distances is at least 1. ■
(c) The angle is not a continuous function on the circle. Crossing
the negative horizontal axis, it jumps from π to −π. Part (a)
needed continuity and does not apply.
The two inputs are 10∘ apart on the circle, at distance
2sin5∘=0.174311. Their codes are ±3.054326, which are
6.108652 apart. The code halfway between is 0, which decodes to
(1,0), the point 175∘ around from either input and at distance
2sin87.5∘=1.998096 from it.
The exact autoencoder has put two neighbours at opposite ends of its code
and the far side of the circle between them.
(d) Width two. Take the encoder and the decoder to be the identity on
the plane. Both are continuous and every point of the circle is
reconstructed exactly.
The circle has one degree of freedom and needs two code numbers. A closed
curve cannot be laid along a line without cutting it somewhere, and (a)
and (b) are that fact stated for encoders. The width a code needs depends
on the shape of the data as well as on its dimension.
As a check, the script takes
F(θ)=cosθ+0.3sin2θ+0.2sinθ, for which
h(θ)=2cosθ+0.4sinθ, and finds the shared code by
bisection at θ⋆=π−arctan5=1.768192.