Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Bidirectional and deep RNNs

Notes on Bidirectional and deep (stacked) RNNs: definitions, forward/backward equations, gradient derivations (BPTT), parameter counts, examples and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two-sentence practical use: Engineers use bidirectional RNNs when the prediction at a time step benefits from both past and future context (e.g., speech recognition, named-entity recognition). They use deep (stacked) RNNs when modelling complex temporal patterns that need multiple levels of representation (e.g., hierarchical language features). These architectures improve modelling power but need careful training (BPTT, regularisation).

Key ideas

  • ht = f(Whh·ht-1 + Wxh·xt + bh)
  • ŷt = g(Why·ht + by) where f is a hidden activation (e.g., tanh), g an output map (e.g., softmax), Whh is the recurrent weight, Wxh the input-to-hidden, Why hidden-to-output, bh, by biases.

Define each term before use: activation function f(x) (elementwise), parameters = all trainable matrices and biases.

  • Unrolling / time-unfolding: For derivations, draw the RNN as a feedforward network by duplicating the single-layer cell for each time t and connecting ht-1 → ht with Whh. Diagram to draw for an exam: draw T rectangles in a row labelled Cell 1..Cell T. Inside each cell write xt at bottom-left, ht at centre, yt at top. Draw arrows xt → ht (label Wxh), ht-1 → ht (label Whh), ht → yt (label Why). If bidirectional, draw a mirrored chain above reversed in time.

  • Backpropagation Through Time (BPTT): RNN gradients are computed by applying the chain rule on the unrolled network across time. The gradient of the loss L wrt Whh accumulates contributions from all times and includes products of Jacobians across time steps which can cause vanishing/exploding gradients.

  • Deep / Stacked RNN: Stack RNN layers vertically. For layer ℓ and time t, hℓ,t = f(Wℓ_hh·hℓ,t-1 + Wℓ_xh·hℓ-1,t + bℓ) with base input h0,t = xt. Each layer has its own recurrent and input weights. The network is deep in depth (number of layers) and in time (unrolled steps). Training uses BPTT through both time and depth.

  • Bidirectional RNN (BiRNN): Maintain two hidden sequences: forward h→t processes t=1..T, backward h←t processes t=T..1. The output at t combines both directions, e.g., ŷt = g(Wy→·h→t + Wy←·h←t + by) or by concatenation hbt = [h→t; h←t]. Each direction has its own recurrent weights. BiRNNs need the entire sequence available (not suitable for online streaming unless latency allowed). Boundary conditions for exams: use →h_0 = 0 and ←h_{T+1} = 0 (or learned initial states) unless otherwise stated.

  • Combining deep and bidirectional: You can stack bidirectional layers: each layer takes the concatenated bi-directional outputs from previous layer as its input. This increases parameter count but allows complex representations.

  • Parameter count: For one-layer unidirectional RNN with input size d, hidden size H, output size K: params = Wxh (H×d) + Whh (H×H) + Why (K×H) + biases. For bidirectional hidden H per direction, effective hidden becomes 2H at output.

  • Assumptions used in derivations below: activations are elementwise differentiable; sequences finite length T; loss is scalar sum over time L = ∑t ℓ(y_t, ŷ_t).

Formulas

h_t = f( W_xh · x_t + W_hh · h_{t-1} + b_h ) where h_t ∈ R^H is hidden state at t, x_t ∈ R^d input; no SI unit; applies for each time t.

a^y_t = W_hy · h_t + b_y; ŷ_t = g(a^y_t) where ŷ_t ∈ R^K is the network output at t; g is the output map.

L = Σ_{t=1..T} ℓ(y_t, ŷ_t) where y_t is the target at time t, ŷ_t the prediction, and ℓ is per-step loss (e.g., cross-entropy); scalar.

∂L/∂W_hy = Σ_t e_t · h_t^T where e_t ≡ ∂ℓ_t/∂a^y_t (gradient at the output pre-activation); gradient of output weight.

∂L/∂W_hh = Σ_t δ_t · h_{t-1}^T where δ_t is the local gradient for hidden pre-activation at t (see below).

δ_t = (W_hy^T · e_t + W_hh^T · δ_{t+1}) ⊙ f'(a_t) backward recurrence for δ (assuming time runs 1..T; compute backward from T to 1). Symbols: a_t = W_xh x_t + W_hh h_{t-1} + b_h; a^y_t = W_hy h_t + b_y; e_t ≡ ∂ℓ_t/∂a^y_t; ⊙ elementwise product.

Bidirectional forward/backward equations: →h_t = f( W_x→ · x_t + W_→ · →h_{t-1} + b_→ ) ←h_t = f( W_x← · x_t + W_← · ←h_{t+1} + b_← ) ŷ_t = g( W_y→ · →h_t + W_y← · ←h_t + b_y ) (or ŷ_t = g( W_y · [→h_t; ←h_t] + b_y ) ).

Parameter count example (one-layer bidirectional): params = (H×d + H×H + H) * 2 + K×(2H) + K where H per direction, d input dim, K output dim.

Units: All parameters and activations are dimensionless arrays (no SI). Formulas apply for standard dense RNN cells; replace f by gated-cell equations for LSTM/GRU.

