Md. Asif Uddin
    Problem I.9.B04

    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 N=4N=4, sequence length T=6T=6, input width D=3D=3, hidden width H=5H=5 and output width C=2C=2. 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 (N,T,D)(N,T,D).

    FIND

    Tensor shapes, trainable parameter count, main matrix-product multiply-accumulates, and storage for the TT 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

    QuantityShape
    Inputs(4,6,3)(4,6,3)
    One input slice XtX_t(4,3)(4,3)
    UU, WW, bb(5,3)(5,3), (5,5)(5,5), (5)(5)
    One state batch HtH_t(4,5)(4,5)
    All hidden outputs(4,6,5)(4,6,5)
    VV, cc(2,5)(2,5), (2)(2)
    All readouts(4,6,2)(4,6,2)

    The row-batch form of the preactivation is XtUT+Ht−1WT+bX_tU^{\mathsf T}+H_{t-1}W^{\mathsf T}+b. Writing XtUX_tU would try to multiply (4,3)(4,3) by (5,3)(5,3), whose inner dimensions disagree.

    The parameter count is

    HD+H2+H+CH+C=15+25+5+10+2=57.\begin{aligned} HD+H^2+H+CH+C &=15+25+5+10+2\\ &=57. \end{aligned}

    Each example-step needs HD+H2+CH=50HD+H^2+CH=50 multiply-accumulates for the main matrix products. There are NT=24NT=24 example-steps, giving 12001200 such multiply-accumulates. Counting each multiplication and addition separately gives about 24002400 operations for those products only. Bias additions, activations, losses and backward work are excluded.

    The hidden outputs contain NTH=120NTH=120 scalars. Four bytes each gives 480480 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 NH=20NH=20 state scalars, or 8080 bytes for that state alone.

    Answer

    There are 5757 parameters, 12001200 main-product multiply-accumulates, and 480480 bytes of hidden-output storage under the stated conventions. All output logits have shape (4,6,2)(4,6,2).

    Check — sanity

    Doubling TT doubles these products and stored hidden outputs, but leaves the parameter count unchanged. Doubling HH makes the recurrent matrix four times as large.

    Where this breaks

    A two-bias implementation adds another H=5H=5 parameters, giving 6262. 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 NT(HD+H2)=960NT(HD+H^2)=960 multiply-accumulates, but readout products cost only NCH=40NCH=40, for a total of 10001000. The parameter count is unchanged.

    Draws on