S14 · Lesson 27 · Recurrent networks
Networks with state, the recurrent network as a nonlinear IIR filter, backpropagation through time and why its gradients explode or vanish, clipping, and the gated LSTM and GRU cells that carry memory across long sequences
A feedforward network sees each input on its own. Speech, sensor streams and text need memory of what came before. A recurrent network adds it the way an IIR filter does: by feeding its own output back. That feedback brings a filter's problems along. A pole inside the unit circle forgets the past exponentially fast, one outside it blows up, and training a recurrent network runs into the same two failures in its gradients.
- The curves show how much a hidden state still depends on something that happened k steps earlier.
- The solid curve is a simple recurrent network whose feedback multiplies the state by a gain ρ each step (the weight times the activation's slope): below 1 it fades exponentially, above 1 it explodes.
- The dashed curve is an LSTM's memory cell, multiplied each step by its forget gate σ(b): a bias of 3 or more holds memory for dozens of steps, and the gradient through the cell follows the same product.
Predict
A linear recurrent network updates . How much does an input from 50 steps ago still contribute to ?
The input is multiplied by 0.9 at every step: = 0.0052. The network is a one-pole filter with its pole at 0.9 and a memory time constant of = 9.5 steps. Remembering something for hundreds of steps needs a pole within a fraction of a percent of 1, which a plain recurrent network can't keep while also learning.
Name it
Recurrent networks
A recurrent network keeps a hidden state and updates it from the previous state and the current input:
The same weights serve every step, so with H hidden units and D inputs the recurrence has parameters whatever the sequence length. Linearized, it is a multichannel IIR filter whose poles are the eigenvalues of W (times the activation slopes): stable when they lie inside the unit circle.
Backpropagation through time
Unrolled over T steps, the network is a T-layer feedforward network with shared weights, and backpropagation applies. The gradient of the state at time t with respect to the state k steps earlier is a product of k Jacobians, , so its size grows or shrinks roughly as , with ρ the largest gain per step. With ρ < 1 the gradient vanishes, and the network can't learn dependencies longer than a few time constants; with ρ > 1 it explodes. Gradient clipping rescales the gradient whenever its norm exceeds a threshold θ, , which cures explosions but not vanishing. Truncated backpropagation stops after a fixed window of steps to bound the cost.
Gated cells
The LSTM adds a memory cell updated additively, under gates that are sigmoids of the input and state:
Along the cell, the gradient is multiplied only by the forget gates, which the network can hold near 1; initializing the forget gate's bias to 1 or more starts it with long memory. Four gates and candidates give parameters. The GRU merges the cell and state with two gates and a candidate: .
Worked example
Count the parameters of a simple recurrent layer, a GRU and an LSTM, each with 256 hidden units on 100 inputs.
Show the solution
- One block. Recurrent, input and bias weights:
- Count the blocks. The simple layer has one, the GRU three (update gate, reset gate, candidate) and the LSTM four (forget, input and output gates, candidate):
- Work per step. Each step costs about as many multiply-adds as parameters, so the LSTM is four times the work of a simple layer of the same width.
Answer the simple layer has 91,392 parameters, the GRU 274,176 and the LSTM 365,568.
Worked example
An LSTM's forget gate is initialized with a bias of 1, or of 5, and inputs that leave its activation near the bias. How long does the cell remember?
Show the solution
- Bias of 1. σ(1) = 0.731, so the cell's contents fall by that factor each step, a time constant of barely better than a simple network.
- Bias of 5. σ(5) = 0.9933 gives
- Why start positive. That is why forget biases are initialized positive: training can shorten a memory that starts long more easily than it can discover one that starts short, since the gradient through a short memory has already vanished.
Answer the cell remembers for about 3.2 steps with a bias of 1, and about 149 steps with a bias of 5.
Worked example
A 10,000-step sequence is trained with backpropagation truncated to 100 steps. How much gradient is lost for a simple network with per-step gain 0.95, and for an LSTM cell with forget gate 0.999?
Show the solution
- Simple network. Contributions from beyond the window are scaled by at most so truncation loses almost nothing, because the network couldn't use those steps anyway.
- LSTM. Its contributions are scaled by so truncation throws away dependencies the cell could carry.
- What to do. Long-memory models need longer windows, or carrying the state across windows while cutting only the gradient.
Answer the simple network's lost contributions are scaled by at most 0.006, so almost nothing is lost; the LSTM's are scaled by 0.90, so truncation discards dependencies it could use.
Check
A simple recurrent layer has 128 hidden units on 10 inputs, followed by a linear output layer of 20 units.
(a)
How many parameters does the recurrent layer have?
(b)
And the output layer?
Check
A recurrent layer with 256 hidden units reads 32 inputs per step.
(a)
How many parameters does an LSTM layer have?
(b)
And a GRU layer?
Check
A linear recurrent unit updates .
(a)
How much of an input from 5 steps ago remains in ?
(b)
What is its memory time constant?
Check
The backward signal through a recurrent network is multiplied by a gain of about 0.9 per step.
(a)
By what factor has it changed after 10 steps?
(b)
After how many steps has it changed by a factor of 1000?
Check
An LSTM's forget gate sits at its bias, 1.
(a)
What fraction of the cell's contents survives each step?
(b)
What is the cell's memory time constant?
Check
A gradient's norm is 40, and training clips it to norm 1.
(a)
By what factor is the gradient scaled?
(b)
What does a component of −6 become?
Check
Why can an LSTM learn dependencies hundreds of steps long when a simple recurrent network can't?
Check
A linear recurrent unit updates , starting from zero.
Step 1
What is after a unit impulse at time 0?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What does settle to under a constant input x?
Explain why
Why do gradients in a recurrent network vanish or explode over long sequences?
Backpropagating from a late output to an early input passes through every step in between, and each step multiplies the signal by the recurrent weights and the activation's slope. A product of many such factors behaves like a power: if the typical gain per step is below one, the gradient shrinks exponentially and the network can't learn that early inputs matter; if it's above one, the gradient grows exponentially and training diverges. It's the same as a feedback filter with its pole inside or outside the unit circle. Clipping tames explosions, and gated cells avoid vanishing by giving the memory an additive path whose gain the network controls directly.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.