RNN Architecture › Unrolling Through Time
Language AI HandbookAll chapters 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 , the unrolled RNN has copies of the hidden layer, connected in a chain:
Each hidden state receives two inputs: from the current position in the sequence, and from the preceding hidden state. The same weight matrices , , and 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 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 steps behaves like a feedforward network of depth . This means the gradient must flow backward through layers of activations during training. Consider what happens as the gradient flows from back to . At each step, the chain rule multiplies by the Jacobian of the hidden state update, which includes and the derivative of . If the spectral radius of 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 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 , the model can only learn dependencies up to 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.