Neural Networks
Advanced 15 min read Neural Networks

Backpropagation & Learning

How the model actually learns

Backpropagation is the chain rule applied to networks: compute loss, then derive each weight's gradient layer by layer, moving backward. It is why 'learning' works, and understanding it demystifies training failures.

backpropagationgradient descentchain rulelearning rateoptimizer
Prerequisites: Activation Functions
Before we start

What you will learn

  • Explain gradient descent and the role of the chain rule
  • Identify exploding/vanishing gradient pathologies
  • Read a learning-rate failure from loss curves
Introduction

What is this?

Backpropagation computes, for every weight, how much a tiny change would reduce the loss — via the chain rule propagated backwards through the network. Gradient descent then nudges weights opposite those gradients. Training failure modes (exploding or vanishing gradients, too-high or too-low learning rate) all live in this single mechanism.

Motivation

Why do you need it?

  • It is the exact reason modern ML trains at all — no training, no AI.
  • Exploding/vanishing gradients explain most deep-training blowups you will debug.
  • Learning-rate pathologies are the most common silent training failure.
  • LLM fine-tuning inherits all of it: a bad LR wrecks LoRA adapters the same way.
Think about it

Mental model

Imagine

Descending a mountain in fog

Think of

Gradient descent

You can't see the valley, so you feel which way the ground drops sharply and step that way. Descent with a too-big stride overshoots the slope; with a too-small stride you camp on a shelf forever. Backprop is that sense of direction; learning rate is your stride.

Mechanics

How it works

  • The loss summarizes how wrong predictions are on a batch.
  • The chain rule flows error derivatives from output back through each layer's weights.
  • An optimizer (SGD, Adam) updates weights using gradient + learning-rate + momentum.
  • Repeated batches/epochs lower loss; validation loss tracks generalization.
  • Monitoring gradient norms exposes vanished or exploded flows early.

Forward, then backward, then step.

Forward
Backward
Update
Repeat
Real world

In production

  • Fine-tuning a small adapter with AdamW and a low LR — the success of PEFT/LoRA depends on gradient stability.
  • Layer-freezing (training only the head) relies on gradients stopping at the frozen boundary.
  • Gradient-norm monitoring in training dashboards catches instability before loss looks bad.
Guidelines

Best practices

  • Track gradient norms on every training run.
  • Warm up the learning rate at the start (LR warm-up).
  • Pretesting Adam with LR ~1e-4..3e-4 is a sane default for fine-tuning.
  • Validate that gradients actually change parameters (a dry-run one batch).
Watch out

Common mistakes

  • Blindly trusting 'it trained' without looking at loss AND gradient curves.
  • Fine-tuning a huge model with LR meant for full training — guarantees instability.
  • Reading a flat loss as 'converged' when it is a stuck shelf from too-small LR.
Root cause analysis

Common issues

Error
Loss goes to NaN mid-training
Root cause

Exploding gradients through unstable layers

Fix

Lower LR, add gradient clipping, improve normalization/init.

Error
Training never moves the model
Root cause

Vanishing gradients or frozen-feature wiring

Fix

Check gradient norms, unfreeze/pass gradients, use modern activations.

Error
Validation loss rises after few steps
Root cause

Learning rate too high for the data/scale

Fix

Reduce LR, shorten warm-up, and watch a LR-vs-loss sweep.

When things break

Debugging tips

  • Loss unchanged but gradients near zero → vanishing flow or plateau.
  • Loss nan becomes gradient norm exploding — clamp/renormalize or lower LR.
  • Validation diverges while train improves → LR too high or batch too small.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

What does the learning rate control?

2

Gradient norms should be monitored during training.

Go deeper

Official documentation