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 for non-exact numerical results.
STATEMENT
Run a scalar LSTM through three inputs and separate what the input gate writes
from what the forget gate keeps.
GIVEN
A scalar cell follows (I.10.1) and (I.10.2) with every recurrent weight zero.
The gates read only their biases: bi=0, bf=ln3, bo=ln3, and
ui=uf=uo=0. The candidate is c~t=tanh(xtln3), so
uc=ln3 and bc=0. Start from c0=h0=0 and feed (1,0,0).
FIND
The three gate values, the candidates, all three cell and hidden states, the
third cell state when bf=ln99 instead, and the number of trainable scalars.
STRATEGY
Evaluate the gates first; they are constants here. Then apply the cell update
one step at a time and read each hidden state through the output gate.
SOLUTION
Since σ(lna)=a/(1+a), the gates are i=σ(0)=1/2 and
f=o=σ(ln3)=3/4. The first candidate is
The hidden states are ht=43tanh(ct):
0.284962, 0.218484 and 0.165959.
With bf=ln99 the forget gate is 0.99, and
c3=0.4⋅0.992=0.39204. The input gate decided how much was written;
the forget gate alone decided how much of it survived two silent steps.
Each of the four blocks has one input weight, one recurrent weight and one
bias, so the scalar cell has 12 trainable scalars. The plain scalar cell of
I.9.B01 had three before
its readout.
Answer
Gates (i,f,o)=(1/2,3/4,3/4); candidates (0.8,0,0); cell states
(0.4,0.3,0.225); hidden states (0.284962,0.218484,0.165959); with
f=0.99, c3=0.39204; twelve trainable scalars.
Check — sanity
Each hidden state is smaller than its cell state. For c>0,
tanhc<c, and the output gate multiplies by a further three quarters.
The hidden state is a view of the cell, not a copy of it.
It checks all fifteen problems of the chapter. Its first line reports the
cell states 0.4, 0.3, 0.225, the hidden states above and the
f=0.99 cell state 0.392040. Its second line confirms that a full two-unit
LSTM backward pass matches central finite differences for every parameter.
Executed in CI. The digits above are the digits it printed.
Where this breaks
Nonzero recurrent weights make the gates depend on ht−1, so they change
from step to step and the cell states are no longer a geometric sequence.
The write-then-keep reading survives; the clean numbers do not.
Variation
Feed (1,1,0) instead. The second step keeps 0.3 and writes another 0.4,
so c2=0.7 and c3=0.525. The cell accumulates; it does not overwrite
unless the forget gate is small.
Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Derive the backward pass of an LSTM step, identify the carry line in it, and
measure how much of a real derivative the carry line accounts for.
GIVEN
The cell of (I.10.1) and (I.10.2), with a loss that may read any ht.
For the numerical part, use the scalar cell of
I.10.B01 with one change:
the candidate also reads the hidden state, c~t=tanh(xtln3+2ht−1).
Inputs are (1,0,0) from zero states.
FIND
The recursions for gˉc,t and the four pre-activation
derivatives; the derivatives sent to ht−1 and ct−1; the
parameter gradients; and, in the scalar case, ∂c3/∂c1
compared with its carry-line term.
STRATEGY
Draw the arrows leaving ct and ht, add the returns, and only
then differentiate the activations. For the scalar part, notice that a
constant output gate makes ht a function of ct.
SOLUTION
Arrows out of the cell. The cell state ct is used twice: by
ht=ot⊙tanh(ct), and by the next cell update, where it
is multiplied by ft+1. Reverse mode adds the two returns:
Here gˉh,t is already the total for ht: its readout term
plus everything step t+1 returned through its gates.
Pre-activations. Write ak,t for the argument of the gate or
candidate k. Differentiating
ht=ot⊙tanhct with respect to ao,t, and
ct=ft⊙ct−1+it⊙c~t with
respect to the other three,
This is (I.10.6). The output gate is the only one that reads
gˉh,t directly; the other three see the cell’s total.
What goes back one step. Every pre-activation contains
Wkht−1, so the recurrent return to ht−1 is
∑k∈{i,f,o,c}WkTδk,t. The cell one step
back receives gˉc,t⊙ft, the carry line, which is the
second term of the next gˉc,t−1. At t=1 these two quantities
are the derivatives with respect to h0 and c0.
Parameters. Each block is used at every step, so its gradients are sums,
as in (I.9.4):
The scalar case. With constant gates i=1/2, f=o=3/4, the hidden state
ht=43tanhct is a function of ct, so the state is c alone and
the one-step derivative is
The forward pass gives c1=0.4, h1=0.284962, then
c~2=tanh(0.569924) and onwards. Evaluating along that trajectory,
k2=1.221326 and k3=1.078827, so
∂c1∂c3=k3k2=1.317600,carry line alone: f2=0.5625.
Expanding k3k2 gives four terms. Only f⋅f avoids the recurrent
weight; the other three each contain it at least once, as I.10.T1 says.
Here they contribute 0.755100, more than the carry line.
The reproduction script perturbs c1 by 10−6, reruns steps two and three,
and matches 1.317600 by central differences. It then checks the vector
recursions above on a two-unit cell with non-symmetric recurrent weights,
losses at three of four steps and nonzero initial states. All twelve parameter
blocks, c0 and h0 agree with finite differences.
Answer
gˉc,t=gˉh,t⊙ot⊙(1−tanh2ct)+gˉc,t+1⊙ft+1,
with the four δ above, a return of ∑kWkTδk,t
to ht−1 and gˉc,t⊙ft to ct−1.
In the scalar case ∂c3/∂c1=1.317600, of which the carry
line accounts for 0.5625.
Check — sanity
Set every recurrent weight to zero. Then Mt=0, the hidden-state return
vanishes, and the derivative between cell states is exactly the product of
forget values. In the scalar case kt=f and ∂c3/∂c1=0.5625.
Where this breaks
Treating ot as a function of ct would add a false term:
the output gate reads ht−1 and xt, not the cell. A peephole
LSTM, which does let gates read the cell, adds genuine terms of that kind and
needs its own derivation.
Variation
Replace the candidate weight two with zero. Both factors become 3/4 and the
derivative falls to 0.5625. The weight did not change the carry line; it
changed how much signal reached the cell by other routes.
Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Turn a constant forget value into a number of steps, bound that number, and
find the forget value and bias a given horizon requires.
GIVEN
One coordinate’s forget value is f∈(0,1) at every step. The carry-line
factor after n steps is fn. The memory horizon is m=−1/lnf.
FIND
(a) f100 and f1000 for f=0.99, with its horizon and half-life.
(b) Bounds on m in terms of f. (c) The smallest f with m≥100, and
the bias that produces it when the gate’s other inputs are zero.
(d) The behaviour of m as f→1 and as the bias grows.
STRATEGY
Write fn=enlnf and use lnx≤x−1 twice. For the bias, invert the
sigmoid.
SOLUTION
(a) Directly, 0.99100=0.366032 and 0.991000=4.317125×10−5.
The horizon is −1/ln0.99=99.499162 steps. The half-life solves
fn=1/2, so n1/2=ln2/(−lnf)=mln2=68.967564 steps.
(b) The inequality lnx≤x−1 is strict for x=1. At x=f it
gives −lnf>1−f. At x=1/f it gives ln(1/f)<1/f−1, that is,
−lnf<(1−f)/f. Taking reciprocals,
1−ff<m<1−f1.
The two bounds differ by exactly one, so 1/(1−f) gives the horizon to within
a step. For f=0.99 they are 99 and 100.
(c) The condition m≥100 is −lnf≤1/100, so
f≥e−1/100=0.990050. A gate with only its bias has f=σ(b), so
b=ln(f/(1−f))=4.600166.
(d) As f→1, −lnf→0+ and m→∞; by (b), m grows like
1/(1−f). With f=σ(b), (I.10.5) gives 1/(1−f)=1+eb. Each extra unit
of bias multiplies the horizon by about e, but no finite bias reaches f=1.
Answer
(a) 0.366032, 4.317125×10−5; horizon 99.499162; half-life
68.967564. (b) f/(1−f)<m<1/(1−f). (c) f≥0.990050, bias at least
4.600166. (d) m→∞ like 1/(1−f)=1+eb, never infinite at finite b.
Check — sanity
At f=1/2 the bounds give 1<m<2, and indeed m=1/ln2≈1.44. A zero
forget bias forgets most of a value within two steps.
Where this breaks
A trained gate’s value changes with the input, so the horizon of a real cell
is a distribution over steps, not a constant. The bound also concerns the
carry line only; I.10.B02 shows the full derivative can be several times
larger or smaller.
Variation
For a horizon of exactly m in the sense of 1/(1−f)=m, set b=ln(m−1).
For m=100 that is ln99=4.595120, slightly below the answer to (c), because
1/(1−f) exceeds −1/lnf.
Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Show that applying the reset gate before or after the recurrent matrix gives
different GRUs, and find exactly when the two agree.
GIVEN
A two-unit GRU step with zero input contribution and zero candidate biases.
The recurrent candidate matrix swaps coordinates,
Wn=[0110]. The preceding state is
ht−1=(0.5,−0.5). The reset pre-activations are (ln3,−ln3) and the
update pre-activations are zero. Use PyTorch’s update,
ht=(1−zt)⊙nt+zt⊙ht−1, for both.
FIND
The gates, both candidates and both new states; then a condition on
Wn and rt under which the two placements agree for every state.
STRATEGY
Compute W(r⊙h) and r⊙(Wh) separately.
For the general condition, write both as products with diag(r)
and compare entries.
SOLUTION
The gates are r=(σ(ln3),σ(−ln3))=(0.75,0.25) and
z=(0.5,0.5). The update gate is the same in both models, so any
difference comes from the candidate.
Reset before the matrix, as in Cho and colleagues:
r⊙h=(0.375,−0.125), and the swap gives (−0.125,0.375). Hence
Reset after the matrix, as in PyTorch: Wh=(−0.5,0.5), then
r⊙Wh=(−0.375,0.125), and
nafter=(−0.358357,0.124353).
With z=1/2, the new states are the averages of candidate and old state:
(0.187823,−0.070821) before and (0.070821,−0.187823) after. Same weights,
same state, same gates, and every coordinate differs by more than 0.1.
When they agree. The candidates’ recurrent parts are
Wdiag(r)h and diag(r)Wh.
They agree for every h exactly when the matrices are equal. Their
(j,k) entries are Wjkrk and rjWjk, so the condition is
Wjk(rk−rj)=0for all j,k.
A weight joining two coordinates may be nonzero only if their reset values
are equal. A uniform reset, r=ρ1, satisfies it for every
W; a diagonal W satisfies it for every r. The swap matrix
with unequal resets satisfies neither, which is why this example separates them.
Biases add a second difference. In the after form the recurrent bias
bhn is inside the reset, r⊙(Wh+bhn),
so the reset scales it. In the before form a bias added outside is not scaled.
Even scalar models differ when that bias is nonzero.
Answer
r=(0.75,0.25), z=(0.5,0.5). Before: candidate (−0.124353,0.358357),
state (0.187823,−0.070821). After: candidate (−0.358357,0.124353), state
(0.070821,−0.187823). They agree for all states exactly when
Wjk(rk−rj)=0 for all j,k.
Check — sanity
The swap matrix moves each coordinate’s reset to the other side. Reset before:
coordinate one’s value, scaled by 0.75, lands in coordinate two. Reset after:
coordinate two’s value lands in coordinate one and is scaled there by
0.75. The mirror-image answers are what that bookkeeping predicts.
Where this breaks
The commutation test is for one step with fixed gates. Over a sequence the
two models also produce different gates, because the gates read states that
have already diverged. Load a checkpoint into the placement it was trained
with. The update-gate convention can be converted by negating weights; this
example gives no such conversion for the reset placement.
Variation
Make the reset uniform at (0.5,0.5) and keep the swap matrix. Both forms now
give the candidate (tanh(−0.25),tanh(0.25))=(−0.244919,0.244919).
Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Audit the shapes, parameter counts, arithmetic and training storage of a
tanh RNN, a GRU and an LSTM, then find a fair width for comparing two of them.
GIVEN
Input width D=3, hidden width H=5. Count one bias per gate as in (I.10.8),
then PyTorch’s two bias vectors per block. For storage, take N=4 sequences of
length T=6 in float32, as in
I.9.B04. Assume the plain
cell stores ht for the backward pass and the LSTM stores
it,ft,ot,c~t,ct,ht.
FIND
(a) PyTorch’s LSTM weight and bias shapes, and the rows of the forget gate.
(b) Parameter counts for all three cells under both bias conventions, with the
GRU’s reset-after form. (c) Multiply–accumulates per step for one sequence.
(d) Stored activation bytes. (e) The GRU width whose one-bias count equals the
LSTM’s at H=5.
STRATEGY
Each gated block is a plain recurrent layer’s affine map with its own
weights. Count one block, then multiply.
SOLUTION
(a) PyTorch stacks the four LSTM blocks in the order input, forget,
candidate, output. The input weights are 4H×D=20×3, the recurrent
weights 4H×H=20×5, and each bias vector has 4H=20 entries.
Counting from zero, the forget gate occupies rows 5 to 9. Rows 0 to 4
are the input gate. For a GRU the order is reset, update, new, so the blocks
are 3H rows tall.
(b) One block has H(D+H+1)=5⋅9=45 parameters with one bias. So
RNN 45,GRU 3⋅45=135,LSTM 4⋅45=180.
The reset-after GRU keeps bhn inside the reset, which cannot be merged
with bn: 135+5=140. PyTorch keeps two bias vectors per block,
H(D+H)+2H each: 40+10=50, then 150 and 200.
(c) One affine map from (xt,ht−1) costs H(D+H)=40
multiply–accumulates. The LSTM computes four, 160 per step; the GRU three,
120; the plain cell one, 40. The elementwise gate products add O(H) per
step and do not change the leading count.
(d) The plain cell stores NTH=120 numbers, 480 bytes. The LSTM, under
the stated scheme, stores six vectors per step: 6⋅120=720 numbers,
2880 bytes. Other schemes store tanhct or recompute gates in the
backward pass. Name the scheme before quoting a ratio.
(e) A GRU of width H′ at D=3 has 3H′(H′+4) parameters. Setting this
to 180 gives H′2+4H′−60=0, whose positive root is H′=6:
3⋅6⋅10=180 exactly. Its arithmetic is 3⋅6⋅9=162
multiply–accumulates per step, against the LSTM’s 160. In PyTorch’s
convention the two counts are 198 and 200.
A plain RNN cannot match exactly: H′(H′+4)=180 has no integer root, and
widths 11 and 12 give 165 and 192.
Answer
(a) 20×3, 20×5, two biases of 20; forget rows 5–9.
(b) One bias: 45, 135 (140 reset-after), 180. PyTorch: 50, 150, 200.
(c) 40, 120, 160 per step. (d) 480 and 2880 bytes.
(e) H′=6, 180 parameters, 162 multiply–accumulates.
Check — sanity
Setting D=0 removes every input weight: the one-bias LSTM count becomes
4H(H+1)=120, four copies of a plain recurrent map with bias. Counts should
always factor this way.
Where this breaks
Multiply–accumulate counts are not runtimes. Fused kernels compute all four
LSTM blocks as one 4H-row product, and memory traffic often decides speed.
Projection layers, peepholes and bidirectional stacks change every count above.
Variation
At D=H=512, a common width, the one-bias LSTM block count is
4⋅512⋅1025=2,099,200 parameters, and the GRU’s is
1,574,400.
Exercises
Every one has a published solution. A hidden solution is a solution; a missing one is an abandonment.
I.10.X01A saturated gate still leakslimit▲△△
A forget gate has bias 10 and no other input, so f=σ(10). Is the carry
line lossless? Compute f, 1−f, the carry-line factor after 105 steps and
the memory horizon.
Hint
Use 1−σ(b)=σ(−b), and (I.10.5) for the horizon.
Solution
f=σ(10)=0.9999546 and 1−f=σ(−10)=4.539787×10−5.
After 105 steps the factor is f100000=0.010675: about one per cent of
the signal is left.
The horizon is −1/lnf=22026.97 steps. By (I.10.5) it lies just below
1/(1−f)=1+e10=22027.47.
The carry line is not lossless. A sigmoid gate can be pushed as close to one
as the bias allows, which sets a long but finite horizon. Only the 1997
design, with no forget gate at all, had a factor of exactly one, and
I.10.X03 shows what that cost.
I.10.X02Which slice is the forget gate?shape▲△△
A PyTorch nn.LSTM has hidden width H=5. You want the forget bias to start
at one. Which entries of bias_ih_l0 hold the forget gate? What effective
bias results if you set that slice to one in both bias_ih_l0 and
bias_hh_l0, and what horizons 1/(1−f) do the intended and the actual
settings give? What range does PyTorch’s default leave each bias in?
Hint
PyTorch stacks the gates as input, forget, candidate, output. The two bias
vectors are added inside every gate.
Solution
Each bias vector has 4H=20 entries in four blocks of five. The forget block is
the second: entries 5 to 9, counting from zero. Entries 0 to 4 are
the input gate, so setting them would raise the input gate instead.
The gate computes σ(⋯+bif+bhf). Setting both slices to one
gives an effective bias of two: f=σ(2)=0.880797 instead of
σ(1)=0.731059. By (I.10.5) the horizons are
1+e2=8.389056 and 1+e=3.718282 steps. Set one slice to one and the other
to zero, or split the target between them.
By default PyTorch draws every bias from U(−1/5,1/5), that is,
within ±0.447214. The effective forget bias is the sum of two such
draws, so it lies within ±0.894427, centred on zero: f starts near
one half. Keras, by contrast, adds one to the forget bias by default.
I.10.X05Masking a padded step freezes two statesnumeric▲△△
An LSTM coordinate has c=1 before a padded step. During padding the input is
zero and the gates are f=0.5, i=0.5, o=0.75 with candidate 0.6. The
next real step has f=0.5, i=0.5, candidate 0 and o=0.75. Compare the
next real hidden state when the padded step updates c but freezes h, and
when it freezes both.
Hint
Freezing h alone still runs the cell update.
Solution
Freezing only h: the padded step computes c=0.5⋅1+0.5⋅0.6=0.8.
The next real step gives c=0.5⋅0.8=0.4 and
h=0.75tanh(0.4)=0.284962.
Freezing both: the cell stays at 1, the next step gives c=0.5 and
h=0.75tanh(0.5)=0.346588.
The masked update of I.9 applied to h alone is not enough for an LSTM.
Mask both states: if ctnew is the cell update the step
computed, keep mtctnew+(1−mt)ct−1, and apply the
same rule to ht. Or pack the sequence so padded steps never run.
I.10.X06Which side does z multiply?numeric▲△△
A GRU’s update gate sits at z=0.9 for ten steps while the candidate is zero.
How much of the starting state survives under PyTorch’s convention,
ht=(1−z)nt+zht−1, and under Chung and colleagues’,
ht=(1−z)ht−1+znt? How do you convert trained weights from one
convention to the other?
Hint
With a zero candidate, each step multiplies the state by whichever factor
sits next to ht−1.
Solution
Under PyTorch’s convention each step keeps z: after ten steps
0.910=0.348678 of the starting state remains. Under the other, each step
keeps 1−z: 0.110=10−10. The direct Jacobians are diag(z)
and diag(1−z) respectively.
The two conventions describe the same functions. Since
1−σ(a)=σ(−a), negating the update gate’s input weights, recurrent
weights and bias turns z into 1−z. Loading weights without that step
silently swaps keeping and overwriting.
I.10.X03No forget gate, no boundnumeric▲▲△
A cell without a forget gate, as in 1997, has f=1. Its input gate and
candidate are constant at i=1 and c~=0.1, its output gate at o=1,
and c0=0. Find c200 and the derivative ∂h/∂c there,
both by the formula 1−tanh2c evaluated in float64 and by
sech2c. Repeat with f=0.9.
Hint
Without forgetting, the cell adds the same amount every step. With f<1 it
approaches ic~/(1−f).
Solution
With f=1, ct=0.1t, so c200=20. Mathematically
∂h/∂c=1−tanh2(20)=sech2(20)=1.699342×10−17.
In float64, tanh(20) rounds to exactly one, because it differs from one by
about 8.5×10−18, less than half the spacing of doubles below one.
The formula 1−tanh2c then returns exactly 0.0.
Either way the output path into the cell is closed. The carry line still
passes gradients from cell to cell with factor one, but no loss on h can send
anything into the cell. On a long unsegmented stream the cell keeps growing,
which is the failure Gers, Schmidhuber and Cummins identified.
With f=0.9, ct=0.1(1−0.9t)/(1−0.9)=1−0.9t, so c200=1.000000 to
six places, and sech2(1)=0.419974. The bound of I.10.T3,
1/(1−φ)=10, is not tight here, but it is finite.
I.10.X04The hidden state is not the memorycounterexample▲▲△
Two runs of an LSTM coordinate reach ct=0.5 and ct=−0.5. At step t the
output gate is 0.01. At step t+1 the gates are f=0.99, i=0.01, with
candidate 0 and output gate 0.99. How far apart are the two hidden states
at t, and at t+1? What does this say about reading the memory from ht?
Hint
The cell update at t+1 writes nothing, so ct+1=0.99ct.
Solution
At t, h=0.01tanh(±0.5), a gap of
0.01⋅2tanh(0.5)=0.009242. A readout of ht sees runs that are almost
identical.
At t+1 the cells are ±0.495, and h=0.99tanh(±0.495), a gap of
0.907188. The difference was in the cell all along. The output gate hid it
for one step and then exposed it.
So ht is a view of the cell through a gate, not the memory itself. A probe
or classifier trained on ht can miss information the network will use one
step later. To ask what an LSTM remembers, look at ct.
I.10.X07The GRU state stays in the boxproof▲▲△
Prove that a GRU in the form (I.10.7) keeps every coordinate of ht in
[−1,1] for all t if h0 starts there. Then show the premise
matters: start a coordinate at h0=3 with z=0.5 and candidate 0.
Hint
A convex combination of two numbers lies between them.
Solution
Fix a coordinate and write ht=zht−1+(1−z)n with z∈(0,1) and
n=tanh(⋅)∈(−1,1). By the triangle inequality,
∣ht∣≤z∣ht−1∣+(1−z)∣n∣≤max(∣ht−1∣,∣n∣).
If ∣ht−1∣≤1, the right-hand side is at most one, so ∣ht∣≤1.
Induction on t from ∣h0∣≤1 completes the proof. In fact ∣ht∣<1 for
t≥1, since 1−z>0 and ∣n∣<1.
The argument uses the starting value. With h0=3, z=0.5 and n=0 the
states are 3, 1.5, 0.75, 0.375: outside the box for one step, then
inside. The same inequality shows ∣ht∣≤max(∣h0∣,1) in general.
An LSTM has no such bound on ct, which is why I.10.T3 needs
forget values below one for the cell.
I.10.X08Chrono initialisationsymbolic▲▲△
Show that the forget bias bf=ln(m−1), with all forget weights zero, gives
1/(1−f)=m exactly. Show that bi=−bf then makes each cell coordinate an
exponential moving average of its candidates. Give bf for m=2, 10 and
100, and say what goes wrong at m=1.
Hint
σ(lna)=a/(1+a) and σ(−a)=1−σ(a).
Solution
With bf=ln(m−1), f=σ(ln(m−1))=(m−1)/m, so 1−f=1/m and
1/(1−f)=m. Equivalently, (I.10.5) gives 1+eln(m−1)=m.
With bi=−bf, i=σ(−bf)=1−f, so
ct=fct−1+(1−f)c~t,
a weighted average whose weights sum to one: an exponential moving average
of the candidates, with horizon set by m.
The biases are ln1=0, ln9=2.197225 and ln99=4.595120. At m=1 the
formula needs ln0: a horizon of one step would require f=0, which no
finite bias produces.
Tallec and Ollivier draw bf=lnu with u uniform between 1 and
Tmax−1, so the initial horizons spread from two steps up to the longest
dependency the task is expected to need.
I.10.X09A forget gate of one half can still amplifycounterexample▲▲△
A scalar LSTM has constant gates f=i=1/2 and o=3/4, zero input, and a
candidate c~t=tanh(3ht−1). The state c=h=0 is a fixed point.
Linearise around it, find the factor by which a small perturbation of c
grows per step, and estimate a perturbation of 10−6 after twenty steps.
Does gating remove the need for gradient clipping?
Hint
Near zero, tanhu≈u. Then h=43tanhc≈43c.
Solution
Near the fixed point, ht−1≈43ct−1 and
c~t≈3ht−1, so
ct≈21ct−1+21⋅3⋅43ct−1=1.625ct−1.
The carry line contributes 0.5; the path through ht−1 and the candidate
contributes 1.125. A perturbation of 10−6 grows to about
10−6⋅1.62520=0.016484 after twenty steps; the script’s nonlinear
run gives 0.016480, slightly less because tanh begins to bend.
The same factor multiplies the backward signal, so the gradient through this
cell grows like 1.625n while the trajectory stays near the origin.
A forget gate below one does not bound the total Jacobian. Clipping (I.9.7)
remains a standard part of training gated networks.
I.10.X10What backpropagation through time keepscomplexity▲▲△
A batch of N=32 sequences of length T=1000 trains a layer of width
H=512 in float32. Under the storage scheme of
I.10.B05, how many bytes of
activations does a plain tanh layer keep for the backward pass, and how many
does an LSTM layer keep? How do both scale with T, and what does inference
need instead?
Hint
The plain cell stores one H-vector per step and sequence; the LSTM stores six.
Solution
The plain layer stores 4NTH=65,536,000 bytes, 62.5 MiB.
The LSTM stores six times as much, 393,216,000 bytes, 375 MiB.
Both grow linearly in T: doubling the sequence doubles the storage.
Streaming inference keeps only the current state: ht for the plain
cell, ht and ct for the LSTM, about 2NH numbers, independent
of T. The distinction of I.9 holds for gated cells too: constant inference
memory says nothing about training memory. Truncated backpropagation or
checkpointing reduces the training figure at the costs I.9 described.