A single-layer LSTM has input size 128 and hidden size 256. 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 W[ht−1;xt]+b with W∈RH×(H+D), so one block has H(H+D)+H parameters. An LSTM has 4 blocks (input, forget, output, candidate), a GRU has 3, a plain RNN has 1.

Steps

  1. One block with hidden size H=256 and input size D=128: weights H(H+D)=256×384=98,304 plus bias 256, so 98,560.
  2. LSTM has four blocks: 4×98,560=394,240 parameters.
  3. GRU has three blocks (reset gate, update gate, candidate): 3×98,560=295,680. A plain RNN has one block: 98,560, so an LSTM is 4× and a GRU 3× the size of a vanilla RNN at the same width.
  4. Bidirectional doubles everything, because there is a separate forward and backward network: 2×394,240=788,480. The output concatenates both hidden states, so the next layer sees 2H inputs.
  5. Some libraries (for example PyTorch) keep two bias vectors per gate, giving 4×(98,304+512)=395,264. State which convention you use.

Common mistakes

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 H1=(0.4, 0.5) and H2=(0.6, 0.6, 0.6, 0.6). Which one does beam search prefer by raw log-probability? Which by average log-probability, and which by GNMT length normalisation log⁡P/((5+∣Y∣)/6)? What bias does this show?
Next in Sequence models and transformers: In scaled dot-product attention, why divide QK⊤ by dk, where Q and K are the query and key matrices and dk is the key dimension?