Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we derived the backpropagation algorithm, understanding it as an efficient application of the chain rule to compute the gradient of the loss function with respect to every network parameter. You learned the four key equations that govern this backward flow of information. Now that we know how gradients are calculated, we must investigate a critical question: what is the nature of these gradients, especially in the context of the very deep networks that are common today?
This lesson explores two fundamental instabilities that plagued deep learning for decades: the vanishing and exploding gradient problems. We will trace their origins back to the mathematical structure of the backpropagation algorithm itself—the repeated multiplication of derivatives. You will learn the precise causes of these phenomena, how they manifest during training, and the different ways they sabotage a network's ability to learn. This foundational knowledge is crucial for understanding why modern deep learning architectures are designed the way they are.
The Mathematical Origin of Gradient Instability
Let's recall the core recursive step of backpropagation from our previous lesson. The "error" signal at layer is computed from the error signal at layer as follows:
To get the gradient at a much earlier layer, say , we have to apply this transformation recursively many times. Unrolling this recurrence for a deep network with layers, the gradient at the first layer is proportional to a long product of weight matrices and derivatives of the activation function:
This long product of matrices is the root cause of gradient instability. From a mathematical standpoint, repeatedly multiplying matrices can have two extreme outcomes on the norm of the resulting vector:
- If the matrices in the product tend to have singular values less than 1, the norm of the gradient will shrink exponentially, approaching zero. This is the vanishing gradient problem.
- If the matrices in the product tend to have singular values greater than 1, the norm of the gradient will grow exponentially, approaching infinity. This is the exploding gradient problem.
Let's examine the two main factors in this product—activation function derivatives and weight magnitudes—to understand how they contribute to each problem.
The Vanishing Gradient Problem
The vanishing gradient problem is a "silent failure" where the network appears to be training but the early layers learn extremely slowly, if at all.
Cause 1: Saturating Activation Functions
The historical prevalence of the vanishing gradient problem is tightly linked to the use of saturating activation functions like sigmoid and tanh. A function "saturates" when its output approaches a horizontal asymptote, causing its derivative to approach zero.
- Sigmoid: . Its derivative is . The maximum value of this derivative occurs at and is only 0.25. For large positive or negative inputs, the derivative is nearly zero.
- Tanh: . Its derivative is . While its maximum value is 1.0 (at ), it also saturates and its derivative rapidly drops towards zero for inputs away from the origin.
When you multiply many numbers that are significantly less than 1 together, the product diminishes exponentially. In a deep network using sigmoid activations, the gradient signal passed backward is multiplied by a factor with a maximum value of 0.25 at each layer. This causes the gradient to "vanish" by the time it reaches the early layers.
The following video provides an excellent mathematical walkthrough of how the sigmoid's bounded derivative leads directly to this exponential decay.
The Fundamental Problem with Neural Networks - Vanishing Gradients
This video from ritvikmath provides a clear, step-by-step derivation of the vanishing gradient problem, showing how the chain rule and the properties of the sigmoid function combine to cause it.
Focus on the core of the argument from the breakdown of the chain rule. Pay close attention to how the derivative of the sigmoid is calculated and why it's bounded by 0.25. Then, watch how this upper bound leads to an exponential decay term, (1/4)^(k+1), as a function of network depth k. Finally, see how this leads to the practical problem of early layers failing to learn from the implications.
Cause 2: Weight Initialization
Even if the activation function derivatives were not an issue, the magnitude of the weights plays a crucial role. If the weight matrices are initialized such that their norms are small (e.g., less than 1), they too will contribute to the shrinking of the gradient during backpropagation. The combination of small weights and saturating activation functions creates a perfect storm for vanishing gradients.
The practical consequence is that the gradients for the weights in the initial layers become vanishingly small. When an optimization algorithm like gradient descent tries to update the weights using these tiny gradients, the updates are minuscule:
The early layers, which are responsible for learning low-level features from the raw data, remain stuck near their random initialization. The network effectively loses its depth, as only the last few layers are able to learn meaningfully.
The Exploding Gradient Problem
The exploding gradient problem is the mirror image of vanishing gradients. It's a much more obvious failure, typically causing the training process to crash.
This occurs when the product of weight matrices and activation derivatives leads to exponential growth. In feedforward networks with sigmoid/tanh, this is less common than vanishing gradients because their derivatives are bounded by 1. However, it can still happen, particularly if the weights are initialized with large values.
Imagine a scenario where the weights in are large, and the inputs to the activation functions are consistently near zero (where the tanh derivative is 1). The repeated multiplication of matrices with norms greater than 1 causes the gradient to grow exponentially.
When the gradients become excessively large, the weight updates are massive and erratic. This can cause the optimizer to overshoot the minimum of the loss function, leading to a rapid increase in loss and divergence. In practice, the large numbers often exceed the maximum representable value for a floating-point number, resulting in NaN (Not a Number) values for the weights and loss, and a complete halt of the training process.
The following article provides a concise summary and a helpful table comparing these two problems.
Vanishing Gradient Problem: What It Is & How to Fix It In 2026
This article from ArticSledge gives a clear, high-level overview of the causes and symptoms of gradient instability.
First, read the section "The Mathematics of Vanishing Gradients" for a recap of the core mathematical cause. Then, review the three root causes outlined in the section "Root Causes". Finally, pay close attention to the table in the section "Vanishing vs. Exploding Gradients", as it provides an excellent side-by-side comparison.
Visualizing and Diagnosing Gradient Instability
In a real-world project, how would you diagnose these problems? The most direct way is to monitor the magnitude (specifically, the L2 norm) of the gradients for each layer's weights during training.
- Vanishing Gradients: You would observe that the gradient norms of the layers closer to the input are several orders of magnitude smaller than the norms of the layers closer to theoutput. The training loss may plateau after an initial decrease, as only the later layers are effectively learning.
- Exploding Gradients: You would see the gradient norms for some layers spike to extremely large values, often followed by the loss becoming
inforNaN.
The plot below illustrates this phenomenon. It shows the gradient norms for an 11-layer network over 10 epochs of training. Notice the log scale on the y-axis.

