Overfitting & Underfitting

GATE Relevance: High

🎯 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

Overfitting

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.

Underfitting

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.

IndicatorUnderfittingGood FitOverfitting
Training errorHighLowVery low
Test errorHighLow (similar to train)High (much > train)
Gap (test−train)SmallSmallLarge
BiasHighBalancedLow
VarianceLowBalancedHigh

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)

Ridge Regression Loss

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)

Lasso Regression Loss

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

Elastic Net Loss

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)
PropertyRidge (L2)Lasso (L1)Elastic Net
Penaltyλ∑w²λ∑|w|λ₁∑|w| + λ₂∑w²
Feature selectionNoYesYes
Sparse solutionNoYesYes
MulticollinearityHandles wellMay arbitrarily pick oneGroups correlated features
Closed-formYesNoNo
GATE Numerical: Ridge Regression

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.
For GATE

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.

Early Stopping Procedure
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
Caution

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
GATE Example: Diagnosing from Learning Curve

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

TechniqueMechanismWhen to Use
L2 (Ridge)Shrinks all weights toward zeroMany relevant features, multicollinearity
L1 (Lasso)Drives some weights to exactly zeroFeature selection needed, sparse models
Elastic NetCombines L1 and L2Correlated features, best of both worlds
DropoutRandomly deactivates neuronsDeep neural networks
Early StoppingStops training before overfittingAny iterative training algorithm
Data AugmentationIncreases effective training setLimited training data, images/text
Cross-ValidationBetter model selectionHyperparameter tuning
PruningRemoves tree branchesDecision trees
Ensemble MethodsAverages multiple modelsReducing variance generally

8. GATE-Style Practice

GATE Example: L1 vs L2

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.

GATE Example: λ Effect

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.