๐ก๏ธ Regularization
499 words
2 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# ๐ก๏ธ Regularization ## 1. ๐ฏ Learning Objectives - Explain L1 vs L2 regularization: effect on weights, sparsity - Implement dropout: forward pass with mask, inverted dropout - Explain batch normalization: training vs inference behavior - Apply early stopping and data augmentation - Diagnose overfitting and apply ap...

๐ก๏ธ Regularization
1. ๐ฏ Learning Objectives
- Explain L1 vs L2 regularization: effect on weights, sparsity
- Implement dropout: forward pass with mask, inverted dropout
- Explain batch normalization: training vs inference behavior
- Apply early stopping and data augmentation
- Diagnose overfitting and apply appropriate regularization
2. ๐ Core Content
3.1 L2 Regularization (Weight Decay)
Adds squared magnitude of weights to loss:
Gradient: โwiโโLโ=โwiโโL0โโ+ฮปwiโ
Update: wiโโwiโโฮท(โwiโโL0โโ+ฮปwiโ)=wiโ(1โฮทฮป)โฮทโwiโโL0โโ
The wiโ(1โฮทฮป) term is "weight decay" โ it shrinks weights each step.
3.2 L1 Regularization (Lasso)
L=L0โ+ฮปโฃwโฃ1โ=L0โ+ฮปโโฃwiโโฃEffect: Produces sparse weights (many weights become exactly zero). Good for feature selection.
3.3 L1 vs L2 Comparison
| Property | L2 | L1 |
|---|---|---|
| Penalty | โwi2โ | $\sum |
| Gradient | ฮปwiโ | ฮปโ sign(wiโ) |
| Weights | Small but non-zero | Sparse (many zero) |
| Best for | General regularization | Feature selection |
| Differentiable at 0 | Yes (smooth) | No (sharp) |
3.4 Dropout
During training, randomly "drop" (set to 0) a fraction p of neurons in a layer. This prevents co-adaptation โ networks learn redundant representations.
Forward pass (train):
Inverted Dropout (common implementation): During training: y=1โp1โโ
f(Wx)โm During inference: y=f(Wx) (no scaling needed)
Why it works: Creates an ensemble of exponentially many subnetworks.
3.5 Batch Normalization
Normalizes layer inputs to have zero mean and unit variance:
Where ฮณ and ฮฒ are learnable parameters.
Training: Use batch statistics ฮผBโ,ฯB2โ Inference: Use running averages of ฮผ and ฯ2 computed during training
3.6 Early Stopping
Monitor validation loss. Stop training when validation loss stops improving (with patience).
textbest_val_loss = infinity patience_counter = 0 for epoch in range(max_epochs): train_loss = train_one_epoch() val_loss = evaluate() if val_loss < best_val_loss: best_val_loss = val_loss save_model() patience_counter = 0 else: patience_counter += 1 if patience_counter >= patience: break
3.7 Data Augmentation
Generate more training data through label-preserving transformations:
- Images: rotation, flip, crop, color jitter, noise
- Text: synonym replacement, back-translation
- Audio: time stretching, pitch shifting, noise addition
4. ๐ Practice Questions
Q1: With L2 regularization ฮป=0.01, learning rate ฮท=0.1, and weight w=0.5, compute one update step (assume gradient from loss = 0.1).Answer: Gradient = 0.1 + 0.01ร0.5 = 0.105. w_new = 0.5 - 0.1ร0.105 = 0.5 - 0.0105 = 0.4895. Weight decay factor: (1-0.1ร0.01) = 0.999. Q2: With dropout p=0.5 and layer output [2, 3, 1, 4], what is the training output using inverted dropout?Answer: Random mask: [1,0,1,0] (example). Output: (1/0.5)ร[2ร1, 3ร0, 1ร1, 4ร0] = 2ร[2,0,1,0] = [4,0,2,0]. Join Discord PreviousOptimization MethodsNextCNN Operations