前面我们介绍过CNN中普通的BP反向传播算法的推导，但是在RNN（比如LSTM）中，反向传播被称作BPTT（Back Propagation Through Time），它是和时间序列有关的。
A few weeks ago I released some code on Github to help people understand how LSTM’s work at the implementation level. The forward pass is well explained elsewhere and is straightforward to understand, but I derived the backprop equations myself and the backprop code came without any explanation whatsoever. The goal of this post is to explain the so called backpropagation through time in the context of LSTM’s.
If you feel like anything is confusing, please post a comment below or submit an issue on Github.
Note: this post assumes you understand the forward pass of an LSTM network, as this part is relatively simple. Please read this great intro paper if you are not familiar with this, as it contains a very nice intro to LSTM’s. I follow the same notation as this paper so I recommend reading having the tutorial open in a separate browser tab for easy reference while reading this post.
Introduction (Simple LSTM)
The forward pass of an LSTM node is defined as follows:
(注：这里最后一个式子h(t)
的计算，普遍认为s(t)
前面还有一个tanh激活，然后再乘以o(t)
，不过 peephole LSTM paper中建议此处激活函数采用 f(x) = x
，所以这里就没有用tanh
（下同），可以参见Wiki  Long_shortterm_memory上面所说的)
By concatenating the x(t)
and h(t1)
vectors as follows:
we can rewrite parts of the above as follows:
Suppose we have a loss l(t)
that we wish to minimize at every time step t
that depends on the hidden layer h
and the label y
at the current time via a loss function f
:
where f
can be any differentiable loss function, such as the Euclidean loss:
Our ultimate goal in this case is to use gradient descent to minimize the loss L
over an entire sequence of length T
：
Let’s work through the algebra of computing the loss gradient:
where w
is a scalar parameter of the model (for example it may be an entry in the matrix W_gx
). Since the loss l(t) = f(h(t),y(t))
only depends on the values of the hidden layer h(t)
and the label y(t)
, we have by the chain rule:
where h_i(t)
is the scalar corresponding to the i’th
memory cell’s hidden output and M
is the total number of memory cells. Since the network propagates information forwards in time, changing h_i(t)
will have no effect on the loss prior to time t
, which allows us to write:
For notational convenience we introduce the variable L(t)
that represents the cumulative loss from step tonwards:
such that L(1)
is the loss for the entire sequence. This allows us to rewrite the above equation as:
With this in mind, we can rewrite our gradient calculation as:
Make sure you understand this last equation. The computation of dh_i(t) / dw
follows directly follows from the forward propagation equations presented earlier. We now show how to compute dL(t) / dh_i(t)
which is where the so called backpropagation through time comes into play.
Backpropagation through time (BPTT)
This variable L(t)
allows us to express the following recursion:
Hence, given activation h(t)
of an LSTM node at time t
, we have that:
Now, we know where the first term on the right hand side dl(t) / dh(t)
comes from: it’s simply the elementwise derivative of the loss l(t)
with respect to the activations h(t)
at time t
. The second term dL(t+1) / dh(t)
is where the recurrent nature of LSTM’s shows up. It shows that the we need the next node’s derivative information in order to compute the current current node’s derivative information. Since we will ultimately need to compute dL(t) / dh(t)
for all t = 1, 2, ... , T
, we start by computing
and work our way backwards through the network. Hence the term backpropagation through time. With these intuitions in place, we jump into the code.
Code (Talk is cheap, Show me the code)
We now present the code that performs the backprop pass through a single node at time 1 <= t <= T
. The code takes as input:
And computes:
whose values will need to be propagated backwards in time. The code also adds derivatives to:
since recall that we must sum the derivatives from each time step:
Also, note that we use:
where we recall that X_c(t) = [x(t), h(t1)]
. Without any further due, the code:


Details
The forward propagation equations show that modifying s(t)
affects the loss L(t)
by directly changing the values of h(t)
as well as h(t+1)
. However, modifying s(t)
affects L(t+1)
only by modifying h(t+1)
. Therefore, by the chain rule:
Since the forward propagation equations state:
we get that:
Putting all this together we have:


The rest of the equations should be straightforward to derive, please let me know if anything is unclear.
Test LSTM Network
此 代码 其是通过自己实现 lstm 网络来逼近一个序列，y_list = [0.5, 0.2, 0.1, 0.5]，测试结果如下：


可以看出迭代100轮，最后Loss在不断收敛，并且逐渐逼近了预期序列：y_list = [0.5, 0.2, 0.1, 0.5]。
Reference
 深度学习 — 反向传播(BP)理论推导 (zhwhong)
 Nico’s Blog：Simple LSTM
 Github仓库：https://github.com/zhwhong/lstm
 RECURRENT NEURAL NETWORKS TUTORIAL, PART 3 – BACKPROPAGATION THROUGH TIME AND VANISHING GRADIENTS
 [福利] 深入理解 RNNs & LSTM 网络学习资料
 关于简书中如何编辑Latex数学公式
(转载请联系作者并注明出处，谢谢！)