Underfitting Shallow Nets and Dying Gradients in Deep Sigmoid MLPs
Machine LearningModel answerLast updated
Analyze why a shallow network underfits and a very deep sigmoid network fails to train, using the bias-variance decomposition, the backprop chain rule, ReLU and residual connections.
Context
You are building a fully connected network that predicts the probability a user saves an item, P(save | user, item). All hidden layers use sigmoid activations. Sweeping the depth from 2 to 20 layers, you see two separate problems:
- Shallow models (2-3 layers) converge quickly, but their training loss stays high and the test metric is poor.
- Very deep models (15-20 layers) barely train. The weights in the earliest layers stay close to their initial values.
Give a theoretical explanation and a practical diagnosis for both.
Part 1: Bias and variance
- Write the decomposition of expected squared error at a point
xfor an estimatorf_hat(x), when the target isy = f(x) + epsilon, withepsilonzero-mean noise of variancesigma^2. - Which failure mode does the shallow network show, in bias/variance terms? Justify using the observed training and test behavior, and say what you would change.
- Very large networks can fit the training data perfectly and still generalize well ("double descent"). Explain conceptually why this does not match the classical U-shaped expectation.
Part 2: Vanishing gradients
Consider an L-layer network with, for layer l:
z^(l) = W^(l) a^(l-1) + b^(l)a^(l) = sigma(z^(l)), witha^(0) = xandsigma(z) = 1 / (1 + exp(-z))applied element-wise.
- Using the chain rule, write the gradient of the scalar loss with respect to the first-layer weights,
dLoss/dW^(1). - State the maximum of
sigma'(z). Show mathematically why repeated multiplication by this factor makes the gradient shrink exponentially withL. - Compare two remedies: (a) replacing sigmoid with ReLU, and (b) adding residual connections of the form
a^(l) = sigma(z^(l)) + a^(l-1). Explain how each one keeps the gradient from vanishing, and what limitations each has.
Answer in a structured way, with equations where they help.
Hints
Hint 1
Training loss is the key observation for Part 1: variance appears only as a gap between train and test performance, so ask whether that gap exists here.
Hint 2
For the gradient, define an error signal delta^(l) = dLoss/dz^(l) and write the recursion delta^(l) = diag(sigma'(z^(l))) (W^(l+1))^T delta^(l+1), then unroll it down to layer 1 and finish with the transpose of the input.
Hint 3
Sigmoid's derivative is sigma(z)(1 - sigma(z)); find where it peaks, then bound the product of L such factors. For the remedies, look at the per-layer Jacobian: diag(sigma') W for the plain network versus I + diag(sigma') W with the skip connection.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Part 1: Bias and variance
1. Decomposition
Fix a test point x. Take the expectation over random training sets D and over the noise epsilon:
E[(y - f_hat(x))^2] = (E_D[f_hat(x)] - f(x))^2 (bias squared)
+ E_D[(f_hat(x) - E_D[f_hat(x)])^2] (variance)
+ sigma^2 (irreducible noise)
To derive it, write y - f_hat = (f - E f_hat) + (E f_hat - f_hat) + epsilon, square, and take expectations. The cross terms vanish because E[epsilon] = 0 with epsilon independent of D, and because E_D[E f_hat - f_hat] = 0. The term sigma^2 is a floor no model can beat.
2. The shallow network is high bias (underfitting)
The deciding evidence is that the training loss is high. Variance shows up as a gap between training and test error, and a model that cannot fit the data it sees is not overfitting. The picture is consistent with a small hypothesis class: fast convergence means the optimizer quickly reaches the best the class can do, and that best is poor. Saving behavior depends on high-order user-by-item interactions that a shallow, saturating network cannot express at its width.
Fixes target bias:
- more depth or width,
- richer features or explicit feature crosses,
- less regularization, a better optimizer or longer training.
More data and dropout mainly reduce variance, so they would not help much here.
3. Double descent
The classical view says test error is U-shaped in complexity, so an interpolating model (zero training error) should be in the high-variance regime. Double descent shows:
- Test error peaks near the interpolation threshold, where parameters
pare roughly the number of samplesn. - It then falls again as
p >> n.
The explanation is that parameter count is not effective complexity.
- Near
p ~ nthere is essentially one function that interpolates the data, and fitting the noise forces it to be wild (large norm), so variance blows up. - For
p >> nthere are many interpolators. Gradient-based training has an implicit bias toward low-norm, smooth solutions (for linear models it finds the minimum-norm interpolator), so the solution it picks is comparatively simple and variance decreases withp.
The tradeoff is not repealed. Capacity is better measured by norm or smoothness of the learned function than by raw parameter count.
Part 2: Vanishing gradients
1. Chain rule
Let delta^(l) = dLoss/dz^(l) and D^(l) = diag(sigma'(z^(l))). Then:
delta^(L) = D^(L) * grad_{a^(L)} Loss
delta^(l) = D^(l) * (W^(l+1))^T * delta^(l+1)
dLoss/dW^(1) = delta^(1) * x^T
Unrolled:
dLoss/dW^(1) = [ D^(1) W^(2)^T D^(2) W^(3)^T ... W^(L)^T D^(L) grad_{a^(L)} Loss ] x^T
It is a product of L derivative matrices and L-1 transposed weight matrices.
2. Exponential decay
sigma'(z) = sigma(z)(1 - sigma(z)) peaks at z = 0, where it equals 1/4. So ||D^(l)||_2 <= 1/4, and by submultiplicativity:
||dLoss/dW^(1)|| <= (1/4)^L * prod_{l=2..L} ||W^(l)|| * ||grad_{a^(L)} Loss|| * ||x||
With ordinary initializations (||W|| <= ~1) this shrinks like 4^(-L). At L = 20, 4^(-20) is about 10^(-12), so early layers receive essentially no signal.
The bound is optimistic, since pre-activations that are not near zero saturate the sigmoid (sigma'(5) is about 0.0066). Scaling up the weights does not rescue it: cancelling the 1/4 needs ||W|| ~ 4, but larger weights push |z| up and drive units into saturation, where sigma' falls toward 0. The two factors fight each other.
3. ReLU versus residual connections
ReLU. Its derivative is 1[z > 0], so D^(l) is a 0/1 mask. On active units the per-layer factor is exactly 1 instead of at most 1/4, and there is no saturation for positive inputs. The gradient's scale is then set by the weights, which He initialization keeps near 1 in variance. Limitations: the protection is conditional. Gradients can still vanish or explode through the weight products, and units with z <= 0 pass no gradient (dead ReLUs).
Residual connections. With a^(l) = sigma(z^(l)) + a^(l-1), the layer Jacobian is I + D^(l) W^(l). So:
dLoss/da^(l) = dLoss/da^(L) * prod_{k=l+1..L} (I + D^(k) W^(k))
= dLoss/da^(L) * (I + sum_k D^(k) W^(k) + higher-order terms)
The expansion contains a direct identity term that passes through no weights and no sigma'. Even if every D^(k) W^(k) is tiny, the gradient reaching early layers stays about as large as the gradient at the top. The first-layer weight gradient then carries only its own local sigma' factor, not L of them. Limitations: layer dimensions must match (or use a projection on the skip), and very deep unnormalized stacks can still have growing activations, so residuals are normally paired with normalization.
| ReLU | Residual | |
|---|---|---|
| Mechanism | Per-layer derivative equals 1 on active units | Additive identity path in the Jacobian |
| Depends on weight scale? | Yes | Not for the identity term |
| Needs | Careful initialization | Matching dimensions or projection |
In practice they are combined, usually with normalization layers and a sensible initialization (Xavier for tanh/sigmoid-like, He for ReLU).
Common mistakes
- Calling the shallow net high variance even though its training loss is high.
- Leaving out
sigma^2, or folding it into the bias. - Saying double descent "breaks" the tradeoff, rather than showing parameter count is a poor proxy for complexity.
- Stating the maximum of
sigma'as 1/2, or at a point other thanz = 0. - Claiming ReLU guarantees no vanishing gradient.
- Dropping the trailing
x^Tor the transposes and ordering in the chain-rule product.