#40 Machine Learning Specialization [Course 1, Week 3, Lesson 4]

Andrew Ng shows how to add L2 regularization to gradient descent for linear regression. The weight update shrinks w_j by a factor of (1 - alphalambda/m) each iteration, while the bias term b stays the same. Optional material covers the math derivation if you want it.
Chapters
In this video, we'll figure out how to get gradient descent to work with regularized linear regression. Let's jump in. Here's a cost function we come up with in the last video for regularized linear regression. The first part is the usual squared error cost function. And now you have this additional regularization term where lambda is the regularization parameter, and you'd like to find parameters W and B that minimize the regularized cost function. Previously, we were using gradient descent for the original cost function, just the first term before we added that second regularization term. And previously we had the following gradient descent algorithm, which is that we repeatedly Update the parameters wj and b for j equals 1 through n according to this formula, and B is also updated similarly. Again, alpha is a very small positive number called the learning rate. In fact, the updates for regularized linear regression look exactly the same, except that now the cost J is defined a bit differently. Previously, the derivative of J with respect to WJ was given by this expression over here. And the derivative respect to B was given by this expression over here. Now that we've added this additional regularization term, the only thing that changes is that the expression for the derivative with respect to WJ ends up with one additional term, this + lambda over M times WJ. And in particular, for the new definition of the cost function j, these two expressions over here, these are the new derivatives of j with respect to W.J. and the derivative of j with respect to B. Recall that we don't regularize B, so we're not trying to shrink B. That's why the updated B remains the same as before, whereas the updated W changes, because the regularization term causes us to try to shrink wj. So 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. So, to implement gradient descent for regularized linear regression, this is what you would have your code do. Here's the update for wj for j equals 1 through n, and here's the update for B. And as usual, please remember to carry out simultaneous updates for all of these parameters. Now, in order for you to get this algorithm to work, this is all you need to know. But what I'd like to do in the remainder of this video is to go over some optional material to convey a slightly deeper intuition about what this formula is actually doing, as well as chat briefly about how these derivatives are derived. The rest of this video is completely optional. It's completely okay if you skip the rest of this video. Um, but if you have a strong interest in math, then stick with me. It's always nice to hang out with you here. And through these equations, perhaps you can build a deeper intuition about what the math and what the derivatives are doing as well. So let's take a look. Let's look at the update rule for wj and rewrite it in another way. We're updating wj as 1 times wj minus alpha times lambda over m times wj. So I've moved the term from the end to the frontier, and then minus alpha times 1 over M and then the rest of that term over there. So we just rearrange the terms a little bit, and if we simplify, then we're saying that WJ is updated as WJ times 1, minus Alpha times Lambda over m, minus alpha times this other term over here. And you might recognize this second term as the usual gradient descent update for unregularized linear regression. This is the update for linear regression before we had regularization. And this is the term we saw in week two of this course. And so the only change when you add regularization is that instead of WJ being set to be equal to WJ minus alpha times this term is now W times this number minus the usual update. So this is what we had in week one of this course. So what is this first term over here? Well, alpha is a very small positive number, say 0.01. Lambda is usually a small number, say 1 or maybe 10. Let's say lambda is 1 for this example, and M is the training set size, say 50. And so when you multiply alpha lambda over M, say 0.01 times 1 divided by 50, this term ends up being a small positive number, say 0.0002. And thus 1 minus alpha lambda over M is going to be a number just slightly less than 1, in this case, 0.99998. And so the effect of this term is that on every single iteration of gradient descent, you're taking WJ and multiplying it by 0.99998, that is by some number slightly less than 1 before carrying out the usual update. So what regularization is doing on every single iteration is you're multiplying W by a number slightly less than 1. And that has the effect of shrinking the value of WJ just a little bit. So this gives us another view on why regularization has the effect of shrinking the parameters WJ a little bit on every iteration. And so that's how regularization works. If you're curious about how these derivative terms were computed. I have just one last optional slide that goes through just a little bit of the calculation of the derivative term. Again, this slide and the rest of this video are completely optional, meaning you won't need any of this to do the practice labs and the quizzes. So let's step through quickly. The derivative calculation. The derivative of j with respect to W.J. looks like this. Recall that F for linear regression is defined as w dot x plus b or w dot product x plus b. And it turns out that by the rules of calculus, the derivatives look like this is 1 over 2m times the sum I equals 1 through m of w dot x plus b minus y times 2xj plus the derivative of the regularization term, which is lambda over 2m times 2wj. Notice that the second term does not have the summation term from j equals 1 through n anymore. The twos cancel out here and here and also here and here. And so it simplifies to this expression over here. And finally, remember that wx plus B is F of X, and so you can rewrite it as this expression down here. So this is why this expression is used to compute the gradient in regularized linear regression. So you now know how to implement regularized linear regression using this. You really reduce overfitting when you have a lot of features and a relatively small training set. And this should let you get linear regression to work much better on many problems. In the next video, we'll take this regularization idea and apply it to logistic regression to avoid overfitting for logistic regression as well. Let's take a look at that in the next video.










