S1427 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ht=0.9ht−1+xt. How much does an input from 50 steps ago still contribute to ht?

Name it

Recurrent networks

A recurrent network keeps a hidden state 𝐡t and updates it from the previous state and the current input:

𝐡t=tanh⁡(W𝐡t−1+U𝐱t+𝐛),𝐲t=V𝐡t+𝐜

The same weights serve every step, so with H hidden units and D inputs the recurrence has H(H+D)+H 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, ∏diag(ϕ′)W, so its size grows or shrinks roughly as ρk, 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:

𝐜t=𝐟t⊙𝐜t−1+𝐢t⊙𝐠t,𝐡t=𝐨t⊙tanh⁡(𝐜t)

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 4[H(H+D)+H] parameters. The GRU merges the cell and state with two gates and a candidate: 3[H(H+D)+H].

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
  1. One block. Recurrent, input and bias weights: 256×(256+100)+256=91,392
  2. 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): GRU=3×91,392=274,176LSTM=4×91,392=365,568
  3. 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
  1. Bias of 1. σ(1) = 0.731, so the cell's contents fall by that factor each step, a time constant of τ=−1/ln⁡0.731=3.2steps barely better than a simple network.
  2. Bias of 5. σ(5) = 0.9933 gives τ=149steps
  3. 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
  1. Simple network. Contributions from beyond the window are scaled by at most 0.95100=0.006 so truncation loses almost nothing, because the network couldn't use those steps anyway.
  2. LSTM. Its contributions are scaled by 0.999100=0.90 so truncation throws away dependencies the cell could carry.
  3. 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 ht=0.9ht−1+xt.

(a)

How much of an input from 5 steps ago remains in ht?

(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 ht=wht−1+uxt, starting from zero.

Step 1

What is hk 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°.

Explain why

Why do gradients in a recurrent network vanish or explode over long sequences?

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.