Skip to content

Latest commit

 

History

History
811 lines (520 loc) · 32.2 KB

File metadata and controls

811 lines (520 loc) · 32.2 KB

1. Understanding the Generalization Gap

What is the generalization gap?

The generalization gap is the difference between how well a model performs on the data it was trained on, and how well it performs on new, unseen data. Formally, let $D$ be the true data distribution, and let $L(y,\hat y)$ be the loss. For a model parameterized by $\theta$:

  • True (expected) risk or population loss:

    $$ R(\theta) = \mathbb{E}{(x,y)\sim D}\big[L\big(y, f\theta(x)\big)\big] $$

  • Empirical risk (training loss on $n$ samples):

    $$ \hat R_n(\theta) = \frac{1}{n}\sum_{i=1}^n L\big(y_i, f_\theta(x_i)\big) $$

  • Generalization gap:

    $$ \text{Gap}(\theta) = R(\theta) - \hat R_n(\theta) $$

A small gap means training performance reflects real-world performance. A large positive gap means the model fits training data well but fails on new data, which is the usual symptom of overfitting.


Why it matters

  • Real-world systems must perform well on unseen examples, not just on the training set.
  • Deployment risk, safety, fairness, and economic cost all depend on good generalization.
  • Most model selection and hyperparameter tuning aim to minimize the generalization gap.

Why networks overfit or underfit

Overfitting — model performs well on training but poorly on unseen data

Root causes

  • Excessive model capacity: too many parameters relative to data, model can memorize training examples.
  • Insufficient training data: small $n$ means empirical risk is a poor estimate of true risk.
  • High noise in labels: model learns noise or annotation errors.
  • Data leakage: training data contains information that will not be available at test time, producing artificially low training error but poor real performance.
  • Poor regularization: no or weak penalties that prevent complexity.
  • Overtraining: keep training after the point validation loss starts increasing.
  • Class imbalance: rare classes cause the model to focus on majority patterns only.

Manifestation

  • Training loss low, validation/test loss high.
  • Large gap between training accuracy and validation accuracy.

Underfitting — model cannot even fit the training data well

Root causes

  • Insufficient model capacity: model too simple to capture underlying function.
  • Too much regularization: strong L1/L2 or dropout that prevents learning.
  • Poor features or architecture: wrong inductive bias for the task.
  • Insufficient training time or improper optimization (bad learning rate).
  • Bad hyperparameters: e.g., tiny network, too large batch size without adaptation.

Manifestation

  • Both training and validation loss are high and close to each other, model has high bias.

Bias–Variance decomposition (intuition and formula, for squared loss)

For squared loss and a fixed input $x$, expected error decomposes into:

$$ \mathbb{E}\big[(y - \hat f(x))^2\big] = \underbrace{(\text{Bias})^2}_{\text{error from wrong model family}} + \underbrace{\text{Variance}}_{\text{error from sensitivity to training data}} + \underbrace{\text{Noise}}_{\text{irreducible}} $$

  • High bias (low variance) → underfitting.
  • High variance (low bias) → overfitting.
  • Tradeoff: increasing model complexity typically reduces bias but increases variance.

Practical diagnostics — learning curves and what they tell you

Plot training and validation loss (or accuracy) vs epochs:

  • Underfitting: training loss high, validation loss similar and high. Remedy: bigger model, less regularization, more training.
  • Good fit: both training and validation losses low and close.
  • Overfitting: training loss low, validation loss significantly higher. Remedy: regularization, more data, early stopping.

Quick map:

Pattern Interpretation
Train low, Val high Overfitting
Train high, Val high Underfitting
Train low, Val low Good generalization
Val < Train (rare) Possible data leakage or metric error

Remedies: how to reduce the generalization gap

Data-level

  • More labeled data: most reliable way to improve generalization.
  • Data augmentation: artificially enlarge dataset, e.g., flips, crops, noise for images; synonym/word dropout for text.
  • Better labeling / clean labels: reduce label noise.
  • Address class imbalance: reweighting, oversampling, focal loss.

