§10.0 Networks built for sequences
A recurrent neural network recurrent neural network (rnn) A family of neural networks for processing a sequence of values x^(1),…,x^(τ). Its power comes from sharing the same parameters across every time step, letting it handle variable-length sequences and generalize across positions. defined in ch. 10 — open in glossary (RNN, Rumelhart 1986) is to a sequence sequence An ordered list of values x^(1),…,x^(τ) (e.g. words, audio frames, or measurements over time). Sequences are what RNNs specialize in, as grids are what CNNs specialize in. defined in ch. 10 — open in glossary what a convolutional network is to a grid. A CNN processes an image ; an RNN processes an ordered list of values — words in a sentence, frames of audio, measurements over time. And just as CNNs scale to large images and handle variable sizes, RNNs scale to sequences far longer than an unspecialized network could, and most can process variable-length inputs.
The single idea that turns a feedforward net into a recurrent one is sharing parameters across time. Consider the two sentences “I went to Nepal in 2009” and “In 2009, I went to Nepal.” To extract the year the narrator went to Nepal, we want the model to spot 2009 whether it is the sixth word or the second. A fully-connected feedforward net over fixed-length sentences has separate weights for each input position, so it must relearn the same rule at every slot. An RNN instead applies the same update rule to each step — one set of weights, reused everywhere.
Two ways to share across time — convolution vs recurrence
A 1-D convolution across a temporal sequence — a time-delay neural network time-delay neural network (tdnn) A 1-D convolutional network applied to time series (Lang & Hinton 1988) — the first CNNs trained with back-propagation, preceding LeCun's 2-D image CNNs. defined in ch. 9 — open in glossary — also shares parameters across time by sliding one kernel along the sequence. But it is shallow: each output depends on a small window of neighboring inputs. Recurrence shares parameters differently: each output is a function of the previous outputs, produced by the same update rule applied again and again. That recurrent formulation shares parameters through a very deep computational graph — one layer per time step.
This chapter extends the computational graph (§6.5) to include cycles: the present value of a variable influencing its own value at a future step. Those cycles are what define a recurrent network.
§10.1 Unfolding computational graphs
Start from the classical form of a dynamical system — a value that feeds back into its own definition:
The recurrence, and its unfolding
| the STATE of the system at step t — everything carried forward | vector | |
| the transition function, parametrized by θ — the SAME θ at every step | shared map | |
| refers back to the same definition one step earlier — this back-reference is what makes it recurrent | vector | |
| unfolding = apply the definition τ−1 times, turning the recurrence into a plain nested composition with no cycle | acyclic |
Unfolding for yields an expression with no recurrence left — just composed with itself — which is a plain directed acyclic graph:
Now drive the system with an external input , and rename the state to signal that it is the network’s hidden units:
The state now carries information about the whole past sequence. A network trained to predict the future from the past learns to use as a lossy summary state (of an rnn) The hidden vector h^(t) carried from one time step to the next. It is a lossy summary of the task-relevant aspects of the past sequence x^(1),…,x^(t). defined in ch. 10 — open in glossary of the task-relevant history — necessarily lossy, because it maps an arbitrary-length past into a fixed-length vector. A language model need only keep enough to predict the rest of the sentence; the most demanding case is an autoencoder (chapter 14) that must let approximately reconstruct the input.
The same recurrence can be drawn two ways — and the unfold operation maps one to the other:
Writing the state after steps as one big function of the entire past, unfolding factorizes it into repeated applications of the single function :
That factorization buys two decisive advantages:
- Fixed input size. The model is specified as a transition from one state to the next, not as a function of a variable-length history — so its input size never changes, however long the sequence.
- One shared function. The same transition with the same parameters is used at every step. So we learn a single model that generalizes to sequence lengths never seen in training, from far fewer examples than a separate per length would need.
The folded graph is succinct; the unfolded graph makes the flow of information explicit — forward in time to compute outputs and losses, backward in time to compute gradients.
§10.2 Recurrent neural networks
Armed with unfolding and parameter sharing, we can design many RNNs. The workhorse — the one we return to all chapter — produces an output every step and has hidden-to-hidden recurrence (fig 10.3). Toggle it between the folded circuit and the unfolded graph, grow the length , and highlight a weight matrix to watch it get reused at every step:
Only 3 weight matrices — U, W, V — plus biases parametrize the whole model. The parameter count is independent of τ: unfolding reuses one transition function f across all steps, so the model generalizes to sequence lengths never seen in training.
Three important design patterns recur throughout the chapter — they differ in where the recurrence lives, and that changes both their power and how cheaply they train:
| Recurrence | Output | Power & training | |
|---|---|---|---|
| Fig 10.3 | hidden → hidden | one per step | Most powerful; expensive |
| Fig 10.4 | output → hidden | one per step | Weaker; parallelizable |
| Fig 10.5 | hidden → hidden | single, at the end | Summarizer |
“Universal” — an RNN is a Turing machine
The fig 10.3 RNN is universal: any function computable by a Turing machine can be computed by a finite-size RNN of this form (Siegelmann & Sontag 1995 use 886 units). The Turing machine’s input is a specification of the function to compute, so a single fixed network suffices for all problems. This is a statement about representational power, not about how easily gradient descent finds the right weights.
Forward propagation. Take the hidden nonlinearity to be and the output to be discrete (predicting words or characters), so holds unnormalized log-probabilities and softmax normalizes them. Starting from an initial state , each step runs:
Forward propagation (eqs 10.8–10.11)
| the RECURRENT term — the previous state carried forward through the shared hidden→hidden matrix W | the memory | |
| the INPUT term — this step's input mapped through the shared input→hidden matrix U | the new evidence | |
| hidden pre-activation: recurrent term + input term + bias b | |h| vector | |
| the new hidden STATE, squashed into (−1, 1) — the lossy summary of the past | |h| vector | |
| logits read out of the state through the shared hidden→output matrix V + bias c | |y| vector | |
| softmax turns the logits into a probability distribution over the discrete output | probabilities |
Try a tiny numeric step. Suppose a scalar state with , , , previous state and input . Then , so — the input pushed the state up, and the recurrent term nudged it further using the same every step.
The loss over the whole sequence is simply the sum of the per-step negative log-likelihoods:
Total sequence loss (eqs 10.12–10.14)
| per-step loss: the negative log-likelihood of the correct target y⁽ᵗ⁾ given all inputs up to t | scalar | |
| the sequence loss is just the SUM of per-step losses over all τ steps | scalar | |
| read off as the y⁽ᵗ⁾ entry of the output distribution ŷ⁽ᵗ⁾ | probability |
The price of power. Computing this gradient runs a forward pass left-to-right through the unrolled graph, then a backward pass right-to-left. The runtime is and cannot be parallelized — each step needs the previous one — and every forward state must be stored until the backward pass reuses it, so memory is too. Back-propagation applied to the unrolled graph is called back-propagation through time back-propagation through time (bptt) Ordinary back-propagation applied to the unfolded computational graph of an RNN. Cost and memory are O(τ) and the forward/backward passes are inherently sequential (not parallelizable across time). defined in ch. 10 — open in glossary (BPTT). The hidden-to-hidden RNN is very powerful, but expensive to train. Is there a cheaper alternative? That question — and the exact BPTT gradient equations — come next: teacher forcing and computing the gradient.
Check yourself — unfolding and the recurrent neural network
1.Why does a recurrent network share the same parameters across every time step, rather than learning separate weights per position?
2.What are the two advantages the book gives for unfolding a recurrent computation into a computational graph?
3.In the workhorse RNN (fig 10.3), what do the weight matrices U, W, and V connect?
4.The RNN of figure 10.3 that produces an output each step with hidden-to-hidden recurrence is described as “universal.” What does that mean, and what is its training cost?
5.In the UnfoldLab widget, you increase τ from 3 to 5 and highlight the W matrix. What do you observe, and what does it teach?