Practice / Convolutions and recurrences
Backprop through time: RNN and LSTM gradients
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 steps it is a -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 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, where belongs, a missing , where 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 of with a function of , . is the elementwise product.
- The network: inputs and an initial state . For , , and , with applied elementwise, the hidden state and the output. The same , , , , are used at every step.
- The loss is , where each is a scalar loss of step 's output alone; a step with no target has .
- , so the derivative of with respect to , entry by entry, is ( minus each entry of ).
- Three gradients per step: , the gradient of step 's own loss at its output; , the gradient of the whole loss at , including every later step; and .
- is the spectral norm, the largest singular value of . It satisfies , 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
- ·
Give the shapes of , , , , and the number of parameters. Which of does depend on?
- ··
Compute the Jacobians and .
- ··
Derive the backward recursion for and , starting at .
- ···
Compute , , , and in terms of the , , and .
- ··
By hand: a scalar linear RNN (no , no bias, ), with , , , , , and a loss at the last step only, . Compute and with the recursion, then check them by writing as a function of and .
- ···
For , write as a product of the Jacobians of Problem 2, and show that . What does this say about how the loss at step reaches when ?
- ··
Many-to-one: a classifier reads the whole sequence and is scored once. for , and with and one-hot. Give the , and .
- ··
Compute and . When is needed?
- ···
An LSTM keeps a cell state beside :
and
where the forget, input and output gates and the candidate are computed from and . Treat these four as inputs to the cell. You are given , the total gradient of at , and , the gradient that reaches from step through . Compute , the total gradient at , and the gradients to (through this cell), , , and .
- ···
Follow the cell states alone: holding every gate and candidate fixed, compute for , and compare it with Problem 6.
Answers
- , , , , ; parameters, whatever is; depends on and , and on no later input
- and
- ; for ;
- , , , ,
- and
- with , and
- , for ; and
- and
- ; to ; , , ,
Worked solutions
Problem 1
Give the shapes of , , , , and the number of parameters. Which of does depend on?
- for , so is ; for , so is ; .All three terms of are added to give the entries of , so each must be an -vector.
- is and . maps the -vector to the -vector .
- The count is .The sizes of the five arrays. None of them has a time index, because the same arrays are reused at every step.
- and, unrolling, depends on .Each step adds one new input to what the previous state already carried; by induction from .
- , , , , ; parameters, whatever is; depends on and , 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 and .
- and . is affine in each of and , and the Jacobian of is .
- . is elementwise, so its Jacobian is diagonal, with on the diagonal; the forward pass has already computed .
- and The chain rule in numerator layout puts the outer Jacobian on the left: and , the shapes of an -vector differentiated by an -vector and by a -vector.
Problem 3
Derive the backward recursion for and , starting at .
- reaches only through , so .There is no step , and the later losses are the only other way could matter.
- For , reaches through and through , and through nothing else. is used in exactly two places in the forward pass: the output of step and the pre-activation of step .
- .The chain rule adds the two routes of step 2; is the total gradient at , so it already carries every step after .
- . reaches only through , and a diagonal Jacobian acts as an elementwise product (Problem 2, step 2).
- ; for ; The recursion runs backwards from to , one step per forward step, so the backward pass costs about as much as the forward pass; it needs every , which is why training stores the whole sequence of states.
Problem 4
Compute , , , and in terms of the , , and .
- Give step its own copy of , used only in ; then evaluated at . depends on only through its uses, and the chain rule adds the contributions of every use: this is the unrolled network with its shared weights untied.
- appears only in , with coefficient , so , that is .The copy reaches only through , and is the total gradient there; is an input to step , so it is not differentiated here, and its own dependence on is the other copies' contribution.
- In the same way and . multiplies into , and is added to with coefficient .
- and . and appear only in , which reaches only through , so the gradient at is ; the pattern is the outer product of the one-hidden-layer page.
- , , , , Sums of the copies' gradients, steps 1 to 4, each with the shape of its parameter: is . The output weights see only the local ; the recurrent weights see the , which carry the future.
Problem 5
By hand: a scalar linear RNN (no , no bias, ), with , , , , , and a loss at the last step only, . Compute and with the recursion, then check them by writing as a function of and .
- and .The forward pass, which the backward pass needs.
- ., and without a the factor becomes , so .
- .Problem 3, step 3, with no loss at step () and .
- .Problem 4: one term per step, each pairing with the state that came into step .
- .Problem 4 with in place of .
- Directly: , so , and .Substituting into removes the recursion; the two routes agree.
- and The in is the contribution of through two steps; a gradient that stopped at the last step would be .
Problem 6
For , write as a product of the Jacobians of Problem 2, and show that . What does this say about how the loss at step reaches when ?
- . depends on only through the chain , with the inputs fixed; numerator layout puts the latest step on the left.
- Each factor is with , .Problem 2.
- . is diagonal with entries , and puts every entry in .
- .Submultiplicativity applied to the factors, then step 3.
- with , and The loss at step reaches as , so with its size shrinks at least geometrically with the distance : the vanishing gradient, and saturated units ( near ) shrink it further. With 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. for , and with and one-hot. Give the , and .
- for and .Steps before have no loss of their own; the softmax page gives the gradient of cross-entropy at the logits.
- and for .Problem 3 with step 1: only the route through the next step is left.
- and .Problem 3, step 4.
- .Problem 4; every term with is .
- , for ; and The 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 and . When is needed?
- is used only in , so . appears in no other step, and is the total gradient at .
- is used only in , so .There is no output , so the route through a step's own output is absent: Problem 3, step 3, with only the second term.
- and continues the backward pass into whatever produced : an embedding table or a lower RNN layer. is needed when 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 fixed at zero it is discarded.
Problem 9
An LSTM keeps a cell state beside :
and
where the forget, input and output gates and the candidate are computed from and . Treat these four as inputs to the cell. You are given , the total gradient of at , and , the gradient that reaches from step through . Compute , the total gradient at , and the gradients to (through this cell), , , and .
- reaches through and through , so .Two routes, added: is the second by definition, and the first goes through , whose derivative in is .
- . appears only in , entry by entry, with coefficient .
- for each , so the gradient at each of the four factors is the other factor of its product times .Every term is a product of two entries with the same index, and is the only place they appear in this cell.
- ; to ; , , , is the of the previous step, so the cell gradients run backwards by the recursion . The gate gradients then go through each gate's sigmoid or into the weights, as in Problem 4, and into .
Problem 10
Follow the cell states alone: holding every gate and candidate fixed, compute for , and compare it with Problem 6.
- With the gates fixed, . is elementwise in with coefficient , and the second term does not involve .
- .The chain rule along , as in Problem 6, step 1.
- A product of diagonal matrices is diagonal, with the products of the entries on its diagonal.Diagonal matrices multiply entry by entry.
- Unlike Problem 6 there is no and no in the product, only gates the network sets for itself: where it keeps , the gradient reaches step almost undiminished however large is. This path is why LSTMs learn long-range dependencies that vanilla RNNs cannot; the full gradient also has paths through the gates and , 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.
- and in Problem 5Right so far: Problem 5, steps 2 and 3.
- “The layer multiplies its input by , so 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 is used at both steps.
- is used at every step, and the chain rule adds every use: (Problem 5). The missing is the influence of , 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.
- Right so far: Problem 4, step 1.
- “Step 's delta goes with step 's hidden state.”The shortcut that causes the mistake: matching indices instead of asking what multiplies in .
- The coefficient of in is , the state coming into step , so the term is . In Problem 5 the wrong pairing gives instead of . 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 itself along.
- Right so far: Problem 3, step 3, before naming .
- “The gradient coming back from step is , sent through .”The analogy that causes the mistake: the linear RNN of Problem 5, where and are equal.
- acts on to make , and sits between and , so the route goes through . 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 at every step, and the backward pass seems to need the same matrix.
- with Right so far: Problem 3, step 3.
- “The state goes forward through , so its gradient comes back through .”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.
- is square, so the shapes give no warning. sums over 's first index, which is the product with ; the two agree only for a symmetric .
5. Cell-state gradient without the path from the next cell
The LSTM's output is , and it is natural to send the cell state only the gradient that arrives through .
- and is givenRight so far: Problem 9.
- “ affects the loss through , so is pulled back through .”The analogy that causes the mistake: the vanilla RNN, whose state reaches the next step only through ; the LSTM's cell state also feeds the next cell directly.
- The term from is missing (Problem 9, step 1). That term is the path of Problem 10, the one with no and no 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).