Md. Asif Uddin
    Problem I.10.B01

    Write once, then keep

    numeric▲△△

    Exact values where possible; six decimal places for non-exact numerical results.

    STATEMENT

    Run a scalar LSTM through three inputs and separate what the input gate writes from what the forget gate keeps.

    GIVEN

    A scalar cell follows (I.10.1) and (I.10.2) with every recurrent weight zero. The gates read only their biases: bi=0b_i=0, bf=ln⁡3b_f=\ln3, bo=ln⁡3b_o=\ln3, and ui=uf=uo=0u_i=u_f=u_o=0. The candidate is c~t=tanh⁡(xtln⁡3)\tilde c_t=\tanh(x_t\ln3), so uc=ln⁡3u_c=\ln3 and bc=0b_c=0. Start from c0=h0=0c_0=h_0=0 and feed (1,0,0)(1,0,0).

    FIND

    The three gate values, the candidates, all three cell and hidden states, the third cell state when bf=ln⁡99b_f=\ln99 instead, and the number of trainable scalars.

    STRATEGY

    Evaluate the gates first; they are constants here. Then apply the cell update one step at a time and read each hidden state through the output gate.

    SOLUTION

    Since σ(ln⁡a)=a/(1+a)\sigma(\ln a)=a/(1+a), the gates are i=σ(0)=1/2i=\sigma(0)=1/2 and f=o=σ(ln⁡3)=3/4f=o=\sigma(\ln3)=3/4. The first candidate is

    tanh⁡(ln⁡3)=3−1/33+1/3=45,\tanh(\ln3)=\frac{3-1/3}{3+1/3}=\frac45,

    and the later candidates are tanh⁡(0)=0\tanh(0)=0.

    The cell update writes once and then only keeps:

    c1=34⋅0+12⋅45=0.4,c2=34⋅0.4+12⋅0=0.3,c3=34⋅0.3=0.225.\begin{aligned} c_1&=\tfrac34\cdot0+\tfrac12\cdot\tfrac45=0.4,\\ c_2&=\tfrac34\cdot0.4+\tfrac12\cdot0=0.3,\\ c_3&=\tfrac34\cdot0.3=0.225. \end{aligned}

    The hidden states are ht=34tanh⁡(ct)h_t=\tfrac34\tanh(c_t): 0.2849620.284962, 0.2184840.218484 and 0.1659590.165959.

    With bf=ln⁡99b_f=\ln99 the forget gate is 0.990.99, and c3=0.4⋅0.992=0.39204c_3=0.4\cdot0.99^2=0.39204. The input gate decided how much was written; the forget gate alone decided how much of it survived two silent steps.

    Each of the four blocks has one input weight, one recurrent weight and one bias, so the scalar cell has 1212 trainable scalars. The plain scalar cell of I.9.B01 had three before its readout.

    Answer

    Gates (i,f,o)=(1/2,3/4,3/4)(i,f,o)=(1/2,3/4,3/4); candidates (0.8,0,0)(0.8,0,0); cell states (0.4,0.3,0.225)(0.4,0.3,0.225); hidden states (0.284962,0.218484,0.165959)(0.284962,0.218484,0.165959); with f=0.99f=0.99, c3=0.39204c_3=0.39204; twelve trainable scalars.

    Check — sanity

    Each hidden state is smaller than its cell state. For c>0c>0, tanh⁡c<c\tanh c<c, and the output gate multiplies by a further three quarters. The hidden state is a view of the cell, not a copy of it.

    Check — numeric · i-10-gating-checks.py

    The complete reproduction script uses only the Python standard library. From a checkout of the repository, run:

    python3 scripts/snippets/i-10-gating-checks.py

    It checks all fifteen problems of the chapter. Its first line reports the cell states 0.40.4, 0.30.3, 0.2250.225, the hidden states above and the f=0.99f=0.99 cell state 0.3920400.392040. Its second line confirms that a full two-unit LSTM backward pass matches central finite differences for every parameter.

    Executed in CI. The digits above are the digits it printed.

    Where this breaks

    Nonzero recurrent weights make the gates depend on ht−1h_{t-1}, so they change from step to step and the cell states are no longer a geometric sequence. The write-then-keep reading survives; the clean numbers do not.

    Variation

    Feed (1,1,0)(1,1,0) instead. The second step keeps 0.30.3 and writes another 0.40.4, so c2=0.7c_2=0.7 and c3=0.525c_3=0.525. The cell accumulates; it does not overwrite unless the forget gate is small.

    Draws on