Deep Learning · Sequence models: the vanilla (Elman) RNN · Recurrent Neural Networks (RNN) · Interactive sheet

The
Recurrent Neural Networks (RNN)
Blueprint

One small cell reads a sequence one element at a time and hands a hidden state to itself at the next step, so the network carries a memory: h_t = tanh(W_xh x_t + W_hh h_(t−1) + b_h). Because the same weights are multiplied in again at every step, that memory fades over long sequences, which is exactly what LSTMs and Transformers were built to fix.

Every number on this page is clickable. Every underlined term has a definition.

Before you start: how a feed-forward layer computes Wx + b and why it needs a non-linearity, how backpropagation applies the chain rule layer by layer, and the habit of tracing tensor shapes from the CNN material.

(50, 10) → (50, 20) → (50, 10)
shape path
830
parameters
800
multiply-adds per step
≈ 50 %
lower perplexity (2010)
1 set of weights for all T steps
the design rule
01

Sequences and the problem of memory

Sentences, speech and sensor readings come in any length, and the word that decides the answer may sit far back, so a model needs a memory rather than a fixed-size window.

Fill the gap: does the model still see the clue?

example
fixed window of a feed-forward network (words)3
feed-forward, window
RNN, hidden state

929k / 10,000
Penn Treebank words / vocab
≈ 50 % and ≈ 18 %
perplexity · word error rate
T = any
sequence length
h = 20
numbers of memory

Why the headline mattered

Count-based n-gram models only see the last few words. An RNN language model can, in principle, use the whole history, and in 2010 it beat the best n-gram systems by a wide margin. Click a milestone:

1986 · 1990 1994 · 1997 2014 · 2017
02

Walk a sentence through an unrolled RNN

Each column is the same cell at a different time step: drag the scrubber, press play, or click a bead to see the live shapes, the cost of each step and the hidden state filling up.
sentence
cell
input x_t (d = 10) hidden h_t (h = 20) output y_t (k = 10)
Elman RNN · unrolled100%
time-step scrubbert = 0
RNN rule: the same cell, the same weights, one step after another.
hidden state h_t, all 20 units, step by step · red = positive, blue = negative
t = 0
weights so far
0
multiply-adds so far
0

Worked example: one step of the worked example, shape by shape

The input word is a one-hot vector over a 10-word vocabulary
x_t : (10) → d = 10
Input weights map it into hidden space
W_xh x_t : (20 × 10)·(10) = (20) · 20 · 10 = 200 multiply-adds
Recurrent weights bring in the memory
W_hh h_(t−1) : (20 × 20)·(20) = (20) · 20 · 20 = 400 multiply-adds
Add the bias and squash with tanh
h_t = tanh(200-path + 400-path + b_h) : (20), every value in (−1, 1)
Read out the prediction, then repeat for every step
y_t = W_hy h_t + b_y : (10 × 20)·(20) + (10) = (10) · 200 · total 800 per step × 50 = 40,000

The same three matrix products happen at every step. Section 05 lets you change T, d, h and k.

In the walker, how many distinct weights does the cell use at step 3 compared with step 7?
The parameter counter stays flat while the multiply-add counter grows. Only the hidden state changes from step to step; the weights are shared.
03

The ideas that made it work: hidden state, weight sharing, backpropagation through time

Three ideas turn a feed-forward layer into a sequence model: a state that is carried forward, one set of weights reused at every step, and a way to train it by unrolling time.

Idea 1A hidden state carries memory

A one-unit RNN gets a single input pulse at t = 1 and nothing afterwards: h_t = tanh(w · h_(t−1) + x_t). How long does the pulse survive?

recurrent weight w0.80

Idea 2Weight sharing across time

Give every step its own weights and the model grows with the sentence. Share them and it does not: 1 set of weights for all T steps.

sequence length T50
shared
830
one copy per step

Idea 3Backpropagation through time

Unroll the loop into a deep feed-forward chain, run ordinary backpropagation, and add up the gradient for the shared W_hh from every step.

