ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Recurrent Neural Networks and Sequence Modeling
Chapter 1: Introduction to Sequential Data
Understanding Sequential Data
Characteristics of Sequence Data
The Need for Sequence Models
Representing Sequences Numerically
Common Sequence Modeling Tasks
Quiz for Chapter 1
Chapter 2: Recurrent Neural Network Fundamentals
The Core Idea: Processing Sequences Iteratively
Simple RNN Architecture
The Role of the Hidden State
Mathematical Formulation of an RNN Cell
Information Flow in RNNs
Backpropagation Through Time (BPTT)
Unrolling the Network for Training
Quiz for Chapter 2
Chapter 3: Building Simple RNNs
Setting Up the Development Environment
RNN Cell Implementation
Using Framework APIs for Simple RNN Layers
Handling Input and Output Shapes
Constructing a Basic RNN Model
Training Loop for RNNs
Hands-on Practical: Simple Sequence Prediction
Quiz for Chapter 3
Chapter 4: Challenges in Training RNNs
The Problem of Vanishing Gradients
The Problem of Exploding Gradients
Impact on Long-Range Dependency Learning
Gradient Clipping Explained
Weight Initialization Strategies
Activation Functions Considerations
Quiz for Chapter 4
Chapter 5: Long Short-Term Memory (LSTM) Networks
Addressing RNN Limitations with Gating
The LSTM Cell Architecture
The Forget Gate
The Input Gate
Updating the Cell State
The Output Gate
Information Flow Through an LSTM Cell
Advantages of LSTMs
Quiz for Chapter 5
Chapter 6: Gated Recurrent Units (GRUs)
Introducing GRUs: A Simpler Gated Architecture
The GRU Cell Architecture
The Update Gate
The Reset Gate
Calculating the Candidate Hidden State
Calculating the Final Hidden State
Comparing GRU and LSTM
Computational Efficiency Considerations
When to Choose GRU or LSTM
Quiz for Chapter 6
Chapter 7: Implementing LSTMs and GRUs
Using LSTM Layers in Deep Learning Frameworks
Using GRU Layers in Deep Learning Frameworks
Configuring LSTM/GRU Layer Parameters
Stacking Recurrent Layers
Understanding Bidirectional RNNs
Implementing Bidirectional Layers
Hands-on Practical: Sentiment Analysis
Quiz for Chapter 7
Chapter 8: Preparing Sequence Data for RNNs
Text Data Preprocessing Overview
Tokenization and Vocabulary Building
Integer Encoding Sequences
Introduction to Embedding Layers
Handling Variable Length Sequences
Padding Sequences
Masking Padded Values
Batching Sequential Data
Preprocessing Time Series Data
Practice: Data Preparation Pipeline
Quiz for Chapter 8
Chapter 9: Sequence Modeling Application Techniques
Sequence Prediction Approaches
Time Series Forecasting Models
Sequence Classification Techniques
Text Classification Models
Sequence Generation Methods
Text Generation Models
Introduction to Encoder-Decoder Architecture
Brief Overview of Attention Mechanisms
Hands-on Practical: Time Series Forecasting
Quiz for Chapter 9
Chapter 10: Evaluating and Tuning Sequence Models
Metrics for Sequence Classification
Metrics for Sequence Prediction
Metrics for Sequence Generation
Visualizing Model Behavior
Hyperparameter Tuning Strategies
Regularization Techniques for RNNs
Troubleshooting Common Training Issues
Practice: Tuning an RNN Model
Quiz for Chapter 10

The Forget Gate

Simple RNNs struggle with long-range dependencies because they lack a mechanism to explicitly control their memory. Information from many steps ago could either vanish or overwhelm the network. Long Short-Term Memory (LSTM) networks introduce specialized components, called gates, to manage the information stored in the network's memory, which is known as the cell state (CCC).

The first gate we'll examine is the forget gate. Its function is straightforward but significant: it decides which information should be discarded or kept from the cell state. Think of the cell state as the LSTM's long-term memory. As new input arrives, the forget gate looks at the previous state and the new input to determine which parts of the existing long-term memory are no longer relevant.

How the Forget Gate Operates

The forget gate works by passing the previous hidden state (ht−1h_{t-1}ht−1​) and the current input (xtx_txt​) through a sigmoid activation function (σ\sigmaσ). The sigmoid function is ideal here because it outputs values between 0 and 1.

Inputs (ht−1h_{t-1}ht−1​ and xtx_txt​) are processed by the forget gate's sigmoid layer to produce the forget vector ftf_tft​.

Mathematically, the calculation is:

ft=σ(Wf⋅[ht−1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)ft​=σ(Wf​⋅[ht−1​,xt​]+bf​)

Let's break this down:

  1. [ht−1,xt][h_{t-1}, x_t][ht−1​,xt​]: This represents the concatenation of the previous hidden state vector ht−1h_{t-1}ht−1​ and the current input vector xtx_txt​. If ht−1h_{t-1}ht−1​ has dimension NhN_hNh​ and xtx_txt​ has dimension NxN_xNx​, the concatenated vector has dimension Nh+NxN_h + N_xNh​+Nx​.
  2. WfW_fWf​: This is the weight matrix associated with the forget gate. It's a learnable parameter adjusted during training. Its dimensions are typically (Nh+Nx)×Nh(N_h + N_x) \times N_h(Nh​+Nx​)×Nh​, where NhN_hNh​ is the dimension of the cell state (and hidden state).
  3. bfb_fbf​: This is the bias vector for the forget gate, also a learnable parameter, with dimension NhN_hNh​.
  4. σ\sigmaσ: The sigmoid function is applied element-wise to the result of the matrix multiplication and addition.

Interpreting the Output (ftf_tft​)

The output ftf_tft​ is a vector with the same dimension as the cell state Ct−1C_{t-1}Ct−1​. Each element in ftf_tft​ is a number between 0 and 1, corresponding to an element in the cell state Ct−1C_{t-1}Ct−1​.

  • A value close to 0 in ftf_tft​ means "forget" or "discard" the corresponding information in Ct−1C_{t-1}Ct−1​.
  • A value close to 1 in ftf_tft​ means "keep" or "remember" the corresponding information in Ct−1C_{t-1}Ct−1​.
  • Values between 0 and 1 represent partial retention.

This vector ftf_tft​ acts like a filter. It will be multiplied element-wise with the previous cell state Ct−1C_{t-1}Ct−1​ to decide how much of the old memory should pass through to the next step. We will see this multiplication happen when we discuss updating the cell state later in this chapter.

The ability to selectively forget irrelevant information based on the current input and the past context (via ht−1h_{t-1}ht−1​) is a core reason why LSTMs can maintain useful information over much longer sequences compared to simple RNNs. It prevents the cell state from becoming cluttered with outdated or unnecessary details.

  • Long Short-Term Memory, Sepp Hochreiter, Jürgen Schmidhuber, 1997 Neural Computation, Vol. 9 (MIT Press) DOI: 10.1162/neco.1997.9.8.1735 - Introduces the Long Short-Term Memory (LSTM) architecture, including the foundational concept of gates for managing memory.
  • Deep Learning, Ian Goodfellow, Yoshua Bengio, and Aaron Courville, 2016 (MIT Press) - Provides a comprehensive explanation of Recurrent Neural Networks and LSTMs, detailing the function and operation of gates.
  • Recurrent Neural Networks and LSTMs (CS224n Lecture Notes), Abigail See, Christopher Manning, 2021 (Stanford University) - Provides detailed academic course material explaining the architecture and operation of LSTMs, including the forget gate.

© 2026 ApX Machine Learning