Practice / Convolutions and recurrences

Backprop through time: RNN and LSTM gradients

Ten problems on backpropagation through time: the one-step Jacobians of a vanilla RNN, the backward recursion for the hidden-state gradients, the weight gradients as sums over time, a two-step example by hand, vanishing and exploding gradients as a product of Jacobians, many-to-one classification, and the LSTM cell's backward pass and cell-state path, with worked solutions and the mistakes that drop a time step or a factor.

Before you start

A recurrent network applies the same layer at every step of a sequence, feeding each step's hidden state into the next. Unrolled over TT steps it is a TT-layer network whose layers share their weights, and backpropagation through time is ordinary backprop on that unrolled network. Two things make it different from the earlier pages: each hidden state reaches the loss along two routes, through its own output and through the next step, and each weight is used TT times, so its gradient is a sum over time. These ten problems derive the backward recursion and the weight gradients, check them on a two-step example by hand, show where vanishing and exploding gradients come from, and then work through the LSTM cell, whose cell state is built to avoid them. The five mistakes are the ones that give a gradient of the right shape: one time step instead of all of them, hth_t where ht−1h_{t-1} belongs, a missing tanh⁡′\tanh', WW where W⊤W^\top belongs, and an LSTM cell gradient without its path to the next cell.

  • The conventions are those of the previous pages: vectors are columns, Jacobians are in numerator layout, a gradient has the shape of the variable it is taken with respect to, and for a scalar LL of uu with uu a function of xx, ∇xL=(∂u/∂x)⊤∇uL\nabla_x L = (\partial u/\partial x)^\top \nabla_u L. ⊙\odot is the elementwise product.
  • The network: inputs x1,…,xT∈Rdx_1, \dots, x_T \in \mathbb{R}^d and an initial state h0∈Rmh_0 \in \mathbb{R}^m. For t=1,…,Tt = 1, \dots, T, at=Wht−1+Uxt+ba_t = W h_{t-1} + U x_t + b, ht=tanh⁡(at)h_t = \tanh(a_t) and zt=Vht+cz_t = V h_t + c, with tanh⁡\tanh applied elementwise, ht∈Rmh_t \in \mathbb{R}^m the hidden state and zt∈Rkz_t \in \mathbb{R}^k the output. The same WW, UU, bb, VV, cc are used at every step.
  • The loss is L=∑t=1Tℓt(zt)L = \sum_{t=1}^T \ell_t(z_t), where each ℓt\ell_t is a scalar loss of step tt's output alone; a step with no target has ℓt=0\ell_t = 0.
  • tanh⁡′(u)=1−tanh⁡2(u)\tanh'(u) = 1 - \tanh^2(u), so the derivative of hth_t with respect to ata_t, entry by entry, is 1−ht⊙ht1 - h_t\odot h_t (11 minus each entry of ht⊙hth_t\odot h_t).
  • Three gradients per step: et:=∇ztℓte_t := \nabla_{z_t}\ell_t, the gradient of step tt's own loss at its output; gt:=∇htLg_t := \nabla_{h_t}L, the gradient of the whole loss at hth_t, including every later step; and δt:=∇atL\delta_t := \nabla_{a_t}L.
  • ∥A∥2\|A\|_2 is the spectral norm, the largest singular value of AA. It satisfies ∥AB∥2≤∥A∥2∥B∥2\|AB\|_2 \le \|A\|_2\|B\|_2, and for a diagonal matrix it is the largest absolute value on the diagonal.

Builds on: Jacobians and the chain rule, One-hidden-layer backprop, the whole backward pass

