Hello! Welcome to the next lesson in our journey through advanced neural network architectures.
In our last lesson, we explored ResNets, a powerful architecture for building very deep networks to process static, spatial data like images. We saw how skip connections solved the degradation problem and allowed networks to learn more complex features.
Today, we shift our focus from static data to a new and equally important domain: sequential data. This includes things like time series, text, and audio, where the order of information is crucial. You can't understand a sentence by just looking at a jumble of its words; the sequence matters. This requires a fundamentally different kind of network.
Your learning outcome for this lesson is to describe the architecture of a Recurrent Neural Network (RNN) for processing sequential data. We will explore why traditional networks fall short, what makes an RNN unique, and how its structure is perfectly suited for handling sequences.
1. Why Standard Networks Fail with Sequences
Before we build an RNN, let's understand why the architectures we've seen so far, like Artificial Neural Networks (ANNs) and Convolutional Neural Networks (CNNs), are not a good fit for sequential tasks like language translation or stock price prediction.
There are three main reasons:
- Variable-length inputs/outputs: An ANN has a fixed number of input and output neurons. How would you design a network to translate both a 3-word sentence and a 20-word sentence? It's not flexible.
- No sense of order: In a standard ANN, the order in which you present the input features doesn't matter. But in language, "the dog chased the cat" is completely different from "the cat chased the dog."
- No parameter sharing: If an ANN learns a feature related to a word at the beginning of a sentence, it has to re-learn that same feature from scratch if the word appears at the end. This is incredibly inefficient.
The following video provides an excellent overview of these limitations and sets the stage for why a new architecture is needed.
What is Recurrent Neural Network (RNN)? Deep Learning Tutorial 33 (Tensorflow, Keras & Python)
The video 'What is Recurrent Neural Network (RNN)?' from the codebasics channel provides great real-world examples and clearly explains why ANNs are not suited for sequence modeling.
Watch from the beginning up to 07:06. The first two minutes will show you common applications of RNNs in Natural Language Processing (NLP). The next section details the specific problems with using a standard ANN for these tasks. Pay close attention to the concepts of variable input size and parameter sharing.
2. The Core Idea: A Network with "Memory"
To overcome these challenges, we need a network that can "remember" what it has seen in previous steps of a sequence. This is the central idea behind the Recurrent Neural Network.
An RNN does this by incorporating a feedback loop. In a standard feedforward network, information flows in one direction: from input to output. In an RNN, the output of a hidden layer from one step is fed back into the same hidden layer in the next step. This feedback loop allows the network to maintain a hidden state, which acts as a form of memory, carrying information from one time step to the next.

To build your intuition, let's watch a clip that introduces this concept using a stock price prediction example.
Recurrent Neural Networks (RNNs), Clearly Explained!!!
The video 'Recurrent Neural Networks (RNNs), Clearly Explained!!!' from StatQuest with Josh Starmer is fantastic for building intuition. This first clip will show you why a feedback loop is a natural solution for handling sequential data.
Watch from 01:55 to 07:34. This section explains the need for a network that can handle varying sequence lengths and then demonstrates the mechanics of the feedback loop, showing how information from 'yesterday' is combined with information from 'today' to make a prediction for 'tomorrow'.
3. Unrolling the RNN: A Clearer View
The "folded" diagram with the loop can be a bit abstract. A much clearer way to visualize how an RNN processes a sequence is to "unroll" or "unfold" it through time.
Imagine making a copy of the network for each element in the sequence. The hidden state from the first time step is passed to the network copy at the second time step, and so on. This unrolled view makes it clear how information flows sequentially through the network.
Crucially, all these "copies" of the network are not different networks. They share the exact same set of weights. This is the brilliant solution to the parameter-sharing problem we discussed earlier. The network learns a single set of weights that is applied at every time step, allowing it to recognize patterns regardless of where they appear in the sequence.
Let's continue with the StatQuest video to see a great visualization of this unrolling process.
Recurrent Neural Networks (RNNs), Clearly Explained!!!
This next segment from the StatQuest video clearly illustrates the concept of unrolling and emphasizes the critical point about shared weights.
Watch from 07:34 to 11:18. Focus on how unrolling transforms the looped diagram into a linear sequence of network copies. Pay special attention to the note at the end about how all weights and biases are shared across every input.
The Mathematics of an RNN Cell
Now that we have the intuition, let's look at the basic equations that govern a simple RNN cell at a single time step t. Your engineering background will help you see this as a system that updates its state over time.
- Calculate the new hidden state, :
- Calculate the output, :
Let's break this down:
- : The input at the current time step (e.g., a vector representing a word).
- : The hidden state (memory) from the previous time step.
- : The new hidden state, which combines the previous state with the current input. This will be passed to the next time step, .
- : The output at the current time step (e.g., a prediction for the next word).
- , , : These are the shared weight matrices that the network learns during training.
- transforms the input.
- transforms the previous hidden state.
- transforms the new hidden state to produce the output.
- , : These are the bias terms, also learned during training.
- : This is the hyperbolic tangent activation function, a non-linearity that helps the network learn complex patterns.
The key is the first equation: the new state is a function of both the current input and the previous state . This is the recurrence that gives the network its name.
Test your understanding!
In the unrolled RNN diagram, the network at time step t=3 looks identical to the network at time step t=10. If you update the weight matrix based on an error at t=10, does this affect the computation that happened at t=3? Why or why not?
Show answer
Yes, it does. Because the weights are shared across all time steps, an update to at any point in the sequence changes the matrix for the entire network. When you re-run the sequence through the network after the update, the calculation at t=3 will now use this new, updated , resulting in a different hidden state . This weight sharing is what makes RNNs so powerful and efficient.
4. A Walkthrough: RNNs in Action
To solidify these concepts, it's helpful to walk through how a trained RNN processes a real sentence. We'll use a word prediction task as an example: given the input "the emperor has no", we want the network to predict "clothes".
This involves representing words as numerical vectors (word embeddings) and tracking how the hidden state vector evolves as each word is processed.
The intuition behind recurrent neural networks
The article 'The intuition behind recurrent neural networks' provides a fantastic visual walkthrough of this exact process. It uses 2D vectors to make the high-dimensional operations easy to follow.
Please read the sections titled 'Show me a real example' and 'How softmax works and how can we interpret it geometrically'. Follow the diagrams showing how the hidden state vector (the 'memory') changes as each word ('emperor', 'has', 'no') is fed into the network. This will give you a concrete sense of how the equations we just saw play out in practice.
As you saw in the article, the final hidden state (after processing "no") contains a summarized meaning of the sequence so far. This state is then used to predict the most probable next word from the entire vocabulary, often using a softmax layer which we've encountered in classification tasks.
5. Types of RNN Architectures
The basic RNN architecture is incredibly versatile and can be adapted for different types of sequence problems by varying the relationship between inputs and outputs.
Here are the main categories:
| Type | Description | Example Application |
|---|---|---|
| Many-to-one | Sequence of inputs, single output. | Sentiment analysis: A sequence of words (a review) is processed to produce a single output (positive/negative). |
| One-to-many | Single input, sequence of outputs. | Image captioning: A single input (an image) generates a sequence of words (a caption). |
| Many-to-many (Synced) | Sequence of inputs, sequence of outputs where each output corresponds to an input. | Named Entity Recognition (NER): Each word in an input sequence is given a label (e.g., Person, Location, Other). |
| Many-to-many (Delayed) | Sequence of inputs, sequence of outputs where the output sequence begins after the entire input sequence is processed. | Machine translation: The network must read an entire sentence in one language before it can begin generating the translation in another. This often uses an "Encoder-Decoder" structure. |
Introduction to Recurrent Neural Networks
The 'Introduction to Recurrent Neural Networks' article from GeeksForGeeks provides simple and clear diagrams for each of these architecture types.
Scroll down to the section titled 'Types Of Recurrent Neural Networks' and review the four diagrams. Compare each diagram to the descriptions in the table above.
Conclusion
In this lesson, we made the critical leap from processing static data to processing sequential data. We established why traditional networks are unsuitable for this task and introduced the foundational architecture designed to solve it: the Recurrent Neural Network.
Key Takeaways:
- Purpose: RNNs are designed for sequential data where order and context are important.
- Core Mechanism: They use a recurrent loop to pass a hidden state from one time step to the next, creating a form of memory.
- Unrolled View: We can visualize an RNN's operation by "unrolling" it through time, revealing a chain of identical network cells.
- Weight Sharing: The same set of weights (, , ) is used at every time step, making the model efficient and allowing it to generalize patterns across a sequence.
- Versatility: RNNs can be configured into different architectures (many-to-one, one-to-many, etc.) to fit a wide variety of sequence-based problems.
Preview of the Next Lesson:
The simple RNN architecture we've discussed is powerful, but it has a significant weakness: it struggles to remember information over long sequences. This is known as the vanishing and exploding gradient problem. In our next lesson, we will dive into why this happens and then explore the Long Short-Term Memory (LSTM) cell, an advanced RNN variant specifically designed to overcome this limitation and capture long-range dependencies.