Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 6: Fit The Data Well

Sometimes in an application, the algorithm can run into a problem called overfitting, which can cause it to perform poorly. Let’s look at overfitting and its opposite, underfitting.

Technically we say that you want your learning algorithm to generalize well, which means to make good predictions even on brand new examples that it has never seen before.

You can say that the goal of machine learning is to find a model that hopefully is neither underfitting nor overfitting. In other words, hopefully, a model that has neither high bias nor high variance.

Overfitting vs Underfitting

Linear Regression

Let’s go back to our original example of predicting housing prices with linear regression, where you want to predict the price as a function of the size of a house.

FIG

STORY: When I think about underfitting and overfitting, high bias and high variance, I’m sometimes reminded of the children’s story of Goldilocks and the Three Bears. In this tale, a girl called Goldilocks visits the home of a bear family. There’s a bowl of porridge that’s too cold to taste and so that’s no good. There’s also a bowl of porridge that’s too hot to eat. That’s no good either. But there’s a bowl of porridge that is neither too cold nor too hot. The temperature is in the middle, which is just right to eat.

Logistic Regression

FIG

Now you’ve seen how an algorithm can underfit or have high bias or overfit and have high variance.

Addressing Overfitting

When you think overfitting has occurred, let’s talk about what you can do to address it. Let’s say you fit a model and it has high variance, is overfit.

FIG

Method 1: Collect more training data

FIG

Method 2: Feature Selection

Choosing the most appropriate set of features to use is sometimes also called feature selection. One way you could do so is to use your intuition to choose what you think is the best set of features.

NOTE:

To Summarize,

  1. all features + insufficient data => overfit

  2. selected features => just right

    1. disadvantage => useful features could be lost

Method 3: Regularization

FIG

NOTE: setting a parameter to 0 is equivalent to eliminating a feature

Recap

There are three ways to address overfitting:

  1. Collect more data

  2. Select Features (Feature Selection)

  3. Reduce size of parameters using “Regularization”

Regularization

Cost Function with Regularization

We saw that regularization tries to make the parameter values w1w_1 through wnw_n small to reduce overfitting. In this section, we’ll build on that intuition and develop a modified cost function for your learning algorithm that it can use to actually apply regularization.

Intuition

FIG

make w3,w4w_3, w_4 really small (≈0\approx 0)

Let’s say that instead of minimizing this cost function for linear regression,

minw⃗,b12m∑i=1m(fw⃗,b(X⃗(i))−y(i))2+1000w32+1000w42\underset{\vec{w}, b}{\text{min}} \frac{1}{2m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)})^2 + 1000w_{3}^2 + 1000 w_{4}^2

Because if you want to minimize this function, the only way to make this new cost function small is if W3W_3 and W4W_4 are both small, right?

Because otherwise this 1000 times W3 squared and 1000 times W4W_4 squared terms are going to be really, really big. So when you minimize this function, you’re going to end up with W3 close to 0 and W4W_4 close to 0.

So we’re effectively nearly canceling out the effects of the features x cubed and x to the power of 4 and getting rid of these two terms over here.

And if we do that, then we end up with a fit to the data that’s much closer to the quadratic function, including maybe just tiny contributions from the features x cubed and x to the fourth.

And this is good because it’s a much better fit to the data compared to if all the parameters could be large and you end up with this wiggly function.

Regularization

more generally, here’s the idea behind regularization.

The idea is that if there are smaller values for the parameters, then that’s a bit like having a simpler model. Maybe one with fewer features, which is therefore less prone to overfitting. On the last slide we penalized, or regularized only W3 and W4. But more generally, the way that regularization tends to be implemented is if you have a lot of features, say 100 features, you may not know which are the most important features and which ones to penalize.

So the way regularization is typically implemented is to penalize all of the features or more precisely, you penalize all the WJ parameters and it’s possible to show that this will usually result in fitting a smoother, simpler, less wiggly function that’s less prone to overfitting.

So for this example, if you have data with 100 features for each house, it may be hard to pick in advance which features to include and which ones to exclude.

So let’s build a model that uses all nn features.

J(w⃗,b)=12m∑i=1m(fw⃗,b(X⃗(i))−y(i))2+λ2m∑j=1nwj2+λ2mb2J(\vec{w}, b) = \frac{1}{2m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) ^ 2 + \frac{\lambda}{2m} \sum_{j = 1}^{n} w_{j}^2 + \frac{\lambda}{2m}b^2
FIG
Choosing Lambda
FIG
Regularized Linear Regression

