# Backpropagation through time | Backpropagation in RNN

In this article, we will find out why we cannot train Recurrent Neural networks with the regular backpropagation and use its modified version known as the backpropagation through time.

**What is backpropagation?**

First, let us briefly go over backpropagation, Backpropagation is a training algorithm that is used for training neural networks. When training a neural network, we are actually tuning the weights of the network to minimize the error with respect to the already available true values(labels) by using the Backpropagation algorithm. It is a supervised learning algorithm as we find errors with respect to already given labels. The general algorithm is as follows:

- Present a training input pattern and propagate it through the network to get an output.
- Compare the predicted outputs to the expected outputs and calculate the error.
- Calculate the derivatives of the error with respect to the network weights.
- Use these calculated derivatives to adjust the weights to minimize the error.
- Repeat.

**Unrolling the RNN(recurrent neural network)**

Here I am only going to briefly discuss RNN, enough to understand how the backpropagation algorithm is applied to recurrent neural network or RNN. You can click here to get a detailed article about RNN(recurrent neural network). So as you might know RNN is used to work with sequential data and in sequential data, there is a lot of information present in the data as well as in the sequence of the data. So in RNN, we have these loops that allow information to persist through time.

Take a look at the architecture of RNN below:

Let us understand what happens by unfolding the above figure:

.

So now that we unfolded the RNN with loop, we got the above figure. At time =t_{0 }, we input x_{0 }to our network and we get an output y_{0} , at time =t_{1 }, we input x_{1 }to our network and we get an output y_{1, }Now as you can see the figure, to calculate output the network uses input x and the cell state from the previous timestamp.To calculate certain Hidden state here is the formula

We usually use tanh activation, but you can use anyone more suitable. And to calculate the output at each step, here is the formula

Now to calculate the error, we just take the output and calculate its error with respect to the label(real output), but here we have multiple outputs, one at each time stamp and thus the regular backpropagation is not suitable here. Thus we modify this algorithm and call the new algorithm as backpropagation through time.

Note: It is important to remember that the value of W_{hh} ,W_{xh } and W_{hy }does not change across the timestamps, which means that for all inputs in a sequence, the values of these weights is same.

**Backpropagation through time**

Now the questions that should come to your mind are

- What is the total loss for this network?
- How do we update the weights W
_{hh},W_{xh }and W_{hy}?

The total loss is simply the sum of the losses overall timestamps.For example,in the figure below,E_{n} is the loss at each time stamp and instead of h to denote cell state, I am using s here.

Then total loss = E_{0}+ E_{1}+ E_{2}+ E_{4}

In general, we can say the total error is equal to summation over all the errors across timestamps t,

Where E_{t}(y_{t,}y_{t}) is the error made at timestamp t.

Now we have to calculate the gradient of this loss with respect to W_{hh} ,W_{xh } and W_{hy}.For simplicity I am going to denote W_{hh} as W,W_{xh } as U and W_{hy }.as V.

It is quite easy to calculate the derivative of loss with respect to V as it only depends on the values at the current time step. But when it comes to calculating the derivative of loss with respect to W and U, it becomes a bit tricky.

As you can see the first part of this equation,i.e derivative of E w.r.t s_{4} can be easily calculated as it only depends on V.But to calculate the derivative of s_{4} with respect to W is not so easy.

The value of s_{4}depends on s_{3} which itself is a function W, therefore we cannot calculate the derivative of s4 keeping s3 as constant. In such networks, the derivative has two parts, implicit and explicit. In explicit part, we assume all other inputs as constant whereas in implicit part we sum over all the indirect paths. Therefore we calculate the derivative as :

Now we can easily put this value in the previous formula to calculate the derivative of E with respect to W. In a similar manner, you can also calculate the derivative of E with respect to U.Now that we have all the three derivatives, we can easily update our weights. This algorithm is called backpropagation through time or BPTT for short as we used values across all the timestamps to calculate the gradients.

It is very difficult to understand these derivations in text, here is a good explanation of this derivation

**Limitations of ** **backpropagation through time :**

When using BPTT(backpropagation through time) in RNN, we generally encounter problems such as exploding gradient and vanishing gradient.

To avoid exploding gradient, we simply use a method called gradient clipping where at each timestamp, we can check if the gradient > threshold and if it is, we normalize it. This helps to tackle exploding gradient. To tackle the vanishing gradient problem these are the possible solutions:

- Use ReLU instead of tanh or sigmoid activation function.
- Proper initialization of the W matrix can reduce the effect of vanishing gradients. It has been seen that initializing with an identity matrix helps in tackling this problem.
- Using gated cells such as LSTM or GRUs

This brings us to the end of this article where we learned how we update weights in RNN by using Backpropagation through time. To know more about Machine learning and get a free course, click the banner below:

## Further reading

Source : https://www.mygreatlearning.com/blog/artificial-intelligence/