Let's understand the problems of recurring neural networks

Contents

This article was published as part of the Data Science Blogathon

Introduction

Red neuronal recurrente (RNN) was one of the best concepts introduced that could make use of memory elements in our red neuronal. Before that, we had a neural network that could propagate back and forth to update weights and reduce errors in the network. But, as we know, many problems in the real world are temporary in nature and highly time dependent.

Many language apps are always sequential and the next word in a sentence depends on the previous one. These problems were solved by a simple RNN. But if we understand RNN, we appreciate the fact that even RNN can't help us when we want to keep track of the words that were previously used in our sentence. In this article, I will discuss some of the main drawbacks of RNN and why we use a better model for most language based applications.

Understanding backpropagation through time (BPTT)

RNN uses a technique called Backpropagation over time to backpropagate through the network to adjust its weights so that we can reduce the error in the network. Got his name “through time”, since in RNN we deal with sequential data and each time we go back it is like going back in time to the past. Here is how BPTT works:

rnn-2055804

Source: (http://www.wildml.com/2015/10/recurrent-neural-networks-tutorial-part-3-backpropagation-through-time-and-vanishing-gradients/)

In step BPTT, we calculate the partial derivative at each weight in the network. Then, if we are in time t = 3, then we consider the derivative of E3 with respect to that of S3. Now, x3 is also connected to s3. Then, its derivative is also considered. Now, if we see that s3 is connected to s2, then s3 depends on the value of s2 and here the derivative of s3 with respect to s2 is also considered. This acts as a chain rule and we accumulate all the dependency with its derivatives and use it to calculate the error.

At E3 we have a gradient which is of S3 and its equation at that moment is:

screen-shot-2017-11-21-at-3-42-29-pm-8810245

Now we also have s2 associated with s3 so,

screen-shot-2017-11-21-at-3-44-15-pm-9346560

And s1 is also associated with s2 and, Thus, now everything s1, s2, s3 and has an effect on E3,

screen-shot-2017-11-21-at-3-45-50-pm-5414493

By accumulating everything, we end up obtaining the following equation that has contributed Ws to that network at time t = 3,

screen-shot-2017-11-21-at-3-49-24-pm-1033097

The general equation for which we fit Ws in our BPTT network can be written as,

screen-shot-2017-11-21-at-4-17-35-pm-9413359

Now, as we have noticed, Wx is also associated with the network. Then, by doing the same, we can usually write,

screen-shot-2017-11-21-at-4-17-19-pm-3001606

Now that you have understood how BPTT works, it's basically about how RNN adjusts its weights and reduces the error. Now, the main flaw here is that this is basically only for a small network with 4 covers. But imagine if we had hundreds of layers and, at once, let's say t = 100, we would end up calculating all the partial derivatives associated with the network and this is a huge multiplication and this can reduce the overall value to a very small value or minute value such that it may be useless to correct the error. This problem is called Gradient problem disappearing.

Gradient problem disappearing

As we all know, in RNN to predict an output we will use a wake function sigmoidea so that we can get the probability output for a particular class. As we saw in the previous section when it comes to saying E3, there is a long-term dependency. The problem occurs when we take the derivative and the derivative of the sigmoid is always below 0.25 Y, Thus, when we multiply many derivatives together according to the chain rule, we end up with a leak value such that we cannot use them for the error calculation. .

16a3a_rt4ymumhusvtvvtxw-7780692

Source: (https://towardsdatascience.com/the-vanishing-gradient-problem-69bf08b15484)

Therefore, weights and biases will not update correctly and, as the layers continue to increase, we fell further into this and our model does not work properly and generates inaccuracies throughout the network.

Some ways to solve this problem are to initialize the weight array correctly or opt for something like a resume instead of sigmoid or tanh functions.

Explosive gradient problem

Gradient explosion is a problem where the value of the gradient becomes very large and this happens often when we initialize larger weights and we could end up with NaN. If our model suffered from this problem, we can't update the weights at all. But fortunately, gradient cropping is a process we can use for this. At a predefined threshold value, we cut the gradient. This will prevent the gradient value from exceeding the threshold and we will never end up with large numbers or NaN.

Long-term dependence on words

Now, let's consider a sentence like, "The clouds are in the ____". Our RNN model can easily predict ‘Sky’ here and this is due to the context of the clouds and very soon it comes as an input to your previous layer. But it may not always be so.

Picture if we had a sentence like: “Jane was born in Kerala. Jane used to play for the women's soccer team and has also topped the state level exams. Jane speaks ____ fluently “.

This is a very long sentence and the problem here is that, as human, I can say that, since Jane was born in Kerala and passed her state exam, it is obvious that you should master the “malayalam” very fluently. But, How does our machine know about this? At the point where the model wants to predict words, you may have forgotten the context of Kerala and more about something else. This is the problem of long-term dependence on RNN.

Unidirectional in RNN

As we have previously commented, RNN takes data sequentially and word by word or letter by letter. Now, when we try to predict a particular word, we are not thinking in its future context. Namely, let's say we have something like: "The mouse is really good. The mouse is used to ____ to facilitate the use of computers “. Now, if we can travel bi-directionally and we can also see the future context, we can say that ‘Displacement’ is the appropriate word here. But, if it is unidirectional, our model has never seen computers, then, How do you know if we are talking about the animal mouse or the computer mouse?

These problems are solved later using language models like BERT, where we can enter complete sentences and use the self-attention mechanism to understand the context of the text.

Use long-term short-term memory (LSTM)

One way to solve the problem of leakage gradient and long-term dependence on RNN is to opt for LSTM networks. LSTM has an introduction to three doors called entry doors, exit and oblivion. In which the doors of oblivion take care of the information that needs to be allowed to pass through the network. Thus, we can have short and long term memory. We can pass the information through the network and retrieve it even at a much later stage to identify the prediction context. The following diagram shows the LSTM network.

1280px-the_lstm_cell-svg_-3503279

(https://en.wikipedia.org/wiki/Long_short-term_memory#/media/File:The_LSTM_Cell.svg)

Follow this tutorial for a better understanding and intuitive example of LSTM: https://towardsdatascience.com/illustrated-guide-to-lstms-and-gru-sa-step-by-step-explanation-44e9eb85bf21

Hopefully, now you have understood the problems of using an RNN and why we have opted for more complex networks like LSTM.

References

1.http: //www.wildml.com/2015/10/recurrent-neural-networks-tutorial-part-3-backpropagation-through-time-and-vanishing-gradients/

2. https://analyticsindiamag.com/what-are-the-challenges-of-training-recurrent-neural-networks/

3. https://towardsdatascience.com/the-vanishing-gradient-problem-69bf08b15484

4. https://en.wikipedia.org/wiki/Long_short-term_memory

5. https://www.udacity.com/course/deep-learning-nanodegree–nd101

6. Preview Image: https://unsplash.com/photos/Sot0f3hQQ4Y

Conclution

Feel free to connect with me at:

1. https://www.linkedin.com/in/siddharth-m-426a9614a/

2. https://github.com/Siddharth1698

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker