Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our previous lessons on Convolutional Neural Networks, we explored architectures designed to exploit spatial structure in data, such as images. We saw how convolutions and pooling layers build hierarchical feature representations that are equivariant or invariant to translation. We now pivot from the domain of space to the domain of time and sequence. Many real-world problems involve data where order is not just important, but defines the data's very meaning—stock prices, natural language, or video frames are prime examples.
This lesson introduces the Recurrent Neural Network (RNN), a class of neural networks specifically designed to operate on sequential data. Our goal is to understand the fundamental architecture of a "vanilla" RNN. You will learn how it uses an internal state and a recurrent loop to process sequences step-by-step, how it achieves immense parameter efficiency through weight sharing across time, and how to visualize its operation by "unrolling" the network.
The Challenge of Sequential Data
Feedforward networks, including the CNNs we've studied, typically assume that their inputs are independent and identically distributed (i.i.d.). For a task like image classification, shuffling the training images doesn't affect the outcome. However, for a sentence, shuffling the words destroys its meaning. A model processing such data needs to understand context and dependencies across time.
RNNs provide a flexible framework for handling various types of sequence processing tasks. The following video gives an excellent overview of the different sequence-to-sequence paradigms that RNNs enable.
Lecture 10 | Recurrent Neural Networks
This segment from a Stanford lecture on RNNs introduces the different modalities of sequence processing, contrasting them with the fixed-input/fixed-output nature of standard feedforward networks.
Please watch the introduction from the beginning of the RNN section. Pay close attention to the distinctions between: One-to-many (e.g., image to caption) Many-to-one (e.g., sentence to sentiment) Many-to-many (e.g., machine translation or video frame classification)
To handle such tasks, a model needs a form of memory. It must be able to retain information from earlier parts of a sequence to inform its processing of later parts. This is the core idea behind the RNN.
The Vanilla RNN Architecture: A Recurrent Loop
An RNN processes a sequence element by element. At each time step , it takes an input and updates its internal hidden state, . This hidden state serves as the network's memory, summarizing the information from all previous time steps.
The defining feature of an RNN is its recurrence relation. The hidden state at the current time step, , is a function of both the current input, , and the previous hidden state, .

The "vanilla" RNN, the simplest variant, implements this recurrence with a specific set of operations. The following lecture clips formally define this architecture.
Lecture 10 | Recurrent Neural Networks
This part of the Stanford lecture details the functional form of the vanilla RNN, defining the recurrence relation and the role of the weight matrices.
Watch the segment from here. The lecturer explains how the new hidden state h_t is computed from the previous state h_{t-1} and the current input x_t using shared weights and a tanh non-linearity.
As explained in the video and the image above, the state update and output are governed by two equations:
-
Hidden State Update:
-
Output Calculation:
Let's break down the components:
- : The input vector at time step .
- : The hidden state vector from the previous time step. is typically initialized as a vector of zeros.
- : The new hidden state vector, which captures information from the past up to time .
- : The output vector at time step .
- , , : The weight matrices that are the learnable parameters of the model.
- , : The bias vectors, also learnable parameters.
- : The hyperbolic tangent activation function, which squashes the values of the hidden state to be between -1 and 1.
The crucial insight here is that the computation at each step depends on the result of the previous step. This temporal dependency is what allows the network to maintain and propagate information through the sequence.
Unrolling and Weight Sharing
The compact, looped diagram of the RNN cell is elegant but can be hard to reason about. To understand how the network operates over a full sequence and, critically, how it is trained, we can visualize it by unrolling the loop. Unrolling simply means creating a copy of the network for each time step in the sequence.

