Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 88

RNN Architecture and Sequential Data

Recommended resources

Introduction

In our last lesson, you moved from theory to practice by building and training a Convolutional Neural Network (CNN) for image classification. You saw how CNNs are architecturally specialized for spatial data, like the grid of pixels in an image, using convolutional and pooling layers to extract a hierarchy of features.

Today, we shift our focus to a different, but equally important, type of data: sequential data. This includes text, speech, financial time series, and even DNA. For these tasks, the order of information is critical. A standard feedforward network or a CNN would struggle to understand the context embedded in a sequence.

This lesson addresses the learning outcome: Explain the architecture of Recurrent Neural Networks (RNNs) and their application to sequential data. We will explore how RNNs use an internal "memory" to process information step-by-step, making them uniquely suited for understanding context and dependencies over time. This concept of retaining and building upon past information is fundamental to creating the kind of sophisticated, context-aware AI you are aiming to master.


1. Why Do We Need a New Architecture?

Before diving into the architecture of an RNN, let's understand why our existing tools fall short when dealing with sequences.

A standard neural network takes a fixed-size input and produces a fixed-size output. A CNN does the same, although it's specialized for image grids. But what if your input has a variable length, like a sentence?

  • "I am good." (3 words)
  • "I am not at all bad or sad right now." (9 words)

A fixed-size network can't handle this variability. More importantly, it doesn't have a mechanism to understand that the meaning of "sad" might be negated by "not" appearing several words earlier. It treats every input element independently.

RNNs were designed to solve exactly this problem. To see this in action, watch the beginning of the following video.

Recurrent Neural Networks (RNNs), Clearly Explained!!!

This video from StatQuest with Josh Starmer clearly illustrates the limitations of standard neural networks when faced with sequential data of varying lengths, using stock price prediction as an example. It then introduces the RNN as the solution.

Watch from 01:55 to 04:43. Focus on the core problem being presented: how to create a network that is flexible enough to handle different amounts of sequential data.

As you saw, the key innovation of an RNN is its feedback loop, which allows it to persist information across steps. Let's formalize this idea.


2. The Core Architecture of an RNN

At the heart of an RNN is a simple but powerful idea: a loop. The network processes a sequence one element at a time, and at each step, the output from the previous step is fed back as an input to the current step. This feedback loop creates a form of memory.

The "memory" is stored in a vector called the hidden state, usually denoted by . At each time step , the network calculates a new hidden state based on two things:

  1. The current input element .
  2. The previous hidden state .

This recurrence can be visualized in two ways: as a "folded" model with a loop, and as an "unrolled" model stretched out over time. The unrolled view makes it clear how information flows through the sequence.

Recurrent Neural Network (RNN) Architecture
This diagram shows the core concept of an RNN. On the left is the compact, 'folded' representation with a loop, indicating that the hidden state is passed from one timestep to the next. On the right is the 'unrolled' representation, which visualizes the network as a chain of identical cells, one for each element in the sequence. This makes it clear how the hidden state `h` acts as a carrier of information through time.

Crucially, the same set of weights is used at every single time step. This parameter sharing is what allows the RNN to handle sequences of variable length and generalize what it learns at one position to all other positions.

The following video from IBM Technology provides a concise explanation of the hidden state and the unrolling process.

The Power of Recurrent Neural Networks (RNN)

This clip explains the role of the hidden state and visually demonstrates how an RNN is unrolled over time to process a sequence.

Watch from 00:46 to 02:05. Pay attention to how the hidden state ht is described as carrying what the network has learned from previous inputs.

The Mathematics of a Simple RNN

The core computations within a single RNN cell at each time step are defined by two equations.

  1. The Hidden State Update:

    • is the new hidden state.
    • is the previous hidden state.
    • is the current input vector.
    • and are weight matrices for the recurrent and input connections, respectively.
    • is a bias vector.
    • tanh is a common activation function used to keep the hidden state values within a certain range (-1 to 1).
  2. The Output Calculation:

    • is the output at this time step.
    • is the weight matrix for the hidden-to-output connection.
    • is the output bias vector.

To see these equations within a full network diagram, let's turn to a classic textbook.

Deep Learning – RNN Chapter

The "Deep Learning" book by Goodfellow, Bengio, and Courville offers a more formal look at the RNN architecture. This section provides the forward propagation equations and a detailed diagram.

Read the first part of section 10.2, titled "Recurrent Neural Networks." Focus on the text describing Figure 10.3 and the forward propagation equations (10.8 through 10.11). Note that their notation is slightly different but conceptually identical: U corresponds to our W_xh, W to W_hh, and V to W_hy.

Let's put this all together with a concrete example.

