Skip to main content

Welcome to a Grasp lesson

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

Lesson 44

Gradients Gone Wild: Vanishing & Exploding

Recommended resources

In our previous lesson, we meticulously derived the update rules for a single neuron, applying the chain rule to see how the final loss dictates changes to individual weights and biases. We found that the gradient for a weight is a product of three key factors: the error signal, the sensitivity of the activation function, and the corresponding input signal.

This lesson addresses the crucial follow-up question: what happens when we stack these neurons into a deep network? The chain of derivatives in our backpropagation calculation now stretches across many layers. We will investigate how this repeated multiplication of terms can lead to two fundamental and opposing failure modes of deep learning: the vanishing and exploding gradient problems. Understanding these issues is essential for diagnosing why a network might fail to train and for appreciating the clever architectural innovations designed to overcome them.

The Problem with Deep Backpropagation

Recall the chain rule for a single neuron's weight, :

Now, consider a deep network. The gradient for a weight in an early layer, say layer 1, must propagate backward through all subsequent layers. The chain of derivatives becomes much longer. The gradient of the loss with respect to the pre-activation in the first layer is a product of terms from every layer that follows it:

where is the number of layers. Each term is the weight matrix , and each term is the derivative of the activation function, .

This long chain of matrix and vector products is the heart of the problem. What happens to the magnitude of the gradient as it is multiplied by these terms over and over again?


The Vanishing Gradient Problem

Let's first explore what happens when the terms in this product are consistently small. The ritvikmath video provides an exceptionally clear step-by-step walkthrough of this phenomenon.

The Fundamental Problem with Neural Networks - Vanishing Gradients

This video brilliantly isolates the core mechanism of the vanishing gradient problem, showing how the properties of the sigmoid activation function lead to an exponential decay of the learning signal.

Watch from the high-level overview to see how the gradient calculation for an early weight, w0, is expressed as a long product of terms using the chain rule. The narrator helpfully color-codes the terms into four distinct types. Next, focus on the analysis of the terms involving the activation function, from the sigmoid derivative. The key insight here is that the derivative of the sigmoid function, \sigma'(z), has a maximum value of 0.25. Finally, watch the conclusion of the argument from the product of small terms. This section explains how multiplying these small derivatives together over many layers causes the final gradient to shrink exponentially, effectively "vanishing" and halting learning in the early layers.

As the video explained, the derivative of the sigmoid activation function is a primary culprit.

The sigmoid function \(\sigma(x)\) squashes its input to a range between 0 and 1. Its derivative, \(\frac{d}{dx}\sigma(x)\), is plotted in black. Notice that the derivative is greatest at \(x=0\) (where it is 0.25) and approaches zero in the "saturating" regions where the sigmoid function flattens out.