truncate after k steps6
You train with truncated BPTT, k = 20, on a 50-step sequence. The gradient computed at step 50 can reach back to…
Truncation stops the backward pass after k steps (steps 50, 49, …, 31). The forward pass still carries h_t across the whole sequence, but no gradient flows further back than k steps, which saves memory and time.
04

The RNN rule: one cell, the same weights, every time step

Compute one step of the recurrence by hand with the sliders, then compare the shared recurrent cell with the alternative, a feed-forward network on a fixed window of T inputs.
h_t = tanh(W_xh x_t + W_hh h_(t−1) + b_h)
click a part: h_t = tanh(W_xh x_t + W_hh h_(t−1) + b_h) · then y_t = W_hy h_t + b_y

One step by hand (one hidden unit)

input x_t1.0
previous h_(t−1)0.5
w_xh0.8
w_hh0.6
bias b_h0.0

Shared recurrent cell vs fixed-window feed-forward network

sequence / window length T50
input size d10
hidden size h20
output size k10
RNN, shared cell
830
vs
window MLP
RNN core h·d + h² + h · window (T·d)·h + h · context: unbounded vs T

Design rules of the vanilla RNN. One cell; the same W_xh, W_hh, W_hy at every step; tanh keeps the state in (−1, 1); the output y_t = W_hy h_t + b_y is read at every step (or only at the last one); h_0 starts at zero.

One hidden unit: x_t = 1, h_(t−1) = 0.5, w_xh = 1, w_hh = 2, b_h = −1. What is h_t?
Pre-activation a = 1·1 + 2·0.5 + (−1) = 1. Do not forget the squashing: h_t = tanh(1) ≈ 0.76. Try it with the sliders above.
05

Shapes by hand and by eye: (T, d) → (T, h) → (T, k)

Do two time steps of a tiny RNN on paper first, then use the calculator to trace the shapes of any sequence through the unrolled network.

Worked example by hand: d = 2, h = 2, two steps

Set-up: inputs x_1 = [1, 0], x_2 = [0, 1]; start state h_0 = [0, 0]; b_h = [0, 0]
W_xh = [[0.5, 0], [0, 0.5]] · W_hh = [[0.5, −0.5], [0.5, 0.5]] · W_hy = [1, 1], b_y = 0
Step 1, input part and memory part
W_xh x_1 = [0.5·1 + 0·0, 0·1 + 0.5·0] = [0.5, 0] · W_hh h_0 = [0, 0]
Step 1, add and squash
a_1 = [0.5, 0] + [0, 0] + [0, 0] = [0.5, 0] → h_1 = tanh(a_1) = [0.462, 0]
Step 2, input part
W_xh x_2 = [0.5·0 + 0·1, 0·0 + 0.5·1] = [0, 0.5]
Step 2, memory part: the old state comes back in
W_hh h_1 = [0.5·0.462 − 0.5·0, 0.5·0.462 + 0.5·0] = [0.231, 0.231]
Step 2, add and squash
a_2 = [0 + 0.231, 0.5 + 0.231] = [0.231, 0.731] → h_2 = tanh(a_2) = [0.227, 0.624]
Read out, and check the shapes
y_2 = 1·0.227 + 1·0.624 + 0 = 0.851 · shapes (2, 2) → (2, 2) → (2, 1): T = 2, d = 2, h = 2, k = 1

Redo it on paper before you press Next step. Notice that h_2 depends on x_1 through h_1: the first input is still "in" the state after it is gone.

The calculator

(T, d) → (T, h) → (T, k) · per step: (h×d)(d) + (h×h)(h) → (h)
time steps T50
input size d10
hidden size h20
output size k10
batch B1
shape path
(50, 10) → (50, 20) → (50, 10)
the cell moves along time: inputs → hidden states → outputs · click to pause100%
A batch of B = 32 sequences, T = 50, d = 10, h = 20, and the RNN returns the hidden state at every step. What is the shape of that output?
Batch and time are carried through; only the last axis changes from d = 10 to h = 20. Returning only the final state would give (32, 20).
06

Where the parameters live: W_xh, W_hh and W_hy

