Visual explainer
Vanilla RNN
Read left to right with one looping cell and a running note, then watch old words fade as gradients shrink going back.
Traditional bag-of-words (BoW) models discard sequence ordering by pooling token counts into a static frequency histogram. When meaning depends entirely on word order, permutation invariance breaks comprehension:
- Permutation invariance: Consider two phrases with opposite meanings:
"dog bites man"(mundane event) versus"man bites dog"(sensational news). In a three-word vocabulary , both sentences map to the identical multiset count vector: - The agency void: Because the representation is identical (), any linear classifier outputs the exact same prediction score () for both inputs. A bag of words cannot determine grammatical roles, agent-patient relationships, or temporal causality. Language requires directional sequence processing.
Read, Note, Carry On
The Recurrent Neural Network (Elman, 1990) processes sentences sequentially from left to right, maintaining a dynamic hidden state vector that carries forward an accumulated memory summary:
- The state update equation: At each time step , the recurrent cell combines the new token input vector with the preceding hidden state : where weights the incoming token and governs the recurrent transition.
- Tracking context forward: Reading
"the cat sat"with initial resting state :- Step 1 ("the"): Ingests the determiner, initializing the context vector to .
- Step 2 ("cat"): Fuses subject semantics into the running note, advancing memory to .
- Step 3 ("sat"): Incorporates the action verb, yielding . Every step updates the same compact memory vector, preserving the trajectory of preceding tokens.
One Cell, Every Step
The core design principle of recurrent architectures is parameter tying across time:
- Shared weight matrices: Rather than instantiating unique parameters for each word index, a single recurrent unit reuses the identical weight matrices and across every time step:
- Constant parameter footprint: Because the same cell loops indefinitely, total model parameters remain strictly . Whether processing a five-word headline or a thousand-word transcript, no new parameters are allocated (). Parameter sharing enforces temporal translation equivariance across the sequence.
Same Cell Any Length
Feedforward architectures like Multi-Layer Perceptrons require fixed input dimensionalities (), requiring padding, truncating, or exploding parameter counts for long sequences. Recurrent networks unroll dynamically:
- Dynamic unrolling: An RNN handles arbitrary sequence length by unrolling through time.
- Short versus long runs: A three-step phrase () and an unrolled thirty-step paragraph () execute through the exact same two weight matrices (, ). Sequence length changes only the depth of the computational graph during forward execution, never the parameter capacity of the model.
Yesterday Whispers, Then Hushes
While recurrent parameter sharing allows processing arbitrary lengths, vanilla RNNs suffer from forward information decay:
- Exponential signal decay: The contribution of the initial token to the hidden state at step decays roughly as the power of the recurrent transition weight:
- Vanishing context horizon: For transition weight :
- Step 1: ( influence).
- Step 3: ( retention through saturation).
- Step 5: ( retention).
- Step 20: ( residual signal). By step twenty, the influence of early subject modifiers has drowned in noise, making vanilla RNNs unable to connect subject-verb dependencies across long clauses.
How Far Must Memory Reach?
Selecting between a vanilla RNN and gated alternatives depends on the maximum dependency distance required by the task:
- Vanilla RNN (Short spans, ): Features a single activation gate with simple arithmetic. For local tasks like part-of-speech tagging or short sensor streams where dependency gaps rarely exceed five steps (retaining signal), vanilla RNNs provide fast, lightweight inference.
- LSTM (Long spans, ): Long Short-Term Memory networks introduce three multiplicative gates (forget , input , output ) alongside an additive cell state conveyor belt: Because gradients can flow unattenuated along the additive cell highway (), LSTMs bridge dependencies exceeding 100 steps, justifying their parameter and compute overhead.
Where It Breaks
The fatal limitation of the vanilla RNN emerges during training via Backpropagation Through Time (BPTT):
- The backward chain rule: To update input weights based on an error loss at terminal step , error gradients must backpropagate through the entire recurrent chain:
- Vanishing gradients: Because and , every backward step multiplies the error signal by a factor :
- Step 5 (Loss origin): Error signal .
- Step 4: .
- Step 3: .
- Step 1: .
- Step 20 (Long sequence): .
- Learning paralysis: With gradients attenuated to , weight updates for early tokens () freeze completely. The network optimizes only for immediate, local token transitions while remaining permanently blind to long-range syntactic structure.
The Quick Version
- Bag of words pools frequencies, predicting the same score () for "dog bites man" and "man bites dog".
- Elman RNNs maintain a 2-number hidden state summary that advances from to across tokens.
- Parameter sharing reuses input weight and recurrent weight across all steps with memory.
- Dynamic unrolling processes three steps or thirty using the exact same cell without allocating new weights.
- Context decays exponentially as : step 3 preserves context, while step 20 retains only .
- Choose vanilla RNNs for cheap short spans ( steps); upgrade to gated LSTMs for long spans ( steps).
- Backpropagation multiplies gradients by per step, decaying error to and freezing early token learning.