# [RNN] Simple LSTM代码实现 & BPTT理论推导

|

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.

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:

$\\g(t) &=& \phi(W_{gx} x(t) + W_{gh} h(t-1) + b_{g}) \\\\ i(t) &=& \sigma(W_{ix} x(t) + W_{ih} h(t-1) + b_{i}) \\\\ f(t) &=& \sigma(W_{fx} x(t) + W_{fh} h(t-1) + b_{f}) \\\\ o(t) &=& \sigma(W_{ox} x(t) + W_{oh} h(t-1) + b_{o}) \\\\ s(t) &=& g(t) * i(t) + s(t-1) * f(t) \\\\ h(t) &=& s(t) * o(t) \\$

(：这里最后一个式子h(t)的计算，普遍认为s(t)前面还有一个tanh激活，然后再乘以o(t)，不过 peephole LSTM paper中建议此处激活函数采用 f(x) = x，所以这里就没有用tanh（下同），可以参见Wiki - Long_short-term_memory上面所说的)

By concatenating the x(t) and h(t-1) vectors as follows:

$x_c(t) = [x(t), h(t-1)]$

we can rewrite parts of the above as follows:

$\\g(t) &=& \phi(W_{g} x_c(t) + b_{g}) \\\\ i(t) &=& \sigma(W_{i} x_c(t) + b_{i}) \\\\ f(t) &=& \sigma(W_{f} x_c(t) + b_{f}) \\\\ o(t) &=& \sigma(W_{o} x_c(t) + b_{o})$

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:

$l(t) = f(h(t), y(t))$

where f can be any differentiable loss function, such as the Euclidean loss:

$l(t) = f(h(t), y(t)) = \| h(t) - y(t) \|^2$

Our ultimate goal in this case is to use gradient descent to minimize the loss L over an entire sequence of length T

$L = \sum_{t=1}^{T} l(t)$

Let’s work through the algebra of computing the loss gradient:

$\frac{dL}{dw}$

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:

$\frac{dL}{dw} = \sum_{t = 1}^{T} \sum_{i = 1}^{M} \frac{dL}{dh_i(t)}\frac{dh_i(t)}{dw}$

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:

$\frac{dL}{dh_i(t)} = \sum_{s=1}^T \frac{dl(s)}{dh_i(t)} = \sum_{s=t}^T \frac{dl(s)}{dh_i(t)}$

For notational convenience we introduce the variable L(t) that represents the cumulative loss from step tonwards:

$L(t) = \sum_{s=t}^{s=T} l(s)$

such that L(1) is the loss for the entire sequence. This allows us to rewrite the above equation as:

$\frac{dL}{dh_i(t)} = \sum_{s=t}^T \frac{dl(s)}{dh_i(t)} = \frac{dL(t)}{dh_i(t)}$

With this in mind, we can rewrite our gradient calculation as:

$\frac{dL}{dw} = \sum_{t = 1}^{T} \sum_{i = 1}^{M} \frac{dL(t)}{dh_i(t)}\frac{dh_i(t)}{dw}$

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:

$L(t) = \begin{cases} l(t) + L(t+1) & \text{if} \, t < T \\ l(t) & \text{if} \, t = T \end{cases}$

Hence, given activation h(t) of an LSTM node at time t, we have that:

$\frac{dL(t)}{dh(t)} = \frac{dl(t)}{dh(t)} + \frac{dL(t+1)}{dh(t)}$

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

$\frac{dL(T)}{dh(T)} = \frac{dl(T)}{dh(T)}$

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:

$\frac{dL}{dw} = \sum_{t = 1}^{T} \sum_{i = 1}^{M} \frac{dL(t)}{dh_i(t)}\frac{dh_i(t)}{dw}$

Also, note that we use:

where we recall that X_c(t) = [x(t), h(t-1)]. 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:

$\\\frac{dL(t)}{ds_i(t)} = \frac{dL(t)}{dh_i(t)} \frac{dh_i(t)}{ds_i(t)} + \frac{dL(t)}{dh_i(t+1)} \frac{dh_i(t+1)}{ds_i(t)} \\\\\\= \frac{dL(t)}{dh_i(t)} \frac{dh_i(t)}{ds_i(t)} + \frac{dL(t+1)}{dh_i(t+1)} \frac{dh_i(t+1)}{ds_i(t)} \\\\\\= \frac{dL(t)}{dh_i(t)} \frac{dh_i(t)}{ds_i(t)} + \frac{dL(t+1)}{ds_i(t)} \\\\\\ = \frac{dL(t)}{dh_i(t)} \frac{dh_i(t)}{ds_i(t)} + [\texttt{top\_diff\_s}]_i \\$

Since the forward propagation equations state:

$h(t) = s(t) * o(t)$

we get that:

$\frac{dL(t)}{dh_i(t)} * \frac{dh_i(t)}{ds_i(t)} = o_i(t) * [\texttt{top\_diff\_h}]_i$

Putting all this together we have:

The rest of the equations should be straightforward to derive, please let me know if anything is unclear.

# Reference

(转载请联系作者并注明出处，谢谢！)