Model-level / algorithmic

  • Reduce capacity: smaller network, fewer layers, pruning.

  • Regularization:

    • L2 weight decay: add $\lambda |w|^2$ to loss, discourages large weights.
    • L1 regularization: promotes sparsity.
    • Dropout: randomly drop activations during training, acts like ensemble averaging.
    • Batch Normalization: stabilizes and sometimes improves generalization.
    • Label smoothing: prevents overconfidence by softening targets.
  • Early stopping: stop training when validation loss stops improving, acts as regularizer.

  • Ensembling: average predictions of multiple models to reduce variance.

  • Optimizer choice and hyperparameters: learning rates, batch size, Adam vs SGD with momentum, learning rate schedules, warm restarts. Small batch sizes sometimes increase implicit regularization.

Evaluation and selection

  • Cross-validation: k-fold CV for robust performance estimates, nested CV for hyperparameter tuning.
  • Holdout validation set: keep an untouched test set for final evaluation.
  • Careful metric selection: accuracy may hide problems for imbalanced data, use precision, recall, F1, ROC-AUC, etc.

Robustness & real-world concerns

  • Domain shift: covariate shift, label shift, concept drift. Monitor data distribution and adapt via domain adaptation or continual learning.
  • Detect data leakage: ensure no features leak label info, avoid using test-time information in training.
  • OOD detection: flag inputs outside training distribution.

Practical checklist to reduce generalization gap

  1. Inspect learning curves.
  2. Check for data leakage.
  3. If underfitting: increase capacity, train longer, reduce reg strength.
  4. If overfitting: get more data, augment, add regularization, use early stopping, reduce complexity, try ensembling.
  5. Use cross-validation for robust model selection.
  6. Tune optimizer and learning rate schedule.
  7. Re-evaluate with a clean test set that simulates real-world distribution.

Short illustrative example (brief)

  • Suppose training accuracy is 99%, validation accuracy is 75%, gap = 24%. That signals overfitting. Try data augmentation, dropout, or smaller model. If both accuracies are 70%, underfitting is likely, try a larger model or more training.

Summary — compact

  • Generalization gap = true risk minus empirical risk.
  • Overfitting happens when a model captures noise or memorizes, producing low train error but high test error. Underfitting happens when a model is too simple or not trained enough.
  • Causes include model capacity, data size and quality, optimization dynamics, and data leakage.
  • Remedies: more/better data, augmentation, regularization, early stopping, ensembling, careful model selection and validation.
  • Modern deep learning adds complexity, with phenomena like double descent and implicit regularization, so practical diagnostics and fixes remain central.

2. How to Avoid Overfitting and Underfitting: Balancing Model Complexity and Training Data

The key to building effective neural networks is achieving a balance between bias and variance. Overfitting occurs when the model has learned the training set too well, including noise and outliers, which reduces its ability to generalize. Underfitting occurs when the model is too simple or poorly trained to capture the underlying structure of the data.

Factors to balance

  1. Model Complexity

    • Deep networks with many layers and millions of parameters can represent very complex functions. However, with limited data, this capacity often leads to memorization (overfitting).
    • Simpler models (fewer layers, smaller hidden units) may fail to capture intricate patterns (underfitting).
    • The right balance depends on dataset size and difficulty: complex models require either larger datasets or stronger regularization.
  2. Training Data

    • The amount and quality of data directly impact whether a model generalizes.
    • More diverse data reduces variance by exposing the model to more scenarios.
    • Poor data (e.g., with noise or label errors) leads to overfitting because the model tries to fit random fluctuations.
  3. Training Dynamics

    • Longer training without proper control often drives overfitting.
    • Too few epochs can leave the model underfitted.
    • Monitoring validation performance ensures training stops at the right point.
  4. Evaluation and Hyperparameters

    • Choosing the right learning rate, optimizer, and batch size contributes to balanced generalization.
    • Hyperparameter tuning (via cross-validation or grid search) is critical to prevent both extremes.

3. Regularization Techniques

Regularization adds constraints or modifications to the training process to prevent the model from fitting noise. Below are the most widely used techniques:

3.1. L1 and L2 Regularization

  • L1 Regularization (Lasso)

    • Adds the sum of absolute values of weights to the loss:

      $$ L_{total} = L_{original} + \lambda \sum |w_i| $$

    • Encourages sparsity: drives some weights to zero, leading to simpler models and feature selection.

    • Useful when many input features may be irrelevant.

  • L2 Regularization (Ridge)

    • Adds the sum of squared weights to the loss:

      $$ L_{total} = L_{original} + \lambda \sum w_i^2 $$

    • Shrinks weights but rarely forces them to zero.

    • Distributes learning more evenly across all features.

    • More common in deep learning than L1 because it works smoothly with gradient-based optimization.

  • Elastic Net

    • Combination of L1 and L2.
    • Useful when both sparsity and smoothness are needed.

3.2. Early Stopping

  • Training is monitored using both training and validation losses.
  • Typically, training loss continues to decrease, but validation loss eventually starts increasing due to overfitting.
  • Early stopping halts training once validation performance degrades, preventing the model from memorizing noise.
  • Advantages: simple, computationally efficient, does not require altering the architecture.
  • Often combined with checkpointing to save the best-performing model during training.

3.3. Dropout

  • During training, randomly sets a proportion of neurons (e.g., 20–50%) to zero at each iteration.
  • This forces the network to learn redundant representations rather than depending heavily on specific neurons.
  • Acts like training multiple sub-networks and averaging them at inference (an implicit ensemble).
  • Reduces co-adaptation of neurons, leading to better generalization.
  • At test time, all neurons are used but their activations are scaled by the dropout rate to match training distribution.
  • Side effects: slows convergence slightly, requires more epochs to train.


3.4. Batch Size and Its Effect on Convergence & Stability

What is Batch Size?

  • The number of training samples used to compute the gradient before updating weights.
  • A key hyperparameter in training.

Effects of Batch Size

1. Small Batch Size (e.g., 16, 32)

  • Pros:

    • Noisy gradients → acts like regularization, preventing overfitting.
    • Better generalization (helps find flatter minima).
  • Cons:

    • Training slower per epoch.
    • Noisier convergence curve (zigzaggy loss function).
    • May under-utilize GPUs.

2. Large Batch Size (e.g., 256, 512, 1024)

  • Pros:

    • Faster training per epoch (parallelization on GPUs).
    • More stable gradient estimates.
  • Cons:

    • Poor generalization (sharp minima, overfitting risk).
    • Requires lower learning rates to avoid divergence.
    • Higher memory requirements.

Batch Size and Learning Rate Scaling

There’s a well-known heuristic:

$$ \eta_{new} = \eta_{old} \times \frac{B_{new}}{B_{old}} $$

  • When batch size increases, learning rate should also increase proportionally to maintain stability (used in large-scale training like ImageNet).

BN and Batch Size Interaction

  • BN relies on mini-batch statistics. If batch size is too small (like 2, 4, 8), the estimates of mean/variance become unstable → training may oscillate.
  • Small batches + BN → leads to poor convergence.
  • Alternatives in such cases: Layer Normalization, Group Normalization, Instance Normalization.

Summary: BN vs. Batch Size in Convergence

Concept Effect on Convergence Effect on Stability Effect on Generalization
Batch Normalization Faster convergence Stabilizes gradients Adds mild regularization
Small Batch Size Slower per epoch Noisy updates Better generalization
Large Batch Size Faster per epoch Stable gradients Risk of overfitting
BN + Large Batch Works well Very stable Good training, risk of sharp minima
BN + Tiny Batch Unstable Oscillations Bad performance

In practice:

  • Use mini-batch sizes between 32 and 256 for balanced performance.
  • Combine with Batch Normalization for stable and fast convergence.
  • For very small batch sizes, prefer LayerNorm or GroupNorm instead of BN.

