Skip to content

Training Deep Networks

Choose step sizes, adapt them, normalize activations, and regularize the model without fooling yourself

Quick recap
  • Training is optimization: follow a noisy gradient without overshooting or crawling.
  • Schedules, momentum, and Adam change how the step size behaves over time and per parameter.
  • Initialization, normalization, dropout, and validation decide whether the optimization is usable.

This lesson follows Backpropagation and leads into Convolutional Neural Networks. The numerical background is in Numerical Methods.

1. The training picture

Your training loss falls for a while, then starts jumping between 0.4 and 1.2. Is the model broken, or is the step too large?

Backpropagation gives you a gradient. Training turns that gradient into a sequence of parameter updates. Suppose a weight is 1, its gradient is 0.1, and the learning rate is 0.1. The update gives 1 − 0.1(0.1) = 0.99. In general, the simplest rule subtracts a learning-rate multiple of the gradient:

Gradient descent

Here w is the parameter, η is the learning rate, and ∇L(w) is the gradient. A step can be too small, too large, or poorly scaled across parameters. Deep networks make all three failure modes visible: some weights receive tiny gradients, some receive huge ones, and the curvature — how quickly the slope changes — changes as the network moves.

The animation compares five one-dimensional schedules on the same quadratic loss. Watch how the red curve oscillates while the green curve settles.

2. Learning-rate schedules

Try it

Validation loss improves quickly, then bounces around the same value. Should you raise the learning rate, lower it, or leave it?

A schedule makes the step size a function of the step. Step decay multiplies the rate by a factor every fixed number of steps:

Step decay

Here η_t is the rate at step t, η_0 is the initial rate, d is the drop factor, and s is the interval in steps.

Cosine decay decreases smoothly from an initial rate to a floor:

Cosine decay

η_min is the floor and T is the total number of steps.

The intuition is simple: take bigger steps while the model is far from a good region, then smaller steps when it is trying to settle. A schedule can also be driven by validation loss: reduce the rate when improvement stalls.

Warmup does the opposite at the start: it raises the rate gradually so a randomly initialized network is not hit by a huge update. The right schedule is empirical, not universal.

3. Momentum and Adam

Momentum accumulates a moving average of gradients, so consistent directions build speed and oscillating directions cancel. Adam also tracks a moving average of squared gradients, giving each parameter its own effective step size. With g_t = 0.1, β₁ = 0.9, and β₂ = 0.999, the first step gives m = 0.01 and v = 0.00001.

Adam first moment
Adam second moment

Here g_t is the gradient at step t, and β₁ and β₂ control how quickly the two averages forget old values. The first and second moments are biased toward zero at the start. Bias correction divides by 1 − β^t:

Adam update

m̂_t and v̂_t are the bias-corrected moments, and ε is a small constant that keeps the division stable.

Adam is a strong default, not a guarantee. It can generalize slightly worse than well-tuned SGD with momentum on some vision tasks, and it still has a learning rate to choose.

Try it

Why does Adam divide by a moving average of squared gradients?

4. Initialization and norm

Initialization controls the scale of activations and gradients. If every hidden unit starts with the same weights, symmetry never breaks. If the scale is too large, tanh saturates and ReLU gates die; if it is too small, the signal fades layer by layer.

Xavier/Glorot initialization scales weights by the number of inputs and outputs connected to a unit (the fan-in and fan-out); He initialization is tuned for ReLU. The details differ, but the goal is the same: keep variance roughly stable through the forward and backward passes.

Batch normalization standardizes activations across the batch and then applies a learned scale and shift. For the batch (1, 2, 3), the mean is 2 and the variance is 2/3; the normalized values are about −1.22, 0, and 1.22.

Batch normalization

Here μ_B and σ_B² are the batch mean and variance, γ and β are the learned scale and shift, and ε is a small constant for stability. This can make optimization easier, but it couples examples in a batch and behaves differently at training and inference time. Running statistics are used at inference; small batches make those statistics noisy.

5. Dropout and regularization

Dropout randomly removes units during training and rescales the survivors. With p = 0.5, a kept unit is multiplied by 2.

Inverted dropout

Here m is a binary mask, h is the activation vector, and p is the drop probability. At inference, no units are dropped and no rescaling is applied; inverted dropout has already compensated during training. Dropout is a regularizer, not a way to add capacity.

Weight decay, early stopping, data augmentation, and smaller models are other ways to reduce variance. They can all hurt if pushed too far: a model that cannot fit the training set will not generalize either.

6. Worked example

Worked example: schedules, Adam, and batch norm

Start with η₀ = 0.4, step decay 0.5 every 10 steps. At step 20 the rate is 0.4 × 0.5² = 0.10. A cosine schedule from 0.4 to 0.02 over 50 steps gives 0.21 at the midpoint.

For Adam, take w = 1, gradient 0.1, learning rate 0.01, and zero initial moments. The first bias-corrected step is:

The movement is about 0.01. The corresponding implementation result is 0.990, with m = 0.010 and v = 0.00001.

For batch norm, the batch (1, 2, 3) has mean 2 and variance 0.667. The normalized values are -1.225, 0.000, 1.225. With γ = 2 and β = 1 they become roughly −1.449, 1, and 3.449.

7. Debugging

Check the gradient on a tiny batch before trusting a long run. Compare analytic and finite-difference gradients; a relative error around 1e-7 is reassuring for smooth functions. Turn off dropout and data augmentation during the check.

Sanity checks catch many bugs: the initial loss should match a random baseline, increasing weight decay should increase the loss, and a tiny subset should be overfittable. If the model cannot overfit 20 examples, the optimization or the implementation is suspect.

Monitor training and validation loss separately. A falling training loss with a rising validation loss is the overfitting signature; both flat suggests underfitting or a broken learning rate.

8. Where it fails

Hyperparameters interact. A learning rate that works with one batch size, architecture, or normalization scheme may fail with another. Random search on a log scale is usually a better use of compute than a fine grid.

Batch statistics can leak across examples, and evaluation must use the right running statistics. Large models can memorize; small models can underfit. Neither Adam nor batch norm removes the need for held-out evaluation.

The next lesson applies these training ideas to images: convolutions share weights across positions and build feature maps with far fewer parameters than a dense layer.

Next lesson: Convolutional Neural Networks

9. Going deeper

Second-order methods use curvature, but the Hessian is too large for modern networks. Quasi-Newton methods and Kronecker-factored approximate curvature (K-FAC) approximate it; most large-scale training still uses first-order stochastic methods with adaptive preconditioners.

Learning-rate schedules can be learned or searched, and warmup matters for transformers. Gradient clipping controls exploding gradients. Mixed precision reduces memory and increases throughput, at the cost of numerical care.

The most important debugging tool is still a clean validation split. A training curve is evidence about optimization; it is not evidence about generalization by itself.

10. Practice

Try it

A loss curve oscillates violently and then diverges. What is the first hyperparameter to inspect?

Try it

Why can dropout hurt at inference if you forget to turn it off?

Try it

Training loss falls, validation loss rises. What are two reasonable responses?

11. Resources

The MIT appendix derives momentum, Adam, and batch normalization. CS231n covers gradient checks, learning-rate schedules, hyperparameter search, and training diagnostics. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Numerical methods.

All Machine Learning lessons

Site soundtrack