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.