Chapter 12 left the recurrent network with a precise complaint, not a vague one.
Equation (12.5) showed that a gradient travelling back through time is multiplied by one Jacobian per step. Each Jacobian is , so the product behaves like a number raised to the power of the distance.
Below one it vanishes. Above one it explodes. Nothing holds it at one.
The diagnosis contains the cure. If repeated multiplication is what destroys the gradient, then give the state a path that does not multiply.
A gated network does exactly that. It keeps a memory that is updated by addition, and it learns how much of the old memory to carry forward.
The long short-term memory network carries two vectors rather than one.
The cell state is the memory. The hidden state is what the rest of the network sees.
Three gates control the traffic. Each is a sigmoid layer reading the same input, the previous hidden state concatenated with the current word, so each produces numbers between and .
A gate value of closes the channel completely. A value of opens it. Everything in between is a partial opening, which is what makes the whole thing differentiable.
The cell state is then updated in one line, and that line is the reason the architecture exists:
Read it as two decisions. The forget gate says what fraction of the old memory survives. The input gate says how much of the new candidate to admit.
The hidden state is a filtered view of that memory:
So the network can hold something in for many steps without exposing it, and reveal it only when the output gate opens.
Differentiate Equation (13.1) along the cell state path. The result is startlingly simple:
No weight matrix. No derivative. The gradient is multiplied by the forget gate and nothing else.
Compare the two routes directly.
| distance | RNN, factor | LSTM, | LSTM, |
|---|---|---|---|
| 1 | |||
| 10 | |||
| 47 | |||
| 100 | |||
| 500 |
At a gradient still carries one per cent of its strength after steps. At it is gone before step .
Be careful about what has changed. The LSTM did not remove the decay. It put the decay rate under the network’s control.
That is enough, and it is the whole contribution. is learned, so the network can decide to keep a memory alive when the task rewards it. was never anybody’s decision.
Here is a detail the lectures single out, and it is worth a section because skipping it is so common.
At the start of training the weights are small and random, so Equation (13.4) gives .
The default initialisation sets . That makes .
| after 10 | after 47 | after 100 | ||
|---|---|---|---|---|
| 0 | 0.5000 | |||
| 1 | 0.7311 | |||
| 2 | 0.8808 | |||
| 3 | 0.9526 | |||
| 5 | 0.9933 |
Look at the first row against the previous table. A default-initialised LSTM decays at exactly the rate the plain RNN did.
So it starts out no better than the architecture it was invented to replace, and it has to climb out of that by learning.
Set instead, as recommend, and begins at . After steps the gradient retains rather than .
That is times more signal, from changing one number before training starts.
The lesson generalises past LSTMs. A model that appears incapable of a task is sometimes a model whose initialisation put it somewhere it could not leave.
Take a cell with two units and an input of size two, and set .
Every gate reads the same concatenated vector:
| gate | formula | value |
|---|---|---|
| forget | ||
| input | ||
| candidate | ||
| output | ||
| cell | ||
| hidden |
Split the cell update into its two terms and the arithmetic becomes plain.
| kept from the past, | |
| added this step, | |
| new cell state, |
The forget gate sits at and , so most of the old memory survives the very first step. That is the initialisation already doing its job.
And notice how was reached. By addition, not by a matrix multiplication. That single structural fact is the difference from Equation (12.8), and it is why Equation (13.3) looks the way it does.
The gated recurrent unit asks whether four gates were ever necessary.
It keeps one state vector instead of two, and two gates instead of three.
Equation (13.11) is the interesting line. The coefficients are and , so they sum to one.
The LSTM used two independent gates for those two jobs, and could in principle forget everything while adding nothing. The GRU ties them, so forgetting more means admitting more.
The reset gate does a different job. It decides how much of the old state the candidate may see, which lets the unit propose something genuinely new when the topic changes.
Running the same numbers as above through a GRU gives this.
| gate | formula | value |
|---|---|---|
| update | ||
| reset | ||
| candidate | ||
| hidden |
Each gate reads a vector of length and produces numbers, so it costs parameters. Count the gates and you have the model.
| model | gates | parameters | what they are |
|---|---|---|---|
| plain RNN | 1 | 300,500 | one state update |
| GRU | 3 | 901,500 | update, reset, candidate |
| LSTM | 4 | 1,202,000 | forget, input, candidate, output |
Those figures use and . A GRU is three quarters the size of an LSTM, which here is fewer parameters and a proportionally faster step.
On most tasks the two score within noise of each other . So the choice is usually made on size and speed rather than on quality, and the GRU often wins on those.
Try it yourself.
code/worked_examples/gated.pyproduces every table in this chapter.--highwaycompares the two decay routes,--biasshows what does,--stepruns the single cell through both architectures, and--paramscounts the gates.
Gating worked. From roughly 2014 to 2017 the LSTM was the default for almost every sequence task, and it powered the first generation of neural machine translation.
Two problems remained, and neither is about gradients.
The first is sequential computation. Equation (13.1) needs before it can compute . So the time steps cannot be computed in parallel, however many processors are available.
That is a hard ceiling on training speed, and it arrived exactly when hardware was becoming massively parallel.
The second is the bottleneck. Everything the model knows about the prefix has to fit in one fixed-size vector.
Gating made that vector’s contents last longer. It did not make the vector bigger, and a whole paragraph still has to be squeezed into it.
Both problems have the same shape. They come from insisting that information travel through the sequence, one step at a time.
The next idea removes that insistence. Let every position look directly at every other position, with no intervening steps. Distance stops mattering, and the computation parallelises because nothing waits for anything.
That is attention, and it is the subject of Chapter 16.
Before that, one chapter closes Part III on a different note. Everything from Chapter 10 to here has built models. None of it has said how you would know whether one is any good.
Chapter 14 answers that. It takes the simplest task these models are put to, and uses it to establish the evaluation discipline the rest of the book depends on.
is the original LSTM paper. introduce the forget gate and the bias initialisation of Section 1.3. propose the GRU. search thousands of gated architectures and find no consistent winner, which is the best evidence that the gating idea matters more than its packaging. is the clearest diagram-led explanation of the LSTM in existence and worth reading alongside this chapter.
Trace one cell. Using the numbers of this chapter, verify , , and , then confirm and . Now set to zero by hand and recompute. What has the cell become, and what has it forgotten?
The gradient highway. Derive Equation (13.3) from Equation (13.1), treating as constant. Then explain why the corresponding derivative for the plain RNN, Equation (12.6), cannot be made this simple.
Initialise it wrong. Compute the surviving gradient after steps for and for . Express the ratio as a power of ten. Then argue why a paper reporting that LSTMs cannot learn long dependencies should be read with this table in hand.
Count the gates. For and , compute the parameter count for a plain RNN, a GRU and an LSTM. What fraction does the GRU save, and is that fraction independent of and ?
Tie the gates. The GRU uses and where the LSTM uses independent and . Describe a sequence for which the independent pair can do something the tied pair cannot. Then say why this rarely matters in practice.
What the reset gate is for. Set in Equation (13.10) and describe the resulting unit. Now set . Which of the two settings would you expect at a topic boundary, and why?
Name the remaining limit. Gating solved the vanishing gradient. State the two problems it did not solve, and for each one explain in two sentences why attention removes it.