A single-layer LSTM has input size and hidden size . How many trainable parameters does it have (one bias vector per gate), and how does the count compare with a GRU and a plain RNN of the same sizes? What changes if the LSTM is bidirectional?
Sequence models and transformers, Recurrent networks
Level L2 Elementary. Length Short. Nature Mat Mathematical.
What's being asked
Whether you can count parameters from the gate equations, which is a common GATE-style and interview check on recurrent networks.
Key idea
Every gate or candidate computes with , so one block has parameters. An LSTM has 4 blocks (input, forget, output, candidate), a GRU has 3, a plain RNN has 1.
Steps
- One block with hidden size and input size : weights plus bias , so .
- LSTM has four blocks: parameters.
- GRU has three blocks (reset gate, update gate, candidate): . A plain RNN has one block: , so an LSTM is and a GRU the size of a vanilla RNN at the same width.
- Bidirectional doubles everything, because there is a separate forward and backward network: . The output concatenates both hidden states, so the next layer sees inputs.
- Some libraries (for example PyTorch) keep two bias vectors per gate, giving . State which convention you use.
Common mistakes
- Counting only and forgetting the input weights .
- Forgetting the biases, or counting the cell state as a parameter. It is a state, not a weight.
- Multiplying by the sequence length. The same weights are reused at every time step.
How long your answer should be
4–5 lines with the formula and the three numbers.
Read this
Previous in Sequence models and transformers: Two finished hypotheses have token probabilities and . Which one does beam search prefer by raw log-probability? Which by average log-probability, and which by GNMT length normalisation ? What bias does this show?
Next in Sequence models and transformers: In scaled dot-product attention, why divide by , where and are the query and key matrices and is the key dimension?