Text has an order, and the network that reads it needs a memory. A recurrent network reads one character at a time, keeps a small hidden state that carries what it has seen so far, and at every step guesses the next character. The same three weight matrices are used at every step, just as example 6 used the same filter at every position. Train it on four sentences about a cat and a dog, then let it continue text of your own.
Each column is one time step: the character read (top), the 12-number hidden state (middle, darker = larger, red = negative), and the character predicted next with the probability given to the true one (bottom). The horizontal arrows are the memory passing from step to step. Yellow = being computed, green = computed, red dashed = receiving blame.
Keyboard: → or space for Next, ← for Back. An epoch is the six snippets of the training text. About 300 epochs gives good text; after that the held-out loss starts to rise.
Memory. The hidden state h is a vector of 12 numbers that the network carries from one character to the next. At each step it is recomputed from the character just read and the previous state: h_t = tanh(W_xh·x_t + W_hh·h_{t−1} + b_h). Nothing else survives between steps, so whatever the network needs to remember about "the ca" in order to guess "t" has to be encoded in those 12 numbers.
One-hot input is a column lookup. A character is a vector of 15 zeros and a single 1, so W_xh·x_t is simply column x_t of W_xh: no multiplication needed. That makes the first step of a snippet, where the previous state is all zeros, checkable by hand with the tanh table on the right.
Unrolling. Reading a 16-character snippet is the same as running a 16-layer network in which every layer shares the same weights. Draw it out (the diagram above) and the forward pass is just example 4 again, layer after layer.
Backpropagation through time. Blame arrives at h_t from two directions: from this step's prediction, and from the next step's hidden state, because h_t was an input to h_{t+1}. Add them, pass through tanh (multiply by 1 − h²), and continue to the previous step. Each weight matrix collects blame from all 16 steps, exactly as a filter collected blame from every position.
Vanishing and exploding. Blame that travels back through many steps is multiplied by W_hh and by 1 − h² each time. Products of many small numbers vanish; products of many large ones explode. Stage 7 shows the blame shrinking as it travels back, and the update clips every gradient to ±1 to keep training from blowing up. Long-range memory is genuinely hard for this architecture, which is why LSTMs, GRUs and eventually transformers were invented.
Only the 15 characters of the corpus are allowed: letters a c d e g h l m n o r s t, space and full stop. The bars show what the network expects after the seed.
W_xh: 12 rows × 15 character columns (each column is what a character adds to the state). W_hh: 12 × 12, how the old state feeds the new one. W_hy: 15 × 12, how the state votes for the next character. 531 parameters in all.
Nothing trained yet. Cross-entropy per character; ln 15 ≈ 2.71 is pure guessing.
| z | tanh(z) | 1 − tanh² |
|---|
tanh squashes to −1..1 and is symmetric: tanh(−z) = −tanh(z). Its derivative 1 − tanh² is at most 1 (at z = 0) and tiny once |z| is large.
h_t = tanh(W_xh[:, x_t] + W_hh·h_{t−1} + b_h) h_0 = 0
z_t = W_hy·h_t + b_y p_t = softmax(z_t)
L = mean over steps of −ln p_t[next character]
δy_t = p_t − onehot(next_t)
δh_t = W_hyᵀ·δy_t + W_hhᵀ·δz_{t+1} (δz_{T+1} = 0)
δz_t = δh_t ⊙ (1 − h_t²)
∂W_hy += δy_t·h_tᵀ ∂W_hh += δz_t·h_{t−1}ᵀ ∂W_xh[:, x_t] += δz_t ∂b_h += δz_t ∂b_y += δy_t
clip every gradient entry to ±1, then p ← p − 0.02 · ∂L/∂p