Problems

  1. ·

    Give the shapes of WW, UU, bb, VV, cc and the number of parameters. Which of h0,x1,…,xTh_0, x_1, \dots, x_T does hth_t depend on?

  2. ··

    Compute the Jacobians ∂ht/∂ht−1\partial h_t/\partial h_{t-1} and ∂ht/∂xt\partial h_t/\partial x_t.

  3. ··

    Derive the backward recursion for gtg_t and δt\delta_t, starting at t=Tt = T.

  4. ···

    Compute ∇WL\nabla_W L, ∇UL\nabla_U L, ∇bL\nabla_b L, ∇VL\nabla_V L and ∇cL\nabla_c L in terms of the δt\delta_t, ete_t, hth_t and xtx_t.

  5. ··

    By hand: a scalar linear RNN ht=w ht−1+u xth_t = w\,h_{t-1} + u\,x_t (no tanh⁡\tanh, no bias, m=d=1m = d = 1), with h0=0h_0 = 0, x1=1x_1 = 1, x2=2x_2 = 2, w=12w = \tfrac12, u=1u = 1, and a loss at the last step only, L=12h22L = \tfrac12h_2^2. Compute ∂L/∂w\partial L/\partial w and ∂L/∂u\partial L/\partial u with the recursion, then check them by writing LL as a function of ww and uu.

  6. ···

    For t<Tt < T, write ∂hT/∂ht\partial h_T/\partial h_t as a product of the Jacobians of Problem 2, and show that ∥∂hT/∂ht∥2≤∥W∥2 T−t\|\partial h_T/\partial h_t\|_2 \le \|W\|_2^{\,T-t}. What does this say about how the loss at step TT reaches gtg_t when ∥W∥2<1\|W\|_2 < 1?

  7. ··

    Many-to-one: a classifier reads the whole sequence and is scored once. ℓt=0\ell_t = 0 for t<Tt < T, and ℓT=−∑jyjlog⁡sj\ell_T = -\sum_j y_j\log s_j with s=softmax⁡(zT)s = \operatorname{softmax}(z_T) and yy one-hot. Give the δt\delta_t, ∇WL\nabla_W L and ∇VL\nabla_V L.

  8. ··

    Compute ∇xtL\nabla_{x_t}L and ∇h0L\nabla_{h_0}L. When is ∇h0L\nabla_{h_0}L needed?

  9. ···

    An LSTM keeps a cell state ct∈Rmc_t \in \mathbb{R}^m beside hth_t:

    ct=ft⊙ct−1+it⊙c~tc_t = f_t\odot c_{t-1} + i_t\odot\tilde c_t

    and

    ht=ot⊙tanh⁡(ct),h_t = o_t\odot\tanh(c_t),

    where the forget, input and output gates ft,it,ot∈(0,1)mf_t, i_t, o_t \in (0, 1)^m and the candidate c~t∈(−1,1)m\tilde c_t \in (-1, 1)^m are computed from ht−1h_{t-1} and xtx_t. Treat these four as inputs to the cell. You are given dhtdh_t, the total gradient of LL at hth_t, and cˉt\bar c_t, the gradient that reaches ctc_t from step t+1t + 1 through ct+1c_{t+1}. Compute dctdc_t, the total gradient at ctc_t, and the gradients to ct−1c_{t-1} (through this cell), ftf_t, iti_t, c~t\tilde c_t and oto_t.

  10. ···

    Follow the cell states alone: holding every gate and candidate fixed, compute ∂cT/∂ct\partial c_T/\partial c_t for t<Tt < T, and compare it with Problem 6.

Worked solutions

Problem 1

Give the shapes of WW, UU, bb, VV, cc and the number of parameters. Which of h0,x1,…,xTh_0, x_1, \dots, x_T does hth_t depend on?

  1. Wht−1∈RmW h_{t-1} \in \mathbb{R}^m for ht−1∈Rmh_{t-1} \in \mathbb{R}^m, so WW is m×mm\times m; Uxt∈RmU x_t \in \mathbb{R}^m for xt∈Rdx_t \in \mathbb{R}^d, so UU is m×dm\times d; b∈Rmb \in \mathbb{R}^m.All three terms of ata_t are added to give the mm entries of ata_t, so each must be an mm-vector.
  2. VV is k×mk\times m and c∈Rkc \in \mathbb{R}^k.zt=Vht+cz_t = Vh_t + c maps the mm-vector hth_t to the kk-vector ztz_t.
  3. The count is m2+md+m+km+km^2 + md + m + km + k.The sizes of the five arrays. None of them has a time index, because the same arrays are reused at every step.
  4. ht=tanh⁡(Wht−1+Uxt+b)h_t = \tanh(W h_{t-1} + U x_t + b) and, unrolling, ht−1h_{t-1} depends on h0,x1,…,xt−1h_0, x_1, \dots, x_{t-1}.Each step adds one new input to what the previous state already carried; by induction from h1=tanh⁡(Wh0+Ux1+b)h_1 = \tanh(Wh_0 + Ux_1 + b).
  5. W:m×mW: m\times m, U:m×dU: m\times d, b:mb: m, V:k×mV: k\times m, c:kc: k; m2+md+m+km+km^2 + md + m + km + k parameters, whatever TT is; hth_t depends on h0h_0 and x1,…,xtx_1, \dots, x_t, and on no later inputThe parameter count does not grow with the sequence length, which is the point of sharing the weights; the price is that every weight gradient is a sum over steps (Problem 4).

