Hello! Welcome to the final lesson in our module on the Fundamentals of Model Training.
In our previous lesson, we learned how to diagnose overfitting by observing a growing gap between a model's performance on the training set versus the validation set. Recognizing this problem is a critical first step. Today, we focus on the solution.
This lesson addresses the learning outcome: Describe the purpose of regularization techniques like L1/L2 and Dropout. We will explore a powerful set of techniques designed specifically to combat overfitting, forcing our models to learn more general patterns and improve their performance on unseen data.
1. From Overfitting to Regularization
Recall the problem of overfitting: a model becomes too complex and starts memorizing the noise in the training data instead of learning the underlying signal. This often manifests as the model's weights (parameters) growing to very large values to perfectly fit the training examples.
Regularization is a collection of methods that introduce a penalty for model complexity, typically by discouraging large weights. The core idea is to modify the training process. Instead of just minimizing the loss (the error on the training data), we now aim to minimize a combination of the loss and this complexity penalty.
This forces a trade-off: the model must find a balance between fitting the data well and keeping its weights small, which generally leads to a simpler, more robust model that generalizes better.
Let's begin with a video that provides an excellent overview of this concept.
Regularization in a Neural Network | Dealing with overfitting
This video from AssemblyAI recaps the problem of overfitting and introduces the core idea of regularization as a way to limit model flexibility by penalizing large weights.
Please watch the first 3 minutes of this video. It sets the stage by explaining why we use regularization and how model complexity, often linked to large weights, contributes to overfitting.
2. Penalizing Weights: L1 and L2 Regularization
The most common way to apply a complexity penalty is by modifying the loss function. The new loss is calculated as:
Here, (lambda, often also denoted as , alpha) is a hyperparameter called the regularization strength. It controls how much we penalize complexity. A larger means a stronger penalty, forcing the model to be simpler.
There are two primary ways to calculate this penalty: L1 and L2 regularization.
Regularization in a Neural Network | Dealing with overfitting
Let's dive into L1 and L2 regularization. The same video explains how both methods add a penalty based on the model's weights to the loss function.
Watch from 4:57 to 7:23. Focus on how the penalty term is calculated for L1 (sum of absolute values of weights) and L2 (sum of squared values of weights), and the different effects they have on the model.
Let's break these down further.
L2 Regularization (Ridge or Weight Decay)
- Penalty Term: The sum of the squares of all the weights.
- Effect: L2 regularization encourages the weights to be small and distributed across many features. It pushes weights towards zero but very rarely makes them exactly zero. This results in a "dense" model where all features contribute, but none dominate.
- Why "Weight Decay"? The name comes from how it affects the gradient descent update. The gradient of the L2 penalty with respect to a weight is . This means the weight update rule looks like:
Notice the term. Since and are positive, this term is slightly less than 1. At every step, it shrinks or "decays" the weight towards zero, in addition to the normal gradient update.
This next video provides an excellent mathematical intuition for why L2 is called weight decay. Given your background, seeing the direct impact on the update rule should be insightful.
NN - 16 - L2 Regularization / Weight Decay (Theory + @PyTorch code)
To understand why L2 regularization is often called 'weight decay', let's look at its effect on the gradient descent update rule. This video from Meerkat Statistics provides a clear mathematical explanation.
Watch from 1:16 to 3:33. You don't need to memorize the derivation, but grasp the key idea: the regularization term adds a component to the gradient that systematically 'decays' or shrinks the weights towards zero at each step.
L1 Regularization (Lasso)
- Penalty Term: The sum of the absolute values of all the weights.
- Effect: L1 regularization is unique because it can shrink some weights to be exactly zero. This means it can effectively remove features from the model, performing automatic feature selection. The result is a "sparse" model, where only the most important features have non-zero weights.
L1 vs. L2: Sparsity and the Geometric View
Why does L1 produce sparse models while L2 doesn't? A geometric interpretation makes this clear. Think of the regularization term as a "budget" or a constraint region (a diamond for L1, a circle for L2). The optimization process tries to find the lowest loss (represented by elliptical contours) that still touches the constraint region.

