Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our previous lesson, we unraveled the architecture of a simple Recurrent Neural Network and discovered its critical flaw: the vanishing and exploding gradient problems. We established that the recursive nature of the hidden state, , leads to a long product of Jacobian matrices during backpropagation. This multiplicative structure causes the gradient signal to either decay exponentially into oblivion or explode into instability, making it nearly impossible for the network to learn dependencies between distant timesteps.
Today, we will explore the ingenious architectural solutions that broke through this barrier: Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU) networks. These models introduce the concept of gates—specialized neural network components that regulate the flow of information. We will dissect how these gates create an alternative, primarily additive pathway for information, allowing gradients to travel across long time horizons without vanishing. This lesson will show you precisely how these mechanisms overcome the limitations of simple RNNs.
Part 1: The Core Idea – A Regulated Information Highway
Before diving into the specific equations, let's build an intuition for the solution. The problem with a simple RNN is that its memory, the hidden state , is completely recomputed at every timestep. It's like trying to remember a long story by constantly overwriting your notes with a new summary that includes the latest sentence. Important details from the beginning are likely to get washed out.
LSTMs and GRUs solve this by creating a more persistent "information highway." Imagine a conveyor belt running parallel to the main sequence processing line. This belt can carry information from much earlier timesteps directly to later ones. The innovation lies in creating controllable gates that decide:
- What old information to remove from the belt.
- What new information to place on the belt.
- What information from the belt to use for the current prediction.
To see how this maps to a real-world task, let's start with a brief video that uses a thought experiment to explain this selective memory process.
Illustrated Guide to LSTM's and GRU's: A step by step explanation
This video, from The AI Hacker channel, provides a great intuitive entry point into why selective memory is crucial for processing sequences.
Please watch the section from this thought experiment. Notice how your brain naturally filters for important keywords ("amazing," "perfectly balanced") while discarding filler words. LSTMs and GRUs are designed to learn a similar filtering behavior.
Part 2: Deconstructing the Long Short-Term Memory (LSTM) Unit
The first and most influential gated architecture is the LSTM, proposed by Hochreiter and Schmidhuber in 1997. An LSTM unit enhances a simple RNN by introducing a cell state () in addition to the hidden state ().
- Cell State (): This is the information highway or "long-term memory." It's a vector that can carry information across many timesteps with minimal distortion.
- Hidden State (): This acts as the "short-term memory" and is the output of the LSTM unit at the current timestep, used for making predictions.
The flow of information into, out of, and within the cell state is controlled by three gates. Each gate is a small neural network (an affine transformation followed by a sigmoid activation function) that outputs a vector of values between 0 and 1. These values act as switches: a value of 0 means "let nothing through," while a value of 1 means "let everything through."
Let's watch a video that introduces the components and then break them down one by one.
Illustrated Guide to LSTM's and GRU's: A step by step explanation
This video provides a superb overview of the LSTM cell's structure.
Watch the segment from this core concepts section. It will introduce the cell state and the three main gates, highlighting the role of the sigmoid activation in controlling information flow.
As the video explained, the three gates are the Forget Gate, the Input Gate, and the Output Gate.
1. The Forget Gate ()
The first step is to decide what information to throw away from the previous cell state, . The forget gate looks at the previous hidden state and the current input and outputs a number between 0 and 1 for each element in the cell state vector.
A '1' means "completely keep this part of the old memory," while a '0' means "completely forget this part."
2. The Input Gate () and Candidate Memory ()
Next, the LSTM decides what new information to store in the cell state. This has two parts:
- The input gate () decides which values we'll update.
- A layer creates a vector of new candidate values, , that could be added to the state.
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 (), forgetting the things we decided to forget. Then, we add the new information, scaled by the input gate ().
This equation is the heart of the LSTM. Notice the additive interaction. We are not nesting functions as in a simple RNN (). Instead, we are adding new information to a modified version of the old state.
4. The Output Gate ()
Finally, we need to decide what our output, the hidden state , is going to be. The output will be a filtered version of our cell state.
- First, the output gate () decides what parts of the cell state we're going to output.
- Then, we put the cell state through a function (to squash the values to be between -1 and 1) and multiply it by the output of the output gate.
To see these gates in action with concrete numbers, the StatQuest channel provides an excellent walkthrough.
Long Short-Term Memory (LSTM), Clearly Explained
This StatQuest video explains each gate with clear, step-by-step numerical examples, which will solidify your understanding of how each component contributes to the final output.
Watch the explanation of the Forget Gate. Continue with the Input Gate. Finish with the Output Gate.
The article below provides a handy table summarizing all the LSTM equations.
The Complete Guide to Recurrent Neural Networks: From Sequence ...
This article provides a concise summary of the LSTM equations in a table format.
Focus on the table in the section titled "LSTM: The Revolution of Gated Memory". Compare these equations to the descriptions above to ensure you understand the role of each component.
Part 3: How LSTMs Solve the Vanishing Gradient Problem
Now for the crucial question. How does this architecture prevent the gradient from vanishing?
Socratic Question: Consider the cell state update equation: . If we want to backpropagate the gradient of the loss with respect to the cell state at time , , back to the cell state at time , we need to compute the Jacobian . What is the simplest, most direct path for this gradient? How does its mathematical form differ from the Jacobian in a simple RNN?
Answer
The most direct path for the gradient is through the first term of the sum. The derivative of $c_t$ with respect to $c_{t-1}$ is simply the forget gate's activation, $f_t$ (ignoring more complex paths through $h_{t-1}$ for this intuition). $$ \frac{\partial c_t}{\partial c_{t-1}} = f_t $$ In a simple RNN, the Jacobian was $\frac{\partial h_t}{\partial h_{t-1}} = \text{diag}(\tanh'(\cdot)) W_{hh}$. This involved multiplication by the fixed weight matrix $W_{hh}$ at every step. In an LSTM, the gradient flowing back through the cell state is multiplied by the *output of the forget gate*, $f_t$. This is not a fixed matrix but a dynamic value that the network learns to control.The gradient highway is the additive nature of the cell state update. When backpropagating, the gradient can flow from to directly through the forget gate. The total gradient for a cell state far in the past becomes a product of forget gate activations:
Because the network controls , it can learn to set the forget gates to 1 for timesteps where it needs to preserve memory. This creates an uninterrupted channel where the gradient can flow back through time without being repeatedly squashed by a weight matrix and an activation function's derivative. The LSTM architecture doesn't guarantee that gradients won't vanish, but it makes it much easier for the model to learn to preserve long-range dependencies.
The following resource from a Stanford lecture explains this very clearly.
with Deep Learning CS224N/Ling284
These lecture slides from Stanford's CS224N course provide a concise and accurate explanation of how the LSTM architecture addresses the vanishing gradient problem.
Read the slide titled "How does LSTM solve vanishing gradients?". It directly addresses the role of the forget and input gates in preserving information over many timesteps.
Part 4: Gated Recurrent Unit (GRU) - A Simpler Alternative
The GRU, introduced by Cho et al. in 2014, is a streamlined version of the LSTM that often achieves comparable performance with a simpler structure and fewer parameters. This makes it computationally more efficient, a trait that will likely appeal to your interest in performance systems.
The GRU makes two key changes:
- Combined State: It merges the cell state and hidden state into a single state vector, .
- Fewer Gates: It combines the forget and input gates into a single update gate () and has one other gate, the reset gate ().

