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 is additive. Here and are the forget and input gates (values between 0 and 1), is the candidate content, and is element-wise multiplication. Along the cell path, : there is no repeated multiplication by and no squashing derivative. With , the gradient is carried almost unchanged.
Steps
- Gates, with (the previous hidden state stacked on the input): , , output gate , and candidate .
- State updates: and .
- Gradient along the cell path: (ignoring the indirect paths through ). Compare the RNN's product of factors. The forget gate is learned and depends on the input, so the network can choose to keep information. Initialising to 1 or 2 helps early in training.
- GRU: two gates, update and reset , and no separate cell: , with . It uses the same additive idea with fewer parameters, and usually gives similar accuracy.
- Parameter counts with input size and hidden size : one block (one weight matrix plus its bias) has . The LSTM has 4 blocks, so . The GRU has 3, so . PyTorch uses two bias vectors per block, which gives and .
Common mistakes
- Saying LSTMs eliminate vanishing gradients. With the signal still decays, just at a learned rate.
- Crediting the gates alone. The key is the additive update of .
- Using for the gates or for the candidate.
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 with loss , how does backpropagation through time give for , 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 , and what is the context vector for scores over three encoder states with (scalar) values ?