Hello! Welcome to our next lesson on advanced neural network architectures.
In the previous lesson, we identified a critical flaw in simple Recurrent Neural Networks: the vanishing and exploding gradient problems. We saw how the repeated multiplication of gradients during backpropagation through time makes simple RNNs unstable and unable to learn long-range dependencies, effectively giving them only "short-term memory."
Today, we'll explore the architecture that was specifically designed to solve this problem: the Long Short-Term Memory (LSTM) network.
Your learning outcome for this lesson is to describe the architecture of an LSTM cell and explain how its gates mitigate vanishing gradients. By the end, you'll understand the internal mechanics of an LSTM, which is one of the most successful and widely used architectures for sequence modeling.
1. The Core Idea: A Separate Path for Memory
The brilliant insight behind LSTMs is to create a dedicated pathway for information to travel through time with minimal interference. This pathway is called the cell state. Think of it as an express conveyor belt or a data highway running through the entire sequence.
Information can flow along this cell state largely unchanged, which is the key to preserving gradients over long distances. The LSTM can then carefully add or remove information from this cell state using specialized neural networks called gates.
To get a high-level overview of this concept, let's watch a short segment from a video by StatQuest.
Long Short-Term Memory (LSTM), Clearly Explained
This video introduces the main idea behind LSTMs: using two separate paths for long-term (the cell state) and short-term (the hidden state) memories.
Watch the clip from 03:10 to 03:54. Focus on the distinction between the long-term memory path and the short-term memory path.
This separation is the foundation of the LSTM's power. Now, let's dive into the mechanics of how the cell state is controlled.
2. The Architecture of an LSTM Cell
An LSTM cell looks more complex than a simple RNN cell because it contains four interacting neural network layers instead of just one. These layers work together to implement the gates and update the cell state.
Let's begin with a fantastic article from Christopher Olah's blog, which provides an exceptionally clear, step-by-step walkthrough.
The article 'Understanding LSTM Networks' is a classic resource for learning about LSTMs. We'll start by looking at the overall structure and the core idea of the cell state and gates.
Please read the sections titled 'LSTM Networks' and 'The Core Idea Behind LSTMs'. Focus on the 'conveyor belt' analogy for the cell state and the description of gates as a way to optionally let information through.
As the article explains, the LSTM cell uses three gates to protect and control the cell state:
- Forget Gate: Decides what information to throw away.
- Input Gate: Decides what new information to store.
- Output Gate: Decides what to output as the hidden state.
Before we examine each gate, it's important to understand the two main activation functions used within them: sigmoid and tanh.
- Sigmoid (): This function squashes any real number into the range
[0, 1]. It's perfect for a gate mechanism: a value of0means "let nothing through," while a value of1means "let everything through." - Hyperbolic Tangent (): This function squashes any real number into the range
[-1, 1]. It's typically used to create new "candidate" values to be added to the cell state.
Now, let's walk through the LSTM cell, one gate at a time. The diagram below shows a detailed view of a single cell, which we will refer to as we break down each component.
Step 1: The Forget Gate
The first step is to decide what information to discard from the previous cell state (). The forget gate takes the previous hidden state () and the current input () and passes them through a sigmoid layer. The output, , is a vector with values between 0 and 1. This vector is then multiplied element-wise with the previous cell state.
Let's read the section from the Colah blog that details the forget gate.
Read the first part of the 'Step-by-Step LSTM Walk Through' section, focusing on the description and diagram of the forget gate.
The equation for the forget gate is:
Here, represents the concatenation of the previous hidden state and the current input.
Step 2: The Input Gate
Next, the cell decides what new information to store. This is a two-part process:
- The input gate layer (a sigmoid function) decides which values to update.
- A
tanhlayer creates a vector of new candidate values, , that could be added to the state.
These two parts are then multiplied to create the update.
Now, let's look at how the input gate and the cell state update work.
Read the next two parts of the 'Step-by-Step LSTM Walk Through', which describe the input gate and the update to the cell state.
The equations are:
Step 3: Updating the Cell State
Now we can update the old cell state into the new cell state . We multiply the old state by the forget gate's output, , and then add the new candidate values scaled by the input gate, .
The symbol denotes element-wise multiplication. This additive interaction is the most critical part of the LSTM's design and the key to solving the vanishing gradient problem.
Step 4: The Output Gate
Finally, we decide what the cell's output will be. This output is the new hidden state, . It's a filtered version of the cell state.
- The output gate (a sigmoid layer) decides which parts of the cell state we’re going to output.
- The new cell state is passed through a
tanhfunction. - The two results are multiplied to produce the new hidden state .
Lastly, let's examine the output gate, which produces the hidden state for the current time step.
Read the final part of the 'Step-by-Step LSTM Walk Through' that describes the output gate.
The equations are:
This new hidden state is passed to the next time step and can also be used to make a prediction at the current time step.