When unrolled, the RNN looks like a very deep feedforward neural network, where each time step is effectively a layer. However, there is a fundamental difference.
Weight Sharing Across Time
In this unrolled view, the most important concept is that the same set of weight matrices (, , ) is used at every single time step. This is the principle of parameter sharing across time.
This is directly analogous to the parameter sharing you saw in CNNs.
- In a CNN, a single kernel's weights are shared across different spatial locations in the input.
- In an RNN, a single set of weights is shared across different temporal positions in the sequence.
This design has two profound consequences:
- Efficiency: The number of parameters in the model is independent of the sequence length. Whether you process a sentence of 5 words or a document of 5000 words, the number of weights remains the same. This is a stark contrast to a standard feedforward network, which would require a unique set of input weights for each position in the sequence, leading to a parameter count that grows linearly with sequence length.
- Generalization: The model learns a transition function that is applicable at any point in a sequence. It doesn't have to learn separate rules for the beginning, middle, and end of a sequence.
The following resources provide a formal look at the unrolling process and the concept of weight sharing.
Chapter 12: Recurrent Neural Networks
This section from the MIT course notes explicitly visualizes the unrolled network and highlights the similarity between temporal weight sharing in RNNs and spatial weight sharing in CNNs.
In the section "Back-propagation through time", focus on point (2), which shows the <tf start="“Unroll" the RNN to be length n" end="temporally.">unrolled diagram. The accompanying text makes the direct comparison to convolutional networks, which should resonate with what you've recently learned.
Lecture 10 | Recurrent Neural Networks
This clip from the Stanford lecture provides a clear animation of the unrolling process, making the data flow and weight sharing explicit.
Watch the segment from this point. Notice how the same weight matrix W is shown to be an input to the function f_w at every time step. This visualization is key to understanding weight sharing.
A Concrete Example: Character-Level Language Modeling
To solidify these abstract concepts, let's consider a classic application: a character-level language model. The task is to train an RNN to read a sequence of characters and, at each step, predict the next character in the sequence.
Let's use the training sequence "hello".
- Input: The characters 'h', 'e', 'l', 'l', 'o' are fed into the RNN one at a time. Each character is represented as a one-hot vector (a vector of zeros with a single '1' at the index corresponding to that character in the vocabulary).
- Target: At each time step, the target for the network is the next character in the sequence.
- When the input is 'h', the target is 'e'.
- When the input is 'e', the target is 'l'.
- When the input is 'l', the target is 'l'.
- When the input is 'l', the target is 'o'.
- Process:
- At , the RNN takes (the vector for 'h') and the initial hidden state (a zero vector) to compute . From , it computes an output , which is a vector of scores for every character in the vocabulary.
- At , the RNN takes (the vector for 'e') and the previous hidden state to compute . The output is then computed from .
- This process repeats for the entire sequence. At each step, the hidden state contains a summary of the characters seen so far.
- Loss: At each step, we can use a softmax function on the output vector to get a probability distribution over the possible next characters. We then compare this distribution to the actual next character using a cross-entropy loss. The total loss for the sequence is the sum of the losses at each time step.
This example is brilliantly explained by Andrej Karpathy in his CS231n lecture.
CS231n Winter 2016: Lecture 10: Recurrent Neural Networks, Image Captioning, LSTM
This lecture provides an exceptionally clear and intuitive walkthrough of a character-level RNN using the "hello" example. It connects all the concepts we've discussed: one-hot encoding, the recurrence formula, weight sharing, and the softmax output.
First, watch the conceptual explanation of the character-level model from this segment. Pay close attention to the diagram showing how the input characters are fed in, how the hidden state evolves, and how the network makes predictions at each time step. The lecturer explicitly points out where the shared weights are used. Then, for a more technical view, watch from this earlier section, where he formally defines the vanilla RNN recurrence formula and the weight matrices involved.
Conclusion
In this lesson, we have laid the theoretical groundwork for understanding Recurrent Neural Networks. You have moved from architectures for spatial data to a new paradigm for handling sequences.
Here are the key takeaways:
- RNNs process sequential data by iterating through the sequence and maintaining a hidden state , which acts as a memory.
- The core of an RNN is the recurrence relation, where the current hidden state is a function of the previous state and the current input: .
- The vanilla RNN implements this with the equation .
- Unrolling an RNN reveals a deep computational graph where the same set of weights is shared across all time steps, enabling parameter efficiency and generalization over sequence length.
While the unrolled graph looks like a standard deep network, training it is not so simple. The very mechanism that gives RNNs their power—the repeated application of the same weights—also introduces significant challenges for learning. In our next lesson, we will explore how we train these models using Backpropagation Through Time (BPTT) and confront the critical problem of vanishing and exploding gradients, which hinders the ability of vanilla RNNs to learn long-range dependencies.