In our backpropagation chain, we multiply the derivative of the activation function, , at each layer. Since , the gradient passed to layer from layer is scaled down by a factor of at most 0.25 (and also by the weights, which we'll assume for now are around 1).

After layers, the initial gradient signal from the loss function has been multiplied by of these small numbers. The gradient arriving at the first layer will be proportional to something like .

If the network has just 10 layers, the gradient is scaled by , which is nearly one-millionth of its original value. For deeper networks, this effect becomes catastrophic. The gradients for the early layers become so close to zero that the computer treats them as zero.

The consequence is that the weights in the early layers do not get updated. They remain stuck at or near their random initial values. This is a critical failure mode: the network is unable to learn the simple, foundational features in the initial layers, rendering the entire deep architecture useless. It's like a biologist trying to identify a complex cellular process while being unable to see the basic shapes of the cells themselves.

This pair of graphs illustrates the two problems. The "Vanishing Gradient" plot on the left shows the magnitude of the gradient decreasing exponentially as it propagates backward from the output layer to the input layer. The "Exploding Gradient" plot on the right shows the opposite effect.

The Exploding Gradient Problem

The opposite problem, exploding gradients, occurs when the terms in the backpropagation product are consistently large. This typically happens when the weight matrices have large values.

Andrew Ng provides a very simple and powerful intuition for this in a short video. He uses a toy example with linear activation functions () so we can focus purely on the effect of the weights.

Vanishing/Exploding Gradients (C2W1L10)

In this video, Andrew Ng from DeepLearning.AI uses a simplified deep network with linear activations to demonstrate how repeated multiplication by weight matrices can cause activations—and therefore gradients—to grow or shrink exponentially.

First, watch from the linear activation example. Here, he shows that if the weight matrices are consistently a little larger than the identity matrix (e.g., scaling by 1.5 at each layer), the output Y_hat grows exponentially with the number of layers. Next, watch the segment from the shrinking case, where he shows the converse: if the weights are smaller than 1 (e.g., 0.5), the activations shrink exponentially, which is the same phenomenon as the vanishing gradient problem. Finally, watch from the effect on gradients, where he states that this same logic applies to the gradients during backpropagation.

If the weights in the network are large, the repeated multiplications during backpropagation can cause the gradient to grow exponentially. An initial gradient of 0.1, when multiplied by a factor of 1.5 at each of 20 layers, becomes .

The consequence is that the gradient descent updates are enormous. Instead of taking a small step towards the minimum of the loss function, the algorithm takes a giant leap, completely overshooting the target and often landing in a region of even higher loss. This leads to numerical instability, where the loss diverges to infinity and the weights become NaN (Not a Number). Unlike the vanishing gradient problem which is a "silent failure," an exploding gradient is easy to spot because it crashes the training process.

The following resource provides an excellent summary of both problems and their root causes.

Vanishing Gradient Problem: What It Is & How to Fix It In 2026

This article offers a clear and concise explanation of the mechanics, mathematics, and root causes of the vanishing gradient problem, and provides a useful table comparing it with the exploding gradient problem.

First, read the sections How It Breaks and The Mathematics. These sections reinforce the core mathematical argument about multiplying small numbers from activation function derivatives. Then, review the section on Root Causes, which identifies the three key conditions: saturating activation functions, network depth, and poor weight initialization. Finally, study the table in the section Key Differences. This provides a perfect side-by-side comparison of the two failure modes.


From Mechanics to Dynamical Systems

So far, our explanation has been mechanical, focusing on the multiplication of derivatives. There is a more profound way to view this, which connects to your background in physics and mathematics: we can think of the repeated application of functions in a deep network as a form of a discrete dynamical system. Each layer transforms the state of the system (the activations). The behavior of the gradients is related to the stability of this system.

Lecture 15: Exploding and Vanishing Gradients

These lecture notes from the University of Toronto frame the problem in terms of the Jacobian matrix and iterated functions, providing a slightly more formal perspective.

Please read the section titled Why Gradients Explode or Vanish. This text formalizes our discussion. It introduces the Jacobian as the matrix of partial derivatives \frac{\partial \mathbf{h}^{(t+1)}}{\partial \mathbf{h}^{(t)}} that gets multiplied at each step. In the case of linear activations, the overall Jacobian is simply the weight matrix raised to a power, W^{T-1}. The text correctly notes that this matrix power will explode or vanish if its largest eigenvalue is greater or less than 1, respectively. This is a powerful connection to concepts we will study later in this course.

In regions where a slight change in the initial hidden state leads to a massive change in the final state (like on the boundary between two attractors in a dynamical system), the Jacobian will be large, and gradients will explode. In regions where all initial states converge to the same point (an attractor), the Jacobian will be near zero, and gradients will vanish. This perspective highlights that the vanishing/exploding gradient problem is an intrinsic property of composing many functions together.


Conclusion

In this lesson, we've uncovered a fundamental obstacle in training deep neural networks. The very mechanism of backpropagation—the chain rule—creates a vulnerability when applied over many layers.

Key Takeaways:

  • Backpropagation in a deep network involves a long product of derivatives, one for each layer.
  • Vanishing Gradients occur when these derivatives are consistently less than 1 (e.g., using sigmoid or tanh activations). The gradient signal shrinks exponentially, causing early layers to stop learning. This is a silent but catastrophic failure.
  • Exploding Gradients occur when the weight matrices are large, causing the derivatives to be consistently greater than 1. The gradient signal grows exponentially, leading to unstable updates and a divergent training process.
  • These two problems are mirror images, stemming from the repeated multiplication inherent in the chain rule applied to deep architectures. For many years, they were the primary barrier to building truly deep and powerful neural networks.

Now that we have a firm grasp of the problem, our next lesson will be dedicated to the solutions. We will analyze and compare different activation functions (like the famous ReLU), which were specifically designed to improve gradient flow and mitigate the vanishing gradient problem.