Follow the signal backward
Vanishing gradients:
trace every factor
Understand depth, saturation and the chain rule. Keep the complete original explanation, then check its arithmetic against a reproducible calculation.
Depth attenuation is a chain-rule effect
A useful gradient must reach the parameter you want to update. Along a deep computation path, backpropagation multiplies derivatives. If the resulting product becomes very small, an early parameter may receive a weak learning signal even while later parameters receive larger gradients.
Inspect gradients across layers at the same training step, activation saturation and actual parameter updates. Slow learning or a small gradient alone is not a diagnosis: a disconnected path, a loss reduction, scaling, underflow or an already good fit can also produce small values. Check the computation before choosing a remedy.
σ′(z) = σ(z)(1 − σ(z)) ≤ ¼
The weight factors matter too. For a scalar chain with every weight fixed at 0.5, each input-to-output derivative factor has magnitude at most 0.5 × 0.25 = 0.125. With L such layers, the product is at most 0.125L. This bound applies to that controlled chain; it is not a claim that every deep model must vanish.
Separate corrections to the original lesson
Arithmetic: the initial forward outputs are approximately 0.6224593312, 0.5771853801 and 0.5716515653. The chosen loss ½(a₃ − 0.8)² is 0.0260715038. It is half squared error for one output, not an unexplained general MSE reduction. The original early-weight gradients and updates are too large; the checked values follow below without changing the historical equations.
Depth versus time: the original three-iteration discussion treats decreasing gradients over iterations as the hallmark of vanishing gradients. That is not sufficient evidence. In the corrected three-step calculation, the first two gradient magnitudes rise slightly. Their smaller size relative to the output-layer gradient illustrates attenuation along this particular path, not an inevitable monotone training trajectory.
Clipping: ordinary gradient norm clipping caps large gradients. It does not enlarge already-small gradients or recover an attenuated signal. The retained suggestion to clip gradients that are too small should not be followed as a vanishing-gradient remedy.
Remedies have conditions: ReLU has derivative one only on its positive side and zero on its negative side. He and Glorot initialization aim to control signal scales under their assumptions, not guarantee learning. Batch normalization uses epsilon, optional learned scale/offset and different training/inference statistics, so its final outputs need not have exactly zero mean and unit variance. A residual block has Jacobian I + JF; it offers a direct path but cancellation and other optimization difficulties remain possible.
Corrected three-step scalar calculation
Use the original x = 1, three weights of 0.5, no biases, sigmoid at every layer, target 0.8 and learning rate 0.1. Each row evaluates one weight vector, then updates all three weights simultaneously. Keep full precision internally and round only for display.
δ₂ = δ₃ w₃ a₂(1 − a₂), δ₁ = δ₂ w₂ a₁(1 − a₁)
g₃ = δ₃a₂, g₂ = δ₂a₁, g₁ = δ₁x
| Step | Loss | g₁ | g₂ | g₃ |
|---|---|---|---|---|
| 1 | 0.0260715038 | -0.0008016904 | -0.0042469087 | -0.0322731935 |
| 2 | 0.0259655902 | -0.0008056388 | -0.0042644160 | -0.0322025204 |
| 3 | 0.0258601162 | -0.0008095632 | -0.0042817547 | -0.0321319828 |
(w₁, w₂, w₃)new ≈ (0.5000801690, 0.5004246909, 0.5032273194)
The original rounded weight vectors in its second and third iterations are not the corrected trajectory. Independent checks also evaluate those historical vectors separately; similar-looking rounded forward outputs do not validate their incorrect backward arithmetic.

