Stacked RNNs › How Hidden States Feed the Next Layer

Michael BrenndoerferMay 13, 20254 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 82Stacked RNNs › How Hidden States Feed the Next Layer
Reading section 3 of 16 · How Hidden States Feed the Next Layer · 4 minSection 3 of 16 · 4 min

Book contents

Section 3 of 16 · 4 min read

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 ht−1h_{t-1} and the current input xtx_t and produces the new hidden state hth_t:

ht=f(Whht−1+Wxxt+b)h_t = f(W_h h_{t-1} + W_x x_t + b)

where:

  • ht∈Rdh_t \in \mathbb{R}^d is the hidden state at timestep tt
  • Wh∈Rd×dW_h \in \mathbb{R}^{d \times d} is the recurrent weight matrix
  • Wx∈Rd×mW_x \in \mathbb{R}^{d \times m} is the input projection matrix
  • xt∈Rmx_t \in \mathbb{R}^m is the input at timestep tt
  • ff is the activation function (tanh for vanilla RNNs, sigmoid-tanh combinations for LSTMs)

In a stacked RNN with LL layers, the same recurrence applies, but each layer ℓ\ell takes the output of the layer below as its input:

ht(ℓ)=f ⁣(Wh(ℓ)ht−1(ℓ)+Wx(ℓ)ht(ℓ−1)+b(ℓ))h_t^{(\ell)} = f\!\left(W_h^{(\ell)} h_{t-1}^{(\ell)} + W_x^{(\ell)} h_t^{(\ell-1)} + b^{(\ell)}\right)

where:

  • ht(ℓ)∈Rdℓh_t^{(\ell)} \in \mathbb{R}^{d_\ell} is the hidden state of layer ℓ\ell at timestep tt
  • ht(ℓ−1)h_t^{(\ell-1)} is the hidden state of the layer below, which is the "input" to layer ℓ\ell
  • Wh(ℓ)W_h^{(\ell)} and Wx(ℓ)W_x^{(\ell)} are the recurrent and input weight matrices for layer ℓ\ell, each with their own learned parameters

The first layer (ℓ=1\ell = 1) receives the actual input: ht(0)=xth_t^{(0)} = x_t. Each subsequent layer receives the full hidden-state sequence produced by the layer below.

The entire sequence of hidden states from layer ℓ−1\ell-1 must be computed before layer ℓ\ell 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 ct(ℓ)c_t^{(\ell)} in addition to its hidden state ht(ℓ)h_t^{(\ell)}. The LSTM equations simply replace xtx_t with ht(ℓ−1)h_t^{(\ell-1)}:

ft(ℓ)=σ ⁣(Wf(ℓ)[ht−1(ℓ), ht(ℓ−1)]+bf(ℓ))it(ℓ)=σ ⁣(Wi(ℓ)[ht−1(ℓ), ht(ℓ−1)]+bi(ℓ))c~t(ℓ)=tanh⁡ ⁣(Wc(ℓ)[ht−1(ℓ), ht(ℓ−1)]+bc(ℓ))ct(ℓ)=ft(ℓ)⊙ct−1(ℓ)+it(ℓ)⊙c~t(ℓ)ot(ℓ)=σ ⁣(Wo(ℓ)[ht−1(ℓ), ht(ℓ−1)]+bo(ℓ))ht(ℓ)=ot(ℓ)⊙tanh⁡(ct(ℓ))\begin{aligned} f_t^{(\ell)} &= \sigma\!\left(W_f^{(\ell)} [h_{t-1}^{(\ell)},\, h_t^{(\ell-1)}] + b_f^{(\ell)}\right) \\ i_t^{(\ell)} &= \sigma\!\left(W_i^{(\ell)} [h_{t-1}^{(\ell)},\, h_t^{(\ell-1)}] + b_i^{(\ell)}\right) \\ \tilde{c}_t^{(\ell)} &= \tanh\!\left(W_c^{(\ell)} [h_{t-1}^{(\ell)},\, h_t^{(\ell-1)}] + b_c^{(\ell)}\right) \\ c_t^{(\ell)} &= f_t^{(\ell)} \odot c_{t-1}^{(\ell)} + i_t^{(\ell)} \odot \tilde{c}_t^{(\ell)} \\ o_t^{(\ell)} &= \sigma\!\left(W_o^{(\ell)} [h_{t-1}^{(\ell)},\, h_t^{(\ell-1)}] + b_o^{(\ell)}\right) \\ h_t^{(\ell)} &= o_t^{(\ell)} \odot \tanh(c_t^{(\ell)}) \end{aligned}

where:

  • ft(ℓ),it(ℓ),ot(ℓ)f_t^{(\ell)}, i_t^{(\ell)}, o_t^{(\ell)} are the forget, input, and output gates for layer ℓ\ell
  • ct(ℓ)c_t^{(\ell)} is the cell state for layer ℓ\ell
  • c~t(ℓ)\tilde{c}_t^{(\ell)} is the candidate cell update
  • [ht−1(ℓ), ht(ℓ−1)][h_{t-1}^{(\ell)},\, h_t^{(\ell-1)}] 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 d=3d = 3 (tiny, for illustration). The network processes three tokens: "the", "cat", "sat."

At timestep t=1t=1 (token: "the"):

  1. Layer 1 receives x1x_1 (the embedding for "the"), combines it with its initial hidden state h0(1)=0h_0^{(1)} = \mathbf{0}, and computes h1(1)h_1^{(1)}. This hidden state encodes something about "the" in the context of nothing yet seen.
  2. Layer 2 receives h1(1)h_1^{(1)} as its input, combines it with its initial hidden state h0(2)=0h_0^{(2)} = \mathbf{0}, and computes h1(2)h_1^{(2)}. At this first timestep, the difference between layers is minimal: layer 2 simply applies another nonlinear transformation to layer 1's output.

At timestep t=2t=2 (token: "cat"):

  1. Layer 1 receives x2x_2 (embedding for "cat") and the previous hidden state h1(1)h_1^{(1)}. It computes h2(1)h_2^{(1)}, which now encodes "cat" given what came before. The recurrent state carries the context of "the" forward.
  2. Layer 2 receives h2(1)h_2^{(1)} (layer 1's new state) and its own previous state h1(2)h_1^{(2)}. It computes h2(2)h_2^{(2)}, 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 t=3t=3 (token: "sat"):

  1. Layer 1 produces h3(1)h_3^{(1)}, encoding "sat" in the context of the whole prefix "the cat."
  2. Layer 2 produces h3(2)h_3^{(2)}. 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.