Let's break down the GRU's operations.
-
Update Gate (): This gate decides how much of the previous memory, , to keep. It's analogous to a mix of LSTM's forget and input gates.
-
Reset Gate (): This gate determines how to combine the new input with the previous memory . It decides how much of the past information to "forget" when creating the new candidate memory.
-
Candidate Hidden State (): A new memory content is proposed, using the reset gate to selectively ignore parts of the previous state.
-
Final Hidden State (): The update gate performs a linear interpolation between the old state and the new candidate state .
If the update gate is close to 0, the new state is almost identical to the old state , allowing information and gradients to flow through easily.
The following video and article provide a concise explanation and the core formulas for the GRU.
Illustrated Guide to LSTM's and GRU's: A step by step explanation
This segment of the "Illustrated Guide" video introduces the GRU, highlighting its key differences from the LSTM.
Watch from the GRU section. It explains the purpose of the update and reset gates.
The Complete Guide to Recurrent Neural Networks: From Sequence ...
This article provides the mathematical formulas for the GRU and summarizes the practical trade-offs between GRU and LSTM.
Read the sections titled "4. GRU: A More Streamlined Gating Design" for the core formulas, and continue to the subsequent paragraph that begins "Chung et al.'s empirical study" for a summary of their performance comparison.
In practice, there is no definitive winner between LSTM and GRU. LSTM's separate cell state may give it an edge in modeling particularly long and complex dependencies, but GRU's efficiency makes it a very strong contender, especially when computational resources are a concern.
Conclusion
This lesson moved from the problem of vanishing gradients in simple RNNs to the architectural solution provided by gating mechanisms. By introducing gates, LSTMs and GRUs gain dynamic, learned control over what information to remember and what to forget.
Key Takeaways:
- Gating Principle: LSTMs and GRUs use gates (small neural networks with sigmoid activations) to regulate the flow of information through the network.
- LSTM Architecture: Employs a separate cell state for long-term memory and controls it with a forget gate, an input gate, and an output gate.
- GRU Architecture: A simpler model that merges the cell and hidden states and uses just two gates: an update gate and a reset gate.
- The Gradient Highway: The crucial innovation in both architectures is the additive update mechanism (e.g., ). This creates a direct path for gradients to flow backward through time, conditioned by gate activations rather than being repeatedly multiplied by a fixed weight matrix. This allows the network to learn and preserve long-range dependencies.
- Practical Trade-offs: LSTMs are more expressive but computationally heavier. GRUs are faster and require fewer parameters, often performing on par with LSTMs.
Preview of the Next Lesson:
While LSTMs and GRUs were a monumental leap forward, they still process information sequentially, one step at a time. This can be a bottleneck for very long sequences and prevents parallelization during training. What if a model could look at all parts of a sequence simultaneously and decide which words are most important to each other, regardless of their distance? In our next lesson, we will introduce attention mechanisms, the foundation of the Transformer architecture, which does exactly that. We'll start by defining scaled dot-product attention as a powerful query-key-value lookup system.