1.15 Machine Learning Overview | Gradient descent --[Machine Learning | Andrew Ng]
 Welcome back. In the last video we saw visualizations of the cost function j and how you can try different choices of the parameters w and b and see what cost value that gets you. It would be nice if we had a more systematic way to find the values of w and b that result in the smallest possible cost j of w, b. It turns out there's an algorithm called gradient descent that you can use to do that. Gradient descent is used all over the place in machine learning, not just for linear regression, but for training, for example, some of the most advanced neural network models, also called deep learning models. Deep learning models are something you learn about in the second course. So learning this tool of gradient descent will set you up with one of the most important building blocks in machine learning. Here's an overview of what we'll do with gradient descent. You have the cost function j of w, b right here that you want to minimize. In the example we've seen so far, this is a cost function for linear regression, but it turns out that gradient descent is an algorithm that you can use to try to minimize any function, not just a cost function for linear regression. Just to make this discussion on gradient descent more general, it turns out that gradient descent applies to more general functions, including other cost functions that work with models that have more than two parameters. So for instance, if you have a cost function j as a function of w1, w2, up to wn and b, your objective is to minimize j over the parameters w1 to wn and b. In other words, you want to pick values for w1 through wn and b that gives you the smallest possible value of j. It turns out that gradient descent is an algorithm that you can apply to try to minimize this cost function j as well. What you're going to do is just start off with some initial guesses for w and b. In linear regression, it won't matter too much what the initial values are. So a common choice is to set them both to zero. So for example, you can set w to zero and b to zero as the initial guess. With the gradient descent algorithm, what you're going to do is you'll keep on changing the parameters w and b a bit every time to try to reduce the cost j of wb until hopefully j settles at or near a minimum. One thing I should note is that for some functions j that may not be a bow shape or a hammer shape, it is possible for there to be more than one possible minimum. Let's take a look at an example of a more complex surface plot j to see what gradient descent is doing. This function is not a squared error cost function. For linear regression with the squared error cost function, you always end up with a bow shape or a hammer shape. But this is a type of cost function you might get if you're training a neural network model. Notice the axes. That is w and b on the bottom axis. For different values of w and b, you get different points on the surface j of wb, where the height of the surface at some point is the value of the cost function. Now, let's imagine that this surface plot is actually a view of a slightly hilly outdoor park or a golf course, where the high points are hills and the low points are valleys like so. And I'd like you to imagine, if you will, that you're physically standing at this point on the hill. And if it helps you to relax, imagine that this lots of really nice green grass and butterflies and flowers is a really nice hill. And your goal is to start up here and get to the bottom of one of these valleys as efficiently as possible. So what the gradient descent algorithm does is you're going to spin around 360 degrees and look around and ask yourself, if I were to take a tiny little baby step in one direction, and I want to go downhill as quickly as possible toward one of these valleys, what direction do I choose to take that baby step? Well, if you want to walk down this hill as efficiently as possible, it turns out that if you're standing at this point in the hill and you look around, you may notice that the best direction to take your next step downhill is roughly that direction. Mathematically, this is the direction of steepest descent. And it means that when you take a tiny baby little step, this takes you downhill faster than a tiny little baby step you could have taken in any other direction. So after taking this first step, you're now at this point on the hill over here. Now, let's repeat the process. Standing at this new point, you're going to again spin around 360 degrees and ask yourself, in what direction would I take the next little baby step in order to move downhill? And if you do that and take another step, you end up moving a bit in that direction and you can keep going. From this new point, you can again look around and decide what direction would take you downhill most quickly. Take another step, another step, and so on until you find yourself at the bottom of this valley at this local minimum right here. What you just did was go through multiple steps of gradient descent. It turns out gradient descent has an interesting property. Remember that you can choose a starting point at the surface by choosing starting values for the parameters w and b. When you perform gradient descent a moment ago, you had started at this point over here, right? Now, imagine if you try gradient descent again, but this time you choose a different starting point by choosing parameters that place your starting point just a couple steps to the right over here. If you then repeat the gradient descent process, which means you look around, take a little step in the direction of the steepest descent, so you end up here. Then you again look around, take another step and so on. And if you were to run gradient descent the second time, starting just a couple steps to the right of where we did it the first time, then you will end up in a totally different valley, this different minimum over here on the right. The bottoms of both the first and the second valleys are called local minima, because if you start going down the first valley, gradient descent won't lead you to the second valley. And the same is true if you started going down the second valley, you will stay in that second minimum and not find your way into the first local minimum. So this is an interesting property of the gradient descent algorithm, and you'll see more about this later. So in this video, you saw how gradient descent helps you go downhill. In the next video, let's look at the mathematical expressions that you can implement to make gradient descent work. Let's go on to the next video.