Here is a cost function for regularized linear regression:

J(w⃗,b)=12m∑i=1m(fw⃗,b(X⃗(i))−y(i))2+λ2m∑j=1nwj2J(\vec{w}, b) = \frac{1}{2m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)})^2 + \frac{\lambda}{2m}\sum_{j = 1}^{n} w_{j}^2

our task is to

min w⃗,bJ(w⃗,b)=min w⃗,b12m∑i=1m(fw⃗,b(X⃗(i))−y(i))2+λ2m∑j=1nwj2\underset{\vec{w}, b}{\text{min }} J(\vec{w}, b) = \underset{\vec{w}, b}{\text{min }} \frac{1}{2m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)})^2 + \frac{\lambda}{2m}\sum_{j = 1}^{n} w_{j}^2

Gradient Descent is:

repeat until convergence {w=w−αddwJ(w,b)b=b−αddbJ(w,b)} simultaneous update\begin{aligned} &repeat \ until \ convergence \ \{ \\ &w = w - \alpha \frac{d}{dw}J(w, b) \\ &b = b - \alpha \frac{d}{db}J(w, b) \\ &\} \text{ simultaneous update} \end{aligned}

Previously:

∂∂wjJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))xj(i)\frac{\partial}{\partial w_{j}} J(\vec{w}, b) = \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) x_{j}^{(i)}

and

∂∂bJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))\frac{\partial}{\partial b} J(\vec{w}, b) = \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)})

Now that we’ve added this additional regularization term, the only thing that changes is:

∂∂wjJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))xj(i)+λmwj\frac{\partial}{\partial w_{j}} J(\vec{w}, b) = \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) x_{j}^{(i)} + \frac{\lambda}{m}w_{j}

and we are not regularizing b, so it stays the same:

∂∂bJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))\frac{\partial}{\partial b} J(\vec{w}, b) = \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)})

Let’s take these definitions for the derivatives and put them back into the expression on the left to write out the gradient descent algorithm for regularized linear regression.

repeat until convergence{wj=wj−α[1m∑i=1m[(fw,b(x(i))−y(i))x(i)]+λmwj]b=b−α1m∑i=1m(fw,b(x(i))−y(i))} simultaneous update\begin{aligned} &\text{repeat until convergence} \{ \\ & w_{j} = w_{j} - \alpha \big[ \frac{1}{m}\sum_{i=1}^{m} \big[(f_{w,b}(x^{(i)}) - y^{(i)}) x^{(i)}\big] + \frac{\lambda}{m}w_{j} \big] \\ &b = b - \alpha \frac{1}{m}\sum_{i=1}^{m} (f_{w,b}(x^{(i)}) - y^{(i)}) \\ & \} \text{ simultaneous update} \end{aligned}
Deeper Intuition (Math)
wj=wj−α[1m∑i=1m[(fw,b(x(i))−y(i))x(i)]+λmwj]w_{j} = w_{j} - \alpha \big[ \frac{1}{m}\sum_{i=1}^{m} \big[(f_{w,b}(x^{(i)}) - y^{(i)}) x^{(i)}\big] + \frac{\lambda}{m}w_{j} \big]

Rewrite it as:

wj=1wj−αλmwj−α1m∑i=1m(fw⃗,b(X⃗(i))−y(i))Xj(i)w_{j} = 1 w_{j} - \alpha \frac{\lambda}{m} w_{j} - \alpha \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) X_{j}^{(i)}

Simplifying:

wj=wj(1−αλm)−α1m∑i=1m(fw⃗,b(X⃗(i))−y(i))Xj(i)w_{j} = w_{j} \left( 1 - \alpha \frac{\lambda}{m} \right) - \alpha \frac{1}{m} \sum_{i=1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) X_{j}^{(i)}

You might recognize the second term as the usual gradient descent update for unregularized linear regression. This is the update for linear regression before we had regularization.

Intuition: Let’s say λ\lambda is 1 for this example and mm is the training set size, say 50. When you multiply αλm\alpha \frac{\lambda}{m}, say 0.01 times 1 divided by 50, this term ends up being a small positive number, say 0.0002, and thus, 1−αλm1 - \alpha \frac{\lambda}{m} is going to be a number just slightly less than 1, in this case, 0.9998.

