Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Overfitting and Regularization - Building Generalized Models

One of the most critical challenges in machine learning is building models that generalize well to new, unseen data. This post explores overfitting, underfitting, and proven techniques to prevent them.

The Generalization Problem

The ultimate goal of machine learning is not to fit training data perfectly, but to make accurate predictions on new data the model has never seen.

The Core Tension:

  • Training data: Usually fits too well (memorizes noise)
  • Test data: The real measure of success
  • Gap between training and test performance: Indicator of generalization problem

Bias-Variance Tradeoff

Understanding this fundamental tradeoff is key to managing generalization.

Bias

Definition: Error from overly simplistic assumptions in the model

High Bias Characteristics:

  • Model too simple for problem
  • Consistently wrong predictions
  • Poor performance on both training and test data
  • Underfitting

Example: Using linear regression for highly non-linear data

Variance

Definition: Error from model sensitivity to small fluctuations in training data

High Variance Characteristics:

  • Model too complex for problem
  • Large performance gap between training and test
  • Memorizes training data including noise
  • Overfitting

Example: Using degree-100 polynomial to fit 10 data points

The Tradeoff

Model Complexity vs Performance

Error
  |     High Bias, Low Variance    |    Low Bias, High Variance
  |     (Underfitting)             |    (Overfitting)
  |                                |
  |     \     Total Error          /
  |       \                       /
  |    Bias \                   / Variance
  |          \                /
  |           \____________/
  |         Optimal Point
  |_________________________ Model Complexity

Goal: Find the sweet spot that minimizes total error

Overfitting: The Most Common Problem

Overfitting happens when a model learns the training data too well, including its noise and peculiarities.

Signs of Overfitting

On Training Data: Very high accuracy On Test Data: Much lower accuracy Performance Gap: Large difference between train and test metrics

Example:

  • Training accuracy: 99%
  • Test accuracy: 60%
  • Clear sign of overfitting

Why Overfitting Happens

  1. Model Too Complex: Too many parameters for data size
  2. Insufficient Data: Not enough examples to learn true pattern
  3. Too Much Training: Training until perfect on training set
  4. Noise in Data: Learning noise as pattern
  5. Irrelevant Features: Extra features providing spurious correlations

Underfitting: The Opposite Problem

Underfitting happens when a model is too simple to capture the underlying pattern.

Signs of Underfitting

On Training Data: Mediocre accuracy On Test Data: Also mediocre accuracy Performance Gap: Small but both low

Example:

  • Training accuracy: 70%
  • Test accuracy: 65%
  • Small gap, but both poor - suggests underfitting

Why Underfitting Happens

  1. Model Too Simple: Not enough capacity for problem complexity
  2. Insufficient Features: Important features missing
  3. Too Much Regularization: Over-constraining the model
  4. Insufficient Training: Stopped training too early
  5. Poor Features: Features don’t capture relevant information

Techniques to Prevent Overfitting

1. More Training Data

Principle: More diverse examples prevent memorization

Why It Works:

  • Hard to memorize large, diverse datasets
  • Each example contains different noise
  • Model forced to learn true pattern

Limitations:

  • Expensive to collect more data
  • Sometimes plateau effect (diminishing returns)
  • Doesn’t help if features are poor

2. Regularization: Penalizing Complexity

Add penalty to loss function based on model complexity.

General Idea:

Total Loss = Training Loss + λ × Complexity Penalty

Where λ (lambda) controls regularization strength.

L1 Regularization (Lasso)

Penalty: Sum of absolute values of weights

Penalty = λ × Σ |w_i|

Effects:

  • Encourages small weights
  • Can force weights to exactly zero
  • Feature selection (eliminates irrelevant features)
  • Produces sparse models

When to Use:

  • Want feature selection
  • Suspect many features are irrelevant
  • Need interpretability

L2 Regularization (Ridge)

Penalty: Sum of squared values of weights

Penalty = λ × Σ w_i²

Effects:

  • Encourages small weights
  • Never exactly zero (gradual reduction)
  • Distributes weight across related features
  • Smoother, more stable models

When to Use:

  • Most general purpose
  • Good starting point
  • Better when features are correlated

