Training Neural Networks

Last updated October 16, 2026

David I. Inouye

We still use gradient descent—but where do the gradients come from?

\[\boldsymbol\theta\leftarrow\boldsymbol\theta-\eta\nabla_{\boldsymbol\theta}J.\]

The parameters now sit inside a composition of many layers.

Backpropagation computes \(\nabla_{\boldsymbol\theta}J\) efficiently. Gradient descent (or Adam) uses those gradients to update the parameters.

Training loop: forward computation → backpropagation → parameter update.

Backpropagation evaluates the chain rule from the loss backward

For \(a=f(\theta)\), \(b=g(a)\), and \(\ell=q(b)\):

\[\frac{\partial\ell}{\partial\theta} =\frac{\partial\ell}{\partial b}\, \frac{\partial b}{\partial a}\, \frac{\partial a}{\partial\theta}.\]

Forward: \(\theta\to a\to b\to\ell\); save the values needed by the derivatives.

Backward: start with \(\frac{\partial\ell}{\partial\ell}=1\), then compute

\[\frac{\partial\ell}{\partial b}=\frac{\partial q(b)}{\partial b},\qquad \frac{\partial\ell}{\partial a}=\frac{\partial\ell}{\partial b}\,\frac{\partial b}{\partial a},\qquad \frac{\partial\ell}{\partial\theta}=\frac{\partial\ell}{\partial a}\,\frac{\partial a}{\partial\theta}.\]

Same chain rule, reversed evaluation order. We need the forward output to evaluate the loss’s derivative, and saved intermediate values to evaluate the local derivatives.

Backward computation sums paths at hidden features

Two weight matrices and one scalar sum of losses

Fixed input vector one and two

Hidden vector three and five

Two output scores thirteen and eleven

Scalar loss two point five

Full chain rule for the first score derivative

Full chain rule for the second score derivative

Output parameter gradient chain rule and outer product

Total derivative of hidden feature one sums both output paths

Total derivative of hidden feature two sums both output paths

Full chain rule through the saved active ReLU branches

First-layer parameter gradient chain rule and outer product

Matrix summary; gradients stop at trainable parameters

Training repeats forward, backward, and parameter updates

For a batch \(\mathcal B\) of \(B\) examples, use the mean loss:

\[J_{\mathcal B}(\boldsymbol\theta)=\frac1B\sum_{i\in\mathcal B}\ell_i(\boldsymbol\theta), \qquad \nabla_{\boldsymbol\theta}J_{\mathcal B}=\frac1B\sum_{i\in\mathcal B}\nabla_{\boldsymbol\theta}\ell_i.\]

  1. Start: initialize the parameters once; choose a batch and clear old gradient accumulators.
  2. Forward: compute predictions and the batch loss; save values needed for backward.
  3. Backward: accumulate gradients for all trainable parameters, using the same parameter values.
  4. Update: \(\boldsymbol\theta\leftarrow\boldsymbol\theta-\eta\nabla_{\boldsymbol\theta}J_{\mathcal B}\); repeat with a fresh forward pass.

An epoch visits the training set once. Monitor validation performance across epochs to choose when to stop.

Libraries combine these rules to differentiate the whole network

  1. Forward: record the operations and retain the values their backward rules need.
  2. Backward: start at the scalar loss, apply local rules in reverse order, and sum contributions from shared paths.
  3. Update: use the resulting gradient for every trainable parameter.

For an MLP with biases, that includes \(W,\boldsymbol b,V,\boldsymbol c\).

We specify the model and loss; automatic differentiation organizes the chain rule.

Next topic: implement this training cycle in PyTorch.

Backpropagation computes all parameter gradients efficiently

In 1D: \(\displaystyle\frac{\partial J}{\partial\theta}\approx\frac{J(\theta+\epsilon)-J(\theta)}{\epsilon}\).

One objective evaluation takes 1 second. Reuse the unperturbed value \(J(\boldsymbol\theta)\).

Finite differences: about how long for 10,000 parameters?

A. 2 seconds
B. 100 seconds
C. 10,000 seconds

C: 10,000 parameters → about 10,000 seconds.
One new evaluation per parameter.

Backpropagation: how long for the same full gradient?

Assume backward ≈ forward cost.

A. 2 seconds
B. 100 seconds
C. 10,000 seconds

A: about 2 seconds for forward + backward.
One reverse pass reuses work for all parameters.