AnyLearn
All lessons
AIintermediate

Training: Optimization and Regularization

Go from a raw neural network to one that actually generalizes. Covers loss functions (MSE, cross-entropy), gradient descent variants (SGD, momentum, Adam), learning-rate effects, overfitting vs underfitting, and the regularization toolkit (L2/dropout/early stopping/batch norm).

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 9

Loss functions: what you're actually minimizing

The loss is the single number that training minimizes. Choosing the wrong one silently cripples your model.

  • Mean Squared Error (MSE): L=1Ni(yiy^i)2\mathcal{L} = \frac{1}{N}\sum_i (y_i - \hat{y}_i)^2. Use for regression. Penalizes large errors quadratically — outliers dominate.
  • Binary Cross-Entropy: L=1Ni[yilogp^i+(1yi)log(1p^i)]\mathcal{L} = -\frac{1}{N}\sum_i [y_i \log \hat{p}_i + (1-y_i)\log(1-\hat{p}_i)]. Use for binary classification with a sigmoid output.
  • Categorical Cross-Entropy: L=1Nikyiklogp^ik\mathcal{L} = -\frac{1}{N}\sum_i \sum_k y_{ik} \log \hat{p}_{ik}. Use for multiclass with softmax output.

Cross-entropy is preferred over MSE for classification because it is derived from maximum likelihood, produces sharper gradients near decision boundaries, and avoids the flat gradient saturation problem of MSE + sigmoid.

Full lesson text

All 9 steps on one page, for reading, reference, and search.

Show

1. Loss functions: what you're actually minimizing

The loss is the single number that training minimizes. Choosing the wrong one silently cripples your model.

  • Mean Squared Error (MSE): L=1Ni(yiy^i)2\mathcal{L} = \frac{1}{N}\sum_i (y_i - \hat{y}_i)^2. Use for regression. Penalizes large errors quadratically — outliers dominate.
  • Binary Cross-Entropy: L=1Ni[yilogp^i+(1yi)log(1p^i)]\mathcal{L} = -\frac{1}{N}\sum_i [y_i \log \hat{p}_i + (1-y_i)\log(1-\hat{p}_i)]. Use for binary classification with a sigmoid output.
  • Categorical Cross-Entropy: L=1Nikyiklogp^ik\mathcal{L} = -\frac{1}{N}\sum_i \sum_k y_{ik} \log \hat{p}_{ik}. Use for multiclass with softmax output.

Cross-entropy is preferred over MSE for classification because it is derived from maximum likelihood, produces sharper gradients near decision boundaries, and avoids the flat gradient saturation problem of MSE + sigmoid.

2. Gradient descent: the core update rule

All neural network optimizers are variants of gradient descent:

θθηθL\theta \leftarrow \theta - \eta \nabla_\theta \mathcal{L}

where η>0\eta > 0 is the learning rate and θL\nabla_\theta \mathcal{L} is the gradient of the loss with respect to parameters θ\theta.

Full-batch gradient descent computes the gradient over the entire dataset — exact but slow for large data. Stochastic gradient descent (SGD) uses a single example per step — noisy but fast. Mini-batch SGD (the standard) uses a batch of BB examples, balancing signal quality and compute:

for epoch in range(num_epochs):
    for X_batch, y_batch in dataloader:  # batch size B
        loss = compute_loss(model(X_batch), y_batch)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

The noise in SGD is actually beneficial: it acts as an implicit regularizer and helps escape sharp local minima.

3. Momentum and Adam: going beyond vanilla SGD

Vanilla SGD oscillates in ravines and crawls on flat plateaus. Modern optimizers fix this.

OptimizerKey ideaExtra cost
SGDFollow negative gradientNone
SGD + MomentumAccumulate velocity: vβvηgv \leftarrow \beta v - \eta g, θθ+v\theta \leftarrow \theta + v1 momentum buffer
RMSPropScale η\eta by running average of g2g^2; large gradients get smaller steps1 variance buffer
AdamMomentum + RMSProp + bias correction2 buffers

Adam update (β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=108\epsilon = 10^{-8} are standard defaults):

mt=β1mt1+(1β1)gt,vt=β2vt1+(1β2)gt2m_t = \beta_1 m_{t-1} + (1-\beta_1)g_t, \quad v_t = \beta_2 v_{t-1} + (1-\beta_2)g_t^2 m^t=mt/(1β1t),v^t=vt/(1β2t)\hat{m}_t = m_t/(1-\beta_1^t), \quad \hat{v}_t = v_t/(1-\beta_2^t) θt=θt1ηm^t/(v^t+ϵ)\theta_t = \theta_{t-1} - \eta\, \hat{m}_t / (\sqrt{\hat{v}_t} + \epsilon)

