1.19 Machine Learning Overview| Gradient descent for Linear Regression--[Machine Learning|Andrew Ng]
 So, previously you took a look at the linear regression model and then the cost function and then the gradient descent algorithm. In this video, we're going to put it all together and use the square error cost function for the linear regression model with gradient descent. This will allow us to train the linear regression model to fit a straight line to our training data. Let's get to it. This is the linear regression model and to the right is the squared error cost function and below is the gradient descent algorithm. It turns out if you calculate these derivatives, these are the terms you would get. The derivative with respect to w is this. One over m summed from i equals one through m. Then the error term, that is the difference between the predicted and the actual values, times the input feature x i. And the derivative with respect to b is this formula over here, which looks the same as the equation above except that it doesn't have that x i term at the end. And if you use these formulas to compute these two derivatives and implement gradient descent this way, it will work. Now you may be wondering, where did I get these formulas from? They're derived using calculus. If you want to see the full derivation, I'll quickly run through the derivation on the next slide. But if you don't remember or aren't interested in the calculus, don't worry about it. You can skip the materials on the next slide entirely and still be able to implement gradient descent and finish this class and everything will work just fine. So in this slide, which is one of the most mathematical slides of the entire specialization and again is completely optional, we'll show you how to calculate the derivative terms. Let's start with the first term. The derivative of the cost function j with respect to w. We'll start by plugging in the definition of the cost function j. So j of w b is this. So one over two m times this sum of the squared error terms. And now remember also that f of w b of x i is equal to this term over here, which is w x i plus b. And so what we would like to do is compute the derivative also called the partial derivative with respect to w of this equation right here on the right. If you've taken a calculus class before, and again is totally fine if you haven't, you may know that by the rules of calculus, the derivative is equal to this term over here, which is why the two here and the two here cancel out, leaving us with this equation that you saw on the previous slide. So this, by the way, is why we had to find the cost function with the one half earlier this week is because it makes the partial derivative meter. It cancels out the two that appears from computing the derivative. For the other derivative with respect to b, this is quite similar. I can write it out like this and once again, plug in the definition of f of x i giving this equation. And by the rules of calculus, this is equal to this, where there's no x i anymore at the end. And so the twos cancel once more and you end up with this expression for the derivative with respect to b. And so now you have these two expressions for the derivatives and you can plug them into the gradient descent algorithm. So here's the gradient descent algorithm for linear regression. You repeatedly carry out these updates to w and b until convergence. Remember that this f of x is a linear regression model, so it's equal to w times x plus b. This expression here is the derivative of the cost function with respect to w and this expression is the derivative of the cost function with respect to b. And just as a reminder, you want to update w and b simultaneously on each step. Now let's get familiar with how gradient descent works. One issue we saw with gradient descent is that it can lead to a local minimum instead of a global minimum, where the global minimum means the point that has the lowest possible value for the cost function j out of all possible points. You may recall this surface plot that looks like an outdoor park with a few hills with the grass and the birds is a relaxing outdoor hill. This function has more than one local minimum. Remember depending on where you initialize the parameters w and b, you can end up at different local minima. You can end up here or you can end up here. But it turns out when you're using a squared error cost function with linear regression, the cost function does not and will never have multiple local minima. It has a single global minimum because of this bow shape. The technical term for this is that this cost function is a convex function. Informally, a convex function is a bow shaped function and it cannot have any local minima other than the single global minimum. So when you implement gradient descent on a convex function, one nice property is that so long as your learning rate is chosen appropriately, it will always converge to the global minimum. Congratulations, you now know how to implement gradient descent for linear regression. We have just one last video for this week. In that video, we'll see this algorithm in action. Let's go to that last video.
