Skip to content
mlmentorship

LSTM and GRU: gating as Hadamard products

Recurrent networks fail because gradients vanish through repeated matmul. Gates fix this by using elementwise multiplication to control information flow. Then transformers replaced them anyway.

Published · 5 min read ·Specialist ·Intermediate

Visual quick review

Visual first · depth when needed

Compute one LSTM cell update coordinate by coordinate, then read the direct backward multiplier from the same forget gate.

Preparing the visual…

Summary

LSTM and GRU are recurrent units with learned gates that decide, per timestep, what to keep and what to forget. Each gate is a sigmoid output, applied via Hadamard (elementwise) product to state or candidate vectors. The LSTM’s direct cell-state path is scaled coordinate by coordinate instead of transformed by another dense matrix, so gradients have a controlled route through time.

A vanilla RNN updates its hidden state as . Backpropagating through timesteps multiplies the gradient by a total of times. Eigenvalues of less than 1 vanish; eigenvalues greater than 1 explode. Both kill learning (Pascanu et al., 2013).

LSTMs (Hochreiter & Schmidhuber, 1997) replaced repeated matmul on the cell state with a Hadamard-product update, opening a “gradient highway.” GRUs (Cho et al., 2014) simplified this to two gates with similar empirical performance.

Both are now mostly historical. Transformers replaced them everywhere. But the gating idea persists in attention masking, gating in mixture-of-experts, and residual gating in modern architectures.

The LSTM cell

State: hidden state and cell state .

Gates (each computed from ):

Update:

The forget gate controls what fraction of the previous cell state survives. The input gate controls what fraction of the new candidate is written. The output gate controls what fraction of the cell is exposed.

Learning objective: compute one LSTM cell update coordinate by coordinate, then read the direct backward multiplier from the same forget gate.

Worked state update

Each gate scales matching coordinates; it does not mix them.

Three-coordinate LSTM cell-state update and direct gradient path A worked example has three columns labeled coordinate A, B, and C. The previous cell state 2, negative 1, 0.5 is multiplied coordinate by coordinate by forget gate 0.9, 0.2, 1 to retain 1.8, negative 0.2, 0.5. Candidate negative 1, 0.5, 0.8 is multiplied by input gate 0.1, 0.8, 0.5 to write negative 0.1, 0.4, 0.4. Adding matching coordinates produces the new cell state 1.7, 0.2, 0.9. Along the direct cell-state path, a unit gradient is multiplied by the same forget gate and becomes 0.9, 0.2, 1. MATCHING STATE COORDINATES A B C OLD CELL cₜ₋₁ 2.0 −1.0 0.5 × forget gate fₜ 0.9 0.2 1.0 RETAINED 1.8 −0.2 0.5 CANDIDATE c̃ₜ −1.0 0.5 0.8 × input gate iₜ 0.1 0.8 0.5 WRITTEN −0.1 0.4 0.4 retained + written NEW CELL cₜ 1.7 0.2 0.9 DIRECT BACKWARD PATH unit gradient × fₜ → 0.9 0.2 1.0
Read it this way: read down one column at a time. The forget gate scales only the matching old-state coordinate; the input gate scales only the matching candidate coordinate; then those two contributions add. On the direct cell-state path, backpropagation uses the same forget value: coordinate C copies state and gradient at 1.0, A mostly retains them at 0.9, and B deliberately forgets them at 0.2. Original worked example checked against Hochreiter and Schmidhuber's LSTM formulation and the PyTorch LSTM equations.

Why Hadamard products fix gradients

The cell-state recurrence is . Holding the gate values fixed, the direct cell-state path has Jacobian . If the forget gate stays near 1, gradients pass through nearly unchanged; if it goes near 0, that coordinate and its direct gradient are deliberately forgotten. This direct path cannot amplify a gradient because , although repeated values below 1 can still make it decay.

The GRU cell

Two gates instead of three:

Single hidden state, no separate cell. Slightly fewer parameters. Empirically comparable to LSTM on most tasks.

Tradeoffs vs transformers

  • Sequential: must process tokens one at a time. Cannot parallelize across the sequence dimension at training time.
  • Linear in sequence length at inference (vs for vanilla attention). The advantage that makes RNN-style models attractive again at very long context (Mamba, RWKV, linear attention).
  • No explicit attention. Information from token to token has to survive gate updates. Long-range dependencies are still hard in practice.

Common pitfalls

  • Treating LSTM and GRU as interchangeable. They are close empirically but the cell state in LSTM gives sharper control over long-range memory.
  • Using vanilla RNNs in 2025. Almost never the right choice. Either go LSTM/GRU or, more likely, transformer.
  • Forgetting truncated BPTT. Backpropagating through 100k tokens is infeasible; truncate at a window (typically 64 to 256 tokens) and cut gradients.