What four gates cost
shape▲▲△Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Audit the shapes, parameter counts, arithmetic and training storage of a tanh RNN, a GRU and an LSTM, then find a fair width for comparing two of them.
GIVEN
Input width , hidden width . Count one bias per gate as in (I.10.8), then PyTorch’s two bias vectors per block. For storage, take sequences of length in float32, as in I.9.B04. Assume the plain cell stores for the backward pass and the LSTM stores .
FIND
(a) PyTorch’s LSTM weight and bias shapes, and the rows of the forget gate. (b) Parameter counts for all three cells under both bias conventions, with the GRU’s reset-after form. (c) Multiply–accumulates per step for one sequence. (d) Stored activation bytes. (e) The GRU width whose one-bias count equals the LSTM’s at .
STRATEGY
Each gated block is a plain recurrent layer’s affine map with its own weights. Count one block, then multiply.
SOLUTION
(a) PyTorch stacks the four LSTM blocks in the order input, forget, candidate, output. The input weights are , the recurrent weights , and each bias vector has entries. Counting from zero, the forget gate occupies rows to . Rows to are the input gate. For a GRU the order is reset, update, new, so the blocks are rows tall.
(b) One block has parameters with one bias. So
The reset-after GRU keeps inside the reset, which cannot be merged with : . PyTorch keeps two bias vectors per block, each: , then and .
(c) One affine map from costs multiply–accumulates. The LSTM computes four, per step; the GRU three, ; the plain cell one, . The elementwise gate products add per step and do not change the leading count.
(d) The plain cell stores numbers, bytes. The LSTM, under the stated scheme, stores six vectors per step: numbers, bytes. Other schemes store or recompute gates in the backward pass. Name the scheme before quoting a ratio.
(e) A GRU of width at has parameters. Setting this to gives , whose positive root is : exactly. Its arithmetic is multiply–accumulates per step, against the LSTM’s . In PyTorch’s convention the two counts are and .
A plain RNN cannot match exactly: has no integer root, and widths and give and .
Answer
(a) , , two biases of ; forget rows –. (b) One bias: , ( reset-after), . PyTorch: , , . (c) , , per step. (d) and bytes. (e) , parameters, multiply–accumulates.
Check — sanity
Setting removes every input weight: the one-bias LSTM count becomes , four copies of a plain recurrent map with bias. Counts should always factor this way.
Where this breaks
Multiply–accumulate counts are not runtimes. Fused kernels compute all four LSTM blocks as one -row product, and memory traffic often decides speed. Projection layers, peepholes and bidirectional stacks change every count above.
Variation
At , a common width, the one-bias LSTM block count is parameters, and the GRU’s is .