[{"start": 0.0, "end": 7.5200000000000005, "text": " So, previously you took a look at the linear regression model and then the cost function"}, {"start": 7.5200000000000005, "end": 10.68, "text": " and then the gradient descent algorithm."}, {"start": 10.68, "end": 15.8, "text": " In this video, we're going to put it all together and use the square error cost function for"}, {"start": 15.8, "end": 19.48, "text": " the linear regression model with gradient descent."}, {"start": 19.48, "end": 24.76, "text": " This will allow us to train the linear regression model to fit a straight line to our training"}, {"start": 24.76, "end": 25.76, "text": " data."}, {"start": 25.76, "end": 27.48, "text": " Let's get to it."}, {"start": 27.48, "end": 34.88, "text": " This is the linear regression model and to the right is the squared error cost function"}, {"start": 34.88, "end": 38.480000000000004, "text": " and below is the gradient descent algorithm."}, {"start": 38.480000000000004, "end": 45.760000000000005, "text": " It turns out if you calculate these derivatives, these are the terms you would get."}, {"start": 45.760000000000005, "end": 50.68, "text": " The derivative with respect to w is this."}, {"start": 50.68, "end": 54.2, "text": " One over m summed from i equals one through m."}, {"start": 54.2, "end": 59.88, "text": " Then the error term, that is the difference between the predicted and the actual values,"}, {"start": 59.88, "end": 63.480000000000004, "text": " times the input feature x i."}, {"start": 63.480000000000004, "end": 69.88, "text": " And the derivative with respect to b is this formula over here, which looks the same as"}, {"start": 69.88, "end": 76.28, "text": " the equation above except that it doesn't have that x i term at the end."}, {"start": 76.28, "end": 82.28, "text": " And if you use these formulas to compute these two derivatives and implement gradient descent"}, {"start": 82.28, "end": 84.8, "text": " this way, it will work."}, {"start": 84.8, "end": 89.16, "text": " Now you may be wondering, where did I get these formulas from?"}, {"start": 89.16, "end": 91.56, "text": " They're derived using calculus."}, {"start": 91.56, "end": 95.88, "text": " If you want to see the full derivation, I'll quickly run through the derivation on the"}, {"start": 95.88, "end": 96.88, "text": " next slide."}, {"start": 96.88, "end": 101.76, "text": " But if you don't remember or aren't interested in the calculus, don't worry about it."}, {"start": 101.76, "end": 106.32, "text": " You can skip the materials on the next slide entirely and still be able to implement gradient"}, {"start": 106.32, "end": 110.6, "text": " descent and finish this class and everything will work just fine."}, {"start": 110.6, "end": 115.64, "text": " So in this slide, which is one of the most mathematical slides of the entire specialization"}, {"start": 115.64, "end": 122.08, "text": " and again is completely optional, we'll show you how to calculate the derivative terms."}, {"start": 122.08, "end": 124.28, "text": " Let's start with the first term."}, {"start": 124.28, "end": 129.28, "text": " The derivative of the cost function j with respect to w."}, {"start": 129.28, "end": 134.2, "text": " We'll start by plugging in the definition of the cost function j."}, {"start": 134.2, "end": 139.04, "text": " So j of w b is this."}, {"start": 139.04, "end": 145.84, "text": " So one over two m times this sum of the squared error terms."}, {"start": 145.84, "end": 158.51999999999998, "text": " And now remember also that f of w b of x i is equal to this term over here, which is"}, {"start": 158.51999999999998, "end": 161.89999999999998, "text": " w x i plus b."}, {"start": 161.89999999999998, "end": 168.2, "text": " And so what we would like to do is compute the derivative also called the partial derivative"}, {"start": 168.2, "end": 175.28, "text": " with respect to w of this equation right here on the right."}, {"start": 175.28, "end": 180.32, "text": " If you've taken a calculus class before, and again is totally fine if you haven't, you"}, {"start": 180.32, "end": 187.6, "text": " may know that by the rules of calculus, the derivative is equal to this term over here,"}, {"start": 187.6, "end": 195.48, "text": " which is why the two here and the two here cancel out, leaving us with this equation"}, {"start": 195.48, "end": 199.16, "text": " that you saw on the previous slide."}, {"start": 199.16, "end": 205.12, "text": " So this, by the way, is why we had to find the cost function with the one half earlier"}, {"start": 205.12, "end": 209.48, "text": " this week is because it makes the partial derivative meter."}, {"start": 209.48, "end": 214.92, "text": " It cancels out the two that appears from computing the derivative."}, {"start": 214.92, "end": 220.07999999999998, "text": " For the other derivative with respect to b, this is quite similar."}, {"start": 220.08, "end": 227.72000000000003, "text": " I can write it out like this and once again, plug in the definition of f of x i giving"}, {"start": 227.72000000000003, "end": 230.20000000000002, "text": " this equation."}, {"start": 230.20000000000002, "end": 237.64000000000001, "text": " And by the rules of calculus, this is equal to this, where there's no x i anymore at the"}, {"start": 237.64000000000001, "end": 238.72000000000003, "text": " end."}, {"start": 238.72000000000003, "end": 245.0, "text": " And so the twos cancel once more and you end up with this expression for the derivative"}, {"start": 245.0, "end": 247.24, "text": " with respect to b."}, {"start": 247.24, "end": 252.44, "text": " And so now you have these two expressions for the derivatives and you can plug them"}, {"start": 252.44, "end": 256.36, "text": " into the gradient descent algorithm."}, {"start": 256.36, "end": 260.64, "text": " So here's the gradient descent algorithm for linear regression."}, {"start": 260.64, "end": 266.96000000000004, "text": " You repeatedly carry out these updates to w and b until convergence."}, {"start": 266.96000000000004, "end": 276.2, "text": " Remember that this f of x is a linear regression model, so it's equal to w times x plus b."}, {"start": 276.2, "end": 282.2, "text": " This expression here is the derivative of the cost function with respect to w and this"}, {"start": 282.2, "end": 287.84, "text": " expression is the derivative of the cost function with respect to b."}, {"start": 287.84, "end": 294.71999999999997, "text": " And just as a reminder, you want to update w and b simultaneously on each step."}, {"start": 294.71999999999997, "end": 298.56, "text": " Now let's get familiar with how gradient descent works."}, {"start": 298.56, "end": 303.64, "text": " One issue we saw with gradient descent is that it can lead to a local minimum instead"}, {"start": 303.64, "end": 309.12, "text": " of a global minimum, where the global minimum means the point that has the lowest possible"}, {"start": 309.12, "end": 313.52, "text": " value for the cost function j out of all possible points."}, {"start": 313.52, "end": 318.76, "text": " You may recall this surface plot that looks like an outdoor park with a few hills with"}, {"start": 318.76, "end": 322.15999999999997, "text": " the grass and the birds is a relaxing outdoor hill."}, {"start": 322.15999999999997, "end": 325.56, "text": " This function has more than one local minimum."}, {"start": 325.56, "end": 330.8, "text": " Remember depending on where you initialize the parameters w and b, you can end up at"}, {"start": 330.8, "end": 332.64, "text": " different local minima."}, {"start": 332.64, "end": 337.12, "text": " You can end up here or you can end up here."}, {"start": 337.12, "end": 342.36, "text": " But it turns out when you're using a squared error cost function with linear regression,"}, {"start": 342.36, "end": 347.68, "text": " the cost function does not and will never have multiple local minima."}, {"start": 347.68, "end": 352.56, "text": " It has a single global minimum because of this bow shape."}, {"start": 352.56, "end": 358.47999999999996, "text": " The technical term for this is that this cost function is a convex function."}, {"start": 358.48, "end": 365.8, "text": " Informally, a convex function is a bow shaped function and it cannot have any local minima"}, {"start": 365.8, "end": 369.48, "text": " other than the single global minimum."}, {"start": 369.48, "end": 376.12, "text": " So when you implement gradient descent on a convex function, one nice property is that"}, {"start": 376.12, "end": 382.68, "text": " so long as your learning rate is chosen appropriately, it will always converge to the global minimum."}, {"start": 382.68, "end": 388.3, "text": " Congratulations, you now know how to implement gradient descent for linear regression."}, {"start": 388.3, "end": 391.16, "text": " We have just one last video for this week."}, {"start": 391.16, "end": 394.16, "text": " In that video, we'll see this algorithm in action."}, {"start": 394.16, "end": 419.16, "text": " Let's go to that last video."}]