RNN Architecture › Unrolling Through Time

Michael BrenndoerferMay 7, 20253 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 74RNN Architecture › Unrolling Through Time
Reading section 6 of 19 · Unrolling Through Time · 3 minSection 6 of 19 · 3 min

Book contents

Section 6 of 19 · 3 min read

RNN Architecture › Unrolling Through TimeOne section · Sections in this chapter6 / 19 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

An RNN's recurrent connection forms a loop in the graph, which complicates analysis and differentiation. The standard approach is to unroll (also called unfold) the RNN by creating one copy of the network for each time step, eliminating the cycle. The result is a deep feedforward network where depth equals the number of time steps.

For a sequence of length TT, the unrolled RNN has TT copies of the hidden layer, connected in a chain:

h0→h1→h2→⋯→hT\mathbf{h}_0 \to \mathbf{h}_1 \to \mathbf{h}_2 \to \cdots \to \mathbf{h}_T

Each hidden state ht\mathbf{h}_t receives two inputs: xt\mathbf{x}_t from the current position in the sequence, and ht−1\mathbf{h}_{t-1} from the preceding hidden state. The same weight matrices Wxh\mathbf{W}_{xh}, Whh\mathbf{W}_{hh}, and bh\mathbf{b}_h are used at every copy. This is a key point: the unrolling does not create new parameters. The same transformation is applied at every time step, just to different inputs.

Unrolling Through Time

Unrolling an RNN means replacing the cyclic graph with an equivalent directed acyclic graph (DAG) by expanding the recurrence over all time steps. The resulting graph has one layer per time step, making it possible to apply standard backpropagation. The algorithm for computing gradients in the unrolled graph is called Backpropagation Through Time (BPTT).

Unrolling has an important implication: an RNN processing a sequence of TT steps behaves like a feedforward network of depth TT. This means the gradient must flow backward through TT layers of tanh⁡\tanh activations during training. Consider what happens as the gradient flows from hT\mathbf{h}_T back to h1\mathbf{h}_1. At each step, the chain rule multiplies by the Jacobian of the hidden state update, which includes WhhT\mathbf{W}_{hh}^T and the derivative of tanh⁡\tanh. If the spectral radius of Whh\mathbf{W}_{hh} is less than 1, these repeated multiplications shrink the gradient toward zero. If it is greater than 1, they amplify it exponentially. This instability is the root of the vanishing and exploding gradient problems, which the next chapter analyzes in mathematical detail.

In practice, PyTorch and other frameworks perform this unrolling automatically through their dynamic computation graph (autograd) system. You do not manually unroll the loop; you simply call the RNN forward pass for each time step and then call loss.backward(). The framework traces the computation graph and computes gradients through the recurrent chain correctly.

Truncated Backpropagation Through Time

For very long sequences, unrolling the full sequence and backpropagating through all TT steps is computationally expensive and memory-intensive. The full unrolled graph requires storing all intermediate hidden states for the backward pass, and for sequences of thousands of tokens this can exhaust GPU memory.

Truncated Backpropagation Through Time (truncated BPTT) addresses this by dividing the sequence into segments and backpropagating through each segment independently. The forward pass still carries the hidden state across segment boundaries (so the model sees information from earlier in the sequence during its forward computation), but the gradient does not flow backward across segment boundaries.

The tradeoff is between computational feasibility and gradient signal quality. With truncation length kk, the model can only learn dependencies up to kk steps in the past from gradient information, even if its forward hidden state in principle carries information from further back. In practice, truncation lengths of 50 to 200 tokens are common, and the model learns to encode important information in the hidden state in ways that remain useful even when gradients are truncated.