In this plot, Layer_9 is a clear victim of the vanishing gradient problem, with its gradient norm consistently around or lower. Conversely, Layer_1 and Layer_2 show signs of exploding gradients, with norms consistently above the threshold of . The other layers fall somewhere in between, illustrating the unstable dynamics of gradient flow in a deep network.
A Deeper Look: The Role of the Loss Function
Interestingly, the choice of loss function can mitigate the vanishing gradient problem at the final layer. This is a crucial theoretical point.
Consider a binary classification task. If you use a sigmoid output and a Mean Squared Error (MSE) loss, the gradient of the loss with respect to the pre-activation is:
If the neuron's output is confidently wrong (e.g., when the true label ), the sigmoid is saturated, so . This means the gradient is nearly zero, and the network learns very slowly from its mistake. This is a form of the vanishing gradient problem at the output layer.
However, if you use the more appropriate Binary Cross-Entropy (BCE) loss, the gradient is simply:
Notice that the problematic term has vanished! The gradient is now directly proportional to the error. When the network is confidently wrong, the error is large, the gradient is large, and the network learns quickly. This elegant cancellation is a key reason why cross-entropy is the standard loss function for classification.
These lecture notes from the University of Illinois Urbana-Champaign provide a concise mathematical explanation of why cross-entropy loss is preferred over MSE for classification tasks with sigmoid or softmax outputs.
Start by reading the section on the cons of MSE, which identifies the issue. Then, read the section "Why Cross-Entropy is Useful" which shows the mathematical derivation for how Binary Cross-Entropy (BCE) resolves this issue by canceling out the derivative term. Finally, note the summary points that reiterate this conclusion for both binary and multi-class classification.
This demonstrates that while the core problem of instability in hidden layers remains, intelligent choices in network design (like the loss function) can solve specific instances of it.
Conclusion
In this lesson, we dissected the mathematical underpinnings of the vanishing and exploding gradient problems. You now have a theoretical grasp of why deep neural networks can be notoriously difficult to train.
Our key takeaways are:
- Core Cause: Gradient instability arises from the repeated multiplication of matrices in the backpropagation chain rule through many layers.
- Vanishing Gradients: Occur when the product of weight norms and activation derivatives is consistently less than 1, causing gradients in early layers to shrink exponentially to zero. The primary culprits are deep architectures and saturating activation functions like sigmoid. This results in stalled training where early layers fail to learn.
- Exploding Gradients: Occur when the product is consistently greater than 1, causing gradients to grow exponentially. This is typically caused by large initial weights and results in unstable, diverging training.
- Diagnosis: These problems can be diagnosed by monitoring gradient norms per layer during training.
We have established the problem and its causes. This naturally leads to the next question: How do we fix it? While we briefly touched on the role of the loss function, the most direct and impactful solutions involve architectural changes, new activation functions, and, most critically, smarter weight initialization strategies. In our next lesson, we will derive the famous Xavier/Glorot and He initialization schemes, which were specifically designed to ensure stable gradient flow from the very start of training.