↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What a recurrent network is

Text arrives one symbol at a time, and what comes next depends on what came before. A recurrent network (RNN) reads a sequence step by step and carries a hidden state , a vector that is its only memory of the past. Elman's simple recurrent network computes the new state from the current input and the previous state:

The same weights are used at every step, so one network handles sequences of any length. The network on this page reads text character by character: each of 65 characters becomes a vector of 64 numbers, one tanh layer of 256 units updates the state, and a linear layer with a softmax predicts the next character, 103,297 parameters in all. It was trained here on Tiny Shakespeare with exactly the LSTM tab's batches: 4,000 steps of 64 sequences of 128 characters, Adam at learning rate 0.002, gradients clipped to norm 1, seed 0.

One step

The panel headed by this equation spells out the update for the selected character. The character's vector and the previous state each go through a matrix, their sum goes through tanh, and that is the whole new state. Nothing is kept aside: whatever the network wants to remember has to survive being multiplied by and squashed again at every step. tanh is flat near ±1, and the panel counts the units pinned there (). The hidden state heatmap shows all 256 units over the whole speech.

Predicting the next character

From the state, a linear layer and a softmax give a distribution over the 65 characters, and the loss is the cross-entropy of the real next character, averaged over every position:

The page shades a validation speech, text the network never trained on, by this surprise per character. The validation loss fell from 4.170 nats per character before training to 1.615 after; the LSTM, with 350,593 parameters, reaches 1.564 on the same text. To generate, the network samples a character from , feeds it back in and repeats. The Sampled text panel divides the logits by a temperature of 0.8 before the softmax, which sharpens the distribution, and shows samples from before training, random characters, up to step 4,000.

Units that track the text

Nobody tells the units what to track, but some come to follow simple facts about the text that help prediction. The Units that track the text panel correlates every unit with three counters over 5,000 validation characters. The RNN's best match for inside a speaker's name is unit 68 with , and for characters since the last space unit 18 with ; for characters since the start of the line its best is only , while the LSTM's best cell unit follows line position with . Counting through a line of dozens of characters means holding a value for dozens of steps, which is what the next sections show an RNN cannot easily do. Karpathy, Johnson and Fei-Fei found interpretable units of this kind in character models.

Backpropagation through time

To train, the network is unrolled into one deep feed-forward network with a copy of the cell per character, and backpropagation runs through it from the last step to the first: backpropagation through time. Going back one step multiplies the gradient by the transposed Jacobian of the update, which bounds how much of it survives:

Over steps these factors multiply. If they are mostly below 1 the gradient shrinks exponentially and vanishes; if they are above 1 it can explode. Bengio, Simard and Frasconi (1994) showed a trade-off: the conditions under which a simple recurrent network stores information robustly are the ones under which its gradients vanish. Pascanu, Mikolov and Bengio (2013) gave conditions on the largest singular value of , and the remedy for explosion this page uses, rescaling the gradient when its norm is too large.

Vanishing gradients, measured

The Vanishing gradients chart measures this on 64 validation windows of 128 characters: the norm of for the loss on the last character, as goes back. In the trained RNN, 100 characters back only of the last step's gradient is left, and a thousandth of it reaches 23 characters back. The LSTM's hidden state keeps at 100 characters and its cell , and a thousandth reaches 45 and 65 characters back.

Training made larger (its spectral norm grew from 1.135 to 6.902), which loosens the bound, but by the end 69 % of the units sit where and pass almost nothing back. Before training the gradient underflows to exactly 0 in float32 before it reaches the start of the window. The One step back chart shows the measured ratio for each step beside its bound.

Where it came from

Jordan (1986) and Elman (1990) fed a network's own state back into it so that it could find structure in time, and Werbos (1990) set out backpropagation through time as it is used here. The vanishing gradient, analysed in Hochreiter's 1991 diploma thesis and then by Bengio et al., kept such networks to short contexts, and the LSTM of the next tab was designed around it. Gated RNNs carried language modelling and translation until the transformer replaced recurrence with attention, which reaches any earlier position in one step.

Papers

  1. Finding Structure in Time Jeffrey L. Elman · Cognitive Science 1990 The simple recurrent network: a hidden state fed back as input, the tanh RNN this tab trains.
  2. Backpropagation through time: what it does and how to do it Paul J. Werbos · Proceedings of the IEEE 1990 Backpropagation through an unrolled recurrent network.
  3. Learning long-term dependencies with gradient descent is difficult Yoshua Bengio, Patrice Simard, Paolo Frasconi · IEEE Transactions on Neural Networks 1994 Why gradients through a recurrent network vanish as dependencies grow longer.
  4. On the difficulty of training Recurrent Neural Networks Razvan Pascanu, Tomas Mikolov, Yoshua Bengio · ICML 2013 Vanishing and exploding gradients through the Jacobian's singular values, and gradient-norm clipping.
  5. Visualizing and Understanding Recurrent Networks Andrej Karpathy, Justin Johnson, Li Fei-Fei · arXiv 2015 Character-level RNNs and LSTMs on text, with units that track line length, quotes and brackets.

See also