Skip to main content

Regularization

Regularization is a technique used in machine learning to reduce overfitting. Overfitting happens when a model learns the training data too closely but performs poorly on new data. The main regularization techniques are:
  1. Dropout
  2. Weight Decay
  3. Early Stopping

1. Dropout

Dropout is mainly used in neural networks. During training, dropout randomly turns off some neurons. This prevents the model from depending too much on a small number of neurons.

How Dropout Works

Suppose we have 5 neurons:
With dropout:
Some neurons are temporarily ignored during that training step. In the next training step, a different set of neurons may be ignored.

Dropout Rate

The dropout rate tells us how many neuron outputs are randomly dropped.
For example:
means approximately 50% of the neuron outputs are dropped during training.

Why Do We Use Dropout?

Without dropout:
With dropout:

Simple Example

Imagine a student who always depends on one friend for answers. If that friend is not available, the student cannot solve the problem. Dropout is similar to making the model learn without depending on particular neurons.

Important Point

Dropout is used during training. During testing or prediction, dropout is automatically turned off.

2. Weight Decay

Weight decay is a regularization technique that prevents model weights from becoming too large. A model learns weights while training. For example:
Very large weights can make the model more complex and increase the chance of overfitting. Weight decay encourages the model to keep the weights smaller.

Basic Idea

Mathematical Formula

For L2 regularization, the total loss can be written as: L=Ltraining+λiwi2L = L_{\text{training}} + \lambda \sum_i w_i^2 Here:
  • LL = total loss
  • LtrainingL_{\text{training}} = training loss
  • wiw_i = model weight
  • λ\lambda = regularization strength
A larger λ\lambda means a stronger penalty.

Simple Example

Imagine a student trying to remember every tiny detail from a textbook. Instead of memorizing everything, the student focuses on the important concepts. Weight decay works in a similar way. It encourages the model to learn important patterns instead of using very large weights to memorize the training data.

L2 Regularization with Keras

The important part is:
This adds an L2 penalty to the weights.

Weight Decay with AdamW

Another common approach is AdamW.
Then:

Important Point

Weight decay helps keep model weights under control and can reduce overfitting.

3. Early Stopping

Early stopping stops training when the model stops improving on validation data. A model is usually trained for multiple epochs. For example:
The validation loss is decreasing, so the model is improving. Later:
Now the validation loss is increasing. This can mean the model is starting to overfit. Early stopping can stop training around the best point.

How It Works

Simple Example

Imagine studying for an exam. At first:
After studying for too long by memorizing practice questions:
Early stopping is like stopping when your performance on new questions stops improving.

Important Parameters

monitor

This tells the model to watch the validation loss.

patience

The model waits for 5 epochs without improvement before stopping.

restore_best_weights

This restores the weights from the best epoch.

Dropout vs Weight Decay vs Early Stopping


Easy Way to Remember


Using All Three Together

We can use dropout, weight decay, and early stopping in the same neural network.
The three techniques work like this:

Mathematical View

Without regularization, the model tries to minimize the training loss: minθLtraining(θ)\min_{\theta} L_{\text{training}}(\theta) With L2 regularization: minθ(Ltraining(θ)+λiθi2)\min_{\theta} \left( L_{\text{training}}(\theta) + \lambda \sum_i \theta_i^2 \right) The idea is simple: Total Loss=Training Loss+Regularization Penalty\text{Total Loss} = \text{Training Loss} + \text{Regularization Penalty} The regularization penalty discourages the model from becoming unnecessarily complex.

Quick Revision

Dropout

Meaning: Randomly turns off some neurons during training.
Purpose: Reduce overfitting. Remember:
Dropout = Drop some neurons

Weight Decay

Meaning: Penalizes large weights.
Purpose: Keep the model simpler and reduce overfitting. Remember:
Weight Decay = Control large weights

Early Stopping

Meaning: Stops training when validation performance stops improving.
Purpose: Prevent overtraining. Remember:
Early Stopping = Stop at the right time

Final Summary

Regularization helps a machine learning model perform well on new and unseen data.
The easiest way to remember:
Dropout drops neurons, Weight Decay controls weights, and Early Stopping stops training.