3.8 Regularization to Reduce Overfitting | Addressing Overfitting-[Machine Learning|Andrew Ng]
 Later in this specialization, we'll talk about debugging and diagnosing things that can go wrong with learning algorithms. You also learn about specific tools to recognize when overfitting and underfitting may be occurring. But for now, 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 as overfit. Here's our overfit house price prediction model. One way to address this problem is to collect more training data. So that's one option. If you're able to get more data, that is more training examples on sizes and prices of houses, then with the larger training set, the learning algorithm will learn to fit a function that is less wiggly. So you can continue to fit a high order polynomial or some other function with a lot of features. And if you have enough training examples, it will still do okay. So to summarize, the number one tool you can use against overfitting is to get more training data. Now, getting more data isn't always an option. Maybe only so many houses have been sold in this location. So maybe there just isn't more data to be had. So when the data is available, this can work really well. A second option for addressing overfitting is to see if you can use fewer features. In the previous video, our model's features included the size x as well as the size squared, that is x squared, and x cubed and x to the 4 and so on. These were a lot of polynomial features. So in that case, one way to reduce overfitting is to just not use so many of these polynomial features. But now let's look at a different example. Maybe you have a lot of different features of a house with which to try to predict its price, ranging from the size, number of bedrooms, number of floors, the age, average income of the neighborhood, and so on and so forth to the distance to the nearest coffee shop. It turns out that if you have a lot of features like these, but don't have enough training data, then your learning algorithm may also overfit to your training set. Now instead of using all 100 features, if we were to pick just a subset of the most useful ones, maybe size, bedrooms, and the age of the house, if you think those are the most relevant features, then using just that smaller subset of features, you may find that your model no longer overfits as badly. 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. What's most relevant for predicting the price. Now one disadvantage of feature selection is that by using only a subset of the features, the algorithm is throwing away some of the information that you have about the houses. For example, maybe all of these features, or 100 of them, are actually useful for predicting the price of a house. So maybe you don't want to throw away some of the information by throwing away some of the features. Later in course two, you also see some algorithms for automatically choosing the most appropriate set of features to use for prediction tasks. Now this takes us to the third option for reducing overfitting. This technique, which we'll look at in even greater depth in the next video, is called regularization. If you look at an overfit model, here's a model using polynomial features x, x squared, x cubed, and so on. You find that the parameters are often relatively large. Now if you were to eliminate some of these features, say if you were to eliminate the feature x4, that corresponds to setting this parameter to zero. So setting a parameter to zero is equivalent to eliminating a feature, which is what we saw on the previous slide. It turns out that regularization is a way to more gently reduce the impacts of some of the features without doing something as harsh as eliminating it outright. What regularization does is encourage the learning algorithm to shrink the values of the parameters without necessarily demanding that the parameter is set to exactly zero. And it turns out that even if you fit a higher order polynomial like this, so long as you can get the algorithm to use smaller parameter values, w1, w2, w3, w4, you end up with a curve that ends up fitting the training data much better. So what regularization does is it lets you keep all of your features, but it just prevents the features from having an overly large effect, which is what sometimes can cause overfitting. By the way, by convention, we normally just reduce the size of the wj parameters, that is w1 through wn. It kind of doesn't make a huge difference whether you regularize the parameter b as well. You could do so if you want or not if you don't. I usually don't and it's just fine to regularize w1, w2 all the way to wn, but not really encourage b to become smaller. In practice, it should make very little difference whether you also regularize b or not. So to recap, these are the three ways you saw in this video for addressing overfitting. One, collect more data. If you can get more data, this can really help reduce overfitting. Sometimes that's not possible, in which case some of the options are, two, try selecting and using only a subset of the features. You learn more about feature selection in course two. Three would be to reduce the size of the parameters using regularization. This will be the subject of the next video as well. As for myself, I use regularization all the time. So this is a very useful technique for training learning algorithms, including neural networks specifically, which you see later in the specialization as well. I hope you also check out the optional lab on overfitting. In the lab, you will see different examples of overfitting and adjust those examples by clicking on options in the plots. You also be able to add your own data points by clicking on the plot and see how that changes the curve that is fit. You can also try examples for both regression and classification, and you really change the degree of the polynomial to be x, x squared, x cubed, and so on. The lab also lets you play with two different options for addressing overfitting. You can add additional training data to reduce overfitting, and you can also select which features to include or to exclude as another way to try to reduce overfitting. So please take a look at the lab, which I hope will help you build your intuition about overfitting as well as some methods for addressing it. In this video, you also saw the idea of regularization at a relatively high level. I realize that all of these details on regularization may not fully make sense to you yet, but in the next video, we'll start to formulate exactly how to apply regularization and exactly what regularization means. And then we'll start to figure out how to make this work with our learning algorithms to make linear regression and literacy regression and in the future, other algorithms as well, avoid overfitting. Let's take a look at that in the next video.
