Md. Asif Uddin
    Problem I.10.B02

    Backpropagation through one LSTM cell

    symbolic▲▲▲

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

    STATEMENT

    Derive the backward pass of an LSTM step, identify the carry line in it, and measure how much of a real derivative the carry line accounts for.

    GIVEN

    The cell of (I.10.1) and (I.10.2), with a loss that may read any ht\vec h_t. For the numerical part, use the scalar cell of I.10.B01 with one change: the candidate also reads the hidden state, c~t=tanh⁡(xtln⁡3+2ht−1)\tilde c_t=\tanh(x_t\ln3+2h_{t-1}). Inputs are (1,0,0)(1,0,0) from zero states.

    FIND

    The recursions for gˉc,t\bar{\vec g}_{c,t} and the four pre-activation derivatives; the derivatives sent to ht−1\vec h_{t-1} and ct−1\vec c_{t-1}; the parameter gradients; and, in the scalar case, ∂c3/∂c1\partial c_3/\partial c_1 compared with its carry-line term.

    STRATEGY

    Draw the arrows leaving ct\vec c_t and ht\vec h_t, add the returns, and only then differentiate the activations. For the scalar part, notice that a constant output gate makes hth_t a function of ctc_t.

    SOLUTION

    Arrows out of the cell. The cell state ct\vec c_t is used twice: by ht=ot⊙tanh⁡(ct)\vec h_t=\vec o_t\odot\tanh(\vec c_t), and by the next cell update, where it is multiplied by ft+1\vec f_{t+1}. Reverse mode adds the two returns:

    gˉc,t=gˉh,t⊙ot⊙(1−tanh⁡2ct)+gˉc,t+1⊙ft+1,gˉc,T+1=0.\bar{\vec g}_{c,t}=\bar{\vec g}_{h,t}\odot\vec o_t\odot(1-\tanh^2\vec c_t) +\bar{\vec g}_{c,t+1}\odot\vec f_{t+1}, \qquad\bar{\vec g}_{c,T+1}=0.

    Here gˉh,t\bar{\vec g}_{h,t} is already the total for ht\vec h_t: its readout term plus everything step t+1t+1 returned through its gates.

    Pre-activations. Write ak,t\vec a_{k,t} for the argument of the gate or candidate kk. Differentiating ht=ot⊙tanh⁡ct\vec h_t=\vec o_t\odot\tanh\vec c_t with respect to ao,t\vec a_{o,t}, and ct=ft⊙ct−1+it⊙c~t\vec c_t=\vec f_t\odot\vec c_{t-1}+\vec i_t\odot\tilde{\vec c}_t with respect to the other three,

    δo,t=gˉh,t⊙tanh⁡(ct)⊙ot⊙(1−ot),δf,t=gˉc,t⊙ct−1⊙ft⊙(1−ft),δi,t=gˉc,t⊙c~t⊙it⊙(1−it),δc,t=gˉc,t⊙it⊙(1−c~t2).\begin{aligned} \vec\delta_{o,t}&=\bar{\vec g}_{h,t}\odot\tanh(\vec c_t)\odot\vec o_t\odot(1-\vec o_t),\\ \vec\delta_{f,t}&=\bar{\vec g}_{c,t}\odot\vec c_{t-1}\odot\vec f_t\odot(1-\vec f_t),\\ \vec\delta_{i,t}&=\bar{\vec g}_{c,t}\odot\tilde{\vec c}_t\odot\vec i_t\odot(1-\vec i_t),\\ \vec\delta_{c,t}&=\bar{\vec g}_{c,t}\odot\vec i_t\odot(1-\tilde{\vec c}_t^2). \end{aligned}

    This is (I.10.6). The output gate is the only one that reads gˉh,t\bar{\vec g}_{h,t} directly; the other three see the cell’s total.

    What goes back one step. Every pre-activation contains Wkht−1\mat W_k\vec h_{t-1}, so the recurrent return to ht−1\vec h_{t-1} is ∑k∈{i,f,o,c}WkTδk,t\sum_{k\in\{i,f,o,c\}}\mat W_k^{\mathsf T}\vec\delta_{k,t}. The cell one step back receives gˉc,t⊙ft\bar{\vec g}_{c,t}\odot\vec f_t, the carry line, which is the second term of the next gˉc,t−1\bar{\vec g}_{c,t-1}. At t=1t=1 these two quantities are the derivatives with respect to h0\vec h_0 and c0\vec c_0.

    Parameters. Each block is used at every step, so its gradients are sums, as in (I.9.4):

    ∇WkL=∑tδk,tht−1T,∇UkL=∑tδk,txtT,∇bkL=∑tδk,t.\nabla_{\mat W_k}\mathcal L=\sum_t\vec\delta_{k,t}\vec h_{t-1}^{\mathsf T},\quad \nabla_{\mat U_k}\mathcal L=\sum_t\vec\delta_{k,t}\vec x_t^{\mathsf T},\quad \nabla_{\vec b_k}\mathcal L=\sum_t\vec\delta_{k,t}.

    The scalar case. With constant gates i=1/2i=1/2, f=o=3/4f=o=3/4, the hidden state ht=34tanh⁡cth_t=\tfrac34\tanh c_t is a function of ctc_t, so the state is cc alone and the one-step derivative is

    kt=∂ct∂ct−1=f⏟carry line+i (1−c~t2)⋅2⋅o (1−tanh⁡2ct−1)⏟through ht−1.k_t=\frac{\partial c_t}{\partial c_{t-1}} =\underbrace{f}_{\text{carry line}} +\underbrace{i\,(1-\tilde c_t^2)\cdot2\cdot o\,(1-\tanh^2c_{t-1})}_{\text{through }h_{t-1}}.

    The forward pass gives c1=0.4c_1=0.4, h1=0.284962h_1=0.284962, then c~2=tanh⁡(0.569924)\tilde c_2=\tanh(0.569924) and onwards. Evaluating along that trajectory, k2=1.221326k_2=1.221326 and k3=1.078827k_3=1.078827, so

    ∂c3∂c1=k3k2=1.317600,carry line alone: f2=0.5625.\frac{\partial c_3}{\partial c_1}=k_3k_2=1.317600, \qquad\text{carry line alone: }f^2=0.5625.

    Expanding k3k2k_3k_2 gives four terms. Only f⋅ff\cdot f avoids the recurrent weight; the other three each contain it at least once, as I.10.T1 says. Here they contribute 0.7551000.755100, more than the carry line.

    The reproduction script perturbs c1c_1 by 10−610^{-6}, reruns steps two and three, and matches 1.3176001.317600 by central differences. It then checks the vector recursions above on a two-unit cell with non-symmetric recurrent weights, losses at three of four steps and nonzero initial states. All twelve parameter blocks, c0\vec c_0 and h0\vec h_0 agree with finite differences.

    Answer

    gˉc,t=gˉh,t⊙ot⊙(1−tanh⁡2ct)+gˉc,t+1⊙ft+1\bar{\vec g}_{c,t}=\bar{\vec g}_{h,t}\odot\vec o_t\odot(1-\tanh^2\vec c_t)+\bar{\vec g}_{c,t+1}\odot\vec f_{t+1}, with the four δ\vec\delta above, a return of ∑kWkTδk,t\sum_k\mat W_k^{\mathsf T}\vec\delta_{k,t} to ht−1\vec h_{t-1} and gˉc,t⊙ft\bar{\vec g}_{c,t}\odot\vec f_t to ct−1\vec c_{t-1}. In the scalar case ∂c3/∂c1=1.317600\partial c_3/\partial c_1=1.317600, of which the carry line accounts for 0.56250.5625.

    Check — sanity

    Set every recurrent weight to zero. Then Mt=0\mat M_t=0, the hidden-state return vanishes, and the derivative between cell states is exactly the product of forget values. In the scalar case kt=fk_t=f and ∂c3/∂c1=0.5625\partial c_3/\partial c_1=0.5625.

    Where this breaks

    Treating ot\vec o_t as a function of ct\vec c_t would add a false term: the output gate reads ht−1\vec h_{t-1} and xt\vec x_t, not the cell. A peephole LSTM, which does let gates read the cell, adds genuine terms of that kind and needs its own derivation.

    Variation

    Replace the candidate weight two with zero. Both factors become 3/43/4 and the derivative falls to 0.56250.5625. The weight did not change the carry line; it changed how much signal reached the cell by other routes.

    Draws on