Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding RNN Architecture and the Vanishing/Exploding Gradient Problem

Introduction

In our last lesson, we saw how residual connections conquered the degradation problem in very deep spatial architectures like CNNs. We noted that these skip connections create "highways" for gradients, preventing them from vanishing as they propagate through many layers. This insight is crucial as we now pivot from spatial data to a new domain: sequential data.

Tasks involving language, financial time series, or physics simulations require models that can understand context, remember past events, and handle inputs of variable length. A standard feedforward network, which processes each input independently, is fundamentally unsuited for this. This lesson introduces the Recurrent Neural Network (RNN), an architecture designed from the ground up to model sequences by incorporating a notion of memory. We will dissect its architecture and then uncover its critical flaw: the very mechanism that gives it memory also makes it notoriously difficult to train on long sequences, leading to the infamous vanishing and exploding gradient problems.

Part 1: Modeling Sequences with State

Imagine trying to predict the next word in a sentence. The prediction for "sky" in "The clouds are blue, and so is the ___" depends heavily on the word "blue" that appeared much earlier. A model needs a way to maintain information over time. This is the core idea of recurrence.

Unlike a feedforward network, an RNN processes a sequence one element at a time, and at each step, it combines the current input with information from the previous step. This "information" is stored in a hidden state, a vector that acts as the network's memory.

A Recurrent Neural Network shown in its compact, "rolled" form (left) and its "unrolled" form over time (right). At each timestep `t`, the hidden state `H_t` is a function of the previous hidden state `H_{t-1}` and the current input `X_t`. The same weight matrices (U, V, W) are used at every step.

The key architectural features are:

  1. Hidden State (): A vector that summarizes the sequence up to timestep . It's computed based on the previous hidden state and the current input .
  2. Recurrent Connection: The output of the network at one step (the new hidden state) is fed back into itself as an input for the next step. This is the loop shown on the left of the diagram.
  3. Parameter Sharing: The same set of weights (, , ) are used at every single timestep. The network doesn't learn a new set of weights for each position in the sequence; it learns a single, general update rule. This makes the model efficient and capable of handling sequences of varying lengths.

Let's watch a brief segment from a Stanford lecture that introduces this concept in the context of language modeling.

Stanford CS224N NLP with Deep Learning | Winter 2021 | Lecture 6 - Simple and LSTM RNNs

This video from Stanford's CS224N course provides a clear introduction to the simple RNN architecture for language modeling.

Watch the initial segment from the introduction. This will walk you through the diagram above and the core recurrent equation.

Part 2: The Mathematical Formulation of a Simple RNN

Let's formalize the computations shown in the diagram. At each timestep , an RNN performs two main calculations:

  1. Hidden State Update: The new hidden state is computed by combining the previous state and the current input . This combination passes through an affine transformation followed by a non-linear activation function, typically the hyperbolic tangent ().

    Here:

    • is the input vector at time .
    • is the hidden state vector from the previous time .
    • is the weight matrix for the input-to-hidden connection.
    • is the weight matrix for the hidden-to-hidden (recurrent) connection.
    • is the bias for the hidden layer.
  2. Output Calculation: The output for the current timestep, , is typically computed from the current hidden state .

    • is the weight matrix for the hidden-to-output connection.
    • is the bias for the output layer. The output might then be passed through a softmax function for classification tasks.

To better understand the flow, let's read a short article that details these operations.

The Math Behind Recurrent Neural Networks

This article from Towards Data Science breaks down the RNN architecture and its mathematical operations clearly.

Read Section 2.1, "The Structure of RNNs". This will reinforce the roles of the input, hidden state, and output. Then, read Section 2.2.1, "Forward Pass", which maps the mathematical equations directly to the flow of data during computation.

Part 3: The Gradient Problem in Time

Training an RNN involves computing a loss (e.g., at the end of the sequence) and using gradients to update the shared weights , , and . Because the same weights are used at every step, the process of backpropagation must also flow backward through time. This is called Backpropagation Through Time (BPTT).

An update to at time affects , which in turn affects , which affects , and so on, all the way to the final loss calculation. The total gradient for a shared weight is the sum of its gradient contributions from every timestep.

This dependency creates a long chain of computations, which brings us to a familiar challenge.

Socratic Question: To calculate the influence of the hidden state at an early time on the loss computed at a much later time , we must apply the chain rule. What mathematical form will the expression for take? Given our experience with very deep feedforward networks, what instability might arise from this structure?

Answer The expression for $\frac{\partial L_T}{\partial h_t}$ will involve a long product of Jacobian matrices: $$ \frac{\partial L_T}{\partial h_t} = \frac{\partial L_T}{\partial h_T} \frac{\partial h_T}{\partial h_{T-1}} \frac{\partial h_{T-1}}{\partial h_{T-2}} \cdots \frac{\partial h_{t+1}}{\partial h_t} $$ Just as repeated matrix multiplication through many layers in a deep feedforward network can cause gradients to vanish or explode, this repeated multiplication through many *timesteps* will cause the same instability. The gradient signal from the distant future ($L_T$) has to survive this long multiplicative journey to inform the state at time $t$.