[{"start": 0.0, "end": 7.04, "text": " Later in this specialization, we'll talk about debugging and diagnosing things that"}, {"start": 7.04, "end": 9.52, "text": " can go wrong with learning algorithms."}, {"start": 9.52, "end": 15.8, "text": " You also learn about specific tools to recognize when overfitting and underfitting may be occurring."}, {"start": 15.8, "end": 20.84, "text": " But for now, when you think overfitting has occurred, let's talk about what you can do"}, {"start": 20.84, "end": 22.56, "text": " to address it."}, {"start": 22.56, "end": 27.84, "text": " Let's say you fit a model and it has high variance as overfit."}, {"start": 27.84, "end": 32.28, "text": " Here's our overfit house price prediction model."}, {"start": 32.28, "end": 37.12, "text": " One way to address this problem is to collect more training data."}, {"start": 37.12, "end": 39.2, "text": " So that's one option."}, {"start": 39.2, "end": 46.08, "text": " If you're able to get more data, that is more training examples on sizes and prices of houses,"}, {"start": 46.08, "end": 52.08, "text": " then with the larger training set, the learning algorithm will learn to fit a function that"}, {"start": 52.08, "end": 54.16, "text": " is less wiggly."}, {"start": 54.16, "end": 60.239999999999995, "text": " So you can continue to fit a high order polynomial or some other function with a lot of features."}, {"start": 60.239999999999995, "end": 64.88, "text": " And if you have enough training examples, it will still do okay."}, {"start": 64.88, "end": 70.8, "text": " So to summarize, the number one tool you can use against overfitting is to get more training"}, {"start": 70.8, "end": 71.8, "text": " data."}, {"start": 71.8, "end": 75.19999999999999, "text": " Now, getting more data isn't always an option."}, {"start": 75.19999999999999, "end": 78.64, "text": " Maybe only so many houses have been sold in this location."}, {"start": 78.64, "end": 81.75999999999999, "text": " So maybe there just isn't more data to be had."}, {"start": 81.76, "end": 84.88000000000001, "text": " So when the data is available, this can work really well."}, {"start": 84.88000000000001, "end": 91.64, "text": " A second option for addressing overfitting is to see if you can use fewer features."}, {"start": 91.64, "end": 98.64, "text": " In the previous video, our model's features included the size x as well as the size squared,"}, {"start": 98.64, "end": 104.48, "text": " that is x squared, and x cubed and x to the 4 and so on."}, {"start": 104.48, "end": 108.04, "text": " These were a lot of polynomial features."}, {"start": 108.04, "end": 114.16000000000001, "text": " So in that case, one way to reduce overfitting is to just not use so many of these polynomial"}, {"start": 114.16000000000001, "end": 115.60000000000001, "text": " features."}, {"start": 115.60000000000001, "end": 118.46000000000001, "text": " But now let's look at a different example."}, {"start": 118.46000000000001, "end": 122.36000000000001, "text": " Maybe you have a lot of different features of a house with which to try to predict its"}, {"start": 122.36000000000001, "end": 127.80000000000001, "text": " price, ranging from the size, number of bedrooms, number of floors, the age, average income"}, {"start": 127.80000000000001, "end": 133.32, "text": " of the neighborhood, and so on and so forth to the distance to the nearest coffee shop."}, {"start": 133.32, "end": 138.84, "text": " It turns out that if you have a lot of features like these, but don't have enough training"}, {"start": 138.84, "end": 144.28, "text": " data, then your learning algorithm may also overfit to your training set."}, {"start": 144.28, "end": 149.23999999999998, "text": " Now instead of using all 100 features, if we were to pick just a subset of the most"}, {"start": 149.23999999999998, "end": 157.2, "text": " useful ones, maybe size, bedrooms, and the age of the house, if you think those are the"}, {"start": 157.2, "end": 162.51999999999998, "text": " most relevant features, then using just that smaller subset of features, you may find that"}, {"start": 162.52, "end": 166.72, "text": " your model no longer overfits as badly."}, {"start": 166.72, "end": 172.88, "text": " Choosing the most appropriate set of features to use is sometimes also called feature selection."}, {"start": 172.88, "end": 177.84, "text": " One way you could do so is to use your intuition to choose what you think is the best set of"}, {"start": 177.84, "end": 178.84, "text": " features."}, {"start": 178.84, "end": 181.76000000000002, "text": " What's most relevant for predicting the price."}, {"start": 181.76000000000002, "end": 188.44, "text": " Now one disadvantage of feature selection is that by using only a subset of the features,"}, {"start": 188.44, "end": 193.32, "text": " the algorithm is throwing away some of the information that you have about the houses."}, {"start": 193.32, "end": 198.8, "text": " For example, maybe all of these features, or 100 of them, are actually useful for predicting"}, {"start": 198.8, "end": 200.4, "text": " the price of a house."}, {"start": 200.4, "end": 204.72, "text": " So maybe you don't want to throw away some of the information by throwing away some of"}, {"start": 204.72, "end": 205.72, "text": " the features."}, {"start": 205.72, "end": 212.44, "text": " Later in course two, you also see some algorithms for automatically choosing the most appropriate"}, {"start": 212.44, "end": 215.72, "text": " set of features to use for prediction tasks."}, {"start": 215.72, "end": 219.76, "text": " Now this takes us to the third option for reducing overfitting."}, {"start": 219.76, "end": 224.56, "text": " This technique, which we'll look at in even greater depth in the next video, is called"}, {"start": 224.56, "end": 227.2, "text": " regularization."}, {"start": 227.2, "end": 234.48, "text": " If you look at an overfit model, here's a model using polynomial features x, x squared,"}, {"start": 234.48, "end": 236.07999999999998, "text": " x cubed, and so on."}, {"start": 236.07999999999998, "end": 240.34, "text": " You find that the parameters are often relatively large."}, {"start": 240.34, "end": 245.6, "text": " Now if you were to eliminate some of these features, say if you were to eliminate the"}, {"start": 245.6, "end": 253.12, "text": " feature x4, that corresponds to setting this parameter to zero."}, {"start": 253.12, "end": 258.44, "text": " So setting a parameter to zero is equivalent to eliminating a feature, which is what we"}, {"start": 258.44, "end": 261.06, "text": " saw on the previous slide."}, {"start": 261.06, "end": 266.6, "text": " It turns out that regularization is a way to more gently reduce the impacts of some"}, {"start": 266.6, "end": 272.42, "text": " of the features without doing something as harsh as eliminating it outright."}, {"start": 272.42, "end": 277.40000000000003, "text": " What regularization does is encourage the learning algorithm to shrink the values of"}, {"start": 277.40000000000003, "end": 284.08000000000004, "text": " the parameters without necessarily demanding that the parameter is set to exactly zero."}, {"start": 284.08000000000004, "end": 289.52000000000004, "text": " And it turns out that even if you fit a higher order polynomial like this, so long as you"}, {"start": 289.52000000000004, "end": 296.20000000000005, "text": " can get the algorithm to use smaller parameter values, w1, w2, w3, w4, you end up with a"}, {"start": 296.20000000000005, "end": 300.92, "text": " curve that ends up fitting the training data much better."}, {"start": 300.92, "end": 306.40000000000003, "text": " So what regularization does is it lets you keep all of your features, but it just prevents"}, {"start": 306.40000000000003, "end": 314.56, "text": " the features from having an overly large effect, which is what sometimes can cause overfitting."}, {"start": 314.56, "end": 321.20000000000005, "text": " By the way, by convention, we normally just reduce the size of the wj parameters, that"}, {"start": 321.20000000000005, "end": 323.48, "text": " is w1 through wn."}, {"start": 323.48, "end": 328.20000000000005, "text": " It kind of doesn't make a huge difference whether you regularize the parameter b as"}, {"start": 328.20000000000005, "end": 329.20000000000005, "text": " well."}, {"start": 329.2, "end": 331.59999999999997, "text": " You could do so if you want or not if you don't."}, {"start": 331.59999999999997, "end": 339.68, "text": " I usually don't and it's just fine to regularize w1, w2 all the way to wn, but not really encourage"}, {"start": 339.68, "end": 341.8, "text": " b to become smaller."}, {"start": 341.8, "end": 347.91999999999996, "text": " In practice, it should make very little difference whether you also regularize b or not."}, {"start": 347.91999999999996, "end": 354.64, "text": " So to recap, these are the three ways you saw in this video for addressing overfitting."}, {"start": 354.64, "end": 357.32, "text": " One, collect more data."}, {"start": 357.32, "end": 362.62, "text": " If you can get more data, this can really help reduce overfitting."}, {"start": 362.62, "end": 368.71999999999997, "text": " Sometimes that's not possible, in which case some of the options are, two, try selecting"}, {"start": 368.71999999999997, "end": 372.24, "text": " and using only a subset of the features."}, {"start": 372.24, "end": 377.32, "text": " You learn more about feature selection in course two."}, {"start": 377.32, "end": 383.8, "text": " Three would be to reduce the size of the parameters using regularization."}, {"start": 383.8, "end": 387.2, "text": " This will be the subject of the next video as well."}, {"start": 387.2, "end": 390.0, "text": " As for myself, I use regularization all the time."}, {"start": 390.0, "end": 395.03999999999996, "text": " So this is a very useful technique for training learning algorithms, including neural networks"}, {"start": 395.03999999999996, "end": 398.96, "text": " specifically, which you see later in the specialization as well."}, {"start": 398.96, "end": 404.4, "text": " I hope you also check out the optional lab on overfitting."}, {"start": 404.4, "end": 410.21999999999997, "text": " In the lab, you will see different examples of overfitting and adjust those examples by"}, {"start": 410.21999999999997, "end": 413.28, "text": " clicking on options in the plots."}, {"start": 413.28, "end": 419.03999999999996, "text": " You also be able to add your own data points by clicking on the plot and see how that changes"}, {"start": 419.03999999999996, "end": 421.52, "text": " the curve that is fit."}, {"start": 421.52, "end": 427.47999999999996, "text": " You can also try examples for both regression and classification, and you really change"}, {"start": 427.47999999999996, "end": 433.96, "text": " the degree of the polynomial to be x, x squared, x cubed, and so on."}, {"start": 433.96, "end": 439.35999999999996, "text": " The lab also lets you play with two different options for addressing overfitting."}, {"start": 439.36, "end": 444.58000000000004, "text": " You can add additional training data to reduce overfitting, and you can also select which"}, {"start": 444.58000000000004, "end": 451.2, "text": " features to include or to exclude as another way to try to reduce overfitting."}, {"start": 451.2, "end": 455.96000000000004, "text": " So please take a look at the lab, which I hope will help you build your intuition about"}, {"start": 455.96000000000004, "end": 460.24, "text": " overfitting as well as some methods for addressing it."}, {"start": 460.24, "end": 466.08000000000004, "text": " In this video, you also saw the idea of regularization at a relatively high level."}, {"start": 466.08, "end": 472.47999999999996, "text": " I realize that all of these details on regularization may not fully make sense to you yet, but in"}, {"start": 472.47999999999996, "end": 478.44, "text": " the next video, we'll start to formulate exactly how to apply regularization and exactly what"}, {"start": 478.44, "end": 480.24, "text": " regularization means."}, {"start": 480.24, "end": 485.03999999999996, "text": " And then we'll start to figure out how to make this work with our learning algorithms"}, {"start": 485.03999999999996, "end": 490.47999999999996, "text": " to make linear regression and literacy regression and in the future, other algorithms as well,"}, {"start": 490.47999999999996, "end": 491.47999999999996, "text": " avoid overfitting."}, {"start": 491.48, "end": 496.24, "text": " Let's take a look at that in the next video."}]