interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 7 · ML & applied AI · Lesson 5 of 13

Neural networks, backpropagation and numerical stability

How neural networks compute and learn: layers as differentiable functions, backpropagation as the chain rule organized over a computation graph, and the numerical, initialization and normalization choices that keep training stable. Interviewers use these to check that you can reason below the framework API.
About 4 h 30 minFocus mode

Step 1: Learn the ideas

  • Layers, activation functions and why depth needs nonlinearity
  • Computation graphs and the chain rule
  • Reverse-mode autodiff and tensors saved for backward
  • Gradient checking with finite differences
  • Softmax, log-sum-exp and stable cross-entropy
  • Initialization: Xavier/Glorot and He/Kaiming
  • Batch normalization versus layer normalization and RMSNorm
  • Vanishing and exploding gradients; residual connections
  • Floating-point limits: overflow, underflow, float16 and bfloat16 (awareness)

Why it matters

Frameworks compute gradients for you, but bugs and instabilities live in what they hide: a softmax that overflows, a loss that turns into NaN, gradients that vanish in a deep stack, a batch-norm layer left in training mode at inference. Typical probes in ML engineering and research interviews are a backprop derivation on paper or a "why does this loss become NaN?" question.

Core ideas

Layers and nonlinearity. An MLP alternates affine maps h = W x + b with nonlinearities such as ReLU or GELU. Without the nonlinearities, a stack of linear layers collapses into a single linear map.

Backpropagation. Treat the network as a graph of primitive operations. The forward pass computes outputs and stores what the backward pass will need. The backward pass applies the chain rule from the loss backwards: each node receives the gradient of the loss with respect to its output and multiplies it by its local derivative, a vector-Jacobian product, without ever building a full Jacobian. For Y = X W with X of shape (n, d) and W of shape (d, k): dL/dW = X^T dL/dY and dL/dX = dL/dY W^T; matching shapes tells you where the transposes go. When a value feeds several consumers, its incoming gradients add. Reverse mode costs a small constant multiple of one forward pass however many parameters there are, which is why it suits a scalar loss over millions of weights.

Autograd in practice. PyTorch records operations on tensors that require gradients and saves the tensors each backward formula needs, which is why stored activations are often the largest part of training memory. Modifying a saved tensor in place makes backward fail with an error; torch.no_grad() and inference mode skip graph recording. Gradients accumulate into .grad across backward calls until you zero them.

Gradient checking. Compare analytic gradients with central differences (f(x + h) - f(x - h)) / (2h) in float64, using relative error. It is the fastest way to validate a hand-written backward pass.

Numerical stability. In float32, exp overflows for inputs above roughly 88. Softmax is unchanged by subtracting a constant from every logit, so compute it as exp(z - max(z)) / sum_j exp(z_j - max(z)). For log-probabilities use log_softmax(z) = z - logsumexp(z), where logsumexp(z) = m + log(sum_j exp(z_j - m)) and m = max(z). Prefer the framework's fused cross-entropy on raw logits to taking the log of a softmax output, which can produce log(0); likewise use the with-logits form of binary cross-entropy.

Initialization. Weights that are too large make activations and gradients grow layer by layer; too small, they shrink towards zero. Scaling the weight variance inversely with the number of inputs (or with the average of inputs and outputs) keeps signal variance roughly constant: Xavier/Glorot (Var(w) = 2 / (fan_in + fan_out)) for tanh-like units, He/Kaiming (Var(w) = 2 / fan_in) for ReLU, which zeroes about half its inputs. Initializing every weight in a layer to the same value keeps the units identical forever, so they never learn different features.

Normalization. Batch norm normalizes each feature across the batch (and spatial positions), keeps running statistics for inference, and therefore behaves differently in train and eval mode; it struggles with small or variable batches and with sequences. Layer norm normalizes across the features of each example, so it is independent of batch size and identical at training and inference; transformers use it or RMSNorm, which skips mean-centering. Both learn a scale and usually a shift.

Vanishing and exploding gradients. Backpropagating through many layers multiplies many Jacobians; if their norms sit below or above one, gradients shrink or grow exponentially with depth. Remedies: ReLU-family activations, careful initialization, normalization, residual connections (which give gradients an identity path), and gradient clipping against explosions.