Adam is robust to learning-rate choice and converges fast. SGD + momentum often wins at convergence quality with careful tuning — many large-scale vision papers still use it.

4. Learning rate: the most important hyperparameter

The learning rate η\eta controls how large each step is. Get it wrong and nothing else matters.

  • Too large: loss oscillates or diverges. Weights overshoot the minimum.
  • Too small: training is painfully slow and may stall in flat regions.
  • Just right: loss decreases smoothly and plateaus at a good value.

Learning rate schedules help:

  • Step decay: halve η\eta every kk epochs.
  • Cosine annealing: ηt=ηmin+12(ηmaxηmin)(1+cos(πt/T))\eta_t = \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})(1 + \cos(\pi t/T)).
  • Warmup: start tiny, ramp up over first few epochs, then decay. Critical for large batch training and transformers.
# PyTorch cosine annealing
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
    optimizer, T_max=num_epochs, eta_min=1e-6
)
for epoch in range(num_epochs):
    train_one_epoch()
    scheduler.step()

5. Overfitting, underfitting, and the bias-variance trade-off

Every training run lives somewhere on this spectrum:

  • Underfitting (high bias): training loss is high. Model too simple, or training stopped too early, or learning rate too small.
  • Overfitting (high variance): training loss is low, validation loss is high. Model memorized training data instead of generalizing.
  • Good fit: both training and validation loss are low and close.

Diagnose by watching the validation loss curve. If it stops falling and starts rising while training loss keeps falling — you're overfitting.

Four levers to fight overfitting:

  1. More data (most effective).
  2. Simpler model (fewer parameters).
  3. Regularization (keep the model but constrain it).
  4. Early stopping (stop before the gap grows).

The bias-variance decomposition: expected test error = (bias)2^2 + variance + irreducible noise. Regularization trades a small increase in bias for a large drop in variance.

6. L2 regularization and dropout

L2 regularization (weight decay) adds a penalty to the loss:

Lreg=L+λ2θ22\mathcal{L}_{\text{reg}} = \mathcal{L} + \frac{\lambda}{2} \|\theta\|_2^2

The gradient update becomes θθ(1ηλ)ηL\theta \leftarrow \theta(1 - \eta\lambda) - \eta \nabla \mathcal{L}. Weights shrink toward zero each step. In Adam, weight decay is applied separately from the adaptive scaling (AdamW) — this distinction matters in practice and AdamW is the preferred implementation.

Dropout randomly zeroes each neuron's output with probability pp during training:

import torch.nn as nn
# During training, 50% of activations zeroed; outputs scaled by 1/(1-p)
self.dropout = nn.Dropout(p=0.5)
x = self.dropout(self.relu(self.linear(x)))

At inference, dropout is disabled and all neurons contribute. Effect: the network can't rely on any single neuron — it must learn distributed, redundant representations. This reduces co-adaptation and acts like an ensemble of 2n2^n thinned networks.

7. Batch normalization

Batch normalization (BatchNorm) normalizes the pre-activations within each mini-batch, then applies a learned scale γ\gamma and shift β\beta:

