Lecture 5 of 1421 minInteractiveFree lecture

Regularization and generalization

A network big enough to learn anything is big enough to memorize noise. Train a real network live in your browser, watch it overfit, then tame it with weight decay, dropout and early stopping — the everyday tools that make deep models generalize.

In this lecture you will

  • Recognize overfitting and underfitting from training and validation curves
  • Explain how weight decay, dropout and early stopping reduce overfitting
  • Use a validation set to choose model size and regularization
  • Know when more data or augmentation is the better fix

Deep networks have enormous capacity. A network with a few thousand parameters can draw almost any boundary you like — including one that snakes around every noisy, mislabeled training example. That is overfitting: excellent training scores, disappointing results on new data. This lecture is about the tools that keep networks honest.

See it happen

The playground below trains a real neural network in your browser. Filled dots are training data; hollow rings are test data the network never trains on. The background shows the network’s decision: teal where it predicts one class, amber for the other.

InteractiveNeural-network playgroundPress Train
Loading visual…

Everything runs locally: the network, backpropagation and the Adam optimizer. Change the architecture or data and press Train again.

Try these experiments in order:

  1. Spiral, 2 layers of 8, tanh. Press Train and watch the boundary wind around the arms. Both losses fall together — the model is learning the real pattern.
  2. Raise the label noise to 20–30% and use 3 layers of 16. Train for a while. The training loss keeps falling as the boundary grows islands around mislabeled points, while the test loss turns upwards: overfitting.
  3. Now add L2 weight decay 0.003 or dropout 30%, press Reset and train again. The boundary stays smoother and the test loss stays lower.
  4. Circle with 1 layer of 2 neurons. The network cannot bend enough; both losses stay high: underfitting. Add neurons until it fits.
Generalization
How well a model performs on new data drawn from the same source as the training data. The only performance that matters in production.
Overfitting
The model fits noise and quirks of the training set. Training loss is low; validation loss is high or rising.
Underfitting
The model is too simple or undertrained to capture the pattern. Both training and validation loss stay high.
Validation set
Data held out from training and used to choose models, hyperparameters and when to stop.
Test set
Data touched only once, at the very end, for an honest final estimate.

Weight decay: prefer small weights

L2 regularization, usually called weight decay, adds a penalty for large weights to the loss:

Ltotal=Ldata+λ∑iwi2L_\text{total} = L_\text{data} + \lambda \sum_i w_i^2

The gradient of the penalty is 2λw2\lambda w, so every update also shrinks each weight a little towards zero. Weights survive only if the data keeps pushing them up. Networks with small weights change their outputs gently as the input changes — they produce smoother boundaries that tend to generalize. The strength λ\lambda is a hyperparameter: too little does nothing; too much causes underfitting.

Dropout: train an ensemble in one network

Dropout takes a different route. At each training step it switches off a random fraction of the hidden neurons — typically 10–50%.

AnimationDropout, one training step at a time
Loading animation…
  1. The full networkWithout dropout, groups of neurons can co-adapt: they learn to fix each other’s mistakes on the training data in ways that do not generalize.
  2. Training step 1: a random maskHalf of the hidden neurons are switched off for this step, together with their connections. The survivors’ outputs are scaled up so the next layer sees the same average signal.
  3. Training step 2: a new maskThe next step drops a different random half. Every step trains a different thinned network that shares weights with all the others.
  4. Training step 3: no neuron can rely on anotherBecause any partner might be missing, each neuron has to learn features that are useful on their own. The network becomes robust and redundant.
  5. Prediction: everyone worksAt inference time dropout is switched off and every neuron is used. The result behaves like an average of the many thinned networks — an ensemble at the cost of one model.

Early stopping: quit while you are ahead

The simplest regularizer of all is to stop training at the right moment. Validation loss usually falls, reaches a minimum and then rises as the model starts to memorize. Early stopping keeps the checkpoint with the best validation loss.

InteractiveChoosing when to stopDrag the marker
Loading visual…

A simulated training run. The shaded zone is where validation loss has passed its minimum. Patience is how many epochs without improvement to wait before stopping.

Move the marker to see the cost of stopping too early or too late, then increase the regularization strength: the turn in the validation curve comes later and lower, and the gap between the curves shrinks.

Other tools in the box

  • More data is the most reliable cure for overfitting. When collecting more is impossible, data augmentation creates new training examples by transforming existing ones — flipping and cropping images, or paraphrasing text. You will see this in the Computer Vision course.
  • Batch normalization normalizes each layer’s inputs within a mini-batch. It mainly speeds up and stabilizes training, with a mild regularizing side effect.
  • Smaller or simpler models reduce capacity directly — sometimes the right answer.
  • Label smoothing trains on slightly softened targets (0.9 instead of 1) so the network does not become overconfident.
model = nn.Sequential(
    nn.Linear(784, 256), nn.ReLU(), nn.Dropout(p=0.3),
    nn.Linear(256, 128), nn.ReLU(), nn.Dropout(p=0.3),
    nn.Linear(128, 10),
)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01)

best = float("inf")
for epoch in range(100):
    train_one_epoch(model, optimizer)          # model.train() inside
    val_loss = evaluate(model)                 # model.eval() inside
    if val_loss < best:
        best, bad_epochs = val_loss, 0
        torch.save(model.state_dict(), "best.pt")   # keep the best checkpoint
    else:
        bad_epochs += 1
        if bad_epochs >= 10:                    # patience
            break

You now have the complete toolkit of a working deep-learning engineer: neurons and layers, activations, backpropagation, optimizers and regularization. The rest of the course applies it to the architectures that power modern AI — starting with convolutional networks for images.

Key takeaways

  • Generalization, not training loss, is the goal; always judge a model on data it has not seen.
  • Weight decay (L2) keeps weights small, which favors smoother functions.
  • Dropout switches off random neurons during training so the network cannot rely on fragile co-adaptations.
  • Early stopping keeps the checkpoint with the best validation loss, before the model starts memorizing.

Check your understanding

4 questions · answers explained

Q1Training loss keeps falling, but validation loss has been rising for 15 epochs. What is happening?

A widening gap between falling training loss and rising validation loss is the classic sign of overfitting. The model is fitting details of the training set that do not carry over to new data.

Q2What does dropout do during training?

Each training step uses a random “thinned” network. Because any neuron might be missing, the network learns redundant, robust features. At prediction time all neurons are used.

Q3What does L2 weight decay add to the training objective?

The penalty λΣw² pushes weights towards zero unless the data strongly supports them. Smaller weights generally produce smoother functions that generalize better.

Q4Why must early stopping use a validation set rather than the test set?

The test set must stay untouched until the very end so it gives an honest estimate of performance on new data. Every decision — stopping time, model size, regularization — should be made on validation data.

You’ve finished the free lectures.

Continue with the remaining 9 lectures of Deep Learning — one-time purchase of $79₹5,499.