Worked examples

Example 1 — short derivation/model-answer (exam style): Derive gradient ∂L/∂Whh for a unidirectional RNN unrolled for T steps with loss L = Σt ℓ_t. Model answer (concise steps): 1) Unroll RNN to T feedforward layers. 2) Using chain rule, ∂L/∂Whh = Σ_{t=1..T} ∂L/∂a_t · ∂a_t/∂Whh where a_t = W_xh x_t + W_hh h_{t-1} + b_h. 3) Now ∂a_t/∂Whh = h_{t-1}^T (outer product for matrix). 4) Let δ_t ≡ ∂L/∂a_t be the backprop signal at time t. Then ∂L/∂Whh = Σ_t δ_t · h_{t-1}^T. 5) δ_t computed recursively: δ_t = (W_hy^T · e_t + W_hh^T · δ_{t+1}) ⊙ f'(a_t), where e_t ≡ ∂ℓ_t/∂a^y_t, starting δ_{T+1}=0. End. (In exam draw the unrolled graph and show how δ flows backward through time.)

Example 2 — numeric parameter count and forward pass for a 2-layer bidirectional RNN (model-answer + numeric): Problem statement: Input dim d=3, hidden H=4 per direction, 2 stacked bidirectional layers, output K=2. Compute parameter count and describe forward pass. Answer steps: 1) Layer 1 has two directions; for each direction W_xh: H×d = 4×3 =12, W_hh: H×H = 4×4 =16, bias H =4. So per direction params =12+16+4 =32; both directions =64. Layer 1 output per time is concatenation size 2H =8. 2) Layer 2 takes input dim 2H=8. Per direction W_xh: 4×8 =32, W_hh:16, bias 4 → per direction =52; both =104. 3) Output layer W_y maps 2H=8 to K=2: 2×8 =16 plus bias 2 →18. 4) Total params = layer1(64) + layer2(104) + output(18) = 186 trainable parameters. (Final answer bold.) 5) Forward pass description: at each time t compute →h1_t and ←h1_t using x_t and neighbors; form h1_t = [→h1_t; ←h1_t] (size 8). Then feed h1_t into layer2 to compute →h2_t and ←h2_t, combine to h2_t (size 8), finally compute ŷ_t = g(W_y·h2_t + b_y).

Example 3 — short numerical state update (small numbers) for a simple 1-step RNN (optional small numeric check): Given d=1, H=1, f = tanh, W_xh = 0.5, W_hh = 0.4, b_h=0, initial h0 = 0, input x1 = 1. Then a1 = 0.5·1 + 0.4·0 = 0.5, h1 = tanh(0.5) = 0.462117..., round to 0.4621 (report 0.4621). If Why=1, by=0 and identity output, ŷ1=0.4621.

(I left this optional small numerical example short for clarity.)

Common mistakes

  • Mixing up indices: using h_t instead of h_{t-1} when computing ∂a_t/∂Whh.
  • For bidirectional RNNs, treating forward and backward weights as shared — they are separate.
  • Wrong parameter count: forgetting that concatenation doubles hidden size at outputs in BiRNN or stacked layers.
  • Using online (causal) interpretation for BiRNNs without noting that full future context is required.

For your exam

Typical question types: definitions (define BiRNN, deep RNN, BPTT), short notes (advantages/disadvantages), derivations (derive ∂L/∂Whh using BPTT), numerical parameter-count problems, drawing unrolled diagrams, and longer model-design questions (design a stacked BiRNN for a task and compute params).

What a full-marks answer includes:

  • Clear definitions of terms used (hidden state, unrolling, BPTT).
  • A correct, labelled diagram of the unrolled cell(s) across time (and stacked layers when asked). Specify arrow labels (Wxh, Whh, Why).
  • Full chain-rule derivation for gradients, showing the recursion for δ_t and the final summed formula ∂L/∂Whh = Σ_t δ_t h_{t-1}^T.
  • Correct parameter-count arithmetic with justification (dimensions for each matrix).
  • Discuss assumptions (finite T, differentiable activations) and training issues (vanishing/exploding gradients) briefly when asked.

Quick check

  1. Define a Bidirectional RNN in one sentence.
  2. Give the formula for the recurrent hidden update h_t (one-layer).
  3. State the expression for ∂L/∂Whh in words.

Answers: 1) A BiRNN processes the sequence in both forward and backward directions with separate recurrent parameters and combines the two hidden states at each time step. 2) h_t = f(W_xh·x_t + W_hh·h_{t-1} + b_h). 3) ∂L/∂Whh = sum over time t of (backprop signal at t) outer-product h_{t-1}.

Uncertainties / verify

  • I assumed elementwise activation f (tanh or ReLU) and used its derivative f' in the δ recurrence — confirm wording and that exam contexts expect elementwise derivative (appears in Key ideas and Formulas).
  • The numeric parameter-count example used simple dense RNN cells; confirm that your course counts biases separately as I did (appears in Worked examples Example 2).
  • I used the common convention δ_t = ∂L/∂a_t and the backward recurrence including W_hh^T · δ_{t+1}; please verify sign conventions and index directions expected by your university (appears in Formulas and Worked examples).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?