Core Methods ~40 min

Generalization & Regularization

The fundamental challenge of machine learning: learning patterns that apply beyond the training data, and the techniques that prevent models from memorizing rather than understanding.

  • Understand why generalization—not training accuracy—is the true goal
  • Explain the bias-variance tradeoff and its implications
  • Apply common regularization techniques (L1, L2, dropout)
  • Use validation sets correctly to estimate generalization
  • Recognize overfitting and underfitting from learning curves

The Generalization Problem

Core Challenge

Generalization is a model's ability to perform well on data it hasn't seen during training. A model that achieves 99% accuracy on training data but fails on new data hasn't learned—it has memorized.

Intuition

Consider studying for an exam. A student who memorizes specific practice questions word-for-word will fail when questions are rephrased. A student who understands underlying concepts can handle new formulations.

Neural networks face the same challenge. With enough parameters, they can memorize any training set perfectly—but this "overfitting" means they've learned noise rather than signal, specific examples rather than patterns.

Technical

We distinguish between:

  • Training error: Performance on data used to train the model
  • Generalization error: Expected performance on unseen data from the same distribution
  • Generalization gap: The difference between training and generalization error

The goal of learning theory is to bound the generalization gap—to guarantee that low training error implies low generalization error.

In Practice

We estimate generalization error using a held-out test set—data the model never sees during training. But once you use the test set to make decisions (architecture, hyperparameters), you risk overfitting to it too.

The solution: use a validation set for development decisions, and touch the test set only once, at the very end.

The Hold-Out Methodology

Critical Principle

Never evaluate on data the model has seen. The test set must be a true hold-out that simulates deployment conditions. Using test data for model selection invalidates your performance estimates.

Training Set (60-80%)
Validation (10-20%)
Test (10-20%)

Training Set

Purpose: Fit model parameters (weights, biases)

Used for: Gradient descent, backpropagation

Look at: As many times as needed

Validation Set

Purpose: Tune hyperparameters, select architecture

Used for: Learning rate, regularization strength, early stopping, model selection

Look at: During development (but not too often)

Test Set

Purpose: Final, honest evaluation of generalization

Used for: Final performance number only

Look at: ONCE, at the very end

Data Leakage

Data leakage occurs when information from outside the training set improperly influences model development. Common forms:

  • Scaling/normalizing before splitting (uses test set statistics)
  • Feature engineering on the full dataset
  • Repeated testing → selecting models that happen to do well on test set
  • Time series: using future data to predict past

Result: Reported performance is optimistic; model fails in deployment.

Correct Workflow

  1. Split first: Before any preprocessing, separate train/val/test
  2. Fit preprocessors on training only: Compute means, scales from training set
  3. Apply same transformations: Use training statistics to transform val/test
  4. Develop on validation: Try architectures, hyperparameters, measure on validation
  5. Final evaluation: Once satisfied, evaluate once on test set
  6. Report honestly: The test set number is your generalization estimate

Cross-Validation: When Data is Limited

With small datasets, a single train/val/test split may be too variable. K-fold cross-validation rotates which portion serves as validation:

  1. Divide data into K equal folds (typically K=5 or K=10)
  2. For each fold: train on K-1 folds, validate on remaining fold
  3. Average performance across all K experiments
  4. Still reserve a separate test set for final evaluation

Cross-validation gives more reliable performance estimates but requires K times more computation.

The Bias-Variance Tradeoff

Decomposing Prediction Error

Total error can be decomposed into: Bias² + Variance + Irreducible Noise. Reducing one often increases the other—finding the sweet spot is the art of machine learning.

High Bias (Underfitting)

Model too simple for the data

Symptoms: Both training and test error are high. The model fails to capture the underlying pattern.

Example: Fitting a straight line to clearly curved data.

Fix: Increase model complexity, add features, train longer.

High Variance (Overfitting)

Model too sensitive to training data