This next article provides a more detailed comparison of L1 and L2, which will help you decide when to use each.
Understanding L1 and L2 regularization: techniques for optimized model training
To solidify the differences between L1 and L2, please read this excellent article from Weights & Biases. It provides a detailed comparison and practical examples.
Read the sections 'Understanding L1 Regularization', 'Understanding L2 Regularization', and 'Comparing L1 and L2 Regularization'. Pay special attention to the comparison table and the discussion on feature selection (sparsity) vs. handling multicollinearity.
Test your understanding!
You are building a model to predict house prices. Your dataset has 200 features, but you suspect that only about 15-20 of them are actually important (e.g., square footage, number of bedrooms). The rest might just be noise.
Which regularization technique, L1 or L2, would be a better choice to start with, and why?
Show answer
L1 regularization (Lasso) would be a better starting choice. Because L1 can shrink unimportant feature weights to exactly zero, it performs automatic feature selection. This is ideal for a situation where you believe many of your features are redundant or irrelevant, helping you build a simpler, more interpretable model that focuses only on the important predictors.
3. Dropout: Regularization by Randomness
Dropout is a completely different regularization technique that is extremely effective and widely used in neural networks. Instead of modifying the loss function, it modifies the network itself during training.
The core idea is simple: for each training step, randomly "drop out" (i.e., temporarily set to zero) a fraction of the neurons in a layer.

Let's watch a short video explaining this mechanism.
Regularization in a Neural Network | Dealing with overfitting
Now let's look at Dropout, a regularization technique that works very differently from L1 and L2.
Watch from 7:23 to 9:12. Focus on the core mechanism: randomly deactivating neurons during training. Also, note the important detail about scaling the outputs during testing to compensate.
The Intuition Behind Dropout:
- Forces Robust Features: Since any neuron can disappear at any moment, other neurons cannot rely on it. This forces each neuron to learn features that are useful on their own, leading to more robust and independent feature detectors.
- Ensemble of Networks: You can think of Dropout as training a huge number of different "thinned" networks. At each training step, you train a different sub-network. At test time, you use the full network, which can be seen as an approximation of averaging the predictions of all these thinned networks. This ensemble effect is a powerful way to reduce variance and prevent overfitting.
Important Note: During testing or inference, we do not drop any neurons. The full network is used. To ensure the overall output magnitude is consistent with training, the activations of the dropout layer are typically scaled down by the "keep probability" (1 - dropout rate). Thankfully, deep learning frameworks like PyTorch handle this for you automatically.
Conclusion
You have now learned about the primary strategies for tackling overfitting, one of the most persistent challenges in training AI models. This is a crucial skill for moving from a model that simply "runs" to one that performs well in the real world.
Key Takeaways:
- Regularization is a set of techniques used to prevent overfitting by penalizing model complexity.
- L2 Regularization (Weight Decay) adds a penalty based on the sum of squared weights. It encourages smaller, diffuse weights, leading to smoother, less complex models. It is the most common type of regularization.
- L1 Regularization (Lasso) adds a penalty based on the sum of absolute weights. It encourages sparsity by driving some weights to exactly zero, effectively performing feature selection.
- Dropout works differently by randomly deactivating a fraction of neurons during training. This prevents neurons from becoming co-dependent and acts like training a large ensemble of smaller networks.
Next up:
This lesson concludes our module on the Fundamentals of Model Training. You now have the foundational knowledge of how models are trained (gradient descent), how they are evaluated (validation sets), and how to diagnose and fix common problems (overfitting/underfitting and regularization).
In the next module, Deep Learning with PyTorch, we will leave the "from-scratch" implementations behind and start using a powerful, industry-standard deep learning framework. You will see how to implement everything we've discussed—neural networks, loss functions, optimizers, and regularization techniques like Dropout—with just a few lines of code.