Problem 2

Compute the Jacobians ∂ht/∂ht−1\partial h_t/\partial h_{t-1} and ∂ht/∂xt\partial h_t/\partial x_t.

  1. ∂at/∂ht−1=W\partial a_t/\partial h_{t-1} = W and ∂at/∂xt=U\partial a_t/\partial x_t = U.ata_t is affine in each of ht−1h_{t-1} and xtx_t, and the Jacobian of Mv+constMv + \text{const} is MM.
  2. ∂ht/∂at=diag⁡(1−ht⊙ht)\partial h_t/\partial a_t = \operatorname{diag}(1 - h_t\odot h_t).tanh⁡\tanh is elementwise, so its Jacobian is diagonal, with tanh⁡′(at,i)=1−ht,i2\tanh'(a_{t,i}) = 1 - h_{t,i}^2 on the diagonal; the forward pass has already computed hth_t.
  3. ∂ht/∂ht−1=diag⁡(1−ht⊙ht) W\partial h_t/\partial h_{t-1} = \operatorname{diag}(1 - h_t\odot h_t)\,W and ∂ht/∂xt=diag⁡(1−ht⊙ht) U\partial h_t/\partial x_t = \operatorname{diag}(1 - h_t\odot h_t)\,UThe chain rule in numerator layout puts the outer Jacobian on the left: (m×m)(m×m)(m\times m)(m\times m) and (m×m)(m×d)(m\times m)(m\times d), the shapes of an mm-vector differentiated by an mm-vector and by a dd-vector.

Problem 3

Derive the backward recursion for gtg_t and δt\delta_t, starting at t=Tt = T.

  1. hTh_T reaches LL only through zTz_T, so gT=(∂zT/∂hT)⊤eT=V⊤eTg_T = (\partial z_T/\partial h_T)^\top e_T = V^\top e_T.There is no step T+1T + 1, and the later losses are the only other way hTh_T could matter.
  2. For t<Tt < T, hth_t reaches LL through ztz_t and through at+1a_{t+1}, and through nothing else.hth_t is used in exactly two places in the forward pass: the output of step tt and the pre-activation of step t+1t + 1.
  3. gt=V⊤et+(∂at+1/∂ht)⊤δt+1=V⊤et+W⊤δt+1g_t = V^\top e_t + (\partial a_{t+1}/\partial h_t)^\top\delta_{t+1} = V^\top e_t + W^\top\delta_{t+1}.The chain rule adds the two routes of step 2; δt+1\delta_{t+1} is the total gradient at at+1a_{t+1}, so it already carries every step after t+1t + 1.
  4. δt=(∂ht/∂at)⊤gt=(1−ht⊙ht)⊙gt\delta_t = (\partial h_t/\partial a_t)^\top g_t = (1 - h_t\odot h_t)\odot g_t.ata_t reaches LL only through hth_t, and a diagonal Jacobian acts as an elementwise product (Problem 2, step 2).
  5. gT=V⊤eTg_T = V^\top e_T; gt=V⊤et+W⊤δt+1g_t = V^\top e_t + W^\top\delta_{t+1} for t<Tt < T; δt=(1−ht⊙ht)⊙gt\delta_t = (1 - h_t\odot h_t)\odot g_tThe recursion runs backwards from t=Tt = T to t=1t = 1, one step per forward step, so the backward pass costs about as much as the forward pass; it needs every hth_t, which is why training stores the whole sequence of states.

Problem 4