Symptoms: Low training error but high test error. Small changes in training data cause large changes in the model.

Example: A high-degree polynomial that perfectly fits noisy training points but oscillates wildly elsewhere.

Fix: Regularization, more training data, simpler model.

Learning Curves: The Diagnostic Tool

Plot training and validation error against training set size or epochs:

  • High bias: Both curves plateau at high error; more data won't help
  • High variance: Large gap between curves; more data should help
  • Good fit: Both curves converge to low error

Regularization Techniques

Regularization constrains the model to prevent it from fitting noise. The idea: prefer simpler models even if they fit training data slightly worse—they'll generalize better.

L2 Regularization (Weight Decay)

L_total = L_data + λ Σ w²

Adds the squared magnitude of weights to the loss. Penalizes large weights, pushing them toward zero. The result: smoother, more stable functions.

Effect: All weights shrink proportionally. Prevents any single feature from dominating.

L1 Regularization (Lasso)

L_total = L_data + λ Σ |w|

Adds the absolute magnitude of weights to the loss. Can push weights all the way to zero, performing implicit feature selection.

Effect: Sparse models—many weights exactly zero. Good when you expect few features matter.

Dropout

During training, randomly zero neurons with probability p

During each forward pass, randomly "drop" neurons (set to 0). Forces the network to not rely on any single neuron—learned representations become more distributed.

Effect: Approximates training an ensemble of networks. At test time, use all neurons but scale outputs.

Early Stopping

Stop training when validation error starts increasing

Monitor validation error during training. Stop before the model has time to overfit—even if training error could still decrease.

Effect: Implicitly limits model complexity by limiting how far from initialization weights can move.

Data Augmentation

Create modified versions of training examples

For images: rotations, flips, crops, color changes. For text: synonym replacement, back-translation. Artificially increases training set size with realistic variations.

Effect: Forces invariance to transformations that shouldn't affect the label.

Batch Normalization

Normalize activations within each batch

Normalizes layer inputs to have zero mean and unit variance. Originally designed to stabilize training, but also has regularization effects.

Effect: Adds noise through batch statistics, reducing overfitting. Also enables higher learning rates.

Common Misconceptions

"100% training accuracy means the model works"

Perfect training accuracy is often a sign of overfitting. A model with enough capacity can memorize any finite training set, including its noise.

The accurate framing: What matters is validation/test accuracy. Training accuracy is meaningful only in comparison—a large gap between training and validation accuracy signals overfitting.

"More data always helps"

More data helps variance (overfitting) but not bias (underfitting). If your model is too simple to capture the pattern, more examples of the same pattern won't help.

The accurate framing: More data helps if you have high variance. If you have high bias, you need a more expressive model, not more data. Diagnose first, then prescribe.

"Regularization always hurts training performance"

While regularization does constrain the model, techniques like batch normalization can actually improve optimization, enabling faster training and sometimes better results.

The accurate framing: Regularization trades some training performance for generalization. But techniques like dropout and batch norm often have beneficial side effects on optimization dynamics.

Interactive Lab: Regularization Explorer

Experiment with different regularization techniques and see how they affect model behavior, decision boundaries, and generalization.

20%
0.00
0%

Training Data

Test Data

Train Accuracy
Test Accuracy
Gap
Select model complexity and run the experiment.

Key Observations

  • Without regularization: Boundaries can become very complex, fitting noise in training data.
  • L2 regularization: Smoother boundaries, smaller weights, better generalization.
  • Dropout: Forces distributed representations, acts like an ensemble.
  • The gap: Large train-test gap = overfitting. Increase regularization.

Check Your Understanding

1

What is the primary goal of regularization?

2

What symptom indicates high bias (underfitting)?

3

What distinguishes L1 from L2 regularization?

4

How does dropout regularize a neural network?

5

If validation error increases while training error decreases, you should:

0 / 5

Previous ← Backpropagation & Learning Next Module Embeddings & Vector Spaces →