3.6 Gradient Descent | Gradient Descent Implementation  --[Machine Learning | Andrew Ng]
 To fit the parameters of a logistic regression model, we're going to try to find the values of the parameters w and b that minimise the cost function j of w and b. And we're going to apply gradient descent to do this. Let's take a look at how. In this video, we'll focus on how to find a good choice of the parameters w and b. After you've done so, if you give the model a new input x, say a new patient at the hospital with a certain tumour size and age that needs a diagnosis, the model can then make a prediction or it can try to estimate the probability that the label y is 1. The algorithm you can use to minimise the cost function is gradient descent. Here again is the cost function, and so if you want to minimise the cost j as a function of w and b, well, here's the usual gradient descent algorithm where you repeatedly update each parameter as the O value minus alpha, the learning rate, times this derivative term. Let's take a look at the derivative of j with respect to wj, this term up on top here, where as usual j goes from 1 through n, where n is the number of features. If someone were to apply the rules of calculus, you can show that the derivative with respect to wj of the cost function capital J is equal to this expression over here. Is 1 over m times the sum from 1 through m of this error term, that is f minus the label y times xj. Here this xij is the j feature of training example i. Now let's also look at the derivative of j with respect to the parameter b. It turns out to be this expression over here, and it's quite similar to the expression above, except that it is not multiplied by this x super strip i subscript j at the end. So just as a reminder, similar to what you saw for linear regression, the way to carry out these updates is to use simultaneous updates, meaning that you would first compute the right hand side for all of these updates, and then simultaneously overwrite all the values on the left at the same time. So let me take these derivative expressions here and plug them into these terms here. This gives you gradient descent for logistic regression. Now one funny thing you might be wondering is, huh, that's weird. These two equations look exactly like the algorithm we had come up with previously for linear regression. So you might be wondering, is linear regression actually secretly the same as logistic regression? Well, even though these equations look the same, the reason that this is not linear regression is because the definition for the function f of x has changed. In linear regression, f of x is this is w x plus b, but in logistic regression, f of x is defined to be the sigmoid function applied to w x plus b. So although the algorithm written looked the same for both linear regression and logistic regression, actually they're two very different algorithms because the definition for f of x is not the same. When we talked about gradient descent for linear regression previously, you saw how you can monitor gradient descent to make sure it converges. You can just apply the same method for logistic regression to make sure it also converges. I've written out these updates as if you're updating the parameters w j one parameter at a time. Similar to the discussion on vectorized implementations of linear regression, you can also use vectorization to make gradient descent run faster for logistic regression. I won't dive into the details of the vectorized implementation in this video, but you can also learn more about it and see the code in the optional labs. So now you know how to implement gradient descent for logistic regression. You might also remember feature scaling when we were using linear regression, where you saw how feature scaling that is scaling all the features to take on similar ranges of values, say between negative one and plus one, how that can help gradient descent to converge faster. Feature scaling applied the same way to scale the different features to take on similar ranges of values can also speed up gradient descent for logistic regression. In the upcoming optional lab, you also see how the gradient for the regression can be calculated in code. This will be useful to look at because you also implement this in a practice lab at the end of this week. After you run gradient descent in this lab, there'll be a nice set of animated plots that show gradient descent in action. You see the sigmoid function, the control plot of the cost, the 3d surface plot to the cost and the learning curve all evolve as gradient descent runs. There will be another optional lab after that, which is short and sweet, but also very useful because they'll show you how to use the popular scikit-learn library to train the logistic regression model for classification. Many machine learning practitioners in many companies today use scikit-learn regularly as part of their job. And so I hope you check out the scikit-learn function as well and take a look at how that is used. So that's it. You now know how to implement logistic regression. This is a very powerful, very widely used learning algorithm and you now know how to get it to work yourself. Congratulations.
