Bias-Variance, Normalization Axes, and Gradient Decay in Deep Nets
Machine LearningModel answerLast updated
Explain the bias-variance tradeoff, compare batch and layer normalization, and describe why gradients vanish in deep networks and how architecture can prevent it.
Three Deep Learning Foundations
Imagine you are walking a teammate through the ideas that most affect how neural networks are built and tuned. Give a clear, compact explanation of each topic below, including the underlying reasoning and when it matters in practice.
Part A: Error Decomposition
Describe the bias-variance tradeoff for a supervised predictor.
- What happens to bias and to variance as a model gets more flexible?
- Looking at training and validation error (including how they change with dataset size), how would you tell whether a model suffers mainly from high bias or high variance, and what would you do in each case?
Part B: Batch Norm vs. Layer Norm
Both layers rescale activations to stabilize optimization, yet they compute their statistics over different dimensions.
- Compare which axes the mean and variance are taken over in each method, for both image tensors and sequence tensors.
- In which situations would you choose layer normalization over batch normalization, and why?
Part C: Vanishing Gradients
- Define the vanishing gradient problem.
- Explain, using the chain rule and the shape of the activation function's derivative, why it arises in backpropagation.
- Propose two architectural remedies and explain why each helps.
Expect to be pushed on follow-ups, such as how weight initialization interacts with gradient flow, why batch norm tracks running statistics, and whether more complexity can ever reduce both bias and variance.
Hints
Hint 1
For Part A, write the expected squared error at a point as three terms: squared bias, variance across training sets, and irreducible noise. Then ask which terms move when capacity grows.
Hint 2
For Part B, list the tensor shape first (batch, channels, height, width, or batch, tokens, features) and name which axes are averaged for each method. Then ask which method needs other examples in the batch.
Hint 3
For Part C, write the gradient to an early layer as a product of per-layer Jacobians and check how big each factor is for sigmoid. Then ask which change keeps a factor near the identity.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Overview
A strong answer gives precise definitions, a short piece of math for each idea, and a practical rule for when it matters.
A. Bias-Variance Tradeoff
Setup. Assume y = f(x) + ε with zero-mean noise of variance σ². Train a model f̂ on a random dataset D. The expected squared error at x, averaged over datasets and noise, splits into:
Bias² + Variance + σ²
- Bias is error from wrong or overly rigid assumptions, such as a straight line fit to curved data.
- Variance is how much the fitted function changes when the training sample changes.
- σ² is irreducible noise, a floor for every model.
Complexity. In the classical regime, more capacity lowers bias (the model can express f) but raises variance (it starts fitting noise). Training error keeps falling, while validation error is U-shaped.
Diagnosis.
| Pattern | Train error | Val error | Gap | Remedies |
|---|---|---|---|---|
| High bias | High | High | Small | Larger model, better features, weaker regularization, longer training |
| High variance | Low | High | Large | More data, augmentation, L2/dropout, early stopping, smaller model |
Learning curves against dataset size help further. If both curves have flattened at a high error, more data won't help (bias). If a wide gap is still closing as data grows, more data will help (variance).
B. BatchNorm vs. LayerNorm
Both compute x̂ = (x − μ)/√(σ² + ε), then y = γx̂ + β with learned γ and β. The only difference is the axes used for μ and σ².
| BatchNorm | LayerNorm | |
|---|---|---|
| Image (N, C, H, W) | Per channel, over N, H, W | Per sample, over C, H, W |
| Sequence (N, T, D) | Per feature, over N (and T) | Per token, over D |
| Uses other samples | Yes | No |
| Train vs. inference | Differ (batch vs. running stats) | Same computation |
| Batch-size sensitivity | High for small batches | None |
Prefer LayerNorm when:
- Working with sequence models. Lengths vary, padding pollutes batch statistics, and RNNs would need per-timestep statistics. Per-token normalization avoids all this.
- Batches are tiny or variable (large models, streaming inference, reinforcement learning).
- You want to avoid cross-device synchronization of statistics.
Prefer BatchNorm when: training CNNs for vision with decent batch sizes. Per-channel statistics suit convolutions, batch noise adds mild regularization, and at inference BN folds into the preceding conv layer at no cost.
C. Vanishing Gradients
Definition. Gradients shrink roughly exponentially as they travel back toward early layers, so those layers learn very slowly. The opposite case, exponential growth, is the exploding gradient problem.
Cause. With h_l = σ(z_l) and z_l = W_l h_{l−1}, the chain rule gives
∂L/∂h_1 = ∂L/∂h_L · ∏ [diag(σ′(z_l)) · W_l]
so the gradient is a product of L Jacobians. For a sigmoid, σ′(z) = σ(z)(1 − σ(z)) never exceeds 0.25 and approaches 0 when |z| is large (saturation). With moderate weights, each factor has norm below 1 and the product scales like (0.25·‖W‖)^L. For 20 layers, 0.25²⁰ ≈ 10⁻¹². Tanh peaks at 1 but also saturates. Vanilla RNNs suffer the same effect across time steps.
Two architectural fixes.
- ReLU-type activations (ReLU, Leaky ReLU, GELU). ReLU's derivative is exactly 1 for positive inputs, so active paths do not shrink the gradient. Leaky or smooth variants reduce dead units.
- Residual connections. For h_{l+1} = h_l + F(h_l), the Jacobian is I + ∂F/∂h_l. The identity term gives gradients a direct route to early layers, which is why very deep ResNets and Transformers train.
Other helpful tools: normalization layers (keep pre-activations out of saturation), LSTM/GRU gating (an additive cell path acts like a skip connection over time), and gradient clipping (for the exploding case).
Follow-ups
Initialization. Initialization sets the scale of each W_l in the Jacobian product; the goal is to keep activation and gradient variance roughly constant across layers.
- Xavier/Glorot: Var(W) = 2/(fan_in + fan_out), suited to tanh-like activations near their linear region.
- He: Var(W) = 2/fan_in, which compensates for ReLU zeroing about half the inputs.
Xavier with a deep ReLU network makes signals shrink layer by layer. Overly large weights saturate sigmoid/tanh (vanishing) or blow up ReLU nets (exploding). Initialization fixes the start of training; skips and normalization maintain it afterward.
Running statistics in BatchNorm. At inference the batch may be a single example, and outputs must be deterministic and independent of batch-mates. So BN keeps an exponential moving average during training, μ_run ← (1 − m)μ_run + m·μ_batch (same for variance), and uses it at test time. Common mistakes: forgetting to switch to eval mode, and stale statistics under distribution shift.
Can complexity reduce both bias and variance? The U-curve describes the classical, under-parameterized regime with fixed data. Two exceptions:
- Double descent: beyond the interpolation threshold (parameters ≈ samples), test error can fall again as width grows, since SGD tends toward smooth, low-norm solutions, and variance from initialization and sampling shrinks with width.
- Better inductive bias: moving from an MLP to a CNN adds structured capacity. Bias drops because spatial patterns are representable, and variance drops because weight sharing and locality constrain the hypothesis space.
Common Mistakes
- Saying "more complexity always means more variance" without mentioning exceptions.
- Describing BatchNorm and LayerNorm as normalizing different things rather than the same formula over different axes.
- Blaming vanishing gradients on "small learning rates" instead of the Jacobian product.
- Claiming ReLU eliminates the problem entirely, ignoring dead units and weight scale.