AI Foundations for Developers COURSE·LESSON 12 OF 410 completed

Backpropagation and Training Stability

Follow responsibility backward

Backpropagation efficiently computes how a final loss depends on each parameter by applying the chain rule through a computation graph. It computes gradients; an optimizer uses those gradients to update parameters. These are distinct operations.

Take a = w×x, prediction = v×a, loss = (prediction-y)². Let x=2, w=1, v=3, y=8. Then a=2, prediction is 6, and loss is 4.

The derivative of loss with respect to prediction is 2(6-8)=-4. Therefore dL/dv = -4×a = -8, and dL/dw = -4×v×x = -24. A shared downstream error contributes differently to each parameter because its path through the graph differs.

Rendering diagram…
flowchart LR
  A[x = 2] --> B[a = w times x]
  B --> C[prediction = v times a]
  C --> D[squared error]
  D -. gradients .-> C
  C -. gradients .-> B

Verify a gradient numerically

python
def loss(w):
    return (3 * (w * 2) - 8) ** 2

eps = 0.00001
numerical = (loss(1 + eps) - loss(1 - eps)) / (2 * eps)
print(round(numerical, 3))  # -24.0

Run this in the Python compiler. Finite differences are useful for checking tiny examples; they are too expensive to replace backpropagation for large models.

Why training becomes unstable

Gradients can shrink through repeated multiplication, leaving earlier layers with weak updates, or grow until updates become destructive. Initialization, residual connections, normalization, suitable activations, and gradient clipping can help. Their roles differ: normalization manages activation statistics; a residual connection creates an additive path for information and gradients.

Dropout randomly disables some activations during training. It acts as regularization; ordinary inference usually disables it. Weight decay discourages overly large weights. Early stopping selects an earlier checkpoint based on validation behavior. None is a substitute for a clean evaluation split.

Training usually needs parameter memory, gradients, optimizer state, and saved activations. Inference does not need all of those, explaining why a model can fit for inference but fail to fit for training on the same device.

Checkpoint with answers

Does automatic differentiation remove the need to choose a loss? No. It differentiates the computation you wrote, even if that computation optimizes the wrong objective.

Why reset gradients between ordinary independent optimizer steps? Frameworks often accumulate gradients; failing to reset them changes the update. Intentional accumulation is valid when configured deliberately.

Learn at your own pace0 / 41 lessons complete