Separate parameters, states and matrix products
shape▲▲△Exact values where possible; six decimal places for non-exact numerical results.
STATEMENT
Audit a batched recurrent model before confusing sequence length with model size.
GIVEN
A single-layer, unidirectional tanh RNN uses batch size , sequence length , input width , hidden width and output width . Use (I.9.1), one combined hidden bias, a fixed zero initial state, and a readout at every step. Store a batch of inputs as .
FIND
Tensor shapes, trainable parameter count, main matrix-product multiply-accumulates, and storage for the hidden states in float32.
STRATEGY
Keep the column-vector mathematical convention for a single example. For batched rows, transpose the weight matrices in the implementation. Count each shared matrix once.
SOLUTION
| Quantity | Shape |
|---|---|
| Inputs | |
| One input slice | |
| , , | , , |
| One state batch | |
| All hidden outputs | |
| , | , |
| All readouts |
The row-batch form of the preactivation is . Writing would try to multiply by , whose inner dimensions disagree.
The parameter count is
Each example-step needs multiply-accumulates for the main matrix products. There are example-steps, giving such multiply-accumulates. Counting each multiplication and addition separately gives about operations for those products only. Bias additions, activations, losses and backward work are excluded.
The hidden outputs contain scalars. Four bytes each gives bytes. This is not total training memory: inputs, readouts, other saved values, parameters, gradients and optimiser state also need storage. Streaming inference can instead retain the current state scalars, or bytes for that state alone.
Answer
There are parameters, main-product multiply-accumulates, and bytes of hidden-output storage under the stated conventions. All output logits have shape .
Check — sanity
Doubling doubles these products and stored hidden outputs, but leaves the parameter count unchanged. Doubling makes the recurrent matrix four times as large.
Where this breaks
A two-bias implementation adds another parameters, giving . A learned initial state adds its own parameters. Bidirectionality and additional layers change the calculation again; the number 57 is not a universal RNN count.
Variation
Suppose only the final state is read out. The recurrent products still cost multiply-accumulates, but readout products cost only , for a total of . The parameter count is unchanged.