Regularization for Deep Networks
The Overfitting Problem
Here is a paradox: deep networks are powerful because they have millions of parameters, but those same millions of parameters are a curse — given enough capacity, the network will memorize the training data (including its noise and mistakes) rather than learning generalizable patterns. A ResNet-50 with 25 million parameters can easily memorize the entire CIFAR-10 dataset of 50,000 images; what you want is for it to learn the concept of “cat” vs “dog,” not pixel-perfect recall of every training image. An analogy: Imagine a student who memorizes every answer in the textbook word-for-word. They ace the practice problems but fail the exam because the questions are worded differently. Regularization is like telling the student “you cannot take notes into the exam” — it forces them to understand the underlying concepts rather than memorize surface patterns. Regularization constrains the model, making memorization harder and forcing the network to learn simpler, more generalizable patterns. There is no single “best” regularizer — you typically combine several techniques, each attacking overfitting from a different angle.Weight Decay (L2 Regularization)
The simplest form of regularization: add a penalty on weight magnitude to the loss function. Think of it as a tax on complexity — the bigger the weights, the higher the tax. This pushes the network toward solutions with smaller weights, which tend to be smoother and more generalizable. Add penalty on weight magnitude to loss: Effect: Pushes weights toward zero, preventing extreme values. Geometrically, it constrains the weight vector to a ball centered at the origin. The penalty is quadratic, so large weights are penalized much more than small ones.AdamW vs Adam + L2: These are NOT the same thing. Classic Adam applies L2 regularization before the adaptive scaling, which means the effective regularization depends on the gradient history — parameters with large gradients get less regularization. AdamW applies weight decay after the Adam step, so every parameter is decayed equally regardless of gradient magnitude. This matters in practice: AdamW gives consistently better results for Transformers and most modern architectures. Always use AdamW.
Dropout
Randomly zero activations during training:/ (1 - self.p) scaling factor (called “inverted dropout”) ensures the expected output magnitude stays the same whether dropout is active or not, so you do not need to adjust anything at inference time.
Data Augmentation
The most effective regularizer: artificially expand training set.Advanced Augmentations
These techniques go beyond geometric transforms — they force the network to cope with partial information loss, which dramatically improves robustness.Label Smoothing
Hard labels say “this is 100% cat, 0% everything else.” But real-world data is ambiguous — that blurry image might be 95% cat and 5% could-be-a-small-dog. Label smoothing softens the targets to reflect this uncertainty, preventing the model from becoming overconfident in its predictions. An overconfident model produces sharp, peaky probability distributions that do not calibrate well — label smoothing fixes this. Soften hard labels to prevent overconfidence:Early Stopping
Monitor validation loss; stop when it stops improving:Comparison of Regularization Techniques
Exercises
Exercise 1: Dropout Ablation
Exercise 1: Dropout Ablation
Train a network with dropout rates 0, 0.1, 0.3, 0.5, 0.7. Plot train vs val accuracy for each.
Exercise 2: Augmentation Impact
Exercise 2: Augmentation Impact
Compare model performance with: no augmentation, basic flips, full augmentation pipeline.
Exercise 3: MixUp Implementation
Exercise 3: MixUp Implementation
Implement CutMix (rectangular patches from different images) and compare with MixUp.
What’s Next
Module 18: Optimization Algorithms
SGD, Adam, AdamW, and modern optimizers for deep learning.