Compute ∇WL\nabla_W L, ∇UL\nabla_U L, ∇bL\nabla_b L, ∇VL\nabla_V L and ∇cL\nabla_c L in terms of the δt\delta_t, ete_t, hth_t and xtx_t.

  1. Give step tt its own copy W(t)W^{(t)} of WW, used only in ata_t; then ∇WL=∑t∇W(t)L\nabla_W L = \sum_t \nabla_{W^{(t)}}L evaluated at W(t)=WW^{(t)} = W.LL depends on WW only through its TT uses, and the chain rule adds the contributions of every use: this is the unrolled network with its shared weights untied.
  2. Wij(t)W^{(t)}_{ij} appears only in at,ia_{t,i}, with coefficient ht−1,jh_{t-1,j}, so ∂L/∂Wij(t)=δt,i ht−1,j\partial L/\partial W^{(t)}_{ij} = \delta_{t,i}\,h_{t-1,j}, that is ∇W(t)L=δtht−1⊤\nabla_{W^{(t)}}L = \delta_t h_{t-1}^\top.The copy W(t)W^{(t)} reaches LL only through ata_t, and δt\delta_t is the total gradient there; ht−1h_{t-1} is an input to step tt, so it is not differentiated here, and its own dependence on WW is the other copies' contribution.
  3. In the same way ∇U(t)L=δtxt⊤\nabla_{U^{(t)}}L = \delta_t x_t^\top and ∇b(t)L=δt\nabla_{b^{(t)}}L = \delta_t.Uij(t)U^{(t)}_{ij} multiplies xt,jx_{t,j} into at,ia_{t,i}, and b(t)b^{(t)} is added to ata_t with coefficient 11.
  4. ∇V(t)L=etht⊤\nabla_{V^{(t)}}L = e_t h_t^\top and ∇c(t)L=et\nabla_{c^{(t)}}L = e_t.V(t)V^{(t)} and c(t)c^{(t)} appear only in ztz_t, which reaches LL only through ℓt\ell_t, so the gradient at ztz_t is ete_t; the pattern is the outer product of the one-hidden-layer page.
  5. ∇WL=∑tδtht−1⊤\nabla_W L = \sum_t \delta_t h_{t-1}^\top, ∇UL=∑tδtxt⊤\nabla_U L = \sum_t \delta_t x_t^\top, ∇bL=∑tδt\nabla_b L = \sum_t \delta_t, ∇VL=∑tetht⊤\nabla_V L = \sum_t e_t h_t^\top, ∇cL=∑tet\nabla_c L = \sum_t e_tSums of the copies' gradients, steps 1 to 4, each with the shape of its parameter: δtht−1⊤\delta_t h_{t-1}^\top is (m×1)(1×m)(m\times1)(1\times m). The output weights see only the local ete_t; the recurrent weights see the δt\delta_t, which carry the future.

Problem 5

By hand: a scalar linear RNN ht=w ht−1+u xth_t = w\,h_{t-1} + u\,x_t (no tanh⁡\tanh, no bias, m=d=1m = d = 1), with h0=0h_0 = 0, x1=1x_1 = 1, x2=2x_2 = 2, w=12w = \tfrac12, u=1u = 1, and a loss at the last step only, L=12h22L = \tfrac12h_2^2. Compute ∂L/∂w\partial L/\partial w and ∂L/∂u\partial L/\partial u with the recursion, then check them by writing LL as a function of ww and uu.

  1. h1=12⋅0+1⋅1=1h_1 = \tfrac12\cdot0 + 1\cdot1 = 1 and h2=12⋅1+1⋅2=52h_2 = \tfrac12\cdot1 + 1\cdot2 = \tfrac52.The forward pass, which the backward pass needs.
  2. δ2=g2=h2=52\delta_2 = g_2 = h_2 = \tfrac52.ddh12h2=h\tfrac{d}{dh}\tfrac12h^2 = h, and without a tanh⁡\tanh the factor 1−h⊙h1 - h\odot h becomes 11, so δt=gt\delta_t = g_t.
  3. δ1=g1=w δ2=54\delta_1 = g_1 = w\,\delta_2 = \tfrac54.Problem 3, step 3, with no loss at step 11 (e1=0e_1 = 0) and W⊤=wW^\top = w.
  4. ∂L/∂w=δ2h1+δ1h0=52⋅1+54⋅0=52\partial L/\partial w = \delta_2 h_1 + \delta_1 h_0 = \tfrac52\cdot1 + \tfrac54\cdot0 = \tfrac52.Problem 4: one term per step, each pairing δt\delta_t with the state that came into step tt.
  5. ∂L/∂u=δ2x2+δ1x1=52⋅2+54⋅1=254\partial L/\partial u = \delta_2 x_2 + \delta_1 x_1 = \tfrac52\cdot2 + \tfrac54\cdot1 = \tfrac{25}4.Problem 4 with xtx_t in place of ht−1h_{t-1}.
  6. Directly: h2=w ux1+ux2=u(w+2)h_2 = w\,u x_1 + u x_2 = u(w + 2), so L=12u2(w+2)2L = \tfrac12u^2(w + 2)^2, ∂L/∂w=u2(w+2)=52\partial L/\partial w = u^2(w + 2) = \tfrac52 and ∂L/∂u=u(w+2)2=254\partial L/\partial u = u(w + 2)^2 = \tfrac{25}4.Substituting h1h_1 into h2h_2 removes the recursion; the two routes agree.
  7. ∂L/∂w=52\partial L/\partial w = \tfrac52 and ∂L/∂u=254\partial L/\partial u = \tfrac{25}4The 54\tfrac54 in ∂L/∂u\partial L/\partial u is the contribution of x1x_1 through two steps; a gradient that stopped at the last step would be 55.