The controlled input derivative is below its 0.125L bound at depths 1, 3, 10 and 20. In general networks, matrix products and singular values determine how different directions scale. A small activation derivative is one factor, not a proof that every weight gradient vanishes.
Complete original lesson
All original prose, repeated advice, numerical steps and 103 mathematical expressions follow in source order. Read the separate corrections above alongside the historical values. The original nested document metadata and malformed MathJax configuration are retained as inert recoverable source; the formulas are selectable HTML and do not depend on that legacy script or a new rendering library.
First let’s explain what’s Vanishing Gradient Problem in Neural Networks
Understanding and Addressing the Vanishing Gradient Problem in Deep Learning
Part 1: What is the Vanishing Gradient Problem and How to Solve It?
In the world of deep learning, as models grow deeper and more complex, they bring with them a unique set of challenges. One such challenge is the vanishing gradient problem—a critical issue that can prevent a neural network from learning effectively. In this first part of our discussion, we’ll explore what the vanishing gradient problem is, how to recognize it in your models, and the best strategies to address it.
What is the Vanishing Gradient Problem?
The vanishing gradient problem occurs during the training of deep neural networks, particularly in models with many layers. When backpropagating errors through the network to update weights, the gradients of the loss function with respect to the weights can become exceedingly small. As a result, the updates to the weights become negligible, especially in the earlier layers of the network. This makes it difficult, if not impossible, for the network to learn the underlying patterns in the data.
Why does this happen? The root cause lies in how gradients are propagated through layers during backpropagation. In deep networks, each layer’s gradient is a product of several small derivatives, particularly when using activation functions like the sigmoid or tanh. These activation functions tend to squash input values into a small range, causing their derivatives to be small as well. As these small values are multiplied together across many layers, the gradient can shrink exponentially, leading to the vanishing gradient problem.
How to Recognize the Vanishing Gradient Problem in Your Model
Detecting the vanishing gradient problem in your model is crucial for addressing it early on. Here are some signs that your model might be suffering from this issue:
- Slow or No Learning in Early Layers: If you notice that the weights in the early layers of your network are barely changing during training, it could be due to vanishing gradients. These layers might not be learning effectively because the gradient updates are too small.
- Poor Performance Despite Deep Architecture: If your deep model is performing worse than a shallower version, even after sufficient training, the vanishing gradient could be at fault. This can happen because the network fails to propagate error signals back through the layers effectively.
- Gradients Close to Zero: By monitoring the gradient values during training, you can spot if they are consistently close to zero, particularly in the earlier layers. This is a direct indication that the vanishing gradient problem might be occurring.
Solutions to the Vanishing Gradient Problem
Fortunately, several strategies have been developed to mitigate the vanishing gradient problem, allowing deep networks to train effectively:
Use of ReLU and its Variants
- ReLU (Rectified Linear Unit): Unlike sigmoid and tanh, ReLU does not squash inputs into a small range. It allows for gradients to remain larger and more stable as they are backpropagated. However, ReLU can suffer from “dying ReLUs” where neurons stop activating. Variants like Leaky ReLU, Parametric ReLU (PReLU), and Exponential Linear Unit (ELU) help address this issue by allowing a small gradient for negative inputs.
Better Weight Initialization
- Xavier (Glorot) Initialization: This method helps keep the output variance consistent across layers by initializing weights appropriately. It’s particularly useful with sigmoid and tanh activations.
- He Initialization: Optimized for ReLU and its variants, He initialization further ensures that gradients don’t diminish too quickly by scaling the weights based on the number of input units.
Batch Normalization
- Batch normalization normalizes the inputs of each layer, ensuring that they have a mean of zero and a variance of one. This not only accelerates training but also helps keep gradients from vanishing by stabilizing the distribution of activations throughout the network.
Residual Networks (ResNets)
- ResNets introduce skip connections, allowing gradients to bypass certain layers. This direct gradient path helps mitigate the vanishing gradient problem, making it possible to train networks with hundreds or even thousands of layers effectively.
Monitoring and Gradient Clipping
- While gradient clipping is more commonly used to handle exploding gradients, monitoring and occasionally clipping gradients that are too small can also help in managing vanishing gradients.
How to Address the Vanishing Gradient Problem in Practice
When training your model, consider implementing these strategies from the start, particularly when working with very deep architectures. If you observe signs of the vanishing gradient problem, try switching to ReLU or its variants, ensure proper weight initialization, and consider adding batch normalization layers.
By proactively applying these techniques, you can significantly improve the learning capability of your deep networks, ensuring that all layers contribute effectively to the final output.
Now let’s divide to an example to understand mathematic behind vanishing gradient a example better
Part 2: A Simple Example Demonstrating the Vanishing Gradient Problem
In the first part of this series, we explored what the vanishing gradient problem is and discussed various strategies to address it. In this second part, we will dive into a simple, step-by-step mathematical example using a real neural network. This example will help you understand how the vanishing gradient problem manifests in practice and what you can do to solve it.
Setting Up the Example
Let’s consider a simple neural network with the following structure:
- 1 Input Neuron: x0 =1
- 3 Layers: Each with 1 neuron
- Sigmoid Activation Function for each neuron
- Target Output: ytarget =0.8
- Initial Weights: W1 =0.5, W2 =0.5, W3 =0.5
- Learning Rate: η=0.1
Step 1: Forward Pass
In the forward pass, we calculate the output of the network layer by layer.
Layer 1:
z1 =W1 ·x0 =0.5·1=0.5
y1 =σ(z1)=11+e-0.5≈0.6225
Layer 2:
z2 =W2 ·y1 =0.5·0.6225≈0.31125
y2 =σ(z2)=11+e-0.31125≈0.5772
Layer 3:
z3 =W3 ·y2 =0.5·0.5772≈0.2886
y3 =σ(z3)=11+e-0.2886≈0.5716
Output of the network:
youtput =y3 ≈0.5716
Step 2: Calculating the Loss
The loss function we are using is the Mean Squared Error (MSE):
ℒ=12(youtput -ytarget)2 =12(0.5716-0.8)2 ≈0.0262
Step 3: Backpropagation
Now, let’s compute the gradients for each weight using backpropagation.
Gradient with Respect to W3:
∂ℒ∂W3=∂ℒ∂y3·∂y3∂z3·∂z3∂W3
Step-by-step:
- Gradient of the loss with respect to output y3:
∂ℒ∂y3=y3 -ytarget =0.5716-0.8=-0.2284 - Derivative of the sigmoid function:
σ'(z3)=σ(z3)·(1-σ(z3))=0.5716·(1-0.5716)≈0.2449 - Partial derivative with respect to weight W3:
∂z3∂W3=y2 ≈0.5772
So,
∂ℒ∂W3≈-0.2284·0.2449·0.5772≈-0.0324
Gradient with Respect to W1 (continued):
∂ℒ∂W1=∂ℒ∂y3·∂y3∂z3·∂z3∂y2·∂y2∂z2·∂z2∂y1·∂y1∂z1·∂z1∂W1
Step-by-step:
- Gradient of the loss with respect to output y3: -0.2284
- Derivative of the sigmoid function at layer 3: 0.2449
- Weight of layer 3: W3 =0.5
- Derivative of the sigmoid function at layer 2: 0.244
- Weight of layer 2: W2 =0.5
- Derivative of the sigmoid function at layer 1:
σ'(z1)=σ(z1)·(1-σ(z1))=0.6225·(1-0.6225)≈0.235 - Partial derivative with respect to weight W1:
∂z1∂W1=x0 =1
So,
∂ℒ∂W1≈-0.2284·0.2449·0.5·0.244·0.5·0.235·1≈-0.0082
Step 4: Updating the Weights
Using gradient descent, we update each weight:
Winew =Wiold -η·∂ℒ∂Wi
For each weight:
- W3new =0.5-0.1·(-0.0324)=0.5032
- W2new =0.5-0.1·(-0.0174)=0.5017
- W1new =0.5-0.1·(-0.0082)=0.5008
Step 5: Observe the Gradients Over Iterations
Now, let’s iterate this process a few more times and observe the gradient values.
Second Iteration:
- Forward Pass: Using updated weights W1 =0.5008, W2 =0.5017, W3 =0.5032
- Layer 1:
z1 =W1 ·x0 =0.5008·1=0.5008
y1 =σ(z1)=11+e-0.5008≈0.6226 - Layer 2:
z2 =W2 ·y1 =0.5017·0.6226≈0.3122
y2 =σ(z2)=11+e-0.3122≈0.5774 - Layer 3:
z3 =W3 ·y2 =0.5032·0.5774≈0.2905
y3 =σ(z3)=11+e-0.2905≈0.5721
New Output of the Network:
youtput =y3 ≈0.5721
Calculating the New Loss:
ℒ=12(youtput -ytarget)2 =12(0.5721-0.8)2 ≈0.0260
Backpropagation:
Gradient with Respect to W3:
∂ℒ∂W3=∂ℒ∂y3·∂y3∂z3·∂z3∂W3
∂ℒ∂y3=y3 -ytarget ≈0.5721-0.8=-0.2279
σ'(z3)=σ(z3)·(1-σ(z3))=0.5721·(1-0.5721)≈0.2448
∂ℒ∂W3≈-0.2279·0.2448·0.5774≈-0.0322
Gradient with Respect to W2:
∂ℒ∂W2≈-0.2279·0.2448·0.5032·0.2448·0.6226≈-0.0173
Gradient with Respect to W1:
∂ℒ∂W1≈-0.2279·0.2448·0.5032·0.2448·0.5017·0.2351≈-0.0081
Updating the Weights:
- W3new =0.5032-0.1·(-0.0322)=0.5064
- W2new =0.5017-0.1·(-0.0173)=0.5034
- W1new =0.5008-0.1·(-0.0081)=0.5016
Third Iteration:
- Forward Pass: Using updated weights W1 =0.5016, W2 =0.5034, W3 =0.5064
- Layer 1: z1 =W1 ·x0 =0.5016·1=0.5016 y1 =σ(z1)=11+e-0.5016≈0.6228
- Layer 2: z2 =W2 ·y1 =0.5034·0.6228≈0.3134 y2 =σ(z2)=11+e-0.3134≈0.5777
- Layer 3: z3 =W3 ·y2 =0.5064·0.5777≈0.2923 y3 =σ(z3)=11+e-0.2923≈0.5726
New Output of the Network:
youtput =y3 ≈0.5726Calculating the New Loss:
ℒ=12(youtput -ytarget)2 =12(0.5726-0.8)2 ≈0.0258Backpropagation:
Gradient with Respect to W3:
∂ℒ∂W3=∂ℒ∂y3·∂y3∂z3·∂z3∂W3 ∂ℒ∂y3=y3 -ytarget ≈0.5726-0.8=-0.2274 σ'(z3)=σ(z3)·(1-σ(z3))=0.5726·(1-0.5726)≈0.2447 ∂ℒ∂W3≈-0.2274·0.2447·0.5777≈-0.0321Gradient with Respect to W2:
∂ℒ∂W2≈-0.2274·0.2447·0.5064·0.2447·0.6228≈-0.0172Gradient with Respect to W1:
∂ℒ∂W1≈-0.2274·0.2447·0.5064·0.2447·0.5034·0.2352≈-0.0080Observing the Vanishing Gradient Effect
Now, let’s analyze the gradients after three iterations:
- Gradient ∂ℒ∂W3:
- First iteration: -0.0324
- Second iteration: -0.0322
- Third iteration: -0.0321
- Gradient ∂ℒ∂W2:
- First iteration: -0.0174
- Second iteration: -0.0173
- Third iteration: -0.0172
- Gradient ∂ℒ∂W1:
- First iteration: -0.0082
- Second iteration: -0.0081
- Third iteration: -0.0080
As we can observe, the gradients for the earlier layers (W1 and W2) are getting smaller with each iteration. This is the hallmark of the vanishing gradient problem, where the gradients become so small that the early layers’ weights update very little, leading to ineffective learning.
Mathematical Proof of the Vanishing Gradient Problem
Let’s formalize why the gradients diminish as we propagate them backward through the network. For a deep network, the gradient for a weight in an earlier layer (W1) is given by:
∂ℒ∂W1∝∏k=2L σ'(zk)·∂zk∂yk-1Here, each σ'(zk) is a small number (since for sigmoid activation, σ'(z) is at most 0.25). As we multiply these small numbers together across many layers, the product becomes very small, leading to a tiny gradient.
In our example:
∂ℒ∂W1≈-0.2274·(0.2447·0.5064)·(0.2447·0.5034)·0.2352Each of these terms is small, and as we multiply more of them together (which would happen in a deeper network), the result approaches zero. This explains why the gradient for W1 is much smaller than for W3.
Solution to the Vanishing Gradient Problem
There are several techniques to address the vanishing gradient problem:
- ReLU Activation Function: ReLU has a derivative of 1 for positive inputs, which helps maintain larger gradients. This prevents the gradient from shrinking too much as it propagates through the layers.
- He Initialization: Proper initialization (like He initialization) ensures that the weights start with values that prevent the gradients from vanishing as quickly.
- Batch Normalization: Normalizes the inputs of each layer, ensuring that they have a mean of zero and a variance of one. This helps maintain a consistent gradient size across layers.
- Residual Networks (ResNets): Introduces shortcut connections that allow the gradient to bypass certain layers, which helps preserve the gradient’s magnitude as it propagates backward.
Conclusion
In this example, we’ve seen how gradients can become smaller with each layer during backpropagation, especially in networks using the sigmoid activation function. Although our example used a small network where the problem was not severe, it’s clear that as networks get deeper, the vanishing gradient problem can become a significant issue, leading to ineffective learning.
By using the strategies mentioned, such as ReLU activations, proper initialization, and batch normalization, you can mitigate the vanishing gradient problem and ensure that your deep networks learn effectively, even with many layers.
This completes our detailed exploration of the vanishing gradient problem. By understanding both the theory and practical examples, you are now equipped to recognize and address this issue in your own deep learning models.
Run and inspect the actual calculation
The following complete procedure was executed with TensorFlow 2.21.0 using float64. It checks autodiff against analytic gradients, performs three simultaneous updates, varies the depth of a fixed scalar chain, and verifies that norm clipping leaves tiny gradients unchanged. It does not train a dataset, compare optimizers or test a full residual network. Run in a writable directory with a day20 folder for the JSON output.
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from pathlib import Path
import json, math
import tensorflow as tf
weights = tf.Variable([0.5, 0.5, 0.5], dtype=tf.float64)
rows = []
for step in range(3):
with tf.GradientTape() as tape:
a1 = tf.math.sigmoid(weights[0])
a2 = tf.math.sigmoid(weights[1] * a1)
a3 = tf.math.sigmoid(weights[2] * a2)
loss = 0.5 * (a3 - 0.8) ** 2
grads = tape.gradient(loss, weights)
d3 = float((a3 - 0.8) * a3 * (1 - a3))
d2 = d3 * float(weights[2] * a2 * (1 - a2))
d1 = d2 * float(weights[1] * a1 * (1 - a1))
analytic = [d1, d2 * float(a1), d3 * float(a2)]
assert max(abs(x-y) for x,y in zip(grads.numpy(),analytic)) < 1e-14
rows.append({'step':step+1,'weights':weights.numpy().tolist(),
'activations':[float(a1),float(a2),float(a3)],
'loss':float(loss),'gradients':grads.numpy().tolist()})
weights.assign_sub(0.1 * grads) # One simultaneous update.
depths = []
for depth in [1, 3, 10, 20]:
x = tf.Variable(1.0, dtype=tf.float64)
with tf.GradientTape() as tape:
z = x
for _ in range(depth):
z = tf.math.sigmoid(0.5 * z)
derivative = float(tape.gradient(z, x))
bound = (0.5 * 0.25) ** depth
assert 0 < derivative <= bound
depths.append({'layers':depth,'output_derivative':derivative,'bound':bound})
small = tf.constant([1e-8, -2e-8], dtype=tf.float64)
clipped, norm = tf.clip_by_global_norm([small], 1.0)
assert bool(tf.reduce_all(tf.equal(small, clipped[0])))
result = {'tensorflow':tf.__version__,'steps':rows,
'weights_after_three_updates':weights.numpy().tolist(),
'depth_derivatives':depths,'small_gradients_unchanged_by_clipping':True,
'scope':'Actual float64 TensorFlow autodiff and analytic agreement; controlled scalar chain only. No dataset, model accuracy or general training guarantee.'}
Path('day20/runtime-result.json').write_text(json.dumps(result,indent=2))
print(json.dumps(result,indent=2))
Independent 60-digit Decimal arithmetic, central finite differences and a separate TensorFlow calculation agree with the initial derivatives. Syntax checking is recorded separately from execution. Before adapting the example, compare gradients and update-to-weight ratios across layers and inspect activations; choose a remedy from the observed failure rather than stacking techniques without a diagnostic.
Primary references
- Glorot and Bengio: initialization, saturation and Jacobian scales
- TensorFlow: global norm clipping behavior
- Keras: batch normalization, scale/offset and inference statistics
- He and colleagues: Deep Residual Learning for Image Recognition
Primary references checked 17 November 2026. These sources explain mechanisms and conditions; this lesson does not reproduce their training benchmarks. Continue with the next lesson after tracing the forward and backward paths yourself.
Questions about vanishing gradients
What makes gradients vanish across layers?
Backpropagation multiplies layer derivatives and weight factors. Repeated factors with small norms can attenuate the signal. The whole Jacobian product matters, not the activation derivative alone.
Does a decreasing gradient over training prove vanishing gradients?
No. A gradient may shrink as a model approaches a stationary point. Compare layers at the same step, inspect activations and update sizes, and rule out other causes. The corrected toy example has early-layer magnitudes that rise slightly over its first three steps.
Can gradient clipping enlarge a tiny gradient?
Ordinary norm clipping limits large gradients and leaves sufficiently small ones unchanged. It is useful for exploding gradients; it does not recover information lost to attenuation or saturation.
Does ReLU guarantee that every layer learns?
No. Its derivative is one on positive inputs and zero on negative inputs. Inactive units and weight Jacobian products can still obstruct useful gradients. Initialization and residual design help under suitable conditions, without a universal guarantee.
Does batch normalization always output mean zero and variance one?
No. Training normalization uses batch statistics and epsilon, followed by optional learned scale and offset. Inference uses moving statistics. It can help optimization but does not guarantee a fixed gradient magnitude or solve every failure.
What was actually executed for this lesson?
The new float64 TensorFlow procedure evaluated three simultaneous scalar gradient steps, analytic gradient agreement, controlled depth derivatives and small-gradient clipping. An independent Decimal, finite-difference and TensorFlow check agreed. No dataset training, model accuracy benchmark or ResNet training was performed.