Every figure below is computed from the embedded layer specification; click a bar for its arithmetic and move the sliders to see which matrix grows fastest.
100%
input weightsrecurrent coreoutput layer
input size d10
hidden size h20
output size k10
sequence length T (watch the parameters)50
click a bar

The arithmetic for that block appears here.

Parameters

Compute (multiply-adds)

Worked example: counting the 830 parameters

Input weights W_xh: one row per hidden unit, one column per input value
h·d = 20·10 = 200
Recurrent weights W_hh: every hidden unit listens to every hidden unit
h² = 20² = 400
Hidden bias b_h, then the recurrent core
h·d + h² + h = 20·10 + 20² + 20 = 620
Output layer W_hy and b_y
k·h + k = 20·10 + 10 = 210
Total, for any sequence length
620 + 210 = 830 · 1 set of weights for all T steps

Any framework that builds this RNN with a separate output layer reports the same total, 830, whatever T you feed it.

Keep d = 10 and k = 10 but double the hidden size from 20 to 40. How many parameters does the recurrent core (W_xh, W_hh, b_h) have now?
h·d + h² + h = 40·10 + 40² + 40 = 400 + 1,600 + 40 = 2,040, more than three times 620, because the h² term quadruples. Check it with the h slider.
07

The family: Elman, Jordan, bidirectional, stacked, LSTM and GRU

Six recurrent cells for the same d = 10, h = 20, k = 10, with parameters and cost computed from the same specification; click a row for details and load it into the walker.
cellyearfed backrecurrent params× Elman coretotal paramsmult-adds / stepsees future?long-range memory
cell

Pick a row above.

Why 4× and 3×. An LSTM computes four gate-sized blocks (input, forget and output gates plus the candidate) and a GRU three (update and reset gates plus the candidate), each with its own W_x, W_h and bias. A vanilla RNN has one. So for the same d and h they carry 4× and 3× the recurrent parameters: 2,480 and 1,860 against 620.

Which one?

Short sequences, tiny devices: vanilla or GRU. Long dependencies: LSTM or GRU. Whole sequence available in advance (tagging, classification): bidirectional. Complex patterns: stack 2–3 layers. Real-time streaming: never bidirectional.

You build live speech captions that must print each word as soon as it is spoken. Which variant cannot be used?
The backward direction starts at the last time step, which has not been spoken yet. All the forward-only cells, however deep or gated, can run step by step.
08

The cost: sequential steps, vanishing and exploding gradients

An RNN pays twice for time: the steps must run one after another, and during training the gradient is multiplied by the same recurrent factor once per step until it vanishes or explodes.

Budget · time for one sequence, one epoch, one training run

model
GPU throughput (TFLOPS)10
batch of sequences32
sequence length T50
fixed overhead per step (µs, assumption)10
epochs in the training run20
multiply-adds per step
per sequence
one batch, forward
one epoch · training run

Gradients · the same factor, once per step: vanishing or exploding

factor per step (|W_hh| · tanh′)0.90
steps back in time50
gradient that reaches each earlier step · click a bead
0.9⁵⁰ ≈ 0.005 and 1.1⁵⁰ ≈ 117: a factor only 10 % below or above 1, repeated over 50 steps of backpropagation through time.
activation bound γ
γ = 1 (tanh), γ = ¼ (sigmoid)
largest singular value σ of W_hh0.80
The per-step factor is 0.8. Roughly how much of the gradient reaches a step 10 steps back?
The chain rule multiplies one factor per step: 0.8¹⁰ ≈ 0.107. Set the sliders to 0.80 and 10 to check. After 50 steps it would be about 0.00001.
09

Reuse and transfer: embeddings, encoder–decoder and fine-tuning

You rarely start from nothing: reuse pretrained word embeddings as the input layer, reuse a trained encoder, and choose what to freeze; click blocks to freeze or unfreeze them and watch the code follow.

Which recipe?

Your labelled data is
Similar to the pretraining text?

        

The encoder–decoder bottleneck

A sequence-to-sequence model reads the source with one RNN (encoder) and hands only its final hidden state to a second RNN (decoder). Drag the source length.