Problem 6

For t<Tt < T, write ∂hT/∂ht\partial h_T/\partial h_t as a product of the Jacobians of Problem 2, and show that ∥∂hT/∂ht∥2≤∥W∥2 T−t\|\partial h_T/\partial h_t\|_2 \le \|W\|_2^{\,T-t}. What does this say about how the loss at step TT reaches gtg_t when ∥W∥2<1\|W\|_2 < 1?

  1. ∂hT∂ht=∂hT∂hT−1 ∂hT−1∂hT−2⋯∂ht+1∂ht\dfrac{\partial h_T}{\partial h_t} = \dfrac{\partial h_T}{\partial h_{T-1}}\,\dfrac{\partial h_{T-1}}{\partial h_{T-2}}\cdots\dfrac{\partial h_{t+1}}{\partial h_t}.hTh_T depends on hth_t only through the chain ht+1,…,hT−1h_{t+1}, \dots, h_{T-1}, with the inputs xsx_s fixed; numerator layout puts the latest step on the left.
  2. Each factor is DsWD_s W with Ds=diag⁡(1−hs⊙hs)D_s = \operatorname{diag}(1 - h_s\odot h_s), s=t+1,…,Ts = t+1, \dots, T.Problem 2.
  3. ∥Ds∥2≤1\|D_s\|_2 \le 1.DsD_s is diagonal with entries 1−hs,i21 - h_{s,i}^2, and hs,i=tanh⁡(as,i)∈(−1,1)h_{s,i} = \tanh(a_{s,i}) \in (-1, 1) puts every entry in (0,1](0, 1].
  4. ∥∏sDsW∥2≤∏s∥Ds∥2 ∥W∥2≤∥W∥2 T−t\big\|\prod_s D_s W\big\|_2 \le \prod_s \|D_s\|_2\,\|W\|_2 \le \|W\|_2^{\,T-t}.Submultiplicativity applied to the 2(T−t)2(T - t) factors, then step 3.
  5. ∂hT∂ht=DTW DT−1W⋯Dt+1W\dfrac{\partial h_T}{\partial h_t} = D_T W\,D_{T-1} W\cdots D_{t+1} W with Ds=diag⁡(1−hs⊙hs)D_s = \operatorname{diag}(1 - h_s\odot h_s), and ∥∂hT/∂ht∥2≤∥W∥2 T−t\big\|\partial h_T/\partial h_t\big\|_2 \le \|W\|_2^{\,T-t}The loss at step TT reaches gtg_t as (∂hT/∂ht)⊤V⊤eT(\partial h_T/\partial h_t)^\top V^\top e_T, so with ∥W∥2<1\|W\|_2 < 1 its size shrinks at least geometrically with the distance T−tT - t: the vanishing gradient, and saturated units (DsD_s near 00) shrink it further. With ∥W∥2>1\|W\|_2 > 1 the bound allows the product to grow geometrically, the exploding gradient, which is why RNN training clips gradient norms. The bound is one-sided: it guarantees vanishing, it does not guarantee explosion.

Problem 7

