1. What happens to words outside the window?
Consider this unfinished sentence:
“The kitten avoided traffic today because ___ …”
A plausible next word is “it”, referring to the kitten. But if a feedforward model receives only the last three word tokens, its input is “traffic today because”. The kitten is outside that window.
The model might still make a plausible guess from local patterns. It cannot, however, use an earlier token that was never supplied. Two histories ending in the same three tokens receive the same distribution from this fixed-window model, even when their earlier subjects differ.
Pronouns, earlier events and references between sentences can depend on information far back in the text. Enlarging a window provides more information, but still imposes a boundary.
2. Give the network a hidden state
A recurrent neural network, or RNN, reads a sequence one token at a time. Each step takes the current input vector and the previous hidden state, then produces a new state.
For a simple tanh RNN:
Here is the current token's embedding, while carries information from earlier steps. The initial state can be zero.
This is equivalent to concatenating the two inputs and using one matrix:
If input vectors have dimension and states have dimension :
The operations are familiar: an affine transformation and a nonlinear activation. The change is the connection through time: the state produced at one step becomes an input to the next.
For a language model, an output layer can turn the updated state into next-token probabilities:
After reading token , this distribution predicts . During training, the observed next token supplies the target. It must not be fed into the state before the prediction being evaluated.
One operation, reused throughout the sequence
The same , and are reused at every step. Reading a longer sequence adds more applications of the same operation, not a new set of weights for each position.
Unlike the previous feedforward model, a recurrent model has no architectural cutoff after a fixed number of recent tokens. Earlier inputs can influence later states. But its fixed-size state does not guarantee perfect or unlimited memory, and longer sequences still cost more computation.
3. Calculate a tiny memory by hand
Shrink the state to one number and choose a simple teaching rule:
Give “kitten” an input signal . Set the signals for “avoided”, “traffic”, “today” and “because” to zero. We work with this five-token subsequence, omitting the leading article from the earlier sentence.
These are hand-chosen inputs and a fixed weight, not a trained language model. The scalar state represents one illustrative signal, not a human-readable coordinate we expect every real RNN to learn.
The first update is:
The subsequent zero-input updates give:
Read the context one token at a time
- kitten—
- avoided—
- traffic—
- today—
- because—
Tokens read: 0 / 5 · Current hidden state: 0.0000
Start with zero state; no token has been read.
The bars show a scalar state, not a probability. This hand-built example has no trained output layer and does not actually choose a pronoun.
Click Read next token to apply one update. Use Previous token to inspect an earlier state or Reset to return to the initial state.
After five tokens in total—one signal token and four intervening tokens—the state retains about 11.3% of its value immediately after “kitten”. The full-precision states are used in each calculation; the displayed values are rounded.
The state is not a probability. This experiment does not implement an output layer that selects “it”; it shows the information that such a layer would receive before seeing that next token.
4. A long path for information—and gradients
In this particular recurrence, every zero-input step multiplies the previous value by and applies tanh. For positive values, , so after intervening zero-input tokens:
For comparison:
These are factors bounding the retained fraction, not the exact hidden states. The additional tanh operations make the actual states smaller. A tiny residual is not mathematically zero.
| Zero-signal tokens | Hidden state | State fraction | Sensitivity |
|---|---|---|---|
| 0 | 0.9051 | 100.0% | 1.000 |
| 4 | 0.1026 | 11.33% | 0.08610 |
| 5 | 0.06146 | 6.790% | 0.05146 |
| 10 | 0.004776 | 0.5276% | 0.003993 |
| 20 | 0.00002888 | 0.003190% | 0.00002415 |
In a real RNN, new inputs are generally nonzero and the state is multidimensional. Its behaviour depends on the recurrent weights, activations and inputs. Not every state necessarily decays like this scalar example.
Backpropagation through time
Training a recurrent model unrolls these operations and applies the chain rule backwards. This is backpropagation through time, or BPTT. Gradients from multiple time steps contribute to the shared parameters.
For our scalar update, the local derivative is:
The sensitivity of a later state to the state after the first token is a product:
At zero gap the product is empty and equals one. For long gaps it becomes very small in this example, so a later loss can send only a weak gradient through this path to an earlier state.
Insert more zero-signal tokens after “kitten”
State after gap: 0.10256
Fraction of initial state: 11.330%
Sensitivity to initial state: 0.086096
Upper bound on sensitivity: 0.12960
The state and its derivative are different quantities. Small displayed numbers are not exactly zero; this experiment only covers the fixed coefficient 0.6 and zero later inputs.
Compare the state value, its retained fraction, and its derivative as you change the gap. Forward information fading and backward gradients shrinking are related here, but they are not the same quantity.
Vanishing and exploding gradients
For the vector RNN, the local recurrent Jacobian is:
Propagating influence across many steps involves a product such as:
Products that shrink can cause vanishing gradients. Products that grow can cause exploding gradients. The scalar example above demonstrates shrinking; it does not demonstrate explosion with its fixed coefficient .
These effects can make long-range credit assignment difficult. They do not impose a universal maximum of five or ten tokens. Architecture, initialisation, optimisation, data and the task all affect what a recurrent model learns.
Gradient clipping can help control large gradients, while truncated BPTT limits the backward span to manage training cost. Truncation also limits the direct gradient path through earlier steps. Neither method alone gives perfect long-term memory.
5. What you have discovered
An RNN carries a hidden state forward and reuses the same update rule as it reads a sequence.
- Beyond a fixed window: earlier tokens can affect later predictions through the state.
- A recurrent update: combine the current input with the previous state, then apply an activation.
- Shared parameters: every time step uses the same weights and biases.
- A numerical example: our hand-built state falls from about to after four zero-signal updates.
- Long-range learning is difficult: products of recurrent derivatives can shrink or grow, causing vanishing or exploding gradients.
The next lesson explores long short-term memory (LSTM) and learned gates that regulate the flow of information.