This is a great question about vanishing gradients in deep neural networks. Let me break this down systematically.
The Vanishing Gradient Problem
Sigmoid
The user is correct about the sigmoid derivative. Let me verify:
- Sigmoid: σ(x) = 1/(1+e^(-x))
- Derivative: σ'(x) = σ(x)(1-σ(x))
- Maximum occurs when σ(x) = 0.5, giving σ'(x) = 0.25
So when using backpropagation through a sigmoid, gradients are multiplied by values ≤ 0.25 at each layer.
Tanh
- Tanh: tanh(x) = (e^x - e^(-x))/(e^x + e^(-x))
- Derivative: tanh'(x) = 1 - tanh(x)^2
- Maximum occurs when tanh(x) = 0, giving tanh'(x) = 1
- But it still ranges from 0 to 1
So while tanh is better than sigmoid (max of 1 vs 0.25), it still has the vanishing gradient problem.
The Chain Rule Issue
When backpropagating through L layers:
∂L/∂W₁ = (∂L/∂a_L) × (∂a_L/∂a_{L-1}) × ... × (∂a_2/∂a_1) × (∂a_1/∂W_1)
If each of those activation derivatives is ≤ 0.25 (sigmoid), then:
∂L/∂W₁ ≈ 0.25^L × ... (other terms)
For L=100 layers: 0.25^100 ≈ 10^(-60) — essentially zero!
Even with tanh where max is 1, if the typical magnitude is around 0.1-0.5, then multiplying through many layers still compounds.
ReLU as a Solution
ReLU addresses this by having a derivative of exactly 1 for positive activations. During backpropagation through active neurons, gradients pass through unchanged rather than being scaled down by small values. This preserves gradient magnitude across many layers. However, ReLU introduces the dying ReLU problem where neurons can become permanently inactive, and negative inputs still produce zero gradients.
Residual Connections
Skip connections fundamentally change how gradients flow backward. Instead of purely stacking transformations, residual blocks add the input directly to the output: a_{l+1} = a_l + f(W_l a_l + b_l). During backpropagation, the gradient to layer l becomes ∂L/∂a_l = ∂L/∂a_{l+1} × (1 + ...), meaning gradients always have at least a direct path backward through the identity connection, regardless of what the learned transformation contributes.
This additive structure is the key difference from multiplicative chaining. Even when the residual block's gradient is negligible, the +1 term ensures signals can still flow to earlier layers. This enables training networks with 150+ layers by providing an alternative route that doesn't depend on the depth of the network, allowing gradients to maintain roughly constant magnitude as they propagate backward.
Vanishing Gradients: Causes and Solutions
You've identified the core issue correctly. Let me walk through the mechanism and explain how modern architectures fix it.
The Vanishing Gradient Chain Rule Problem
Sigmoid/Tanh Multiplication Chain
During backpropagation through L layers:
∂L/∂W₁ = (∂L/∂aₗ) × (∂aₗ/∂aₗ₋₁) × (∂aₗ₋₁/∂aₗ₋₂) × ... × (∂a₁/∂W₁)
With sigmoid (max derivative 0.25):
∂L/∂W₁ ≈ 0.25ᴸ × (other terms)
For L=100: 0.25¹⁰⁰ ≈ 10⁻⁶⁰ — effectively zero!
Tanh is better (max derivative 1.0), but typical magnitudes still range 0.1-0.5, causing geometric decay.
How ReLU Solves This
The Key: Derivative = 1 on the Active Path
# ReLU activation and derivative
ReLU(x) = max(0, x)
d/dx ReLU(x) = { 1 if x > 0
{ 0 if x < 0
Critical insight: For positive activations, the gradient passes through unchanged:
∂L/∂W₁ = (∂L/∂aₗ) × 1 × 1 × ... × 1 × (∂a₁/∂W₁)
Without multiplicative decay!
Why This Enables Deep Networks
- Before (sigmoid): 100 layers → gradient ≈ 0.25¹⁰⁰
- After (ReLU): 100 layers → gradient ≈ 1¹⁰⁰ = 1 (on active paths)
Trade-off: You lose gradients for negative activations (dying ReLU problem), but the active path through the network preserves information flow.
How Residual Connections Solve This More Elegantly
The Skip Connection Addition
Standard layer:
aₗ₊₁ = f(Wₗ aₗ + bₗ)
Residual block:
aₗ₊₁ = aₗ + f(Wₗ aₗ + bₗ)
Gradient Flow Through Residuals
Backpropagating through the residual block:
∂L/∂aₗ = ∂L/∂aₗ₊₁ · (1 + ∂f/∂aₗ)
↑
Identity term — critical!
Why this matters:
- If the function block's derivative vanishes:
∂f/∂aₗ ≈ 0
- Gradient still flows via:
∂L/∂aₗ ≈ ∂L/∂aₗ₊₁ · (1 + 0)
- The "+1" creates a direct gradient pathway independent of depth
Comparison Across Depths
Without residuals: grad ≈ (f'₁ × f'₂ × ... × f'₁₅₀) → exponential decay
With residuals: grad ≈ 1 + (f'₁ × f'₂ × ...) → gradient persists
This allowed ResNet to train 150+ layer networks effectively—impossible before.
Side-by-Side Comparison
| Activation | Max Gradient | Problem | Solution |
|---|
| Sigmoid | 0.25 | Multiplicative decay | Use ReLU |
| Tanh | 1.0 | Often saturates (~0.1-0.5 range) | Use ReLU |
| ReLU | 1.0 | Dying ReLU (negative paths) | Use residuals + batch norm |
| ReLU + Residual | 1.0 + bypass | None! | ✓ enables very deep nets |
Practical Takeaway
Modern best practice combines both:
# ResNet block
x_input = x
x = Conv2d(x)
x = BatchNorm(x)
x = ReLU(x)
x = Conv2d(x)
x = BatchNorm(x)
x = x + x_input # Residual connection
x = ReLU(x)
- ReLU: Keeps gradients from multiplying by <1 on active paths
- Residuals: Ensure gradients flow via identity bypass even if learned function is weak
- Batch norm: Also stabilizes gradient flow (bonus effect)
Together, these enable training of networks with 1000+ layers.