# Simple LSTM

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

The forward pass of an LSTM node is defined as follows:

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

we can re-write 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 $t$ onwards:

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

With this in mind, we can re-write our gradient calculation as:

Make sure you understand this last equation. The computation of $\frac{dh_i(t)}{dw}$ follows directly follows from the forward propagation equations presented earlier. We now show how to compute $\frac{dL(t)}{dh_i(t)}$ which is where the so called *backpropagation through time* comes into play.

# Backpropagation through time

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 $ \frac{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 $ \frac{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 $\frac{dL(t)}{dh(t)}$ for all $t=1,\dots,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

We now present the code that performs the backprop pass through a single node at time $1 \leq t \leq T$. The code takes as input:

`top_diff_h`

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

$= \frac{dL(t+1)}{ds(t)}$.

And computes:

`self.state.bottom_diff_s`

$= \frac{dL(t)}{ds(t)}$`self.state.bottom_diff_h`

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

whose values will need to be propagated backwards in time. The code also adds derivatives to:

`self.param.wi_diff`

$= \frac{dL}{dW_{i}}$- …
`self.param.bi_diff`

$= \frac{dL}{db_{i}}$- …

since recall that we must sum the derivatives from each time step:

Also, note that we use:

`dxc`

$= \frac{dL}{dx_c(t)}$

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:

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.

Make sure to check out Mathpix if you are a LaTeX user and Losswise if you need a monitoring solution for training deep learning models.

Happy training!