All labs Animated labs The chapter Course home

Lab 09The vanishing gradient

To learn that the word at step 47 depends on the word at step 1, a recurrent network must send a blame signal 47 steps backward, and every step multiplies it by a factor. In a plain RNN you do not control that factor. In an LSTM the forget gate sets it. Move the sliders and watch what survives. The numbers come from rnn.py and gated.py, so they match the chapters.

1. How much blame arrives

RNN factor:

Log scale. The line at 1 is a gradient that arrives at full strength. The dashed vertical line is the 47-step example of the chapter.

What to look for

  • Set the RNN factor to 0.5. After 47 steps only 7.105e-15 of the gradient is left. Below 1 it dies, above 1 it explodes, and only exactly at 1 does it survive, with nothing keeping it there.
  • Set it to 1.2 and the same 47 steps give 5266. That is the exploding case, and clipping handles it.
  • An LSTM does not remove the decay. It puts the decay rate under the network's control through the forget gate f = σ(b_f).
  • Now set b_f to 0. The gate starts at 0.5, which is exactly the plain RNN's decay, so an LSTM initialised this way looks incapable of long dependencies. Raise it to 2 and 47 steps retain 2.565e-03, about 3.6e+11 times more signal.

2. The forget-gate bias, as the chapter tabulates it

At the start of training the weights are small, so f is roughly σ(b_f). The default b_f = 0 is the most commonly skipped line in an LSTM implementation, and skipping it looks exactly like the model being incapable.

3. Clipping the exploding case

If the gradient is longer than a threshold, rescale it to the threshold and keep its direction. Move the sliders to feed in a gradient of any size.

The grey arrow is the gradient and the green one is what the optimiser uses. Clipping changes how far the step goes and never which way it points. It does nothing for the vanishing case, where the gradient is already too small.

Where this fits

This is Recurrent Networks, where the product of Jacobians appears, and Gated Recurrence, where the LSTM's forget gate puts the factor under the network's control. Run rnn.py and gated.py to print the same tables as text.