Many-to-one: a classifier reads the whole sequence and is scored once. ℓt=0\ell_t = 0 for t<Tt < T, and ℓT=−∑jyjlog⁡sj\ell_T = -\sum_j y_j\log s_j with s=softmax⁡(zT)s = \operatorname{softmax}(z_T) and yy one-hot. Give the δt\delta_t, ∇WL\nabla_W L and ∇VL\nabla_V L.

  1. et=0e_t = 0 for t<Tt < T and eT=s−ye_T = s - y.Steps before TT have no loss of their own; the softmax page gives the gradient of cross-entropy at the logits.
  2. gT=V⊤(s−y)g_T = V^\top(s - y) and gt=W⊤δt+1g_t = W^\top\delta_{t+1} for t<Tt < T.Problem 3 with step 1: only the route through the next step is left.
  3. δT=(1−hT⊙hT)⊙V⊤(s−y)\delta_T = (1 - h_T\odot h_T)\odot V^\top(s - y) and δt=(1−ht⊙ht)⊙W⊤δt+1\delta_t = (1 - h_t\odot h_t)\odot W^\top\delta_{t+1}.Problem 3, step 4.
  4. ∇VL=∑tetht⊤=(s−y)hT⊤\nabla_V L = \sum_t e_t h_t^\top = (s - y)h_T^\top.Problem 4; every term with t<Tt < T is 00.
  5. δT=(1−hT⊙hT)⊙V⊤(s−y)\delta_T = (1 - h_T\odot h_T)\odot V^\top(s - y), δt=(1−ht⊙ht)⊙W⊤δt+1\delta_t = (1 - h_t\odot h_t)\odot W^\top\delta_{t+1} for t<Tt < T; ∇WL=∑tδtht−1⊤\nabla_W L = \sum_t \delta_t h_{t-1}^\top and ∇VL=(s−y) hT⊤\nabla_V L = (s - y)\,h_T^\topThe whole training signal for the early steps arrives through Problem 6's product, so this setup is where vanishing gradients bite hardest: the first inputs of a long sequence barely move the weights.

Problem 8

Compute ∇xtL\nabla_{x_t}L and ∇h0L\nabla_{h_0}L. When is ∇h0L\nabla_{h_0}L needed?

  1. xtx_t is used only in ata_t, so ∇xtL=(∂at/∂xt)⊤δt=U⊤δt\nabla_{x_t}L = (\partial a_t/\partial x_t)^\top\delta_t = U^\top\delta_t.xtx_t appears in no other step, and δt\delta_t is the total gradient at ata_t.
  2. h0h_0 is used only in a1a_1, so ∇h0L=(∂a1/∂h0)⊤δ1=W⊤δ1\nabla_{h_0}L = (\partial a_1/\partial h_0)^\top\delta_1 = W^\top\delta_1.There is no output z0z_0, so the route through a step's own output is absent: Problem 3, step 3, with only the second term.
  3. ∇xtL=U⊤δt\nabla_{x_t}L = U^\top\delta_t and ∇h0L=W⊤δ1\nabla_{h_0}L = W^\top\delta_1∇xtL\nabla_{x_t}L continues the backward pass into whatever produced xtx_t: an embedding table or a lower RNN layer. ∇h0L\nabla_{h_0}L is needed when h0h_0 is learned, or when it is another network's output, as in an encoder–decoder where the decoder starts from the encoder's last state; with h0h_0 fixed at zero it is discarded.

Problem 9

An LSTM keeps a cell state ct∈Rmc_t \in \mathbb{R}^m beside hth_t:

ct=ft⊙ct−1+it⊙c~tc_t = f_t\odot c_{t-1} + i_t\odot\tilde c_t

and

ht=ot⊙tanh⁡(ct),h_t = o_t\odot\tanh(c_t),

where the forget, input and output gates ft,it,ot∈(0,1)mf_t, i_t, o_t \in (0, 1)^m and the candidate c~t∈(−1,1)m\tilde c_t \in (-1, 1)^m are computed from ht−1h_{t-1} and xtx_t. Treat these four as inputs to the cell. You are given dhtdh_t, the total gradient of LL at hth_t, and cˉt\bar c_t, the gradient that reaches ctc_t from step t+1t + 1 through ct+1c_{t+1}. Compute dctdc_t, the total gradient at ctc_t, and the gradients to ct−1c_{t-1} (through this cell), ftf_t, iti_t, c~t\tilde c_t and oto_t.

  1. ctc_t reaches LL through hth_t and through ct+1c_{t+1}, so dct=cˉt+dht⊙ot⊙(1−tanh⁡(ct)⊙tanh⁡(ct))dc_t = \bar c_t + dh_t\odot o_t\odot\big(1 - \tanh(c_t)\odot\tanh(c_t)\big).Two routes, added: cˉt\bar c_t is the second by definition, and the first goes through ht,i=ot,itanh⁡(ct,i)h_{t,i} = o_{t,i}\tanh(c_{t,i}), whose derivative in ct,ic_{t,i} is ot,i(1−tanh⁡2ct,i)o_{t,i}(1 - \tanh^2 c_{t,i}).
  2. dot=tanh⁡(ct)⊙dhtdo_t = \tanh(c_t)\odot dh_t.oto_t appears only in hth_t, entry by entry, with coefficient tanh⁡(ct)\tanh(c_t).
  3. ct,j=ft,jct−1,j+it,jc~t,jc_{t,j} = f_{t,j}c_{t-1,j} + i_{t,j}\tilde c_{t,j} for each jj, so the gradient at each of the four factors is the other factor of its product times dctdc_t.Every term is a product of two entries with the same index, and ctc_t is the only place they appear in this cell.
  4. dct=cˉt+dht⊙ot⊙(1−tanh⁡(ct)⊙tanh⁡(ct))dc_t = \bar c_t + dh_t\odot o_t\odot(1 - \tanh(c_t)\odot\tanh(c_t)); ft⊙dctf_t\odot dc_t to ct−1c_{t-1}; dft=ct−1⊙dctdf_t = c_{t-1}\odot dc_t, dit=c~t⊙dctdi_t = \tilde c_t\odot dc_t, dc~t=it⊙dctd\tilde c_t = i_t\odot dc_t, dot=tanh⁡(ct)⊙dhtdo_t = \tanh(c_t)\odot dh_tft⊙dctf_t\odot dc_t is the cˉt−1\bar c_{t-1} of the previous step, so the cell gradients run backwards by the recursion cˉt−1=ft⊙dct\bar c_{t-1} = f_t\odot dc_t. The gate gradients then go through each gate's sigmoid or tanh⁡\tanh into the weights, as in Problem 4, and into dht−1dh_{t-1}.