[{"start": 0.0, "end": 7.28, "text": " To fit the parameters of a logistic regression model, we're going to try to find the values"}, {"start": 7.28, "end": 12.92, "text": " of the parameters w and b that minimise the cost function j of w and b."}, {"start": 12.92, "end": 16.4, "text": " And we're going to apply gradient descent to do this."}, {"start": 16.4, "end": 18.76, "text": " Let's take a look at how."}, {"start": 18.76, "end": 24.8, "text": " In this video, we'll focus on how to find a good choice of the parameters w and b."}, {"start": 24.8, "end": 31.92, "text": " After you've done so, if you give the model a new input x, say a new patient at the hospital"}, {"start": 31.92, "end": 39.08, "text": " with a certain tumour size and age that needs a diagnosis, the model can then make a prediction"}, {"start": 39.08, "end": 46.28, "text": " or it can try to estimate the probability that the label y is 1."}, {"start": 46.28, "end": 51.5, "text": " The algorithm you can use to minimise the cost function is gradient descent."}, {"start": 51.5, "end": 58.24, "text": " Here again is the cost function, and so if you want to minimise the cost j as a function"}, {"start": 58.24, "end": 65.64, "text": " of w and b, well, here's the usual gradient descent algorithm where you repeatedly update"}, {"start": 65.64, "end": 75.32, "text": " each parameter as the O value minus alpha, the learning rate, times this derivative term."}, {"start": 75.32, "end": 82.55999999999999, "text": " Let's take a look at the derivative of j with respect to wj, this term up on top here, where"}, {"start": 82.55999999999999, "end": 88.82, "text": " as usual j goes from 1 through n, where n is the number of features."}, {"start": 88.82, "end": 94.52, "text": " If someone were to apply the rules of calculus, you can show that the derivative with respect"}, {"start": 94.52, "end": 101.35999999999999, "text": " to wj of the cost function capital J is equal to this expression over here."}, {"start": 101.36, "end": 111.64, "text": " Is 1 over m times the sum from 1 through m of this error term, that is f minus the label"}, {"start": 111.64, "end": 116.96, "text": " y times xj."}, {"start": 116.96, "end": 123.44, "text": " Here this xij is the j feature of training example i."}, {"start": 123.44, "end": 129.07999999999998, "text": " Now let's also look at the derivative of j with respect to the parameter b."}, {"start": 129.08, "end": 135.4, "text": " It turns out to be this expression over here, and it's quite similar to the expression above,"}, {"start": 135.4, "end": 142.48000000000002, "text": " except that it is not multiplied by this x super strip i subscript j at the end."}, {"start": 142.48000000000002, "end": 147.36, "text": " So just as a reminder, similar to what you saw for linear regression, the way to carry"}, {"start": 147.36, "end": 153.08, "text": " out these updates is to use simultaneous updates, meaning that you would first compute the right"}, {"start": 153.08, "end": 159.60000000000002, "text": " hand side for all of these updates, and then simultaneously overwrite all the values on"}, {"start": 159.60000000000002, "end": 162.88000000000002, "text": " the left at the same time."}, {"start": 162.88000000000002, "end": 171.28, "text": " So let me take these derivative expressions here and plug them into these terms here."}, {"start": 171.28, "end": 177.5, "text": " This gives you gradient descent for logistic regression."}, {"start": 177.5, "end": 182.52, "text": " Now one funny thing you might be wondering is, huh, that's weird."}, {"start": 182.52, "end": 186.92000000000002, "text": " These two equations look exactly like the algorithm we had come up with previously for"}, {"start": 186.92000000000002, "end": 188.76000000000002, "text": " linear regression."}, {"start": 188.76000000000002, "end": 194.48000000000002, "text": " So you might be wondering, is linear regression actually secretly the same as logistic regression?"}, {"start": 194.48000000000002, "end": 201.52, "text": " Well, even though these equations look the same, the reason that this is not linear regression"}, {"start": 201.52, "end": 206.92000000000002, "text": " is because the definition for the function f of x has changed."}, {"start": 206.92, "end": 214.16, "text": " In linear regression, f of x is this is w x plus b, but in logistic regression, f of"}, {"start": 214.16, "end": 220.2, "text": " x is defined to be the sigmoid function applied to w x plus b."}, {"start": 220.2, "end": 225.56, "text": " So although the algorithm written looked the same for both linear regression and logistic"}, {"start": 225.56, "end": 231.0, "text": " regression, actually they're two very different algorithms because the definition for f of"}, {"start": 231.0, "end": 234.0, "text": " x is not the same."}, {"start": 234.0, "end": 238.92, "text": " When we talked about gradient descent for linear regression previously, you saw how"}, {"start": 238.92, "end": 243.36, "text": " you can monitor gradient descent to make sure it converges."}, {"start": 243.36, "end": 250.52, "text": " You can just apply the same method for logistic regression to make sure it also converges."}, {"start": 250.52, "end": 257.2, "text": " I've written out these updates as if you're updating the parameters w j one parameter"}, {"start": 257.2, "end": 260.96, "text": " at a time."}, {"start": 260.96, "end": 269.15999999999997, "text": " Similar to the discussion on vectorized implementations of linear regression, you can also use vectorization"}, {"start": 269.15999999999997, "end": 273.76, "text": " to make gradient descent run faster for logistic regression."}, {"start": 273.76, "end": 278.76, "text": " I won't dive into the details of the vectorized implementation in this video, but you can"}, {"start": 278.76, "end": 283.15999999999997, "text": " also learn more about it and see the code in the optional labs."}, {"start": 283.15999999999997, "end": 289.12, "text": " So now you know how to implement gradient descent for logistic regression."}, {"start": 289.12, "end": 294.72, "text": " You might also remember feature scaling when we were using linear regression, where you"}, {"start": 294.72, "end": 299.36, "text": " saw how feature scaling that is scaling all the features to take on similar ranges of"}, {"start": 299.36, "end": 304.64, "text": " values, say between negative one and plus one, how that can help gradient descent to"}, {"start": 304.64, "end": 306.8, "text": " converge faster."}, {"start": 306.8, "end": 310.96, "text": " Feature scaling applied the same way to scale the different features to take on similar"}, {"start": 310.96, "end": 316.68, "text": " ranges of values can also speed up gradient descent for logistic regression."}, {"start": 316.68, "end": 324.08, "text": " In the upcoming optional lab, you also see how the gradient for the regression can be"}, {"start": 324.08, "end": 326.16, "text": " calculated in code."}, {"start": 326.16, "end": 330.92, "text": " This will be useful to look at because you also implement this in a practice lab at the"}, {"start": 330.92, "end": 333.2, "text": " end of this week."}, {"start": 333.2, "end": 337.92, "text": " After you run gradient descent in this lab, there'll be a nice set of animated plots that"}, {"start": 337.92, "end": 340.52, "text": " show gradient descent in action."}, {"start": 340.52, "end": 345.92, "text": " You see the sigmoid function, the control plot of the cost, the 3d surface plot to the"}, {"start": 345.92, "end": 351.04, "text": " cost and the learning curve all evolve as gradient descent runs."}, {"start": 351.04, "end": 356.2, "text": " There will be another optional lab after that, which is short and sweet, but also very useful"}, {"start": 356.2, "end": 361.72, "text": " because they'll show you how to use the popular scikit-learn library to train the logistic"}, {"start": 361.72, "end": 365.20000000000005, "text": " regression model for classification."}, {"start": 365.20000000000005, "end": 370.16, "text": " Many machine learning practitioners in many companies today use scikit-learn regularly"}, {"start": 370.16, "end": 371.8, "text": " as part of their job."}, {"start": 371.8, "end": 376.40000000000003, "text": " And so I hope you check out the scikit-learn function as well and take a look at how that"}, {"start": 376.40000000000003, "end": 378.28000000000003, "text": " is used."}, {"start": 378.28000000000003, "end": 379.28000000000003, "text": " So that's it."}, {"start": 379.28000000000003, "end": 382.68, "text": " You now know how to implement logistic regression."}, {"start": 382.68, "end": 387.68, "text": " This is a very powerful, very widely used learning algorithm and you now know how to"}, {"start": 387.68, "end": 389.08000000000004, "text": " get it to work yourself."}, {"start": 389.08, "end": 404.71999999999997, "text": " Congratulations."}]