Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 27

Regularization: Preventing Overfitting and Controlling Complexity

Recommended resources

Hello! Welcome to the fifth module of our course.

In our last few lessons, we've focused on building powerful classifiers like Logistic and Softmax Regression from scratch. You successfully implemented a model that achieved perfect accuracy on a clean, linearly separable dataset. This is a great achievement, but real-world data is rarely so clean.

Today, we'll address a critical question: What happens when our model is too powerful for the data it's learning from? This can lead to a common pitfall in machine learning called overfitting. This lesson will explain what overfitting is, how it relates to the complexity of a model, and introduce regularization, a fundamental technique used to combat it.

This is a conceptual lesson that lays the groundwork for the next two lessons, where you will implement regularization techniques from scratch.

1. The Problem of Overfitting: When Models Learn Too Much

An overfit model is like a student who memorizes the answers to a practice exam but hasn't learned the underlying concepts. They'll ace the practice test, but fail the real one. In machine learning, this means the model performs exceptionally well on the training data but poorly on new, unseen data (like a validation or test set).

The opposite of overfitting is underfitting. An underfit model is too simple to capture the underlying patterns in the data. This would be like trying to fit a straight line to data that follows a curve.

Overfitting, Underfitting, and Good Balance in Machine Learning Models
This image illustrates the key concepts. **High bias (Underfitting)** means the model is too simple. **High variance (Overfitting)** means the model is too complex and captures noise in the training data. The **Good Balance** model generalizes well.

The Bias-Variance Trade-off

The concepts of underfitting and overfitting are formally described by the bias-variance trade-off.

  • Bias is the error from erroneous assumptions in the learning algorithm. High bias can cause an algorithm to miss relevant relations between features and target outputs (underfitting). A simple linear model, for instance, has high bias if the true relationship is non-linear.
  • Variance is the error from sensitivity to small fluctuations in the training set. High variance can cause an algorithm to model the random noise in the training data, rather than the intended outputs (overfitting).

To get a more intuitive feel for bias and variance, watch the following video from StatQuest.

Machine Learning Fundamentals: Bias and Variance

This video provides a clear, visual explanation of bias and variance, which are the core concepts behind underfitting and overfitting.

Watch from 01:45 to 06:06. The video will first explain bias using a simple straight line, then variance using a complex 'squiggly' line. Finally, it will introduce the idea of finding a 'sweet spot' between them, which is where regularization comes in.

As the video explains, our goal is to find a model with both low bias and low variance. We want a model that is complex enough to capture the true underlying patterns but not so complex that it models the noise.

Model Complexity vs. Error and Variance
This graph shows the typical behavior of training and validation error as model complexity increases. In the overfitting region, the model becomes too complex. It gets better at fitting the training data (training error decreases), but worse at generalizing to new data (validation error increases).

2. What is Regularization?

Now that we understand the problem, let's look at the solution. Regularization is a set of techniques used to prevent overfitting by adding a penalty term to the model's loss function.

The standard loss function (like MSE or Cross-Entropy) encourages the model to minimize its prediction error on the training data. The new, regularized loss function looks like this:

Here, (lambda, also called alpha in many libraries) is a hyperparameter that controls the strength of the regularization.

The key idea is that the Regularization Penalty is a function of the model's parameters (its weights). This penalty term discourages the model from learning large weights.

Why does this help? A model with large weights is often a complex one. The output of such a model can change drastically with small changes in the input features, leading to the kind of jagged, noise-fitting decision boundaries characteristic of overfitting. By penalizing large weights, we encourage the model to find a simpler solution that generalizes better.

For a deeper dive into this concept, let's turn to a well-written article.

Fighting Overfitting With L1 or L2 Regularization

This article, 'Fighting Overfitting With L1 or L2 Regularization', provides a solid introduction to the problem and the role of regularization as a solution.

Read the first two sections, 'What is regularization and why do we need it?' and the introduction before it. Focus on the definitions of overfitting and regularization, and the core idea that we add bias to the model to improve its generalization.

Test your understanding!

Imagine you are training a logistic regression model and you set the regularization hyperparameter to an extremely large value. What would you expect to happen to the model's weights? What would this do to the model's predictions?

Show answer

If is extremely large, the regularization penalty will dominate the loss function. To minimize this new loss, the optimization algorithm (gradient descent) will be forced to make the model's weights very close to zero, regardless of the prediction error on the training data.

With weights close to zero, the model becomes extremely simple. For logistic regression, the output of the linear layer () would be close to zero for any input . Passing this through the sigmoid function would result in a prediction of ~0.5 for all samples. The model effectively loses its ability to discriminate between classes and will perform poorly (high bias/underfitting).


3. How Regularization Affects Model Complexity

The optimization process now faces a trade-off. It wants to minimize the original loss (fit the training data well) but also minimize the regularization penalty (keep the weights small).

  • If : There is no penalty. The model is free to learn large weights and overfit the training data.
  • If : The model is punished for having large weights. It must find a balance. It might accept a slightly higher training error in exchange for having smaller weights, which typically leads to a lower error on unseen data.
  • If : As we discussed, the penalty becomes all-important, and the weights are driven to zero, leading to underfitting.

This trade-off is the essence of how regularization simplifies a model. It pulls the model away from a "perfect" solution that is tailored only to the training set and towards a simpler, more robust solution.

The following article offers several excellent intuitions for why this works.

Intuitions on L1 and L2 Regularisation - Towards Data ...

This article, 'Intuitions on L1 and L2 Regularisation', offers a clear, non-mathematical explanation of how adding a penalty term helps prevent overfitting.

Read the section titled '4) How is overfitting prevented?'. Focus on 'Intuition B' and 'Intuition C'. These provide a great conceptual model for thinking about regularization as 'tainting' a perfect-but-overfit solution to achieve better generalization.

There are different ways to define the regularization penalty, with the two most common being L1 and L2 regularization.

  • L2 Regularization (Ridge): The penalty is the sum of the squared values of the weights. This encourages all weights to be small.
  • L1 Regularization (Lasso): The penalty is the sum of the absolute values of the weights. This can push some weights to become exactly zero, effectively performing feature selection.

We will dive deep into the implementation and specific effects of L1 and L2 in the upcoming lessons.


Conclusion

In this lesson, you've built the conceptual foundation for understanding one of the most important techniques for training modern machine learning models.

Key Takeaways:

  • Overfitting occurs when a model is too complex, learning the noise in the training data and failing to generalize to new data. This is a state of high variance.
  • Model complexity is often associated with large parameter (weight) values.
  • Regularization prevents overfitting by adding a penalty for large weights to the loss function.
  • This forces the model to find a simpler solution that balances fitting the training data with maintaining small weights, leading to better generalization.
  • The strength of regularization is controlled by a hyperparameter, , which must be tuned to find the right balance between underfitting and overfitting.

Preview of the Next Lesson:
With the "why" firmly established, we can now move on to the "how." In the next lesson, you will modify the cost function and its gradient to include an L2 (Ridge) regularization term. We will derive the new gradient update rule and apply it to the regression and classification models you have already built, seeing firsthand how it affects the learning process.