Problem 10

Follow the cell states alone: holding every gate and candidate fixed, compute ∂cT/∂ct\partial c_T/\partial c_t for t<Tt < T, and compare it with Problem 6.

  1. With the gates fixed, ∂cs/∂cs−1=diag⁡(fs)\partial c_{s}/\partial c_{s-1} = \operatorname{diag}(f_s).cs=fs⊙cs−1+is⊙c~sc_s = f_s\odot c_{s-1} + i_s\odot\tilde c_s is elementwise in cs−1c_{s-1} with coefficient fsf_s, and the second term does not involve cs−1c_{s-1}.
  2. ∂cT∂ct=diag⁡(fT)diag⁡(fT−1)⋯diag⁡(ft+1)\dfrac{\partial c_T}{\partial c_t} = \operatorname{diag}(f_T)\operatorname{diag}(f_{T-1})\cdots\operatorname{diag}(f_{t+1}).The chain rule along ct+1,…,cTc_{t+1}, \dots, c_T, as in Problem 6, step 1.
  3. A product of diagonal matrices is diagonal, with the products of the entries on its diagonal.Diagonal matrices multiply entry by entry.
  4. ∂cT/∂ct=diag⁡(ft+1⊙ft+2⊙⋯⊙fT)\partial c_T/\partial c_t = \operatorname{diag}(f_{t+1}\odot f_{t+2}\odot\cdots\odot f_T)Unlike Problem 6 there is no WW and no tanh⁡′\tanh' in the product, only gates the network sets for itself: where it keeps f≈1f \approx 1, the gradient reaches step tt almost undiminished however large T−tT - t is. This path is why LSTMs learn long-range dependencies that vanilla RNNs cannot; the full gradient also has paths through the gates and hh, which can still vanish.

Where this goes wrong

1. Weight gradient from the last step only

Feed-forward layers each own their weights, and a recurrent layer drawn as a single box looks like one more of them.

  1. δ2=52\delta_2 = \tfrac52 and δ1=54\delta_1 = \tfrac54 in Problem 5Right so far: Problem 5, steps 2 and 3.
  2. “The layer multiplies its input by uu, so ∂L/∂u\partial L/\partial u is its delta times its input.”The analogy that causes the mistake: the dense-layer rule applied to the rolled-up diagram, which hides that uu is used at both steps.
  3. ∂L/∂u=δ2x2=5\partial L/\partial u = \delta_2 x_2 = 5uu is used at every step, and the chain rule adds every use: δ2x2+δ1x1=254\delta_2x_2 + \delta_1x_1 = \tfrac{25}4 (Problem 5). The missing 54\tfrac54 is the influence of x1x_1, so a network trained this way cannot learn to use anything but the latest input.

2. Pairing each delta with the state after the step

