3.9 Regularization to Reduce Overfitting | Cost function with regularization- [ML | Andrew Ng]
 In the last video, we saw that regularization tries to make the parameter values w1 through wn small to reduce overfitting. In this video, we'll build on that intuition and develop a modified cost function for your learning algorithm they can use to actually apply regularization. Let's jump in. Recall this example from the previous video in which we saw that if you fit a quadratic function to this data, it gives a pretty good fit. But if you fit a very high order polynomial, you end up with a curve that overfits the data. But now consider the following. Suppose that you had a way to make the parameters w3 and w4 really, really small, say close to zero. Here's what I mean. Let's say instead of minimizing this objective function, this is a cost function for linear regression. Let's say you were to modify the cost function and add to it 1000 times w3 squared plus 1000 times w4 squared. And here I'm just choosing 1000 because it's a big number, but any other really large number would be okay. So with this modified cost function, you'd in fact be penalizing the model if w3 and w4 are large. Because if you want to minimize this function, the only way to make this new cost function small is if w3 and w4 are both small, right? Because otherwise this 1000 times w3 squared and 1000 times w4 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 zero and w4 close to zero. So we're effectively nearly canceling out the effect of the features x cubed and x to the power of four 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 quadratic function. 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 penalize or we say we regularize 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 100 features. So you have these 100 parameters, w1 through w100, as well as the 101st parameter b. Because we don't know which of these parameters are going to be the important ones, let's penalize all of them a bit and shrink all of them by adding this new term lambda times the sum from j equals 1 through n, where n is 100, the number of features of wj squared. This value lambda here is the Greek alphabet lambda, and it's also called a regularization parameter. So similar to picking a learning rate alpha, you now also have to choose a number for lambda. A couple things I would like to point out, by convention, instead of using lambda times the sum of wj squared, we also divide lambda by 2m, so that both the first and second terms here are scaled by 1 over 2m. It turns out that by scaling both terms the same way, it becomes a little bit easier to choose a good value for lambda, and in particular, you find that even if your training set size grows, say you find more training examples, so m, the training set size, is now bigger, the same value of lambda that you picked previously is now also more likely to continue to work if you have this extra scaling by 2m. Also by the way, by convention, we're not going to penalize the parameter b for being large. In practice, it makes very little difference whether you do or not, and some machine learning engineers, and actually some learning algorithm implementations, will also include lambda over 2m times the b squared term. But this makes very little difference in practice, and the more common convention which we'll use in this course is to regularize only the parameters w rather than the parameter b. So to summarize, in this modified cost function, we want to minimize the original cost, which is the mean squared error cost, plus additionally the second term, which is called the regularization term. And so this new cost function trades off two goals that you might have. Trying to minimize this first term encourages the algorithm to fit the training data well by minimizing the squared differences of the predictions and the actual values, and trying to minimize the second term, the algorithm also tries to keep the parameters wj small, which will tend to reduce overfitting. The value of lambda that you choose specifies the relative importance or the relative tradeoff or how you balance between these two goals. Let's take a look at what different values of lambda will cause your learning algorithm to do. Let's use the housing price prediction example using linear regression. So f of x is the linear regression model. If lambda was set to be zero, then you're not using the regularization term at all, because the regularization term is multiplied by zero. And so if lambda was zero, you end up fitting this overly wiggly, overly complex curve, and it overfits. So that was one extreme of if lambda was zero. Let's now look at the other extreme. If you set lambda to be a really, really, really large number, say lambda equals 10 to the power of 10, then you're placing a very heavy weight on this regularization term on the right. And the only way to minimize this is to be sure that all the values of w are pretty much very close to zero. So if lambda is very, very large, the learning algorithm will choose w1, w2, w3, and w4 to be extremely close to zero. And thus, f of x is basically equal to b, and so the learning algorithm fits a horizontal straight line and underfits. To recap, if lambda is zero, this model will overfit. If lambda is enormous, like 10 to the power of 10, this model will underfit. And so what you want is some value of lambda that is in between that more appropriately balances these first and second terms of trading off, minimizing the mean squared error and keeping the parameters small. And when the value of lambda is not too small and not too large, but just right, then hopefully you end up able to fit a fourth order polynomial keeping all of these features, but with a function that looks like this. So that's how regularization works. When we talk about model selection later in the specialization, we'll also see a variety of ways to choose good values for lambda. In the next two videos, we'll flesh out how to apply regularization to linear regression and logistic regression, and how to train these models with gradient descent. With that, you'll be able to avoid overfitting with both of these algorithms.
