Follow a gradient backwards through an LSTM. Along the cell state, each step multiplies it by the forget gate and by nothing else, a number the network can hold near 1. A plain RNN multiplies it by a full matrix and a tanh slope at every step.
About 35 minutes
By the end you can
Compute how much of a gradient survives several steps back through a scalar RNN and along an LSTM's cell state, one factor at a time.
Derive ∂ct/∂ct−1=diag(ft) along the direct path, entry by entry, and contrast it with the plain RNN's backward factor Whh⊤diag(1−ht2).
Explain the cell path with the eigenvalues of repeated multiplication from module 4.
State what the cell path does not guarantee, and name the other paths.
The previous lesson ended with a four-step figure. Going forward, the LSTM's cell held a stored 1 almost perfectly, 0.9991,0.9982,0.9973,0.9964, while a plain RNN reading the same input faded, 0.7616,0.5436,0.4094,0.3163.
Holding a value is only half the story, though. A network has to learn to hold it, and learning runs backwards: the loss is computed at the end, at step 4, and the gradient that tells step 1's weights to store the value has to travel back through steps 4, 3 and 2 to get there. So this lesson asks one question: when a gradient travels backwards, how much of it survives each step? We answer it with numbers, first for the plain RNN and then along the LSTM's cell state, then for many steps at once, and last with an honest list of what the answer does not promise.
Start with the plain RNN, because it is the thing to beat. Take the scalar RNN from the figure, with recurrent weight w=0.8:
at=wht−1+xt,ht=tanh(at).
It reads x=1,0,0,0 from h0=0. Going forward, one step at a time:
t
at=0.8ht−1+xt
ht=tanh(at)
ht2
1
0.8(0)+1=1
0.7616
0.5800
2
0.8(0.7616)+0=0.6093
0.5436
0.2955
3
0.8(0.5436)+0=0.4349
0.4094
0.1676
4
0.8(0.4094)+0=0.3275
0.3163
0.1000
Now go one step back. How does ht change when ht−1 changes? The chain rule takes two moves. First, ht=tanh(at), and the slope of tanh is 1−tanh2, so ∂ht/∂at=1−ht2. Second, at=wht−1+xt, so ∂at/∂ht−1=w. Multiply the two:
∂ht−1∂ht=(1−ht2)w.
A gradient going backwards from ht to ht−1 is multiplied by this factor. From h4 back to h1 there are three such steps:
from h4 to h3: 0.8(1−0.1000)=0.8×0.9000=0.7200
from h3 to h2: 0.8(1−0.1676)=0.8×0.8324=0.6659
from h2 to h1: 0.8(1−0.2955)=0.8×0.7045=0.5636
Together: 0.7200×0.6659=0.4794, then 0.4794×0.5636=0.2702. After only three steps, 73% of the gradient is gone.
The same holds with vectors. For ht=tanh(Whhht−1+Wxhxt+b) the Jacobian of one step is ∂ht/∂ht−1=diag(1−ht2)Whh. Here diag(v) is the diagonal matrix with the entries of v on its diagonal, and 1−ht2 (entry by entry) holds the tanh slopes. Going backwards, as in backpropagation through time, the gradient is multiplied by the transpose of that Jacobian at every step:
∂ht−1∂L=Whh⊤(∂ht∂L⊙(1−ht2)).
Two facts turn the transpose into this form. The transpose of a product reverses the order, and a diagonal matrix is its own transpose, so (diag(1−ht2)Whh)⊤=Whh⊤diag(1−ht2). And multiplying a vector by a diagonal matrix scales each entry by the matching diagonal entry, which is the ⊙ inside the brackets.
After k steps the gradient has been multiplied by k such matrices. Each one changes its length by at most the largest singular value of Whh times the largest tanh slope (module 4), so it tends to shrink or grow geometrically, like the powers of a matrix in powers and stability and the product of Jacobians in module 7. And the network cannot freely tune that factor to 1, for two reasons. Whh is also the matrix that does all of its computing. And the tanh slope 1−h2 equals 1 only at h=0: a unit that holds a large value, which is what storing something means, sits where the tanh is flatter and passes back less of its gradient. You can see it in the list above: the smallest of the three factors, 0.5636, belongs to step 2, where the state h2=0.5436 was the largest of the three states involved.
Now the same question for the LSTM. Its memory update is
ct=ftct−1+itgt.
Ask how ct moves when only ct−1 moves. The gates ft, it and the candidate gt are computed from xt and ht−1, not from ct−1, so along this direct route they are constants. The second term, itgt, does not contain ct−1 at all, so its slope is 0. The first term is the constant ft times ct−1, so its slope is ft. Adding the two slopes:
∂ct−1∂ct=ft+0=ft,∂ct−1∂L=ft∂ct∂L.
One step back along the cell state multiplies the gradient by the forget gate, and by nothing else: no weight matrix, no tanh slope. In the four-step figure the forget gate is 0.9991 at every step, so from c4 back to c1:
from c4 to c3: ×0.9991
from c4 to c2: 0.9991×0.9991=0.9982
from c4 to c1: 0.9982×0.9991=0.9973
The RNN kept 0.2702 over the same three steps; the cell path keeps 0.9973. The figure from the previous lesson, opened at its last step, puts the two side by side.
With n units the same argument runs entry by entry. Unit j's memory ct,j depends only on its own ct−1,j, with slope ft,j, so along this direct path
∂ct−1∂ct=diag(ft),∂ct−1∂L=ft⊙∂ct∂L.
After k steps back along the cell path, the gradient on unit j has been multiplied by ft,jft−1,j⋯ft−k+1,j: a product of ordinary numbers, one per step, each between 0 and 1. There is no matrix to raise to a power and no mixing between units.
Go slower: The cell-path Jacobian, entry by entry
Step 1: one entry. Write the cell update for unit j: ct,j=ft,jct−1,j+it,jgt,j. The gates are computed from xt and ht−1, not from ct−1. So with xt and ht−1 held fixed, ft,j, it,j and gt,j are constants. Differentiate with respect to entry k of ct−1: the term it,jgt,j gives 0, and the term ft,jct−1,j gives ft,j when k=j and 0 when k=j:
∂ct−1,k∂ct,j={ft,j0if k=j,if k=j.
Step 2: the matrix. Collect these partial derivatives into the Jacobian (row j, column k). Only the diagonal entries are nonzero, and they are ft,1,…,ft,n. That matrix is diag(ft).
Step 3: many steps. By the chain rule as matrix multiplication (module 5), the direct path over k steps has the product of the one-step Jacobians:
diag(ft)diag(ft−1)⋯diag(ft−k+1)=diag(ft⊙ft−1⊙⋯⊙ft−k+1).
The equals sign holds because multiplying diagonal matrices multiplies their diagonals entry by entry: entry (j,j) of the product is ft,jft−1,j⋯ft−k+1,j, and every off-diagonal entry stays 0.
Step 4: backwards. Backprop multiplies the gradient by the transpose of the Jacobian. A diagonal matrix is its own transpose, so ∂L/∂ct−1=diag(ft)∂L/∂ct. Multiplying a vector by a diagonal matrix scales each entry by the matching diagonal entry, which is ft⊙∂L/∂ct.
Step 5: the RNN for comparison. For ht=tanh(at) with at=Whhht−1+Wxhxt+b, the chain rule gives ∂ht/∂at=diag(1−ht2) (tanh acts entry by entry) and ∂at/∂ht−1=Whh, so ∂ht/∂ht−1=diag(1−ht2)Whh: a diagonal matrix of tanh slopes times a full matrix. Its transpose is Whh⊤diag(1−ht2). Since ∣Av∣≤σ1(A)∣v∣, where σ1 is the largest singular value of module 4 (not the sigmoid), each backward step changes the gradient's length by at most σ1(Whh)⋅maxj(1−ht,j2).
In short: one step back through a plain RNN multiplies the gradient by a full matrix and a set of tanh slopes; one step back along the cell state multiplies each unit's gradient by that unit's forget gate.
Over one step the difference between 0.9991 and 0.72 looks modest. Over long gaps it decides everything, because the factors multiply. Say a unit's forget gate sits at 0.97. Then 0.9710≈0.7374, and fifty steps are five blocks of ten, so 0.9750=(0.9710)5≈0.73745≈0.22 of the gradient survives. An RNN whose per-step factor is 0.8 has 0.810≈0.1074 and keeps 0.850≈0.10745≈0.000014. One is a signal the network can learn from; the other is lost among everything else.
The chart below answers the same question for up to 100 steps. The horizontal axis is k, the number of steps back. The vertical axis is how much of the gradient is left, on a log scale: the line marked 1 means unchanged, and each labelled grid line is 100,000 times the one below it. On a log scale, shrinking by the same factor at every step draws a straight line. The lime line is a plain RNN with 8 tanh units whose recurrent matrix has every singular value equal to ρ (starting at 1), with the tanh slopes included; the dashed white line is the bound ρk. The amber line is the LSTM's cell path with every forget gate equal to σ(bf), starting at bf=3. The table beside it gives each line's factor per step and what is left after 10, 50 and 100 steps.
This is the link from module 4 that the whole part has been building toward. Repeated multiplication by a matrix shrinks or grows along its eigenvectors according to its eigenvalues. A diagonal matrix's eigenvalues are its diagonal entries. Along the cell path the per-step matrix is diag(ft), so its eigenvalues are the forget-gate values themselves, set directly by gates that the network learns. When a unit needs to remember, the network can push its forget gate toward 1 with a large bias, and that unit's product of forget values stays near 1. It cannot explode either, because no forget value exceeds 1.
Because the factors multiply, a few steps with a lower forget value can cost more than many steps close to 1. The exercise below puts numbers on that: follow a gradient by hand through a forget gate that is not the same at every step.
Work it outA gradient along the cell path
Counting back from ct, a unit's forget gate is 0.9 at each of the 10 most recent steps and 0.99 at each of the 40 steps before those. A gradient of size 1 arrives at ct. How large is the part of it that reaches ct−50 along the direct cell path? Give 3 decimal places.
factor
Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.
It would be easy to overclaim here, so this section states exactly what was shown. The cell state reaches the next step along two routes, not one.
The direct route, ct−1→ct, through the forget gate. Its Jacobian is diag(ft).
The route through the output.ct−1 also produces ht−1=ot−1⊙tanh(ct−1), and ht−1 feeds the forget gate, the input gate and the candidate at step t through their U matrices, so it changes ct too. (It feeds the output gate as well, but the output gate does not touch ct.)
The multivariable chain rule adds the two routes. The second route is itself a chain of two moves: from ct−1 to ht−1, whose Jacobian is diag(ot−1⊙(1−tanh2(ct−1))) because both operations act entry by entry, then from ht−1 to ct, which we call ∂ct/∂ht−1. So the total derivative is
dct−1dct=diag(ft)+∂ht−1∂ctdiag(ot−1⊙(1−tanh2(ct−1))).Go slower: What ∂ct/∂ht−1 is made of
Start from ct=ft⊙ct−1+it⊙gt and hold ct−1 fixed. Three of the four factors depend on ht−1: ft, it and gt.
The product rule, entry by entry. Changing ft changes ct at the rate ct−1; changing it, at the rate gt; changing gt, at the rate it. So ∂ct/∂ht−1=diag(ct−1)∂ht−1∂ft+diag(gt)∂ht−1∂it+diag(it)∂ht−1∂gt.
Each gate's Jacobian.ft=σ(Wfxt+Ufht−1+bf), so by the chain rule ∂ft/∂ht−1=diag(ft⊙(1−ft))Uf, using the sigmoid's slope s(1−s). In the same way ∂it/∂ht−1=diag(it⊙(1−it))Ui and ∂gt/∂ht−1=diag(1−gt2)Ug.
Put together. Each of the three terms is a diagonal matrix times a full U matrix, the same shape as the RNN's diag(1−ht2)Whh. In the four-step figure every U is zero, so this route is exactly zero there and 0.9973 is the whole story. In a trained LSTM the U matrices are not zero.
That second term is a full matrix built from the U matrices, and it behaves much like an RNN's Jacobian: it can shrink or grow a gradient geometrically. The full state of an LSTM is the pair (ct,ht), and its one-step Jacobian is a 2n×2n block matrix with paths through both. So the precise claim is this: among the many paths back through time, there is one whose per-step factor is a diagonal matrix of gate values that the network can hold near 1. When the forget gates are near 1, that path by itself carries a usable learning signal across long gaps, even if every other path has faded. And when the forget gates are not near 1, as with a bias of 0 in the chart above, the cell path fades like the others.
The original LSTM had no forget gate at all: its cell update was ct=ct−1+it⊙gt, the accumulator of the previous lesson with an input gate, so the cell-path Jacobian was exactly the identity. That was the constant error carousel. The forget gate gave up that exactness in exchange for the ability to reset, and it hands the network a dial for how long each unit remembers.
To recap: the direct cell path multiplies a gradient by one forget value per unit per step, so it survives exactly as long as those values stay near 1. Other paths exist and behave like an RNN's. Test the distinction on a few tempting explanations.
Quick checkWhy the cell path works
Why can an LSTM keep the backward factor along its cell path close to 1 for many steps, when a plain RNN finds this hard?
Choose one answer, then check.
Last, put the whole argument in your own words. It is the argument the rest of this module relies on.
In your own wordsWhat the gates actually fixed
A colleague says: "LSTMs solved vanishing gradients because they have gates." In your own words, explain what exactly lets gradients cross long gaps in an LSTM, why a plain RNN has trouble with the same gaps, and what the honest limits of the claim are.
A few sentences first: 0 of 60 characters.
Saved in this browser as you type.
The next lesson puts the gates to work: you choose all twelve weights of a one-unit cell yourself and make it latch, hold, count and toggle. It ends by returning to this lesson's product of forget values, at the moment when a network starts training.