A Simple RNN Language Model Architecture
This diagram shows an RNN being used for language modeling. It takes a sequence of words (e.g., \"the students opened their\") and tries to predict the next word. Each word is an input vector `x_t`. The network updates its hidden state `h_t` at each step, summarizing the sentence so far. The final hidden state is used to produce an output `y`—a probability distribution over the vocabulary for the next word.

3. RNN Architectures and Applications

The basic RNN cell can be configured in different ways to solve different kinds of problems. These configurations are often named based on the relationship between the input and output sequence lengths (e.g., "many-to-one").

The following video gives an excellent overview of these common patterns.

The Power of Recurrent Neural Networks (RNN)

This segment from the IBM video details the main RNN architectures and provides clear, practical examples for each.

Watch from 03:24 to 05:43. Pay close attention to the four patterns described and their use cases.

Let's summarize those key architectures:

  • Sequence-to-Vector (Many-to-One): Processes a whole sequence and produces a single output.
    • Application: Sentiment analysis (input: a movie review, output: positive/negative), text classification.
  • Vector-to-Sequence (One-to-Many): Takes a single input and generates a sequence.
    • Application: Image captioning (input: an image vector, output: a sequence of words).
  • Sequence-to-Sequence (Many-to-Many): Processes an input sequence and produces an output sequence of the same length.
    • Application: Time series forecasting (e.g., predicting stock prices for each day based on past days), video frame classification.
  • Encoder-Decoder (Delayed Sequence-to-Sequence): A more advanced architecture that maps an input sequence to an output sequence of a different length. It consists of two RNNs: an "encoder" that reads the input sequence into a single context vector, and a "decoder" that generates the output sequence from that vector.
    • Application: Machine translation (e.g., English to French), question answering.

For more complex tasks, we sometimes need to look at both past and future context to make a prediction. This is where Bidirectional RNNs come in. A Bidirectional RNN is simply two separate RNNs: one that processes the sequence from start to end, and another that processes it from end to start. The outputs from both are combined at each time step.

Deep Learning – RNN Chapter

To formalize your understanding of these more advanced structures, read the sections on Bidirectional RNNs and Encoder-Decoder architectures from the "Deep Learning" book.

Read section 10.3 ("Bidirectional RNNs") and section 10.4 ("Encoder-Decoder Sequence-to-Sequence Architectures"). Focus on the diagrams (Figures 10.11 and 10.12) and the high-level motivation for each architecture.


4. A Key Challenge: Long-Term Dependencies

While powerful, the simple "vanilla" RNN we've discussed has a significant weakness. During training, gradients must be backpropagated through every time step. For long sequences, these gradients can either shrink exponentially until they disappear (vanishing gradient problem) or grow exponentially until they overflow (exploding gradient problem).

The vanishing gradient problem makes it very difficult for the network to learn dependencies between elements that are far apart in a sequence. For example, in the sentence "The man who I saw earlier this morning in the coffee shop, and who was wearing a red hat, ... is happy," the verb "is" depends on the singular noun "man" from the very beginning of the sentence. A simple RNN would struggle to carry this information over such a long distance.

Recurrent Neural Networks (RNNs), Clearly Explained!!!

The StatQuest video you watched earlier provides one of the clearest explanations of the vanishing and exploding gradient problem. This section is essential for understanding the limitations of basic RNNs.

Watch from 11:06 to 15:55. Notice how multiplying a weight smaller than 1 repeatedly causes the gradient to vanish, while multiplying a weight larger than 1 causes it to explode.

To solve this problem, more advanced RNN variants were developed, most notably the Long Short-Term Memory (LSTM) network and the Gated Recurrent Unit (GRU). These architectures introduce "gates"—special mechanisms that allow the network to learn when to add, forget, and output information from its memory, making them much more effective at learning long-range dependencies. We will touch on these in future lessons.


Conclusion

In this lesson, you've learned the fundamental principles behind Recurrent Neural Networks. You have seen how their unique architecture, based on a recurrent feedback loop and shared weights, enables them to process sequential data in a way that other network types cannot.

Key Takeaways:

  • RNNs are specialized for sequential data (e.g., text, time series) where order matters.
  • The core of an RNN is a loop that maintains a hidden state, which acts as the network's memory of the past.
  • RNNs can be visualized as an unrolled chain of identical cells, but the weights are shared across all time steps.
  • Different RNN configurations exist for different tasks, including many-to-one (classification), one-to-many (generation), and encoder-decoder (translation).
  • Simple RNNs struggle with long sequences due to the vanishing and exploding gradient problems, which makes learning long-term dependencies difficult.

Preview of the Next Lesson:
The sequential, step-by-step processing of RNNs, while powerful, has its own limitations—it can be slow and still struggles with extremely long-range dependencies. This led to the development of a revolutionary new architecture that processes all elements of a sequence at once using a mechanism called "attention." In the next lesson, we will explore the high-level concept of the Transformer architecture and understand why it has become the state-of-the-art for most NLP tasks today.