3.5. Other Regularization Methods

  • Batch Normalization: stabilizes training and sometimes improves generalization by reducing internal covariate shift.
  • Label Smoothing: softens target probabilities, discouraging the network from becoming overconfident.
  • Weight Constraints: limits the maximum norm of weights to prevent runaway growth.
  • Data Noise Injection: adding small Gaussian noise to inputs or weights during training can also regularize.

4. Data Augmentation: Using Data to Improve Model Generalization

Data augmentation artificially increases the effective size and diversity of the training dataset by creating modified versions of existing samples. It improves generalization by preventing the model from memorizing specific examples.

4.1. Why it works

  • Enlarges dataset without collecting new samples.
  • Introduces variations that the model must learn to handle.
  • Reduces variance, combats overfitting, and encourages learning more robust features.

4.2. Common Augmentation Techniques

  • Images:

    • Geometric transformations: rotations, flips, cropping, translations, scaling, affine transformations.
    • Color/lighting variations: brightness, contrast, hue, saturation changes.
    • Noise: Gaussian noise, blur, cutout (masking random regions).
    • Advanced: Mixup (blending two images and labels), CutMix (combining image patches with label mixing).
  • Text (NLP):

    • Synonym replacement, back-translation, random word deletion, word embedding mixup.
    • Language-specific augmentations like character swaps or shuffling.
  • Audio:

    • Time shifting, pitch shifting, adding background noise, SpecAugment (masking regions in spectrograms).
  • Tabular Data:

    • Adding small Gaussian noise, synthetic oversampling methods like SMOTE for class imbalance.

4.3. Benefits

  • Reduces overfitting by making training examples less “memorized.”
  • Encourages invariance: e.g., a cat is still a cat if flipped horizontally or brightened.
  • Effective in small datasets where collecting more data is expensive.
  • Often improves robustness against adversarial examples and domain shifts.

4.4. Limitations

  • If augmentation is unrealistic (e.g., flipping digits in handwritten numbers), it can harm performance.
  • Needs task-specific design: audio and text require different augmentation methods than images.
  • Augmented data increases training time.

Summary

  • Avoiding overfitting and underfitting requires a balance between model complexity, dataset size, and training strategy.
  • Regularization methods such as L1/L2 penalties, early stopping, and dropout constrain models to generalize better.
  • Data augmentation creates synthetic variety, making models more robust to unseen conditions.
  • In practice, a combination of these methods is used together: e.g., data augmentation + dropout + early stopping.

5. Hyperparameter Tuning: Cross-Validation and K-fold Cross Validation


What is Hyperparameter Tuning?

Hyperparameters are the external configuration settings of a machine learning model that are not learned directly from the data but control the learning process. Examples include:

  • Learning rate ($\eta$)
  • Number of hidden layers and neurons in a neural network
  • Batch size
  • Dropout rate
  • Regularization strength ($\lambda$)
  • Optimizer choice (Adam, SGD, RMSProp, etc.)

Hyperparameter tuning is the process of finding the optimal combination of these hyperparameters to achieve the best generalization performance on unseen data.


Why Hyperparameter Tuning is Important

  • The same model can perform very differently depending on hyperparameter choices.
  • Proper tuning reduces both overfitting (too complex model) and underfitting (too simple model).
  • Ensures fair comparison between models — bad hyperparameter choices can make even a powerful algorithm look poor.
  • In deep learning, tuning learning rate and batch size often makes the biggest difference in convergence and final accuracy.

Techniques for Hyperparameter Tuning

  1. Grid Search

    • Exhaustively tries all combinations of hyperparameters in a predefined grid.
    • Advantage: systematic, guarantees testing all options.
    • Disadvantage: very expensive for large search spaces.
  2. Random Search

    • Samples random combinations of hyperparameters.
    • Often more efficient than grid search, as not all parameters equally influence performance.
    • Works well when only a few hyperparameters matter most.
  3. Bayesian Optimization

    • Models the performance as a function of hyperparameters and chooses new hyperparameters based on past performance.
    • Smarter search compared to random or grid.
    • More efficient but mathematically heavier.
  4. Automated Methods (AutoML, Hyperband, Optuna, Ray Tune)

    • Use advanced search + pruning methods to quickly discard bad hyperparameter candidates.
    • Scalable for deep learning and large datasets.

