3.11 Regularization to Reduce Overfitting | Regularized logistic regression-- [ML | Andrew Ng]
 In this video, you see how to implement regularized logistic regression. Just as the gradient update for logistic regression has seemed surprisingly similar to the gradient update for linear regression, you find that the gradient descent update for regularized logistic regression will also look similar to the update for regularized linear regression. Let's take a look. Here's the idea. We saw earlier that logistic regression can be prone to overfitting if you fit it with very high-order polynomial features like this. Here z is a high-order polynomial that gets passed into the sigmoid function, like so, to compute f. And in particular, you can end up with a decision boundary that is overly complex and overfits this training set. More generally, when you train logistic regression with a lot of features, whether polynomial features or some other features, there can be a higher risk of overfitting. This was the cost function for logistic regression. If you want to modify it to use regularization, all you need to do is add to it the following term. Let's add lambda, the regularization parameter, over 2m, times the sum from j equals 1 through n, where n is the number of features as usual, of wj squared. So when you minimize this cost function as a function of w and b, it has the effect of penalizing parameters w1, w2, through wn and preventing them from being too large. And if you do this, then even though you're fitting a high-order polynomial with a lot of parameters, you still get a decision boundary that looks like this, something that looks more reasonable for separating positive and negative examples while also generalizing, hopefully, to new examples not in the training set. So when using regularization, even when you have a lot of features, how can you actually implement this? How can you actually minimize this cost function j of wb that includes the regularization term? Well, let's use gradient descent as before. So here's the cost function that you want to minimize. And to implement gradient descent, as before, we'll carry out the following simultaneous updates over wj and b. These are the usual update rules for gradient descent. And just like regularized linear regression, when you compute what are these derivative terms, the only thing that changes now is that the derivative with respect to wj gets this additional term, lambda over m times wj added here at the end. And again, it looks a lot like the update for regularized linear regression. In fact, it's the exact same equation, except for the fact that the definition of f is now no longer the linear function, it is the logistic function applied to z. And similar to linear regression, we will regularize only the parameters wj, but not the parameter b, which is why there's no change to the update you would make for b. In the final optional lab of this week, you revisit overfitting. And in the interactive plot in the optional lab, you can now choose to regularize your models, both regression and classification, by enabling regularization during gradient descent, by selecting a value for lambda. Please take a look at the code for implementing regularized logistic regression in particular, because you implement this in a practice lab yourself at the end of this week. So, now you know how to implement regularized logistic regression. When I walk around Silicon Valley, there are many engineers using machine learning to create a ton of value, sometimes making a lot of money for the companies. And I know you've only been studying this stuff for a few weeks. But if you understand and can apply linear regression and logistic regression, that's actually all you need to create some very valuable applications. While the specific learning algorithms you use are important, knowing things like when and how to reduce overfitting turns out to be one of the very valuable skills in the real world as well. So, I want to say, congratulations on how far you've come. And I want to say great job for getting through all the way to the end of this video. I hope you also work through the practice labs and quizzes. Having said that, there's still many more exciting things to learn. In the second course of this specialization, you learn about neural networks, also called deep learning algorithms. Neural networks are responsible for many of the latest breakthroughs in AI today, from practical speech recognition, to computers accurately recognizing objects and images, to self-driving cars. The way a neural network gets built actually uses a lot of what you've already learned, like cost functions and gradient descent and sigmoid functions. So again, congratulations on reaching the end of this third and final week of course one. I hope you have fun in the labs and I will see you in next week's material on neural networks.
