Overfitting & Underfitting
🎯 Learning Objectives
- Recognise signs of overfitting and underfitting from training/test performance
- Understand L1 (Lasso), L2 (Ridge), and Elastic Net regularisation
- Apply early stopping, dropout, and data augmentation conceptually
- Interpret learning curves to diagnose model issues
1. Recognising Overfitting and Underfitting
The model learns the training data too well, including noise and random fluctuations. It performs excellently on training data but poorly on unseen test data.
Symptoms: Training accuracy ≈ 99%, Test accuracy ≈ 60%. Large gap between training and test error.
The model is too simple to capture the underlying patterns in the data. It performs poorly on both training and test data.
Symptoms: Training accuracy ≈ 55%, Test accuracy ≈ 50%. Both errors are high, with a small gap between them.
| Indicator | Underfitting | Good Fit | Overfitting |
|---|---|---|---|
| Training error | High | Low | Very low |
| Test error | High | Low (similar to train) | High (much > train) |
| Gap (test−train) | Small | Small | Large |
| Bias | High | Balanced | Low |
| Variance | Low | Balanced | High |
2. Regularisation Techniques
Regularisation adds a penalty term to the loss function to discourage overly complex models. It shrinks model parameters toward zero, reducing the model’s effective complexity.
2.1 L2 Regularisation (Ridge Regression)
J(w) = (1/n) ∑(yᵢ − ŷᵢ)² + λ ∑wᵤ²
The L2 penalty λ∑wᵤ² penalises large weights. The closed-form solution becomes:
w* = (XᵀX + λI)⁻¹ Xᵀy
The addition of λI ensures the matrix is always invertible, solving the multicollinearity problem.
- Shrinks all weights toward zero but never exactly zero
- Keeps all features in the model (no feature selection)
- Good when many features are believed to be relevant
2.2 L1 Regularisation (Lasso Regression)
J(w) = (1/n) ∑(yᵢ − ŷᵢ)² + λ ∑|wᵤ|
The L1 penalty uses the absolute value of weights. No closed-form solution — requires iterative methods.
- Can shrink some weights to exactly zero — performs automatic feature selection
- Produces sparse models (fewer non-zero coefficients)
- Good when only a few features are truly important
2.3 Elastic Net
J(w) = (1/n) ∑(yᵢ − ŷᵢ)² + λ₁ ∑|wᵤ| + λ₂ ∑wᵤ²
Combines L1 and L2 penalties. A mixing ratio α controls the balance: α=1 is pure Lasso, α=0 is pure Ridge.
- Overcomes Lasso’s limitation of selecting at most n features where n < d
- Groups correlated features together (selects or excludes as a group)
| Property | Ridge (L2) | Lasso (L1) | Elastic Net |
|---|---|---|---|
| Penalty | λ∑w² | λ∑|w| | λ₁∑|w| + λ₂∑w² |
| Feature selection | No | Yes | Yes |
| Sparse solution | No | Yes | Yes |
| Multicollinearity | Handles well | May arbitrarily pick one | Groups correlated features |
| Closed-form | Yes | No | No |
Q: For a 1D linear regression with 3 data points (1,2), (2,3), (3,5), compute the Ridge regression weight w with λ=1 (no intercept). Use w* = (XᵀX + λI)⁻¹Xᵀy.
Solution:
X = [1, 2, 3]ᵀ, y = [2, 3, 5]ᵀ
XᵀX = 1 + 4 + 9 = 14
Xᵀy = 2 + 6 + 15 = 23
w* = (14 + 1)⁻¹ · 23 = 23/15 = 1.533
(Compare with OLS: w* = 23/14 = 1.643 — Ridge shrinks the weight towards zero.)
3. Dropout (Neural Networks)
Dropout randomly “drops” (sets to zero) a fraction of neurons during each training step. Each training iteration uses a different random subset of the network.
- Training: Each neuron is kept with probability p (typically 0.5 for hidden layers, 0.8 for input).
- Inference: All neurons are used, but weights are scaled by p to compensate.
- Effect: Prevents neurons from co-adapting. Acts as an implicit ensemble of many sub-networks.
Dropout is primarily conceptual in GATE. Know that it is a regularisation technique that reduces overfitting in neural networks by randomly deactivating neurons during training.
4. Early Stopping
Monitor the model’s performance on a validation set during training. Stop training when validation error begins to increase, even if training error continues to decrease.
1. Split data into training and validation sets 2. Train the model iteratively (e.g., gradient descent epochs) 3. After each epoch, evaluate on the validation set 4. Record the best validation error and corresponding model parameters 5. If validation error has not improved for P consecutive epochs (patience P): STOP training and restore the best model parameters
Why it works: Early in training, the model learns genuine patterns (both train and val error decrease). As training continues, the model starts memorising noise (train error decreases but val error increases). Early stopping halts training at the transition point.
5. Data Augmentation
Increase the effective size of the training set by creating modified versions of existing data. More data reduces variance (overfitting).
- Images: Rotation, flipping, cropping, colour jittering, scaling
- Text: Synonym replacement, random insertion, back-translation
- Tabular: SMOTE (Synthetic Minority Oversampling Technique) for imbalanced classes
Augmented data must be semantically valid. For example, flipping a digit “6” vertically creates a “9” — this would introduce incorrect labels.
6. Learning Curves
Learning curves plot training and validation error as a function of training set size or training epochs. They are powerful diagnostic tools.
6.1 High Bias (Underfitting) Learning Curve
- Both training and validation errors are high
- Errors converge to a high value as data increases
- Adding more data will NOT help significantly
- Fix: Increase model complexity, add features
6.2 High Variance (Overfitting) Learning Curve
- Training error is low, validation error is high
- There is a large gap between the two curves
- Adding more data WILL help (the gap narrows)
- Fix: More data, regularisation, simpler model
Q: A model shows training error of 2% and validation error of 25%. What should you try first?
(A) Increase model complexity (B) Add regularisation (C) Remove features (D) Decrease learning rate
A: (B) Add regularisation. The large gap (23%) indicates overfitting (high variance). Regularisation penalises complex models and reduces variance. Increasing complexity (A) would worsen overfitting.
7. Summary of Anti-Overfitting Techniques
| Technique | Mechanism | When to Use |
|---|---|---|
| L2 (Ridge) | Shrinks all weights toward zero | Many relevant features, multicollinearity |
| L1 (Lasso) | Drives some weights to exactly zero | Feature selection needed, sparse models |
| Elastic Net | Combines L1 and L2 | Correlated features, best of both worlds |
| Dropout | Randomly deactivates neurons | Deep neural networks |
| Early Stopping | Stops training before overfitting | Any iterative training algorithm |
| Data Augmentation | Increases effective training set | Limited training data, images/text |
| Cross-Validation | Better model selection | Hyperparameter tuning |
| Pruning | Removes tree branches | Decision trees |
| Ensemble Methods | Averages multiple models | Reducing variance generally |
8. GATE-Style Practice
Q: Which regularisation technique can perform automatic feature selection?
(A) L2 (Ridge) (B) L1 (Lasso) (C) Both (D) Neither
A: (B) L1 (Lasso). The L1 penalty can shrink weights to exactly zero, effectively removing the corresponding features from the model. L2 only shrinks weights close to zero but never exactly zero.
Q: As the regularisation parameter λ increases in Ridge regression, what happens to bias and variance?
A: Bias increases (model becomes simpler), variance decreases (model is less sensitive to training data). As λ → ∞, all weights → 0, giving maximum bias and minimum variance.