This is precisely the vanishing and exploding gradient problem, but manifested in the temporal dimension.

As the gradient of the loss J(θ) propagates backward through time, it is repeatedly multiplied by the Jacobian of the hidden state transition. If these Jacobians are small, the gradient signal vanishes before reaching early timesteps.

Let's watch a clear, mathematical derivation of this problem.

Vanishing Gradients: Why Training RNNs is Hard

The YouTube channel DeepBean provides an excellent and concise mathematical explanation of why training RNNs is hard.

Watch from the quick refresher on RNNs. Continue through the explanation of backpropagation through time (BPTT). Pay close attention to the calculus derivation, which shows how the gradient becomes a product of Jacobian matrices. Finally, watch the conclusion from where this leads to the vanishing and exploding gradient problems.

Part 4: The Mathematical Core of the Instability

As the video explained, the gradient of the loss at time with respect to a hidden state at an earlier time is a product of Jacobians:

Let's analyze the Jacobian term . Starting from our hidden state update equation:

The derivative with respect to is:

This reveals two key components that are multiplied together at every step back in time:

  1. The Activation's Derivative: A diagonal matrix containing the derivatives of the function. The derivative of is , which is always between 0 and 1. When the neuron is "saturated" (its input is large and positive or negative), the derivative is close to 0. This term can only shrink the gradient.
  2. The Recurrent Weight Matrix : This matrix is repeatedly applied. From our study of linear algebra, we know that repeated multiplication of a vector by a matrix will cause the vector's norm to scale by roughly , where is the largest singular value of and is the number of multiplications.

Combining these two factors:

  • Vanishing Gradients: If the singular values of are less than 1, or even if they are slightly larger than 1 but the activation derivatives are small, the norm of the overall Jacobian product will be less than 1. When multiplied over many timesteps, the gradient signal shrinks exponentially, effectively "vanishing" to zero. The model becomes unable to learn dependencies between distant timesteps.
  • Exploding Gradients: If the singular values of are significantly greater than 1, the gradient norm can grow exponentially, leading to massive, unstable weight updates that derail the training process.

The following article provides a fantastic deep dive into this mathematical reasoning.

Vanishing Gradients in RNNs: Why Neural Networks Forget Long ...

This article by Moritz Brenndoerfer is one of the clearest and most thorough explanations of the vanishing gradient problem available.

First, read The Gradient Product Chain. This section masterfully derives the product structure of the gradient. Next, read Why Gradients Vanish. This section connects the mathematical derivation to the eigenvalue/spectral radius perspective, explaining exactly why the product shrinks. Finally, read about the asymmetry of the two problems in Vanishing vs. Exploding Gradients. This explains why exploding gradients have a simple fix (gradient clipping) while vanishing gradients do not.

While exploding gradients can cause training to fail spectacularly (resulting in NaN loss values), they are the easier problem to solve. We can simply implement gradient clipping: if the norm of the total gradient vector exceeds a certain threshold, we scale it down before the weight update. This is a pragmatic but effective fix.

The vanishing gradient problem is far more insidious. A gradient that has decayed to near-zero contains no useful information. There is no simple trick to "amplify" it back into a meaningful signal. This fundamental limitation is what makes simple RNNs struggle with true long-range dependencies in practice.

Conclusion

In this lesson, we introduced the Recurrent Neural Network as an architecture tailored for sequential data. Its core strength—a recurrent hidden state that acts as memory—is also its greatest weakness during training.

Key Takeaways:

  • RNN Architecture: An RNN processes sequences by updating a hidden state based on the previous state and the current input , using the same weights at every timestep.
  • Backpropagation Through Time (BPTT): Training involves unrolling the network in time and propagating gradients backward through the entire sequence.
  • The Gradient Chain: The recursive nature of the hidden state creates a long product of Jacobian matrices when calculating gradients for distant timesteps.
  • Vanishing/Exploding Gradients: This product of matrices causes the gradient signal to either shrink exponentially to zero (vanish) or grow exponentially to infinity (explode).
    • Vanishing occurs when the Jacobians are contractive (e.g., singular values < 1), preventing the model from learning long-term dependencies.
    • Exploding occurs when the Jacobians are expansive (e.g., singular values > 1), causing training instability.
  • Asymmetric Problem: Exploding gradients can be managed with gradient clipping, but vanishing gradients represent a fundamental loss of information that cannot be easily recovered.

Preview of the Next Lesson:

The failure of simple RNNs to capture long-range dependencies was a major bottleneck in sequence modeling. This spurred the development of more sophisticated recurrent units designed specifically to combat the vanishing gradient problem. In our next lesson, we will explore Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU) networks. We will see how their use of "gates" creates an additive "cell state" pathway, a "gradient highway" that allows information to flow across many timesteps without being degraded by repeated multiplication.

Can't find a good explanation? Sign up and we'll make it for you

Sign up