[{"start": 0.0, "end": 6.4, "text": " In this video, you see how to implement regularized logistic regression."}, {"start": 6.4, "end": 11.8, "text": " Just as the gradient update for logistic regression has seemed surprisingly similar to the gradient"}, {"start": 11.8, "end": 16.44, "text": " update for linear regression, you find that the gradient descent update for regularized"}, {"start": 16.44, "end": 21.28, "text": " logistic regression will also look similar to the update for regularized linear regression."}, {"start": 21.28, "end": 22.8, "text": " Let's take a look."}, {"start": 22.8, "end": 24.32, "text": " Here's the idea."}, {"start": 24.32, "end": 28.92, "text": " We saw earlier that logistic regression can be prone to overfitting if you fit it with"}, {"start": 28.92, "end": 32.96, "text": " very high-order polynomial features like this."}, {"start": 32.96, "end": 40.0, "text": " Here z is a high-order polynomial that gets passed into the sigmoid function, like so,"}, {"start": 40.0, "end": 41.0, "text": " to compute f."}, {"start": 41.0, "end": 47.6, "text": " And in particular, you can end up with a decision boundary that is overly complex and overfits"}, {"start": 47.6, "end": 50.480000000000004, "text": " this training set."}, {"start": 50.480000000000004, "end": 55.84, "text": " More generally, when you train logistic regression with a lot of features, whether polynomial"}, {"start": 55.84, "end": 62.28, "text": " features or some other features, there can be a higher risk of overfitting."}, {"start": 62.28, "end": 65.88000000000001, "text": " This was the cost function for logistic regression."}, {"start": 65.88000000000001, "end": 72.80000000000001, "text": " If you want to modify it to use regularization, all you need to do is add to it the following"}, {"start": 72.80000000000001, "end": 73.80000000000001, "text": " term."}, {"start": 73.80000000000001, "end": 81.2, "text": " Let's add lambda, the regularization parameter, over 2m, times the sum from j equals 1 through"}, {"start": 81.2, "end": 86.72, "text": " n, where n is the number of features as usual, of wj squared."}, {"start": 86.72, "end": 92.12, "text": " So when you minimize this cost function as a function of w and b, it has the effect of"}, {"start": 92.12, "end": 99.72, "text": " penalizing parameters w1, w2, through wn and preventing them from being too large."}, {"start": 99.72, "end": 104.60000000000001, "text": " And if you do this, then even though you're fitting a high-order polynomial with a lot"}, {"start": 104.60000000000001, "end": 110.24000000000001, "text": " of parameters, you still get a decision boundary that looks like this, something that looks"}, {"start": 110.24, "end": 116.08, "text": " more reasonable for separating positive and negative examples while also generalizing,"}, {"start": 116.08, "end": 120.44, "text": " hopefully, to new examples not in the training set."}, {"start": 120.44, "end": 126.24, "text": " So when using regularization, even when you have a lot of features, how can you actually"}, {"start": 126.24, "end": 127.36, "text": " implement this?"}, {"start": 127.36, "end": 133.44, "text": " How can you actually minimize this cost function j of wb that includes the regularization term?"}, {"start": 133.44, "end": 137.78, "text": " Well, let's use gradient descent as before."}, {"start": 137.78, "end": 141.68, "text": " So here's the cost function that you want to minimize."}, {"start": 141.68, "end": 146.4, "text": " And to implement gradient descent, as before, we'll carry out the following simultaneous"}, {"start": 146.4, "end": 151.68, "text": " updates over wj and b."}, {"start": 151.68, "end": 155.12, "text": " These are the usual update rules for gradient descent."}, {"start": 155.12, "end": 160.88, "text": " And just like regularized linear regression, when you compute what are these derivative"}, {"start": 160.88, "end": 166.96, "text": " terms, the only thing that changes now is that the derivative with respect to wj gets"}, {"start": 166.96, "end": 174.96, "text": " this additional term, lambda over m times wj added here at the end."}, {"start": 174.96, "end": 179.92000000000002, "text": " And again, it looks a lot like the update for regularized linear regression."}, {"start": 179.92000000000002, "end": 185.48000000000002, "text": " In fact, it's the exact same equation, except for the fact that the definition of f is now"}, {"start": 185.48000000000002, "end": 192.32, "text": " no longer the linear function, it is the logistic function applied to z."}, {"start": 192.32, "end": 197.64, "text": " And similar to linear regression, we will regularize only the parameters wj, but not"}, {"start": 197.64, "end": 204.51999999999998, "text": " the parameter b, which is why there's no change to the update you would make for b."}, {"start": 204.51999999999998, "end": 210.32, "text": " In the final optional lab of this week, you revisit overfitting."}, {"start": 210.32, "end": 215.88, "text": " And in the interactive plot in the optional lab, you can now choose to regularize your"}, {"start": 215.88, "end": 222.6, "text": " models, both regression and classification, by enabling regularization during gradient descent,"}, {"start": 222.6, "end": 225.48, "text": " by selecting a value for lambda."}, {"start": 225.48, "end": 230.6, "text": " Please take a look at the code for implementing regularized logistic regression in particular,"}, {"start": 230.6, "end": 235.51999999999998, "text": " because you implement this in a practice lab yourself at the end of this week."}, {"start": 235.51999999999998, "end": 241.56, "text": " So, now you know how to implement regularized logistic regression."}, {"start": 241.56, "end": 245.85999999999999, "text": " When I walk around Silicon Valley, there are many engineers using machine learning to create"}, {"start": 245.86, "end": 249.8, "text": " a ton of value, sometimes making a lot of money for the companies."}, {"start": 249.8, "end": 253.96, "text": " And I know you've only been studying this stuff for a few weeks."}, {"start": 253.96, "end": 259.16, "text": " But if you understand and can apply linear regression and logistic regression, that's"}, {"start": 259.16, "end": 264.0, "text": " actually all you need to create some very valuable applications."}, {"start": 264.0, "end": 268.56, "text": " While the specific learning algorithms you use are important, knowing things like when"}, {"start": 268.56, "end": 273.40000000000003, "text": " and how to reduce overfitting turns out to be one of the very valuable skills in the"}, {"start": 273.40000000000003, "end": 274.64, "text": " real world as well."}, {"start": 274.64, "end": 279.28, "text": " So, I want to say, congratulations on how far you've come."}, {"start": 279.28, "end": 284.56, "text": " And I want to say great job for getting through all the way to the end of this video."}, {"start": 284.56, "end": 289.24, "text": " I hope you also work through the practice labs and quizzes."}, {"start": 289.24, "end": 293.68, "text": " Having said that, there's still many more exciting things to learn."}, {"start": 293.68, "end": 298.84, "text": " In the second course of this specialization, you learn about neural networks, also called"}, {"start": 298.84, "end": 301.03999999999996, "text": " deep learning algorithms."}, {"start": 301.04, "end": 305.20000000000005, "text": " Neural networks are responsible for many of the latest breakthroughs in AI today, from"}, {"start": 305.20000000000005, "end": 310.36, "text": " practical speech recognition, to computers accurately recognizing objects and images,"}, {"start": 310.36, "end": 312.12, "text": " to self-driving cars."}, {"start": 312.12, "end": 317.16, "text": " The way a neural network gets built actually uses a lot of what you've already learned,"}, {"start": 317.16, "end": 321.20000000000005, "text": " like cost functions and gradient descent and sigmoid functions."}, {"start": 321.20000000000005, "end": 325.88, "text": " So again, congratulations on reaching the end of this third and final week of course"}, {"start": 325.88, "end": 326.88, "text": " one."}, {"start": 326.88, "end": 331.44, "text": " I hope you have fun in the labs and I will see you in next week's material on neural"}, {"start": 331.44, "end": 358.08, "text": " networks."}]