Cross-Validation (CV)

Cross-validation is a resampling method to evaluate the performance of models and hyperparameters more reliably. It ensures that the model’s performance estimate is not dependent on a particular train/test split.

  • Basic Idea: Split dataset into training and validation sets multiple times, train on one part, validate on the other, then average results.
  • Helps in reducing variance of performance estimates.
  • Detects overfitting to a specific validation split.

K-Fold Cross-Validation

One of the most widely used forms of cross-validation.

How it works

  1. Shuffle dataset randomly.

  2. Split data into K folds (subsets) of equal size.

  3. For each iteration:

    • Use $K-1$ folds as training data.
    • Use the remaining 1 fold as validation.
    • Train model and record performance metric (e.g., accuracy, F1-score, MSE).
  4. Repeat until every fold has been used as validation once.

  5. Average the K results → gives a robust estimate of performance.

Formula for error estimate

$$ CV_{error} = \frac{1}{K} \sum_{i=1}^K \text{Error}_{i} $$

Advantages

  • More reliable estimate than a single train/test split.
  • Uses the full dataset for both training and validation.
  • Reduces risk of model evaluation depending on “lucky” or “unlucky” splits.

Disadvantages

  • Computationally expensive for large models like deep neural networks.
  • Not commonly used for very large datasets (e.g., ImageNet) due to cost — instead, a single validation set or train/val/test split is preferred.

Special Variants of Cross-Validation

  1. Stratified K-Fold Cross Validation

    • Ensures that each fold has the same class proportion as the full dataset.
    • Very important for imbalanced datasets (e.g., fraud detection, medical data).
  2. Leave-One-Out Cross Validation (LOOCV)

    • Extreme case where $K = N$ (number of data points).
    • Each sample is used once as validation.
    • Very accurate but computationally infeasible for large datasets.
  3. Nested Cross-Validation

    • Two levels of cross-validation: inner loop for hyperparameter tuning, outer loop for performance estimation.
    • Prevents information leakage from validation into model selection.
    • Used when both tuning and performance estimation need to be unbiased.

Cross-Validation in Neural Networks

  • Often replaced with a train/validation/test split because training deep nets multiple times is costly.
  • Common practice: use validation set for hyperparameter tuning, then test set for final evaluation.
  • For small datasets (medical imaging, bioinformatics), K-Fold CV is still used.

Practical Example: Tuning Learning Rate

  • Suppose we want to find the best learning rate for training.
  • We run a K-fold CV for learning rates $[0.1, 0.01, 0.001]$.
  • After 5-fold validation, we find average accuracy is best for $0.01$.
  • We then retrain on full training set with learning rate $0.01$ and test on the held-out test set.

Summary

  • Hyperparameter tuning is essential for optimal model performance.
  • Cross-validation provides a reliable method to evaluate hyperparameter choices.
  • K-Fold CV is the most popular form, balancing reliability and efficiency.
  • In deep learning, large datasets make K-Fold impractical, but validation sets play a similar role.

7. Learning Efficiently: Optimization Methods

Background

In machine learning and deep learning, optimization is the process of adjusting model parameters (weights and biases) to minimize the loss function. Optimizers guide how parameters are updated during backpropagation, making them one of the most critical components of training.

Different optimization methods have been designed to balance convergence speed, stability, and generalization ability. For shallow models (like linear regression, logistic regression, or SVMs), simpler methods like Gradient Descent (GD) are sufficient. However, in deep learning with millions of parameters, advanced optimizers like Adam or RMSProp are widely used to handle complex error surfaces, sparse gradients, and large datasets efficiently.

Optimizers are used across many areas:

  • Deep Learning: Training neural networks (CNNs, RNNs, Transformers).
  • Reinforcement Learning: Updating policy and value networks.
  • Natural Language Processing (NLP): Large-scale training (e.g., GPT, BERT).
  • Computer Vision: Image classification, object detection, GANs.
  • Recommender Systems: Matrix factorization and neural recommendation models.

