ML from Scratch · Lecture 5 of 5

Neural Networks III: Training That Actually Works

Losses, mini-batches, momentum and Adam, sensible initialisation, normalisation and regularisation. The practical toolkit that turns backprop into a model that generalises.

November 15, 2026By Diganta Bhattacharya5 min read

Loss functions

Cross-entropy is the negative log-likelihood. Minimising it is maximum likelihood, the same principle behind logistic regression.

Stochastic gradient descent

Computing the full gradient over millions of examples per step is wasteful. Use a random mini-batch $B$ (32–1024 examples):

$$\theta\leftarrow\theta-\eta\,\frac1{|B|}\sum_{i\in B}\nabla_\theta\ell_i(\theta).$$

The noisy gradient is unbiased, cheap, and its noise even helps escape sharp minima. The learning rate η is the most important hyperparameter. Too high diverges, too low crawls.

Better optimisers

Momentum accumulates a velocity so consistent directions speed up and oscillations cancel:

$$v\leftarrow\beta v+\nabla\ell,\qquad \theta\leftarrow\theta-\eta v\quad(\beta\approx0.9).$$

Adam keeps running means of the gradient and its square and takes per-parameter steps scaled by the gradient's typical size:

$$m\leftarrow\beta_1m+(1-\beta_1)g,\quad v\leftarrow\beta_2v+(1-\beta_2)g^2,\quad \theta\leftarrow\theta-\eta\frac{\hat m}{\sqrt{\hat v}+\epsilon},$$

with bias corrections $\hat m=m/(1-\beta_1^t)$, $\hat v=v/(1-\beta_2^t)$. Defaults $\beta_1=0.9$, $\beta_2=0.999$, η = 1e-3 work surprisingly often. AdamW decouples weight decay from the adaptive step and is today's default for transformers.

The loss surface here is a curved, narrow valley, typical of real networks. Plain gradient descent zig-zags across the valley walls. Momentum builds speed along the floor. Adam rescales each direction and heads more directly to the minimum. Push the learning rate up and watch plain GD start to bounce.

Initialisation

Weights must break symmetry (not all equal) and keep activations' variance stable from layer to layer:

$$\text{Xavier/Glorot (tanh): }\operatorname{Var}(w)=\frac{2}{n_{\text{in}}+n_{\text{out}}},\qquad \text{He (ReLU): }\operatorname{Var}(w)=\frac{2}{n_{\text{in}}}.$$

The factor 2 for ReLU compensates for it zeroing half of its inputs.

Normalisation and residuals

Regularisation

A training recipe

  1. Overfit a tiny batch first. If you can't get its loss to ~0, there's a bug.
  2. Start with AdamW, a sensible η (1e-3), He/Xavier init and normalisation layers.
  3. Use a learning-rate schedule: warm-up, then cosine or step decay.
  4. Watch train vs validation loss. A gap means more regularisation or data. Both high means a bigger model or more training.
  5. Clip gradients (norm ≈ 1) for recurrent and very deep models.
Trees or nets?On tabular data, gradient-boosted trees (Lecture 2) are still usually the stronger baseline: less tuning, robust to feature scale and missing values. Neural networks win on images, audio, text, sequences and very large datasets, where learned representations matter. Many winning Kaggle solutions blend both.

Key takeaways

Exercises

  1. Show that dropout with rescaling 1/(1−p) keeps the expected activation unchanged.
  2. Implement Adam for the XOR network from Lecture 4 and compare epochs-to-converge with plain GD.
  3. Derive He initialisation by requiring Var(z_{l+1}) = Var(z_l) for ReLU layers.