[{"start": 0.0, "end": 8.2, "text": " Welcome back. In the last video we saw visualizations of the cost function j and how you can try"}, {"start": 8.2, "end": 14.32, "text": " different choices of the parameters w and b and see what cost value that gets you. It"}, {"start": 14.32, "end": 20.080000000000002, "text": " would be nice if we had a more systematic way to find the values of w and b that result"}, {"start": 20.080000000000002, "end": 26.96, "text": " in the smallest possible cost j of w, b. It turns out there's an algorithm called gradient"}, {"start": 26.96, "end": 32.36, "text": " descent that you can use to do that. Gradient descent is used all over the place in machine"}, {"start": 32.36, "end": 37.56, "text": " learning, not just for linear regression, but for training, for example, some of the"}, {"start": 37.56, "end": 43.84, "text": " most advanced neural network models, also called deep learning models. Deep learning"}, {"start": 43.84, "end": 50.2, "text": " models are something you learn about in the second course. So learning this tool of gradient"}, {"start": 50.2, "end": 55.6, "text": " descent will set you up with one of the most important building blocks in machine learning."}, {"start": 55.6, "end": 61.92, "text": " Here's an overview of what we'll do with gradient descent. You have the cost function j of w,"}, {"start": 61.92, "end": 69.04, "text": " b right here that you want to minimize. In the example we've seen so far, this is a cost"}, {"start": 69.04, "end": 74.16, "text": " function for linear regression, but it turns out that gradient descent is an algorithm"}, {"start": 74.16, "end": 82.64, "text": " that you can use to try to minimize any function, not just a cost function for linear regression."}, {"start": 82.64, "end": 88.08, "text": " Just to make this discussion on gradient descent more general, it turns out that gradient descent"}, {"start": 88.08, "end": 94.08, "text": " applies to more general functions, including other cost functions that work with models"}, {"start": 94.08, "end": 101.68, "text": " that have more than two parameters. So for instance, if you have a cost function j as"}, {"start": 101.68, "end": 112.28, "text": " a function of w1, w2, up to wn and b, your objective is to minimize j over the parameters"}, {"start": 112.28, "end": 123.0, "text": " w1 to wn and b. In other words, you want to pick values for w1 through wn and b that gives"}, {"start": 123.0, "end": 129.16, "text": " you the smallest possible value of j. It turns out that gradient descent is an algorithm"}, {"start": 129.16, "end": 135.8, "text": " that you can apply to try to minimize this cost function j as well. What you're going"}, {"start": 135.8, "end": 143.36, "text": " to do is just start off with some initial guesses for w and b. In linear regression,"}, {"start": 143.36, "end": 148.92000000000002, "text": " it won't matter too much what the initial values are. So a common choice is to set them"}, {"start": 148.92000000000002, "end": 156.08, "text": " both to zero. So for example, you can set w to zero and b to zero as the initial guess."}, {"start": 156.08, "end": 161.12, "text": " With the gradient descent algorithm, what you're going to do is you'll keep on changing"}, {"start": 161.12, "end": 169.68, "text": " the parameters w and b a bit every time to try to reduce the cost j of wb until hopefully"}, {"start": 169.68, "end": 176.96, "text": " j settles at or near a minimum. One thing I should note is that for some functions j"}, {"start": 176.96, "end": 182.84, "text": " that may not be a bow shape or a hammer shape, it is possible for there to be more than one"}, {"start": 182.84, "end": 190.92000000000002, "text": " possible minimum. Let's take a look at an example of a more complex surface plot j to"}, {"start": 190.92, "end": 197.6, "text": " see what gradient descent is doing. This function is not a squared error cost function. For"}, {"start": 197.6, "end": 203.16, "text": " linear regression with the squared error cost function, you always end up with a bow shape"}, {"start": 203.16, "end": 209.27999999999997, "text": " or a hammer shape. But this is a type of cost function you might get if you're training"}, {"start": 209.27999999999997, "end": 219.54, "text": " a neural network model. Notice the axes. That is w and b on the bottom axis. For different"}, {"start": 219.54, "end": 227.23999999999998, "text": " values of w and b, you get different points on the surface j of wb, where the height of"}, {"start": 227.23999999999998, "end": 233.48, "text": " the surface at some point is the value of the cost function. Now, let's imagine that"}, {"start": 233.48, "end": 240.6, "text": " this surface plot is actually a view of a slightly hilly outdoor park or a golf course,"}, {"start": 240.6, "end": 246.12, "text": " where the high points are hills and the low points are valleys like so. And I'd like you"}, {"start": 246.12, "end": 252.8, "text": " to imagine, if you will, that you're physically standing at this point on the hill. And if"}, {"start": 252.8, "end": 258.36, "text": " it helps you to relax, imagine that this lots of really nice green grass and butterflies"}, {"start": 258.36, "end": 266.78000000000003, "text": " and flowers is a really nice hill. And your goal is to start up here and get to the bottom"}, {"start": 266.78000000000003, "end": 273.84000000000003, "text": " of one of these valleys as efficiently as possible. So what the gradient descent algorithm"}, {"start": 273.84, "end": 281.56, "text": " does is you're going to spin around 360 degrees and look around and ask yourself, if I were"}, {"start": 281.56, "end": 288.44, "text": " to take a tiny little baby step in one direction, and I want to go downhill as quickly as possible"}, {"start": 288.44, "end": 294.67999999999995, "text": " toward one of these valleys, what direction do I choose to take that baby step? Well,"}, {"start": 294.67999999999995, "end": 300.2, "text": " if you want to walk down this hill as efficiently as possible, it turns out that if you're standing"}, {"start": 300.2, "end": 304.88, "text": " at this point in the hill and you look around, you may notice that the best direction to"}, {"start": 304.88, "end": 311.24, "text": " take your next step downhill is roughly that direction. Mathematically, this is the direction"}, {"start": 311.24, "end": 317.0, "text": " of steepest descent. And it means that when you take a tiny baby little step, this takes"}, {"start": 317.0, "end": 323.4, "text": " you downhill faster than a tiny little baby step you could have taken in any other direction."}, {"start": 323.4, "end": 330.15999999999997, "text": " So after taking this first step, you're now at this point on the hill over here. Now,"}, {"start": 330.16, "end": 335.72, "text": " let's repeat the process. Standing at this new point, you're going to again spin around"}, {"start": 335.72, "end": 342.16, "text": " 360 degrees and ask yourself, in what direction would I take the next little baby step in"}, {"start": 342.16, "end": 348.02000000000004, "text": " order to move downhill? And if you do that and take another step, you end up moving a"}, {"start": 348.02000000000004, "end": 355.68, "text": " bit in that direction and you can keep going. From this new point, you can again look around"}, {"start": 355.68, "end": 362.12, "text": " and decide what direction would take you downhill most quickly. Take another step, another step,"}, {"start": 362.12, "end": 368.08, "text": " and so on until you find yourself at the bottom of this valley at this local minimum right"}, {"start": 368.08, "end": 375.4, "text": " here. What you just did was go through multiple steps of gradient descent. It turns out gradient"}, {"start": 375.4, "end": 382.4, "text": " descent has an interesting property. Remember that you can choose a starting point at the"}, {"start": 382.4, "end": 388.47999999999996, "text": " surface by choosing starting values for the parameters w and b. When you perform gradient"}, {"start": 388.47999999999996, "end": 395.79999999999995, "text": " descent a moment ago, you had started at this point over here, right? Now, imagine if you"}, {"start": 395.79999999999995, "end": 401.71999999999997, "text": " try gradient descent again, but this time you choose a different starting point by choosing"}, {"start": 401.71999999999997, "end": 408.23999999999995, "text": " parameters that place your starting point just a couple steps to the right over here."}, {"start": 408.24, "end": 412.84000000000003, "text": " If you then repeat the gradient descent process, which means you look around, take a little"}, {"start": 412.84000000000003, "end": 419.04, "text": " step in the direction of the steepest descent, so you end up here. Then you again look around,"}, {"start": 419.04, "end": 426.40000000000003, "text": " take another step and so on. And if you were to run gradient descent the second time, starting"}, {"start": 426.40000000000003, "end": 431.16, "text": " just a couple steps to the right of where we did it the first time, then you will end"}, {"start": 431.16, "end": 438.64000000000004, "text": " up in a totally different valley, this different minimum over here on the right. The bottoms"}, {"start": 438.64000000000004, "end": 445.24, "text": " of both the first and the second valleys are called local minima, because if you start"}, {"start": 445.24, "end": 451.08000000000004, "text": " going down the first valley, gradient descent won't lead you to the second valley. And the"}, {"start": 451.08000000000004, "end": 456.6, "text": " same is true if you started going down the second valley, you will stay in that second"}, {"start": 456.6, "end": 463.40000000000003, "text": " minimum and not find your way into the first local minimum. So this is an interesting property"}, {"start": 463.40000000000003, "end": 469.44, "text": " of the gradient descent algorithm, and you'll see more about this later. So in this video,"}, {"start": 469.44, "end": 475.48, "text": " you saw how gradient descent helps you go downhill. In the next video, let's look at"}, {"start": 475.48, "end": 481.24, "text": " the mathematical expressions that you can implement to make gradient descent work. Let's"}, {"start": 481.24, "end": 488.24, "text": " go on to the next video."}]