Basic Optimizers

1. Gradient Descent (GD)

  • Update Rule:

$$ \theta := \theta - \eta \cdot \nabla_{\theta} J(\theta) $$

where: $\theta$ = parameters (weights), $\eta$ = learning rate, $\nabla_{\theta} J(\theta)$ = gradient of cost function.

  • Works well for small datasets.
  • Requires computing gradient on entire dataset, making it slow and memory-intensive.

2. Stochastic Gradient Descent (SGD)

  • Updates weights using only one sample at a time.
  • Faster than GD, introduces randomness which helps escape local minima.
  • However, noisy updates make convergence less stable.

3. Mini-Batch Gradient Descent

  • Compromise between GD and SGD.
  • Uses small random batches of data to compute gradients.
  • Balances efficiency and stability.
  • The default training method in modern deep learning frameworks.

Advanced Optimizers

4. Momentum

  • Accelerates convergence by considering past gradients.
  • Update rule:

$$ v_t = \beta v_{t-1} + \eta \nabla_{\theta} J(\theta) $$

$$ \theta := \theta - v_t $$

where $\beta$ is the momentum factor (typically 0.9).

  • Helps escape local minima and smoothens updates.
  • Intuition: like a ball rolling down a hill, gaining momentum.

5. Adagrad (Adaptive Gradient Algorithm)

  • Adapts learning rate for each parameter individually.

  • Parameters with frequent updates get smaller learning rates, rare features get larger learning rates.

  • Useful for sparse data (e.g., NLP word embeddings).

  • Limitation: learning rate keeps decreasing and may become too small.


6. RMSProp (Root Mean Square Propagation)

  • Fixes Adagrad’s issue of decaying learning rates.
  • Uses an exponentially decaying average of squared gradients.
  • Works very well for non-stationary problems like training RNNs.
  • Update rule:

$$ \theta := \theta - \frac{\eta}{\sqrt{E[g^2]_t + \epsilon}} g_t $$


7. Adam (Adaptive Moment Estimation)

  • Combines Momentum and RMSProp.

  • Maintains running averages of both gradients (first moment) and squared gradients (second moment).

  • Most popular optimizer for deep learning due to:

    • Fast convergence
    • Adaptive learning rates
    • Works well with sparse gradients
  • Update rule (simplified):

$$ m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t $$

$$ v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 $$

$$ \theta := \theta - \eta \frac{m_t}{\sqrt{v_t} + \epsilon} $$

  • Default choice in frameworks like TensorFlow and PyTorch.

Other Optimizers

  • Nadam (Nesterov-accelerated Adam): Adds Nesterov momentum to Adam, making it slightly more responsive.
  • AdaMax: Variant of Adam based on the infinity norm, more stable with large gradients.
  • AMSGrad: Modification of Adam to fix convergence issues by enforcing a non-increasing second moment.
  • L-BFGS (Limited-memory Broyden–Fletcher–Goldfarb–Shanno): Quasi-Newton method, used in traditional ML for smaller models.
  • SGD with Learning Rate Scheduling: Decays learning rate over time (step decay, exponential decay, cosine annealing).

Comparison of Optimizers

Optimizer Speed Stability Memory Best For
GD Very slow Stable Low Small datasets
SGD Fast Noisy Low Large datasets
Mini-batch GD Balanced Stable Medium Most DL tasks
Momentum Faster convergence Stable Medium Deep networks
Adagrad Good for sparse data Poor long-term Medium NLP
RMSProp Good with non-stationary Stable Medium RNNs
Adam Fast & adaptive Very popular Medium Almost all DL
Nadam Even faster than Adam Stable Medium Advanced DL

Summary

  • Optimizers control how networks learn by updating weights.
  • Classical methods like GD and SGD are foundational, but deep learning usually relies on advanced optimizers like Adam, RMSProp, and Momentum.
  • Choice of optimizer significantly affects training time, convergence, and final accuracy.
  • Hyperparameter tuning (learning rate, decay, momentum factors) is crucial for making optimizers effective.