KatInterview

Taming Gradient Decay in a 40-Layer Net and a 100-Step RNN

Machine LearningModel answer
HardModel answerMachine Learningvanishing-gradientsinitializationresidual-connections

Last updated

Explain why gradients shrink or blow up with depth and time, and design a combined fix using activations, initialization, skip connections, normalization, and gated or attention-based sequence models.

Context

A content-recommendation team is building a ranking model over long user histories. They want to grow a fully connected scoring tower from 6 layers to 40 layers. They also want to try a plain unrolled RNN over click sequences of length T = 100.

Both models use sigmoid activations and default weight initialization. In training, the loss flattens out almost immediately. Gradients reaching the earliest layers are many orders of magnitude smaller than gradients at the output side.

What to cover

  1. Derivation. For an L-layer network with h_l = f(W_l h_{l-1} + b_l), write the chain-rule expression for the gradient of the loss with respect to W_1. Define the per-layer Jacobian J_l = ∂h_l / ∂h_{l-1}. Show how the magnitude (e.g. spectral norm) of these Jacobians makes gradients shrink or grow exponentially with L.

  2. Mitigations.

    • Activations and initialization: Compare sigmoid, ReLU, and Leaky ReLU/GELU. State the variance-preservation condition that Xavier/Glorot and He/Kaiming initialization aim for, and explain why each initialization should be matched to its activation.
    • Residual connections: With h_l = h_{l-1} + F(h_{l-1}), derive ∂h_L / ∂h_l and explain why this gives gradients a direct path to early layers.
    • Normalization: Compare BatchNorm and LayerNorm, and explain how normalizing layer inputs stabilizes the backward pass.
  3. Optimization vs. regularization. Classify each technique as mainly helping optimization or generalization, and point out where the two goals conflict.

  4. Feed-forward vs. recurrent. Contrast how the problem appears in a 40-layer feed-forward net and in an RNN unrolled for 100 steps. Explain why initialization alone is usually not enough for a vanilla RNN, and how LSTM/GRU cells or Transformers address it.

Finish with a concrete recommended setup for both models.

Hints

Hint 1

Backprop through depth multiplies one Jacobian per layer, and each Jacobian is diag(f'(z)) times a weight matrix. Bound the product by the product of norms and plug in the sigmoid's maximum slope of 1/4.

Hint 2

For initialization, ask what keeps Var(z) the same from one layer to the next. Count how ReLU changes the second moment of its input, and check what happens when you mismatch the scheme and the activation.

Hint 3

For residual blocks, expand the product of (I + ∂F/∂h) and look at the identity term. For the RNN, notice that the same recurrent matrix appears at every step, so eigenvalues are raised to the power T − k.

Hint 4

For the optimization/regularization split, think about which techniques change the loss landscape and which change the function class or add noise.