z^i=ziμBσB2+ϵ,yi=γz^i+β\hat{z}_i = \frac{z_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \qquad y_i = \gamma \hat{z}_i + \beta

where μB\mu_B and σB2\sigma_B^2 are the batch mean and variance. Benefits:

  • Allows higher learning rates without divergence.
  • Reduces sensitivity to weight initialization.
  • Acts as a mild regularizer (the batch statistics add noise).

At inference, μB\mu_B and σB2\sigma_B^2 are replaced by running statistics accumulated during training. Placement: typically after the linear layer and before the activation (Linear → BN → ReLU). BatchNorm has become almost universal in CNNs; transformers use Layer Norm instead (normalizes over the feature dimension, not the batch — independent of batch size).

8. Early stopping and putting it all together

Early stopping: monitor validation loss after each epoch. Stop training when validation loss stops improving for kk consecutive epochs (patience kk), and restore the best checkpoint.

best_val_loss = float('inf')
patience, epochs_no_improve = 10, 0
for epoch in range(max_epochs):
    train_loss = train_one_epoch()
    val_loss = evaluate(val_loader)
    if val_loss < best_val_loss:
        best_val_loss = val_loss
        torch.save(model.state_dict(), 'best.pt')
        epochs_no_improve = 0
    else:
        epochs_no_improve += 1
        if epochs_no_improve == patience:
            break
model.load_state_dict(torch.load('best.pt'))

A production-grade training loop stacks all of this: AdamW optimizer, cosine LR schedule with warmup, dropout and weight decay in the model, BatchNorm for stable training, early stopping to avoid overfitting. Each component fixes a specific failure mode — none is optional on real problems.

9. Common gotchas in training

Things that silently wreck training runs, ranked by how often they bite:

  • Forgot optimizer.zero_grad(): gradients accumulate across batches, giving wrong updates.
  • Wrong loss for the task: MSE on a classification head (should be cross-entropy), or cross-entropy with logits passed through sigmoid first (double-applies sigmoid — use BCEWithLogitsLoss).
  • Data not normalized: raw pixel values [0, 255] cause wildly different gradient scales vs. [-1, 1]. Always normalize inputs to zero mean and unit variance.
  • Learning rate too high with Adam: Adam is sensitive — start with η=3×104\eta = 3 \times 10^{-4} and tune from there.
  • Using weight decay with vanilla Adam instead of AdamW: in vanilla Adam, weight decay gets absorbed into the adaptive scaling and is much weaker than intended.
  • Evaluating with dropout active: always call model.eval() before validation and model.train() before training.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Why is cross-entropy preferred over MSE for classification tasks?
    • Cross-entropy is computationally cheaper than MSE.
    • MSE cannot handle multi-class outputs.
    • Cross-entropy provides sharper gradients near decision boundaries and is derived from maximum likelihood, whereas MSE combined with sigmoid causes gradient saturation.
    • MSE requires the output layer to use softmax, which is numerically unstable.
  2. What is the key difference between AdamW and vanilla Adam?
    • AdamW uses a different momentum coefficient.
    • AdamW applies weight decay directly to the parameters rather than adding it to the gradient, keeping regularization decoupled from adaptive scaling.
    • AdamW uses second-order gradient information while Adam uses only first-order.
    • AdamW removes bias correction from the moment estimates.
  3. During training, dropout zeroes activations with probability $p$. What happens at inference time?
    • Dropout randomly zeroes activations with probability $p/2$ to maintain consistency.
    • Dropout is disabled and all neurons contribute; no scaling is applied.
    • Dropout is disabled and all neurons contribute; the network implicitly averages over the ensemble of thinned networks.
    • The model is re-trained without dropout for a few epochs.
  4. A training run shows training loss at 0.05 and validation loss at 0.98 after 50 epochs. What is the most likely diagnosis?
    • Underfitting: the model is too simple for the data.
    • A bug in the loss function causing incorrect gradient computation.
    • Overfitting: the model has memorized training data and fails to generalize.
    • The learning rate is too small, preventing convergence.
  5. Batch normalization uses batch statistics ($\mu_B$, $\sigma_B^2$) during training. What does it use at inference time, and why?
    • It recomputes batch statistics on each inference mini-batch for accuracy.
    • It sets both mean and variance to zero and one respectively.
    • It uses running statistics (exponential moving averages) accumulated during training, because single inference samples may lack reliable statistics.
    • It disables normalization entirely and passes raw activations through.

Related lessons

Math
intermediate

Gradient descent: choosing the step and knowing the rate

Gradient descent is three lines of code and a hundred years of theory. This lesson derives why a safe step size is one over the smoothness constant, why the condition number governs everything, and why acceleration reaching order one over k squared is provably the best any first-order method can do.

11 steps·~17 min
Math
advanced

Newton's method and the interior point revolution

Second derivatives buy something gradients cannot: a step shaped by curvature, immune to conditioning, converging quadratically. This lesson builds Newton's method, then layers it on a log barrier to get interior point methods, the machinery that made large constrained problems solvable with a certificate rather than a hope.

13 steps·~20 min
Math
intermediate

Convexity: the property that decides what is solvable

Convexity is what separates optimization problems you can solve with a guarantee from ones you can only hope about. This lesson defines convex sets and functions, proves why every local minimum is global, and gives you the operations that let you recognise convexity without touching a Hessian.

11 steps·~17 min
AI
advanced

Evaluating a Book Model Honestly

If your cost per round trip equals the move you are trying to capture, you need 100 percent directional accuracy to break even. This lesson computes that hurdle, replaces accuracy with metrics tied to a tradeable decision, and covers the capacity and latency limits that decide whether a real edge is worth anything.

10 steps·~15 min