Professional Documents
Culture Documents
Gradient Backpropagation Through A Long Short-Term Memory (LSTM) Cell
Gradient Backpropagation Through A Long Short-Term Memory (LSTM) Cell
Abstract
In this brief document, I am going to derive the equations for gradient backpropagation through an
LSTM cell. Although with the presence of many deep learning softwares there is no longer any need to
compute gradients by hand, this would be a nice exercise on the backpropagation method. Note that this
document is by no means a tutorial on LSTM; instead, it is only a guide on how to do backpropagation
in LSTMs. So, it assumes the reader has a prior knowledge on RNN and LSTM.
Index Terms
I. I NTRODUCTION
Long short-term memory (LSTM) networks have been proven to be much more effective than vanilla
recurrent neural network (RNN) networks in training over long sequences. This is due to the fact that
in contrast to RNNs, in LSTMs, there is a direct flow of the so-called state variables all across the time
axis that prevents gradient vanishing or explosion. However, gradient computations for an LSTM cell are
more involved than vanilla RNNs.
The overall structure of an LSTM cell is shown in Fig. 1. “1 Similar to the vanilla RNN, at each timestep
we receive an input xt ∈ RD and the previous hidden state ht−1 ∈ RH ; the LSTM also maintains an
H -dimensional cell state, so we also receive the previous cell state ct−1 ∈ RH . The learnable parameters
Electrical Engineering Department, Sharif University of Technology, Tehran, Iran (e-mail: [email protected].)
1
http://cs231n.github.io/
2
𝒄𝑡−1 𝒄𝑡
𝒂𝑓 𝒇
𝑴1 𝜎
𝒂𝑖 𝒊
𝑴2 𝜎
𝒂 𝒂𝑔 𝒈
𝒙𝑡 𝑾𝑥 𝑴3 𝑡𝑎𝑛ℎ 𝑡𝑎𝑛ℎ
𝒂𝑜 𝒐
𝒉𝑡−1 𝑾ℎ 𝑴4 𝜎 𝒉𝑡
Fig. 1: An LSTM cell. Square blocks denote matrix-vector multiplication, yellow and brown blocks denote
elementwise operations, and the green blocks denote summation. Note that the matrices M1 , . . . , M4 are
fixed and so, they are not trained. The bias vector has not been shown in this figure, because its gradient
computation is very easy. For the definition of each variable, see the text.
where σ is the sigmoid function and tanh is the hyperbolic tangent, both applied elementwise.
Finally we compute the next cell state ct and next hidden state ht as
ct = f ct−1 + i g ht = o tanh(ct )
To make the computations easier, we define the following matrices: M1 , . . . , M4 , where Mi is the
i-th H × 4H submatrix of the 4H × 4H identity matrix. For instance,
1 0 0 ... 0 0 0 ... 0
0 1 0 . . . 0 0 0 . . . 0
M1 = .. .. .. . . ..
. . 0
. . . 0 . . . 0
0 0 0 ... 1 0 0 ... 0
H×4H
It is then straightforward to verify the following
ai = M1 · a af = M2 · a ao = M3 · a ag = M4 · a
Note that introducing these matrices is mainly for simplicity of the derivations, and it’s not an efficient
way for practical implementations. To derive the gradient expressions, we use graph computations by
breaking the overall computations in an LSTM cell into simple operations. To this end, let’s define the
following additional intermediate variables:
ct = f c + i g ht = o c0t
Now, we are ready for gradient computations! Let’s go ahead! Given the output gradients denoted dht
and dct coming from the next LSTM cell, we want to compute the gradients of all the variables. In other
words, we want to backpropagate the output gradients from the cell outputs all the way to the cell inputs.
To do this, we can write (see also Fig. 2)
• di = dig g (Because ig = i g)
• dg = dig i
• df = dfc ct−1 (Because fc = f ct−1 )
• dai = σ̇(ai ) di = σ(ai )(1−σ(ai )) di = i(1−i) di (Because i = σ(ai ) and σ̇ = σ(1−σ))
• daf = σ̇(af ) df = f (1 − f ) df
4
𝒇𝑐 d𝒄t
𝒄𝑡−1 𝒄𝑡
𝒂𝑓 𝒇
𝑴1 𝜎
𝒂𝑖 𝒊
𝑴2 𝜎
𝒊𝑔
𝒂 𝒂𝑔 𝒈
𝒙𝑡 𝑾𝑥 𝑴3 𝑡𝑎𝑛ℎ 𝑡𝑎𝑛ℎ
𝒄′𝑡
𝒂𝑜 𝒐
𝒉𝑡−1 𝑾ℎ 𝑴4 𝜎 𝒉𝑡
d𝒉𝑡
Fig. 2: The same as Fig. 1 but with illustration of gradient flows directions as well as indicating some
intermediate variables.
• dao = σ̇(ao ) do = o (1 − o) do
• ˙
dag = tanh(a 2
g ) dg = (1 − g ) dg
• da = MT1 · dai + MT2 · daf + MT3 · dao + MT4 · dag (Because the gradient backpropagates through four
paths to reach a; see Fig. 2)
• dxt = WxT · da
• dht−1 = WhT · da
• dWx = da · xTt
• dWh = da · hTt−1
• db = da (Bias gradient)
In the above, the power of 2 is applied elementwise. Note that the effect of M1 , . . . , M4 is to
put dai , . . . , dag in their corresponding positions in a 4H -long vector to create da. That is, they just
concatenate dai , . . . , dag on top of each other.