One state, carried forward
- : the hidden state after reading inputs.
- : the input at this step.
- : the recurrent weights, applied at every step.
The state is a summary of everything read so far, and it is the only thing carried between steps.
The same weights are used at every timestep, which is weight sharing across time in the way a convolution shares across space.
That sharing is what lets one model read a sequence of any length, and it is also the source of everything that goes wrong.
Unrolling, and backpropagation through time
Training treats the steps as a -layer network that happens to share weights, so the gradient at each step is summed into one update.
The gradient reaching step from step is a product of copies of the same matrix.
Because it is the same matrix every time, the product is a power, and a power of anything other than exactly 1 goes to zero or to infinity.
This is why a plain RNN cannot learn a dependency more than about ten steps long, and why gated cells had to be invented.
One sequence, in order
- Initialise the hidden state to zeros.
- Read the first input, combine it with the state, and apply the nonlinearity.
- Emit an output from the new state if the task needs one at this step.
- Repeat for every timestep, carrying the state forward.
- Sum the losses across steps, then backpropagate through the whole unrolled graph.
Long sequences are truncated, so the gradient is only carried back steps, which bounds the memory and formally limits what can be learned.
What it costs on modern hardware
The recurrence is sequential by definition, so the timesteps cannot be computed in parallel on a GPU no matter how many cores are available.
That is the decisive difference. A transformer sees the whole sequence at once and a recurrent network cannot, so training time scales with sequence length rather than with hardware.
Inference is the opposite case. An RNN carries a fixed-size state and costs the same per token however long the history is, while a transformer's cost grows with the context.
That advantage is why recurrent architectures keep returning, most recently as state space models, for settings where the sequence is very long and the state must stay small.