Why recurrent networks need gates

← Back to Blog & Articles

Because tanh's derivative is at most 1 (and typically much less once the unit saturates), the repeated multiplication in BPTT's gradient product shrinks exponentially with sequence length in the typical case — a vanilla RNN can rarely learn dependencies spanning more than a few dozen timesteps.

Because tanh's derivative is at most 1 (and typically much less once the unit saturates), the repeated multiplication in BPTT's gradient product shrinks exponentially with sequence length in the typical case — a vanilla RNN can rarely learn dependencies spanning more than a few dozen timesteps.

The Long Short-Term Memory (LSTM) cell solves this with a separate cell state c_t that is updated additively rather than through a repeated nonlinear transform, plus three gates controlling what to forget, add, and output:

f_t = σ(W_f[h_{t-1}, x_t] + b_f) (forget gate)

i_t = σ(W_i[h_{t-1}, x_t] + b_i) (input gate)

c̃_t = tanh(W_c[h_{t-1}, x_t] + b_c) (candidate cell update)

c_t = f_t ⊙ c_{t-1} + i_t ⊙ c̃_t (additive cell update)

o_t = σ(W_o[h_{t-1}, x_t] + b_o) (output gate)

h_t = o_t ⊙ tanh(c_t)

The key structural point: ∂c_t/∂c_{t-1} = f_t (plus lower-order terms), so when the forget gate is near 1, gradients flow backward through the cell state almost unattenuated across many timesteps — the LSTM's additive update is the sequence-modeling analogue of ResNet's additive skip connection (the companion book), both solving a vanishing-gradient problem by giving the gradient a near-identity path.

The Gated Recurrent Unit (GRU) simplifies the LSTM to two gates and no separate cell state, achieving similar long-range gradient flow with fewer parameters, at a modest cost in expressiveness on tasks that benefit from the LSTM's more fine-grained memory control.

Continue with the book

Explore the full treatment, worked examples, and exercises in The AI Algorithms Compendium.


Companion article for The AI Algorithms Compendium, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top