What are the LSTM cell equations, and why does the cell state ease the vanishing-gradient problem of a vanilla RNN? How does a GRU differ, and how many parameters does each have for input size 100 and hidden size 128?

Sequence models and transformers, Recurrent networks
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.

What's being asked

Whether you know the gate equations, and can point to the additive cell update as the reason gradients survive.

Key idea

The cell update ct=ft⊙ct−1+it⊙c~t is additive. Here ft and it are the forget and input gates (values between 0 and 1), c~t is the candidate content, and ⊙ is element-wise multiplication. Along the cell path, ∂ct∂ct−1=diag⁡(ft): there is no repeated multiplication by W and no squashing derivative. With ft≈1, the gradient is carried almost unchanged.

Steps

  1. Gates, with st=[ht−1;xt] (the previous hidden state stacked on the input): ft=σ(Wfst+bf), it=σ(Wist+bi), output gate ot=σ(Wost+bo), and candidate c~t=tanh⁡(Wcst+bc).
  2. State updates: ct=ft⊙ct−1+it⊙c~t and ht=ot⊙tanh⁡(ct).
  3. Gradient along the cell path: ∂cT∂ck≈∏t=k+1Tdiag⁡(ft) (ignoring the indirect paths through ht−1). Compare the RNN's product of diag⁡(1−ht2) W factors. The forget gate is learned and depends on the input, so the network can choose to keep information. Initialising bf to 1 or 2 helps early in training.
  4. GRU: two gates, update zt and reset rt, and no separate cell: ht=(1−zt)⊙ht−1+zt⊙h~t, with h~t=tanh⁡(Wh[rt⊙ht−1;xt]+bh). It uses the same additive idea with fewer parameters, and usually gives similar accuracy.
  5. Parameter counts with input size x=100 and hidden size h=128: one block (one weight matrix plus its bias) has h(h+x)+h=128⋅228+128=29,312. The LSTM has 4 blocks, so 117,248. The GRU has 3, so 87,936. PyTorch uses two bias vectors per block, which gives 117,760 and 88,320.

Common mistakes

How long your answer should be

About 4 minutes; six equations and one gradient line.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: For a vanilla RNN ht=tanh⁡(Wht−1+Uxt+b) with loss L=∑tLt, how does backpropagation through time give ∂LT/∂hk for k<T, and why do gradients over long time spans vanish or explode?
Next in Sequence models and transformers: In an RNN encoder–decoder for translation, what is the bottleneck problem, and how does attention fix it? What are the equations for the context vector at decoder step t, and what is the context vector for scores [2,0,1] over three encoder states with (scalar) values h=[1,2,3]?