Module 7 · Recurrent Neural Networks for Time Series Forecasting

Vanishing and Exploding Gradients

A recurrent network learns by sending its error backward through time. At every step back, that signal is multiplied by roughly the same factor. A little below 1 and it vanishes. A little above 1 and it explodes. Change the weights and the length of the sequence, then see what an LSTM does differently.

Explained in Part 2: the RNNPart 3: the LSTM

How large the weights from state to state are. ×1 is how Keras starts a simple RNN.
The length of the sequence. The signal has to travel back through every step.
Compare with an LSTM
Where the forget gate starts.
Gradient clipping
The standard patch for exploding gradients.
Try

How much of the error signal is left 16 units, measured on random input

Simple RNN All of it (100%)

Move over the chart to read one step.

Where the weight update comes from share by step

Simple RNN

Size of the update-simple RNN, before clipping
After clipping-clipping is off

Kept per step-
Left after 10 steps-
Left after 50 steps-
Left at the first step-
-

1 · The same factor, again and again

Going back one step multiplies the signal by the slope of tanh times the recurrent weights. That factor is about the same at every step. 0.84 does not sound bad, but 0.84 multiplied by itself 50 times is less than 2 parts in 10,000.

2 · tanh is only half of the story

People blame tanh, because its slope is never more than 1. The weights are the other half, and they can push the other way. Make them twice as large and the signal grows instead of fading.

3 · Clipping fixes one side

For exploding gradients there is a simple patch: put a ceiling on the size of the update. For vanishing gradients there is no patch. You cannot clip a signal back into existence.

4 · What the LSTM changes

Along the cell state, one step back multiplies the signal by one thing: the forget gate. Near 1, the signal survives. Half closed, it dies faster than in the simple RNN. An LSTM does not remove the problem. It gives the gradient a path and lets training keep it open. See the gates move in the LSTM conveyor belt.