[{"start": 0.0, "end": 7.28, "text": " In the last video, we saw that regularization tries to make the parameter values w1 through"}, {"start": 7.28, "end": 10.8, "text": " wn small to reduce overfitting."}, {"start": 10.8, "end": 15.84, "text": " In this video, we'll build on that intuition and develop a modified cost function for your"}, {"start": 15.84, "end": 20.64, "text": " learning algorithm they can use to actually apply regularization."}, {"start": 20.64, "end": 22.12, "text": " Let's jump in."}, {"start": 22.12, "end": 28.16, "text": " Recall this example from the previous video in which we saw that if you fit a quadratic"}, {"start": 28.16, "end": 32.0, "text": " function to this data, it gives a pretty good fit."}, {"start": 32.0, "end": 36.88, "text": " But if you fit a very high order polynomial, you end up with a curve that overfits the"}, {"start": 36.88, "end": 37.88, "text": " data."}, {"start": 37.88, "end": 41.6, "text": " But now consider the following."}, {"start": 41.6, "end": 48.879999999999995, "text": " Suppose that you had a way to make the parameters w3 and w4 really, really small, say close"}, {"start": 48.879999999999995, "end": 49.879999999999995, "text": " to zero."}, {"start": 49.879999999999995, "end": 52.400000000000006, "text": " Here's what I mean."}, {"start": 52.4, "end": 58.56, "text": " Let's say instead of minimizing this objective function, this is a cost function for linear"}, {"start": 58.56, "end": 60.32, "text": " regression."}, {"start": 60.32, "end": 68.2, "text": " Let's say you were to modify the cost function and add to it 1000 times w3 squared plus 1000"}, {"start": 68.2, "end": 70.75999999999999, "text": " times w4 squared."}, {"start": 70.75999999999999, "end": 75.84, "text": " And here I'm just choosing 1000 because it's a big number, but any other really large number"}, {"start": 75.84, "end": 77.88, "text": " would be okay."}, {"start": 77.88, "end": 84.24, "text": " So with this modified cost function, you'd in fact be penalizing the model if w3 and"}, {"start": 84.24, "end": 86.56, "text": " w4 are large."}, {"start": 86.56, "end": 91.36, "text": " Because if you want to minimize this function, the only way to make this new cost function"}, {"start": 91.36, "end": 95.72, "text": " small is if w3 and w4 are both small, right?"}, {"start": 95.72, "end": 102.36, "text": " Because otherwise this 1000 times w3 squared and 1000 times w4 squared terms are going"}, {"start": 102.36, "end": 105.25999999999999, "text": " to be really, really big."}, {"start": 105.26, "end": 111.04, "text": " So when you minimize this function, you're going to end up with w3 close to zero and"}, {"start": 111.04, "end": 113.48, "text": " w4 close to zero."}, {"start": 113.48, "end": 120.52000000000001, "text": " So we're effectively nearly canceling out the effect of the features x cubed and x to"}, {"start": 120.52000000000001, "end": 125.9, "text": " the power of four and getting rid of these two terms over here."}, {"start": 125.9, "end": 130.64000000000001, "text": " And if we do that, then we end up with a fit to the data that's much closer to the quadratic"}, {"start": 130.64, "end": 138.0, "text": " function, including maybe just tiny contributions from the features x cubed and x to the fourth."}, {"start": 138.0, "end": 143.51999999999998, "text": " And this is good because it's a much better fit to the data compared to if all the parameters"}, {"start": 143.51999999999998, "end": 147.83999999999997, "text": " could be large and you end up with this wiggly quadratic function."}, {"start": 147.83999999999997, "end": 151.2, "text": " More generally, here's the idea behind regularization."}, {"start": 151.2, "end": 156.64, "text": " The idea is that if there are smaller values for the parameters, then that's a bit like"}, {"start": 156.64, "end": 161.83999999999997, "text": " having a simpler model, maybe one with fewer features, which is therefore less prone to"}, {"start": 161.83999999999997, "end": 164.35999999999999, "text": " overfitting."}, {"start": 164.35999999999999, "end": 172.48, "text": " On the last slide, we penalize or we say we regularize only w3 and w4."}, {"start": 172.48, "end": 178.2, "text": " But more generally, the way that regularization tends to be implemented is if you have a lot"}, {"start": 178.2, "end": 183.27999999999997, "text": " of features, say 100 features, you may not know which are the most important features"}, {"start": 183.27999999999997, "end": 185.39999999999998, "text": " and which ones to penalize."}, {"start": 185.4, "end": 190.20000000000002, "text": " So the way regularization is typically implemented is to penalize all of the features or more"}, {"start": 190.20000000000002, "end": 195.16, "text": " precisely, you penalize all the wj parameters."}, {"start": 195.16, "end": 200.5, "text": " And it's possible to show that this will usually result in fitting a smoother, simpler, less"}, {"start": 200.5, "end": 204.48000000000002, "text": " wiggly function that's less prone to overfitting."}, {"start": 204.48000000000002, "end": 208.72, "text": " So for this example, if you have data with 100 features for each house, it may be hard"}, {"start": 208.72, "end": 212.76, "text": " to pick in advance which features to include and which ones to exclude."}, {"start": 212.76, "end": 217.64, "text": " So let's build a model that uses all 100 features."}, {"start": 217.64, "end": 227.64, "text": " So you have these 100 parameters, w1 through w100, as well as the 101st parameter b."}, {"start": 227.64, "end": 231.95999999999998, "text": " Because we don't know which of these parameters are going to be the important ones, let's"}, {"start": 231.95999999999998, "end": 239.35999999999999, "text": " penalize all of them a bit and shrink all of them by adding this new term lambda times"}, {"start": 239.36, "end": 248.88000000000002, "text": " the sum from j equals 1 through n, where n is 100, the number of features of wj squared."}, {"start": 248.88000000000002, "end": 256.24, "text": " This value lambda here is the Greek alphabet lambda, and it's also called a regularization"}, {"start": 256.24, "end": 258.0, "text": " parameter."}, {"start": 258.0, "end": 265.92, "text": " So similar to picking a learning rate alpha, you now also have to choose a number for lambda."}, {"start": 265.92, "end": 272.40000000000003, "text": " A couple things I would like to point out, by convention, instead of using lambda times"}, {"start": 272.40000000000003, "end": 280.40000000000003, "text": " the sum of wj squared, we also divide lambda by 2m, so that both the first and second terms"}, {"start": 280.40000000000003, "end": 284.36, "text": " here are scaled by 1 over 2m."}, {"start": 284.36, "end": 290.08000000000004, "text": " It turns out that by scaling both terms the same way, it becomes a little bit easier to"}, {"start": 290.08, "end": 297.03999999999996, "text": " choose a good value for lambda, and in particular, you find that even if your training set size"}, {"start": 297.03999999999996, "end": 302.76, "text": " grows, say you find more training examples, so m, the training set size, is now bigger,"}, {"start": 302.76, "end": 309.44, "text": " the same value of lambda that you picked previously is now also more likely to continue to work"}, {"start": 309.44, "end": 313.44, "text": " if you have this extra scaling by 2m."}, {"start": 313.44, "end": 318.2, "text": " Also by the way, by convention, we're not going to penalize the parameter b for being"}, {"start": 318.2, "end": 319.2, "text": " large."}, {"start": 319.2, "end": 323.68, "text": " In practice, it makes very little difference whether you do or not, and some machine learning"}, {"start": 323.68, "end": 330.47999999999996, "text": " engineers, and actually some learning algorithm implementations, will also include lambda"}, {"start": 330.47999999999996, "end": 334.03999999999996, "text": " over 2m times the b squared term."}, {"start": 334.03999999999996, "end": 338.86, "text": " But this makes very little difference in practice, and the more common convention which we'll"}, {"start": 338.86, "end": 346.0, "text": " use in this course is to regularize only the parameters w rather than the parameter b."}, {"start": 346.0, "end": 353.52, "text": " So to summarize, in this modified cost function, we want to minimize the original cost, which"}, {"start": 353.52, "end": 359.16, "text": " is the mean squared error cost, plus additionally the second term, which is called the regularization"}, {"start": 359.16, "end": 361.22, "text": " term."}, {"start": 361.22, "end": 366.08, "text": " And so this new cost function trades off two goals that you might have."}, {"start": 366.08, "end": 371.0, "text": " Trying to minimize this first term encourages the algorithm to fit the training data well"}, {"start": 371.0, "end": 376.4, "text": " by minimizing the squared differences of the predictions and the actual values, and trying"}, {"start": 376.4, "end": 383.08, "text": " to minimize the second term, the algorithm also tries to keep the parameters wj small,"}, {"start": 383.08, "end": 386.28, "text": " which will tend to reduce overfitting."}, {"start": 386.28, "end": 392.64, "text": " The value of lambda that you choose specifies the relative importance or the relative tradeoff"}, {"start": 392.64, "end": 396.64, "text": " or how you balance between these two goals."}, {"start": 396.64, "end": 401.4, "text": " Let's take a look at what different values of lambda will cause your learning algorithm"}, {"start": 401.4, "end": 402.91999999999996, "text": " to do."}, {"start": 402.91999999999996, "end": 407.06, "text": " Let's use the housing price prediction example using linear regression."}, {"start": 407.06, "end": 410.4, "text": " So f of x is the linear regression model."}, {"start": 410.4, "end": 416.8, "text": " If lambda was set to be zero, then you're not using the regularization term at all,"}, {"start": 416.8, "end": 420.56, "text": " because the regularization term is multiplied by zero."}, {"start": 420.56, "end": 426.5, "text": " And so if lambda was zero, you end up fitting this overly wiggly, overly complex curve,"}, {"start": 426.5, "end": 428.36, "text": " and it overfits."}, {"start": 428.36, "end": 432.2, "text": " So that was one extreme of if lambda was zero."}, {"start": 432.2, "end": 434.38, "text": " Let's now look at the other extreme."}, {"start": 434.38, "end": 439.68, "text": " If you set lambda to be a really, really, really large number, say lambda equals 10"}, {"start": 439.68, "end": 444.82, "text": " to the power of 10, then you're placing a very heavy weight on this regularization term"}, {"start": 444.82, "end": 446.04, "text": " on the right."}, {"start": 446.04, "end": 451.24, "text": " And the only way to minimize this is to be sure that all the values of w are pretty much"}, {"start": 451.24, "end": 454.62, "text": " very close to zero."}, {"start": 454.62, "end": 462.12, "text": " So if lambda is very, very large, the learning algorithm will choose w1, w2, w3, and w4 to"}, {"start": 462.12, "end": 464.4, "text": " be extremely close to zero."}, {"start": 464.4, "end": 472.24, "text": " And thus, f of x is basically equal to b, and so the learning algorithm fits a horizontal"}, {"start": 472.24, "end": 475.38, "text": " straight line and underfits."}, {"start": 475.38, "end": 480.38, "text": " To recap, if lambda is zero, this model will overfit."}, {"start": 480.38, "end": 485.94, "text": " If lambda is enormous, like 10 to the power of 10, this model will underfit."}, {"start": 485.94, "end": 491.26, "text": " And so what you want is some value of lambda that is in between that more appropriately"}, {"start": 491.26, "end": 498.92, "text": " balances these first and second terms of trading off, minimizing the mean squared error and"}, {"start": 498.92, "end": 502.64, "text": " keeping the parameters small."}, {"start": 502.64, "end": 508.15999999999997, "text": " And when the value of lambda is not too small and not too large, but just right, then hopefully"}, {"start": 508.16, "end": 513.4200000000001, "text": " you end up able to fit a fourth order polynomial keeping all of these features, but with a"}, {"start": 513.4200000000001, "end": 516.7, "text": " function that looks like this."}, {"start": 516.7, "end": 519.48, "text": " So that's how regularization works."}, {"start": 519.48, "end": 524.6, "text": " When we talk about model selection later in the specialization, we'll also see a variety"}, {"start": 524.6, "end": 528.44, "text": " of ways to choose good values for lambda."}, {"start": 528.44, "end": 533.38, "text": " In the next two videos, we'll flesh out how to apply regularization to linear regression"}, {"start": 533.38, "end": 537.94, "text": " and logistic regression, and how to train these models with gradient descent."}, {"start": 537.94, "end": 541.62, "text": " With that, you'll be able to avoid overfitting with both of these algorithms."}]