Test your understanding!
A language model is processing the sentence: "The cat on the roof is black. It is sleeping."
When the LSTM processes the word "It", what do you think the forget gate and input gate should ideally do regarding the gender/number information from the word "cat"?
Show answer
When processing "It," the LSTM needs to know that the subject is "cat" (singular).
- Forget Gate: It should not forget the information that the subject is singular. The values in the forget gate's output vector corresponding to this information should be close to
1("keep this"). - Input Gate: Since "It" refers back to "cat", it doesn't introduce fundamentally new subject information. The input gate might decide to add very little new information about the subject, so the values in its output vector could be close to
0("ignore new candidate values for this feature"). The cell state effectively carries the "memory" of "cat" forward.
3. How LSTMs Mitigate Vanishing Gradients
Now we connect the architecture back to the problem. How does this system of gates prevent gradients from vanishing?
The key lies in the additive nature of the cell state update and the dynamic control provided by the forget gate.
Recall the update rule: .
When we perform backpropagation through time, the gradient of the loss with respect to the previous cell state is multiplied by the output of the forget gate, .
Unlike a simple RNN where the gradient is always multiplied by a fixed weight matrix , here the multiplication factor is the forget gate's activation, .
This is crucial: the network can learn to set the forget gate values to be close to 1. If is 1, the gradient from passes back to completely unchanged. By learning to keep the forget gate "open," the LSTM can create an uninterrupted path for gradients to flow back through many time steps, preserving the error signal from distant past events.
Prevent the Vanishing Gradient Problem with LSTM
This article from Baeldung provides a great summary of how the gating mechanisms specifically address the vanishing gradient problem.
Read section 4, 'How Does LSTM Improve the Vanishing Gradient Problem?', including the subsections on the gates and the memory cell state. This will consolidate everything we've discussed and explicitly link it to gradient flow.
In essence, the LSTM's gates learn to regulate the flow of information and, by extension, the flow of gradients. This allows the model to maintain long-term dependencies without the gradient signal dying out.
Conclusion
In this lesson, we opened the "black box" of the LSTM cell and examined its sophisticated internal machinery. You now have a conceptual and structural understanding of one of the most important architectures in modern deep learning.
Key Takeaways:
- LSTM Architecture: An LSTM cell consists of a cell state (long-term memory) and a hidden state (short-term memory).
- Gating Mechanisms: Three gates—Forget, Input, and Output—use sigmoid and tanh activation functions to regulate the flow of information.
- The Forget Gate decides what to remove from the cell state.
- The Input Gate decides what new information to add to the cell state.
- The Output Gate determines the next hidden state based on the cell state.
- Mitigating Vanishing Gradients: The cell state's additive update mechanism and the adaptive nature of the forget gate create a path where gradients can flow back through time without vanishing. The network learns when to "remember" and when to "forget," preserving important long-term dependencies.
Preview of the Next Lesson:
While LSTMs are incredibly powerful, they are also computationally complex. In our next lesson, we will explore a popular and slightly simpler variant called the Gated Recurrent Unit (GRU). Afterward, we will move on to applying an LSTM network to model sequential data in a practical coding example.