source sentence length12
A basic encoder–decoder (no attention) translates a 40-word sentence with h = 20. What does the decoder receive about the source?
Everything must squeeze through one h-sized vector. That bottleneck is why long sentences translated badly, and why attention (letting the decoder look at every encoder state) was added in 2014–2015.
10

History: from Jordan and Elman to LSTM and the Transformer

Penn Treebank test perplexity (lower is better) of representative word-level language models: the RNN language model broke the n-gram plateau, gated RNNs went further, attention took over.
Penn Treebank (929k / 10,000) test perplexity, 2010–2019 · values rounded100%
click a point
1986 · 1990
the loop is born
1994 · 1997
the problem and the fix
2014 · 2017
seq2seq, then attention

Vanilla RNN vs Transformer · tap a card to flip

Why did the Transformer (2017) replace RNNs for most language tasks?
Parallel training on GPUs and a direct path between distant words solve exactly the two costs of section 08. The price is attention cost that grows with T², which is one reason recurrent ideas are returning.
11

Where RNNs are used today

Transformers dominate large language models, but recurrent cells remain the tool of choice wherever data streams in and memory per step must stay constant; click a card for details.
application · streaming

On-device speech recognition

Recurrent transducer models turn audio frames into text as you speak.

The RNN transducer (RNN-T) family processes audio frame by frame with a fixed-size state, so latency and memory do not grow with the length of the utterance. That is ideal for phones and offline dictation.
application · time series

Forecasting and sensors

LSTM and GRU models predict demand, energy load or machine wear.

A forecaster reads the history step by step and outputs the next values; DeepAR-style autoregressive RNNs produce probabilistic forecasts. On sensor streams the same models flag anomalies when the prediction error spikes.
application · control

Robotics and reinforcement learning

Recurrent policies remember what the agent can no longer see.

When a robot or game agent only observes part of the world, an LSTM or GRU policy keeps a belief about the hidden parts in h_t. Small recurrent controllers also run at high frequency on embedded hardware.
application · generation

Handwriting, music, keyboards

Character-level RNNs generate pen strokes, melodies and next-word suggestions.

Graves (2013) generated realistic handwriting with stacked LSTMs; small recurrent language models have powered next-word suggestions on phone keyboards, where model size and latency are tight.
successor

LSTM and GRU

The gated cells of section 07: the default "RNN" in practice.

When people say "RNN" in applied work today they nearly always mean an LSTM or a GRU. Gates give the gradient a path that is not multiplied by W_hh at every step.
successor

Transformers, and recurrence returns

Attention replaced the loop; linear recurrences are bringing it back.

Transformers train in parallel but their memory grows with context length. State-space and linear-recurrent models (for example S4, RWKV and Mamba) train in parallel and still run as a recurrence with constant memory per token, the old RNN advantage.
Which property makes a recurrent model attractive for an always-on keyword spotter on a smartwatch?
The state h_t has a fixed size, so an endless stream costs the same per frame forever. A Transformer would have to keep (or truncate) a growing context.
12

Key takeaways

Seven sentences to remember; click each for the evidence and the section it comes from.
The state is the network's only memory of the past. Sections 01, 02 and 04.
h·d + h² + h for the core: 620, plus 210 for the output, 830 in the worked example, for 5 words or 5,000. Sections 03 and 06.
(50, 10) → (50, 20) → (50, 10). With a batch: (B, T, d) → (B, T, h). Section 05.
800 multiply-adds per step; 40,000 per sequence, step after step. Section 08.
0.9⁵⁰ ≈ 0.005 (vanishing) and 1.1⁵⁰ ≈ 117 (exploding). Clipping tames explosions; gates fix vanishing. Section 08.
2,480 and 1,860 against 620 for d = 10, h = 20. Bidirectional cells need the whole sequence first. Section 07.
1986 · 1990 loop, 1994 · 1997 vanishing gradient and LSTM, 2014 · 2017 seq2seq and attention. Sections 10 and 11.
13

Check your understanding

Five questions with instant feedback and every answer explained; score 4 or more for the stamp, and click a dot to jump to a question.
0 / 5
■ STAMPED · RNN BLUEPRINT · APPROVED
14

Glossary

Every term used on this page; type to filter and click a term to open its definition.