Prerequisites: Recurrent Neural Networks
To solve the problem of Vanishing and Exploding Gradients in a deep Recurrent Neural Network, many variations were developed. One of the most famous of them is the Long Short Term Memory Network(LSTM). In concept, an LSTM recurrent unit tries to “remember” all the past knowledge that the network is seen so far and to “forget” irrelevant data. This is done by introducing different activation function layers called “gates” for different purposes. Each LSTM recurrent unit also maintains a vector called the Internal Cell State which conceptually describes the information that was chosen to be retained by the previous LSTM recurrent unit. A Long Short Term Memory Network consists of four different gates for different purposes as described below:-
- Forget Gate(f): It determines to what extent to forget the previous data.
- Input Gate(i): It determines the extent of information to be written onto the Internal Cell State.
- Input Modulation Gate(g): It is often considered as a sub-part of the input gate and many literatures on LSTM’s do not even mention it and assume it inside the Input gate. It is used to modulate the information that the Input gate will write onto the Internal State Cell by adding non-linearity to the information and making the information Zero-mean. This is done to reduce the learning time as Zero-mean input has faster convergence. Although this gate’s actions are less important than the others and is often treated as a finesse-providing concept, it is good practice to include this gate into the structure of the LSTM unit.
- Output Gate(o): It determines what output(next Hidden State) to generate from the current Internal Cell State.
The basic work-flow of a Long Short Term Memory Network is similar to the work-flow of a Recurrent Neural Network with only difference being that the Internal Cell State is also passed forward along with the Hidden State.
Working of an LSTM recurrent unit:
- Take input the current input, the previous hidden state and the previous internal cell state.
- Calculate the values of the four different gates by following the below steps:-
- For each gate, calculate the parameterized vectors for the current input and the previous hidden state by element-wise multiplication with the concerned vector with the respective weights for each gate.
- Apply the respective activation function for each gate element-wise on the parameterized vectors. Below given is the list of the gates with the activation function to be applied for the gate.
Input Gate : Sigmoid Function Forget Gate : Sigmoid Function Output Gate : Sigmoid Function Input Modulation Gate : Hyperbolic Tangent Function
The above stated working is illustrated as below:-
Note that the blue circles denote element-wise multiplication. The weight matrix W contains different weights for the current input vector and the previous hidden state for each gate.
Just like Recurrent Neural Networks, an LSTM network also generates an output at each time step and this output is used to train the network using gradient descent.
The only main difference between the Back-Propagation algorithms of Recurrent Neural Networks and Long Short Term Memory Networks is related to the mathematics of the algorithm.
Let be the predicted output at each time step and be the actual output at each time step. Then the error at each time step is given by:-
The total error is thus given by the summation of errors at all time steps.
Similarly, the value can be calculated as the summation of the gradients at each time step.
Using the chain rule and using the fact that is a function of and which indeed is a function of , the following expression arises:-
Thus the total error gradient is given by the following:-
Note that the gradient equation involves a chain of for an LSTM Back-Propagation while the gradient equation involves a chain of for a basic Recurrent Neural Network.
How does LSTM solve the problem of vanishing and exploding gradients?
Recall the expression for .
The value of the gradients is controlled by the chain of derivatives starting from . Expanding this value using the expression for :-
For a basic RNN, the term after a certain time starts to take values either greater than 1 or less than 1 but always in the same range. This is the root cause of the vanishing and exploding gradients problem. In an LSTM, the term does not have a fixed pattern and can take any positive value at any time step. Thus, it is not guarenteed that for an infinite number of time steps, the term will converge to 0 or diverge completely. If the gradient starts converging towards zero, then the weights of the gates can be adjusted accordingly to bring it closer to 1. Since during the training phase, the network adjusts these weights only, it thus learns when to let the gradient converge to zero and when to preserve it.
- Deep Learning | Introduction to Long Short Term Memory
- Text Generation using Recurrent Long Short Term Memory Network
- Recurrent Neural Networks Explanation
- ML | OPTICS Clustering Explanation
- Explanation of BERT Model - NLP
- Mathematical explanation for Linear Regression working
- Explanation of Fundamental Functions involved in A3C algorithm
- ML | Mathematical explanation of RMSE and R-squared error
- Chi-Square Test for Feature Selection - Mathematical Explanation
- Scala short ==(x: Int): Boolean
- Short Circuiting Techniques in Python
- Understanding of LSTM Networks
- Introduction to Residual Networks
- Capsule Neural Networks | ML
- Well Equidistributed Long-period Linear
- Introduction to Artificial Neutral Networks | Set 1
- Activation functions in Neural Networks
- Gated Recurrent Unit Networks
- Artificial Neural Networks and its Applications
- Neural Networks | A beginners guide
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.