Stacked RNNs › How Hidden States Feed the Next Layer
Language AI HandbookAll chapters Stacked RNNs › How Hidden States Feed the Next LayerOne section · Sections in this chapter3 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
In a single-layer RNN, the recurrent equation takes the previous hidden state and the current input and produces the new hidden state :
where:
- is the hidden state at timestep
- is the recurrent weight matrix
- is the input projection matrix
- is the input at timestep
- is the activation function (tanh for vanilla RNNs, sigmoid-tanh combinations for LSTMs)
In a stacked RNN with layers, the same recurrence applies, but each layer takes the output of the layer below as its input:
where:
- is the hidden state of layer at timestep
- is the hidden state of the layer below, which is the "input" to layer
- and are the recurrent and input weight matrices for layer , each with their own learned parameters
The first layer () receives the actual input: . Each subsequent layer receives the full hidden-state sequence produced by the layer below.
The entire sequence of hidden states from layer must be computed before layer can run. This introduces a sequential dependency between layers: you cannot start computing layer 2 until all timesteps of layer 1 are finished. This is different from parallelism across timesteps within a single layer, which is also sequential.
For LSTMs, each layer maintains its own cell state in addition to its hidden state . The LSTM equations simply replace with :
where:
- are the forget, input, and output gates for layer
- is the cell state for layer
- is the candidate cell update
- denotes concatenation of the layer's own previous hidden state with the current output from the layer below
Each layer in a stacked LSTM has its own complete set of gate weights and its own cell state, so the number of parameters scales linearly with the number of layers.
Worked Example: Two-Layer Information Flow
To make this concrete, consider a 2-layer stacked LSTM processing the sentence "the cat sat." with a hidden size of (tiny, for illustration). The network processes three tokens: "the", "cat", "sat."
At timestep (token: "the"):
- Layer 1 receives (the embedding for "the"), combines it with its initial hidden state , and computes . This hidden state encodes something about "the" in the context of nothing yet seen.
- Layer 2 receives as its input, combines it with its initial hidden state , and computes . At this first timestep, the difference between layers is minimal: layer 2 simply applies another nonlinear transformation to layer 1's output.
At timestep (token: "cat"):
- Layer 1 receives (embedding for "cat") and the previous hidden state . It computes , which now encodes "cat" given what came before. The recurrent state carries the context of "the" forward.
- Layer 2 receives (layer 1's new state) and its own previous state . It computes , which encodes something about the relationship between layer 1's representation of "cat" and layer 2's memory of what layer 1 produced for "the".
At timestep (token: "sat"):
- Layer 1 produces , encoding "sat" in the context of the whole prefix "the cat."
- Layer 2 produces . By now, layer 2 has had three timesteps to build a representation that is a function of the entire sequence of layer 1 outputs. Its state represents how the sequence of local patterns from layer 1 evolved over all three tokens, rather than a direct transformation of "sat" alone.
This stepped example reveals the key asymmetry: layer 1 operates on raw inputs, and its hidden state is shaped by raw token sequences. Layer 2 operates on transformed inputs, and its hidden state is shaped by the temporal evolution of learned representations. Each timestep of layer 2 sees a more refined signal than layer 1 ever received.