In Problem 4 every recurrent term is an outer product of a delta with a hidden state, and it is easy to take the state with the same index.

  1. ∇WL=∑t∇W(t)L\nabla_W L = \sum_t \nabla_{W^{(t)}}LRight so far: Problem 4, step 1.
  2. “Step tt's delta goes with step tt's hidden state.”The shortcut that causes the mistake: matching indices instead of asking what WW multiplies in at=Wht−1+Uxt+ba_t = Wh_{t-1} + Ux_t + b.
  3. ∇WL=∑tδtht⊤\nabla_W L = \sum_t \delta_t h_t^\topThe coefficient of WijW_{ij} in at,ia_{t,i} is ht−1,jh_{t-1,j}, the state coming into step tt, so the term is δtht−1⊤\delta_t h_{t-1}^\top. In Problem 5 the wrong pairing gives 52⋅52+54⋅1=152\tfrac52\cdot\tfrac52 + \tfrac54\cdot1 = \tfrac{15}2 instead of 52\tfrac52. The shape is right, so nothing fails.

3. Recurrent gradient without the tanh derivative

The recursion passes a gradient from step to step, and it is tempting to pass gg itself along.

  1. gt=V⊤et+(∂at+1/∂ht)⊤∇at+1Lg_t = V^\top e_t + (\partial a_{t+1}/\partial h_t)^\top\nabla_{a_{t+1}}LRight so far: Problem 3, step 3, before naming ∇at+1L\nabla_{a_{t+1}}L.
  2. “The gradient coming back from step t+1t + 1 is gt+1g_{t+1}, sent through W⊤W^\top.”The analogy that causes the mistake: the linear RNN of Problem 5, where gt+1g_{t+1} and δt+1\delta_{t+1} are equal.
  3. gt=V⊤et+W⊤gt+1g_t = V^\top e_t + W^\top g_{t+1}WW acts on hth_t to make at+1a_{t+1}, and ht+1=tanh⁡(at+1)h_{t+1} = \tanh(a_{t+1}) sits between at+1a_{t+1} and gt+1g_{t+1}, so the route goes through δt+1=(1−ht+1⊙ht+1)⊙gt+1\delta_{t+1} = (1 - h_{t+1}\odot h_{t+1})\odot g_{t+1}. Dropping the factor removes exactly the saturation that Problem 6 shows makes gradients vanish faster, so the error also hides the problem it would reveal.

4. Sending the gradient back through W instead of Wᵀ

The forward pass multiplies the state by WW at every step, and the backward pass seems to need the same matrix.

  1. gt=V⊤et+(∂at+1/∂ht)⊤δt+1g_t = V^\top e_t + (\partial a_{t+1}/\partial h_t)^\top\delta_{t+1} with ∂at+1/∂ht=W\partial a_{t+1}/\partial h_t = WRight so far: Problem 3, step 3.
  2. “The state goes forward through WW, so its gradient comes back through WW.”The analogy that causes the mistake: the forward map run again, rather than its transpose, which is what carries a gradient from outputs back to inputs.
  3. gt=V⊤et+Wδt+1g_t = V^\top e_t + W\delta_{t+1}WW is square, so the shapes give no warning. (∂L/∂ht)j=∑iδt+1,iWij(\partial L/\partial h_t)_j = \sum_i \delta_{t+1,i}W_{ij} sums over WW's first index, which is the product with W⊤W^\top; the two agree only for a symmetric WW.

5. Cell-state gradient without the path from the next cell

The LSTM's output is hth_t, and it is natural to send the cell state only the gradient that arrives through hth_t.

  1. ht=ot⊙tanh⁡(ct)h_t = o_t\odot\tanh(c_t) and dhtdh_t is givenRight so far: Problem 9.
  2. “ctc_t affects the loss through hth_t, so dctdc_t is dhtdh_t pulled back through ot⊙tanh⁡o_t\odot\tanh.”The analogy that causes the mistake: the vanilla RNN, whose state reaches the next step only through hth_t; the LSTM's cell state also feeds the next cell directly.
  3. dct=dht⊙ot⊙(1−tanh⁡(ct)⊙tanh⁡(ct))dc_t = dh_t\odot o_t\odot(1 - \tanh(c_t)\odot\tanh(c_t))The term cˉt\bar c_t from ct+1=ft+1⊙ct+…c_{t+1} = f_{t+1}\odot c_t + \dots is missing (Problem 9, step 1). That term is the path of Problem 10, the one with no WW and no tanh⁡′\tanh' in it, so dropping it turns the LSTM's long-range gradient back into the vanishing one of Problem 6.

Print this set: backprop-through-time.pdf (problems, answers, and worked solutions on separate pages).