Long Short-Term Memory (LSTM) is an improved version of the Recurrent Neural Network (RNN) designed to capture long-term dependencies in sequential data. It uses a memory cell to store information over time, solving the limitations of traditional RNNs.
Handles Long Term Dependencies: Remembers information for longer sequences
Memory Cell: Stores and updates important information over time
Better than RNN: Overcomes short term memory limitations
Applications: Used in language translation, speech recognition and time series forecasting
Problem with Long-Term Dependencies in RNN
RNNs are designed to handle sequential data by using a hidden state that stores information from previous steps. However, they struggle to learn long-term dependencies. This happens due to:
Vanishing Gradient: When training a model over time, the gradients which help the model learn can shrink as they pass through many steps. This makes it hard for the model to learn long-term patterns since earlier information becomes almost irrelevant.
Exploding Gradient: Sometimes gradients can grow too large causing instability. This makes it difficult for the model to learn properly as the updates to the model become erratic and unpredictable.
LSTM Architecture
LSTM (Long Short-Term Memory) architecture is designed to learn long-term dependencies in sequential data using memory cells and gates that control the flow of information through the network.
Main Gates in LSTM
Input Gate: Decides which new information should be added to the memory cell
Forget Gate: Determines which information should be removed from the memory cell
Output Gate: Controls which information from the memory cell is passed to the next hidden state and output
Working of LSTM
LSTM consists of a repeating chain like structure with memory cells and gating mechanisms
Information is retained by the cells and the memory manipulations are done by thegates. There are three gates:
1. Forget Gate
The forget gate decides which information should be kept or removed from the cell state. It uses the current input and previous hidden state then applies a sigmoid function to generate values between 0 and 1.
Values close to 0 remove information
Values close to 1 retain information
Helps discard unnecessary past information
Controls memory retention in the LSTM
The equation for the forget gate is:
Where
represents the weight matrix associated with the forget gate.
denotes the concatenation of the current input and the previous hidden state.
The addition of useful information to the cell state is done by the input gate.
First the information is regulated using the sigmoid function and filter the values to be remembered similar to the forget gate using inputs and .
Then, a vector is created using tanh function that gives an output from -1 to +1 which contains all the possible values from and .
At last the values of the vector and the regulated values are multiplied to obtain the useful information.
The equation for the input gate is:
We multiply the previous state by effectively filtering out the information we had decided to ignore earlier. Then we add which represents the new candidate values scaled by how much we decided to update each state value.
The output gate determines which information from the current cell state should be passed as the hidden state (output) at the current time step. It uses the previous hidden state and the current input , followed by a sigmoid function to control the output flow.
Next, the current cell state is passed through a tanh activation to scale its values between and . Finally, this transformed cell state is multiplied element-wise with to produce the hidden state :
Here:
is the output gate activation.
is the current cell state.
represents element-wise multiplication.
is the sigmoid activation function.
This hidden state is then passed to the next time step and can also be used for generating the output of the network.