Common misconceptions

  • "Backprop computes the full Jacobian of every layer." It propagates vector-Jacobian products; materializing Jacobians would be far too expensive.
  • "Subtracting the max changes the softmax." It does not, because softmax is shift-invariant; it only prevents overflow.
  • "Batch norm and layer norm are interchangeable." They normalize over different axes and differ in train and eval behavior; batch norm with tiny batches, or evaluated with stale running statistics, is a classic bug.
  • "Initializing all weights to zero is a safe start." It keeps hidden units identical; biases may start at zero, weights may not all be equal.
  • "A NaN loss means the learning rate is too high." Often, but also check log(0), division by a zero variance, float16 overflow and corrupt inputs.

How to approach it

Derive a two-layer MLP backward pass on paper, implement it in NumPy and gradient-check it. Build a tiny autograd engine to understand graph recording and gradient accumulation. Read the framework notes on what is saved for backward. In interviews, write shapes next to every equation and present stability tricks as consequences of floating-point limits, not as rules to memorize.

Suggested order

  1. 1Read the CS231n notes "Backpropagation, Intuitions" and redo the staged-computation examples on paper.
  2. 2Read D2L 5.3 (Forward Propagation, Backward Propagation, and Computational Graphs) and 5.4 (Numerical Stability and Initialization).
  3. 3Read the PyTorch "Autograd mechanics" notes on saved tensors, in-place operations, gradient accumulation and no-grad and inference modes.
  4. 4Read Deep Learning Ch. 4 (Numerical Computation) on overflow, underflow and stabilizing softmax.
  5. 5Read the CS231n notes "Neural Networks Part 2" sections on weight initialization and batch normalization, then D2L 8.5 (Batch Normalization).
  6. 6Rebuild micrograd from scratch and gradient-check a small MLP; then do the CS231n assignment parts on fully connected networks, batch normalization and dropout.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Stanford CS231n Deep Learning for Computer VisionStart here

    Stanford University

    Section: Course notes: Backpropagation, Intuitions

    Clear derivations of backprop through staged computations, gradient flow patterns and vectorized gradients.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Dive into Deep Learning

    d2l.ai (open-source book)

    Section: 5.4. Numerical Stability and Initialization

    Vanishing and exploding gradients, symmetry breaking and Xavier initialization with runnable code; read 5.3 on computational graphs first.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • PyTorch: Autograd mechanicsStart here

    PyTorch documentation

    How autograd records the graph, saves tensors and handles in-place operations; explains many real gradient errors.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Deep Learning

    MIT Press (free online edition at deeplearningbook.org)

    Section: Ch. 4: Numerical Computation

    Overflow, underflow, the softmax stabilization argument and poor conditioning.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Build micrograd yourself before reading the reference code: a Value class with operator overloading, a topological sort and per-operation backward functions; train a two-layer MLP with it and check every gradient against PyTorch. In the CS231n assignments, implement the affine, ReLU, softmax and batch-norm backward passes and pass the provided numerical gradient checks. Finally, write a naive softmax that overflows on logits near 1000, then fix it with log-sum-exp and compare with the library function.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Why does reverse-mode autodiff suit a scalar loss with many parameters?
  2. Question 2What does PyTorch save during the forward pass, and why does that dominate training memory?
  3. Question 3Why is log(softmax(z)) numerically worse than log_softmax(z)?
  4. Question 4What goes wrong if every weight in a layer starts with the same value?
  5. Question 5Why do transformers use layer normalization rather than batch normalization?
  6. Question 6How do residual connections help gradients flow through deep networks?

You’re done when you can

  • Derive the gradients of a linear layer and of softmax cross-entropy with shapes in under ten minutes.
  • Implement a stable log-softmax and verify it against a library implementation on extreme inputs.
  • Pass numerical gradient checks for a hand-written two-layer MLP.
  • Explain batch norm versus layer norm, including train and eval behavior, without notes.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: Training loops, optimizers and model debugging, Transformers, attention and language-model objectives, GPU architecture, memory hierarchy and arithmetic intensity, PyTorch and autograd fluency

Your notes and study time

Log study time