Elastic Net

Combines both L1 and L2:

Penalty = λ₁ × Σ |w_i| + λ₂ × Σ w_i²

Benefits:

  • Gets advantages of both L1 and L2
  • More flexible regularization
  • Better for high-dimensional data

3. Early Stopping

Monitor performance on validation data during training and stop when it starts degrading.

How It Works:

  1. Split data: Training + Validation
  2. Train model
  3. After each iteration, evaluate on validation data
  4. Track validation error
  5. Stop when validation error stops decreasing
  6. Use model state from best validation performance

Why It’s Effective:

  • Prevents training until overfitting
  • Simple to implement
  • Works for iterative algorithms (neural networks, boosting)

4. Dropout (for Neural Networks)

Randomly remove units during training, forcing network to learn redundant representations.

How It Works:

  1. During training: Randomly set some hidden units to 0
  2. During prediction: Use all units
  3. Probability p of dropout typical 0.5

Why It’s Effective:

  • Prevents co-adaptation of neurons
  • Each neuron learns independently useful features
  • Ensemble effect (averaging many thinned networks)

5. Cross-Validation

Use multiple train-test splits to get robust performance estimate.

K-Fold Cross-Validation:

  1. Divide data into K folds
  2. For each fold:
    • Use as test set
    • Use remaining K-1 as training set
    • Train and evaluate
  3. Average results across folds

Advantages:

  • Uses data efficiently
  • More stable performance estimate
  • Detects overfitting
  • Better hyperparameter selection

6. Feature Selection

Remove irrelevant or redundant features.

Methods:

  • Univariate: Select top features by individual correlation
  • Recursive: Train model, remove least important feature, repeat
  • Domain Knowledge: Use expert knowledge
  • Statistical: Use L1 regularization or mutual information

7. Ensemble Methods

Combine multiple models to reduce overfitting.

How It Works:

  • Train multiple models with different random seeds
  • Average predictions
  • Reduces variance through averaging

Examples:

  • Random Forests: Multiple trees with data randomness
  • Boosting: Sequential models focusing on errors
  • Voting: Combine different algorithm types

8. Architecture Simplification

For neural networks, reduce complexity:

  • Fewer layers
  • Fewer units per layer
  • Simpler models (start simple, add complexity if needed)

9. Data Augmentation

Create additional training data through transformations.

For Images:

  • Rotations, flips, crops
  • Brightness/contrast adjustments
  • Small translations

For Text:

  • Back-translation
  • Synonym replacement
  • Paraphrasing

Benefits:

  • Increases effective training data size
  • Introduces controlled variations
  • Forces model to be robust

Choosing the Right Approach

Diagnosis Flow:

Is training error high?
├─ Yes → Underfitting
│  └─ Solution: More complex model, more features, or train longer

└─ No → Is test error much higher than training error?
   ├─ Yes → Overfitting
   │  └─ Solution: Regularization, more data, simpler model

   └─ No → Both good → Done!

Practical Guidelines

  1. Start Simple: Simple model first, add complexity if needed
  2. Monitor Both Metrics: Always track training AND test performance
  3. Use Validation Set: Never tune hyperparameters on test set
  4. Try Multiple Techniques: Often combining approaches works best
  5. Domain Knowledge: Use understanding of problem
  6. Iterate: Machine learning is iterative process
  7. Document: Record what you tried and why

Regularization Parameter Tuning

The λ (lambda) parameter controls regularization strength:

Small λ: Weak regularization, may overfit Large λ: Strong regularization, may underfit

Tuning Strategy:

  1. Create range: 0.001, 0.01, 0.1, 1, 10, 100
  2. Use cross-validation for each λ
  3. Pick λ with best validation performance
  4. Evaluate on held-out test set

Conclusion

Overfitting is the most common machine learning problem, but many proven techniques prevent it. Understanding the bias-variance tradeoff guides selection of the right approach. Start with regularization and cross-validation as your first defenses. Add other techniques like early stopping, dropout, or ensemble methods as needed. The art of machine learning involves balancing model complexity with generalization - not too simple (underfitting), not too complex (overfitting), but just right for your data and problem.