Hello! Welcome to your fifth lesson on the path to becoming a top-tier AI engineer.
In our last session, we diagnosed a critical illness in simple Recurrent Neural Networks: the vanishing and exploding gradient problems. We saw that the repeated multiplication of matrices during backpropagation through time causes the error signal to either shrink to nothing or blow up to infinity, making it impossible for simple RNNs to learn long-range dependencies.
Today, we'll study the cure. We will explore how two advanced recurrent architectures, Long Short-Term Memory (LSTM) and Gated Recurrent Units (GRU), were specifically designed to overcome these gradient flow issues. You will learn about the ingenious internal mechanisms—gates and an explicit memory cell—that allow these models to selectively remember and forget information, thereby maintaining a healthy flow of gradients over hundreds of timesteps.
The Core Idea: An Uninterrupted Information Superhighway
The fundamental flaw in a simple RNN is that the hidden state, which serves as the network's only memory, is completely recomputed and passed through a non-linear activation function at every single timestep. This repeated transformation is what corrupts the gradient signal.
LSTMs introduce a radical solution: a separate, dedicated information channel called the cell state, often denoted as . Think of it as an information superhighway or a conveyor belt that runs parallel to the main RNN processing path.

Information on this cell state conveyor belt can flow along largely undisturbed, making it the default behavior to remember things long-term. The key is that modifications to the cell state are primarily additive, not multiplicative transformations. In the backpropagation pass, this additive structure avoids the repeated matrix multiplication that causes gradients to vanish or explode.
But a memory that can't be updated is useless. The true power of the LSTM comes from its ability to carefully regulate this information flow using a set of mechanisms called gates.
Deconstructing the LSTM: The Gating Mechanism
Gates are neural networks in their own right, which learn to open and close access to the cell state. They use a sigmoid activation function, which squashes its output to be between 0 and 1. This output can be interpreted as a percentage: 0 means "let nothing through," and 1 means "let everything through."
An LSTM cell has three main gates to manage its cell state:
- Forget Gate (): Decides what information to throw away from the previous cell state, .
- Input Gate (): Decides what new information to store in the current cell state, .
- Output Gate (): Decides what information from the current cell state, , to use for the hidden state , which is the output of the cell for the current timestep.
To get a clear, step-by-step walkthrough of how these gates work together, the following blog post by Chris Olah is considered the canonical explanation.
The article 'Understanding LSTM Networks' provides an exceptional, visual, step-by-step explanation of the LSTM cell. It breaks down the function of each gate and how they interact to update the cell state and produce an output.
Please read the sections 'The Core Idea Behind LSTMs' and 'Step-by-Step LSTM Walk Through'. Focus on the analogy of the conveyor belt for the cell state and the function of the forget, input, and output gates.
For a more dynamic and numerically-grounded explanation, the following video walks through an LSTM cell with concrete values, which can build a strong intuition for how the gates operate in practice.
Long Short-Term Memory (LSTM), Clearly Explained
The video 'Long Short-Term Memory (LSTM), Clearly Explained' by StatQuest provides an excellent, animated walkthrough of an LSTM cell, complete with numerical examples that show how each gate makes its decisions.
Watch from 6:03 to 14:25. This covers the introduction of the cell state and hidden state, and then provides a detailed, numerical example of the Forget Gate, Input Gate, and Output Gate in action.
The Mathematics of Gradient Preservation
Now we can connect this architecture back to the vanishing gradient problem. The LSTM's cell state update equation is:
where is element-wise multiplication.
Notice the key operation is addition. When we calculate the gradient of the loss with respect to the previous cell state , the chain rule gives us:
The derivative path includes the forget gate acting directly on . Because the forget gate's activation is a learned function of the input, the network can dynamically set to be close to 1 for information it needs to preserve. This creates a pathway where the gradient can flow backwards through time without being forced to decay, as it's not being repeatedly multiplied by a fixed weight matrix whose eigenvalues are less than 1.
The network learns to control the gradient flow. This is the crucial leap from a simple RNN. For a deeper dive into the mathematics of the backward pass, the following resource is excellent.
Why LSTMs Stop Your Gradients From Vanishing
The blog post 'Why LSTMs Stop Your Gradients From Vanishing' provides a more rigorous mathematical explanation of how the LSTM's additive update rule and gating mechanism protect against vanishing gradients during backpropagation.
Read the sections 'Preventing Vanishing Gradients with LSTMs' and 'Looking at the full LSTM gradient'. This will provide the mathematical justification for why the gating mechanism is so effective, which is key for your goal of deep mastery.
Test your understanding!
Imagine an LSTM is processing a text for sentiment analysis. It sees the sentence: "The movie, despite its marketing, was not good."
- When the LSTM processes the word "not", which gate would be most crucial for ensuring the negative sentiment is captured and carried forward?
- How does the forget gate help the model correctly interpret the final sentiment, despite the positive-sounding word "marketing" appearing earlier?
Show answer
- The input gate would be crucial. When "not" appears, the input gate should learn to open up (values close to 1) to allow the new "negative" information to be written to the cell state, effectively flipping or modifying the sentiment context.
- The forget gate helps by learning to discard irrelevant information. It might learn that phrases like "despite its..." signal that the upcoming information is more important than the preceding information. It could therefore "forget" the positive connotation of "marketing" so that the cell state is primarily influenced by "not good."
Gated Recurrent Units (GRUs): A Simpler Cousin
The LSTM architecture is powerful but complex, with many parameters. In 2014, researchers introduced the Gated Recurrent Unit (GRU) as a simpler alternative that often achieves comparable performance.
The GRU makes two key simplifications:
- It combines the cell state and hidden state into a single state vector, .
- It reduces the number of gates from three to two:
- Update Gate (): This single gate controls both forgetting and updating, analogous to a combination of the LSTM's forget and input gates. It decides how much of the past state to keep versus how much of the new candidate state to incorporate.
- Reset Gate (): This gate determines how much of the past state to forget when computing the new candidate state.
GRUs are less computationally expensive than LSTMs and can train faster. While LSTMs are sometimes better for problems requiring very long-term memory, the choice between LSTM and GRU often comes down to empirical testing for a specific task.

Conclusion
You've now seen the first major architectural innovation that made deep learning for sequences practical. By moving away from a simple, repeated transformation and toward a managed memory system, LSTMs and GRUs unlocked the ability to model complex, long-range dependencies.
Key Takeaways:
- LSTMs mitigate vanishing gradients by introducing a cell state, an "information superhighway" that allows gradients to flow backwards with minimal degradation.
- Gates (Forget, Input, Output) are learnable neural networks that regulate the flow of information into, out of, and within the cell state, giving the model dynamic control over its memory.
- The additive nature of the cell state update is the mathematical key to preventing the exponential decay of gradients.
- GRUs are a simpler, computationally cheaper alternative to LSTMs that merge the cell and hidden states and use only two gates (Update and Reset).
What's Next?
LSTMs and GRUs were a monumental step forward, dominating NLP for several years. However, they still have one fundamental limitation inherited from their RNN ancestor: they must process information sequentially, one token at a time. This makes it impossible to parallelize processing within a single sequence, creating a computational bottleneck.
In the next lesson, we will introduce the concept that broke this sequential barrier: the attention mechanism. You will see how attention allows a model to look at all parts of an input sequence simultaneously and decide which parts are most important at each step, paving the way for the Transformer architecture.