Neural Sync Active
🛡️ Regularization
Registry Synced
🛡️ Regularization
499 words
2 min read
Reading compass
Now · 1. 🎯 Learning Objectives
🛡️ 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