How we get the derivative term
∂∂wjJ(w⃗,b)=∂∂wj[12m∑i=1m(f(X⃗(i))−y(i))2+λ2m∑j=1nwj2]\frac{\partial}{\partial w_{j}} J(\vec{w}, b) = \frac{\partial}{\partial w_{j}} \big[\frac{1}{2m} \sum_{i=1}^{m} (f(\vec{X}^{(i)})- y^{(i)})^2 + \frac{\lambda}{2m} \sum_{j=1}^{n} w_{j}^2 \big]
=12m∑i=1m[(w⃗.X⃗(i)+b−y(i))2xj(i)]+λ2m2wj= \frac{1}{2m} \sum_{i=1}^{m} \big[ (\vec{w}.\vec{X}^{(i)} + b - y^{(i)}) 2x_{j}^{(i)} \big] + \frac{\lambda}{2m} 2 w_{j}
=1m∑i=1m[(w⃗.X⃗(i)+b−y(i))xj(i)]+λmwj= \frac{1}{m} \sum_{i=1}^{m} \big[ (\vec{w}.\vec{X}^{(i)} + b - y^{(i)}) x_{j}^{(i)} \big] + \frac{\lambda}{m} w_{j}

You now know how to implement regularized linear regression.

Using this, you can really reduce overfitting when you have a lot of features and a relatively small training set. This should let you get linear regression to work much better on many problems.

Regularized Logistic Regression

Cost Function for Logistic Regression:

J(w⃗,b)=−1m∑i=1m[y(i)log⁡(fw⃗,b(X⃗(i)))+(1−y(i))log⁡(1−fw⃗.b(X⃗(i)))]J(\vec{w}, b) = -\frac{1}{m} \sum_{i = 1}^{m} \big[ y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) + (1-y^{(i)}) \log (1 - f_{\vec{w}. b} (\vec{X}^{(i)})) \big]

To add regularization, we just need to add:

J(w⃗,b)=−1m∑i=1m[y(i)log⁡(fw⃗,b(X⃗(i)))+(1−y(i))log⁡(1−fw⃗.b(X⃗(i)))]+λ2m∑j=1nwj2J(\vec{w}, b) = -\frac{1}{m} \sum_{i = 1}^{m} \big[ y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) + (1-y^{(i)}) \log (1 - f_{\vec{w}. b} (\vec{X}^{(i)})) \big] + \frac{\lambda}{2m} \sum_{j=1}^{n} w_{j}^2

When you minimize this cost function as a function of w and b, it has the effect of penalizing parameters w_1, w_2 through w_n, and preventing them from being too large.

min w⃗,bJ(w⃗,b)=min w⃗,b−1m∑i=1m[y(i)log⁡(fw⃗,b(X⃗(i)))+(1−y(i))log⁡(1−fw⃗.b(X⃗(i)))]+λ2m∑j=1nwj2\underset{\vec{w}, b}{\text{min }} J(\vec{w}, b) = \underset{\vec{w}, b}{\text{min }} -\frac{1}{m} \sum_{i = 1}^{m} \big[ y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) + (1-y^{(i)}) \log (1 - f_{\vec{w}. b} (\vec{X}^{(i)})) \big] + \frac{\lambda}{2m} \sum_{j=1}^{n} w_{j}^2

Let’s implement gradient descent with regularization:

repeat {wj=wj−α∂∂wjJ(w⃗,b)b=b−α∂∂bJ(w⃗,b)} Simultaneous Updates\begin{aligned} &\text{repeat \{} \\ &w_{j} = w_{j} - \alpha \frac{\partial}{\partial w_{j}} J(\vec{w}, b) \\ &b = b - \alpha \frac{\partial}{\partial b} J(\vec{w}, b) \\ &\} \text{ Simultaneous Updates} \end{aligned}

repeat {wj=wj−α[1m∑i=1m(fw⃗,b(X⃗(i))−y(i))xj(i)]+λmwjb=b−α[1m∑i=1m(fw⃗,b(X⃗(i))−y(i))]} Simultaneous Updates\begin{aligned} &\text{repeat \{} \\ &w_{j} = w_{j} - \alpha \left[ \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) x_{j}^{(i)} \right] + \frac{\lambda}{m} w_{j}\\ &b = b - \alpha \left[ \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) \right] \\ &\} \text{ Simultaneous Updates} \end{aligned}

b is not regularized, so its update doesn’t change.

Summary

To fit the model well:

  1. Collect more data

  2. Feature Selection

  3. Regularization