๐Ÿ›ก๏ธ Regularization

499 words
2 min read
Python Week 1: the first filter for runtime behavior
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:
L=L0+ฮป2โˆฃโˆฃwโˆฃโˆฃ22=L0+ฮป2โˆ‘wi2L = L_0 + \frac{\lambda}{2}||w||_2^2 = L_0 + \frac{\lambda}{2}\sum w_i^2
Gradient: โˆ‚Lโˆ‚wi=โˆ‚L0โˆ‚wi+ฮปwi\frac{\partial L}{\partial w_i} = \frac{\partial L_0}{\partial w_i} + \lambda w_i Update: wiโ†wiโˆ’ฮท(โˆ‚L0โˆ‚wi+ฮปwi)=wi(1โˆ’ฮทฮป)โˆ’ฮทโˆ‚L0โˆ‚wiw_i \leftarrow w_i - \eta(\frac{\partial L_0}{\partial w_i} + \lambda w_i) = w_i(1-\eta\lambda) - \eta\frac{\partial L_0}{\partial w_i} The wi(1โˆ’ฮทฮป)w_i(1-\eta\lambda) term is "weight decay" โ€” it shrinks weights each step.

3.2 L1 Regularization (Lasso)

L=L0+ฮปโˆฃwโˆฃ1=L0+ฮปโˆ‘โˆฃwiโˆฃL = L_0 + \lambda|w|_1 = L_0 + \lambda\sum |w_i|
Effect: Produces sparse weights (many weights become exactly zero). Good for feature selection.

3.3 L1 vs L2 Comparison

PropertyL2L1
Penaltyโˆ‘wi2\sum w_i^2$\sum
Gradientฮปwi\lambda w_iฮปโ‹…sign(wi)\lambda \cdot \text{sign}(w_i)
WeightsSmall but non-zeroSparse (many zero)
Best forGeneral regularizationFeature selection
Differentiable at 0Yes (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):
y=f(Wx)โŠ™mwhereย miโˆผBernoulli(1โˆ’p)y = f(Wx) \odot m \quad \text{where } m_i \sim \text{Bernoulli}(1-p)
Inverted Dropout (common implementation): During training: y=11โˆ’pโ‹…f(Wx)โŠ™my = \frac{1}{1-p} \cdot f(Wx) \odot m During inference: y=f(Wx)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:
ฮผB=1mโˆ‘i=1mxi\mu_B = \frac{1}{m}\sum_{i=1}^m x_i ฯƒB2=1mโˆ‘i=1m(xiโˆ’ฮผB)2\sigma_B^2 = \frac{1}{m}\sum_{i=1}^m (x_i - \mu_B)^2 x^i=xiโˆ’ฮผBฯƒB2+ฯต\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}} yi=ฮณx^i+ฮฒy_i = \gamma\hat{x}_i + \beta
Where ฮณ\gamma and ฮฒ\beta are learnable parameters. Training: Use batch statistics ฮผB,ฯƒB2\mu_B, \sigma_B^2 Inference: Use running averages of ฮผ\mu and ฯƒ2\sigma^2 computed during training

3.6 Early Stopping

Monitor validation loss. Stop training when validation loss stops improving (with patience).
text
best_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
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.