1.16 Machine Learning Overview | Implementing gradient descent --[Machine Learning | Andrew Ng]
 Let's take a look at how you can actually implement the gradient descent algorithm. Let me write down the gradient descent algorithm. Here it is. On each step, w, the parameter, is updated to the old value of w minus alpha times this term d over dw of the cost function j of wb. So what this expression is saying is update your parameter w by taking the current value of w and adjusting it a small amount, which is this expression on the right, minus alpha times this term over here. So if you feel like there's a lot going on in this equation, it's okay. Don't worry about it. We'll unpack it together. First, this equal notation here. Notice I said we're assigning w a value using this equal sign. So in this context, this equal sign is the assignment operator. Specifically, in this context, if you write code that says a equals c, it means take the value of c and store it in your computer in the variable a. If you write a equals a plus one, it means set the value of a to be equal to a plus one or increment the value of a by one. So the assignment operator in coding is different than truth assertions in mathematics, where if I write a equals c, I'm asserting, that is, I am claiming that the values of a and c are equal to each other. And hopefully I will never write a truth assertion a equals a plus one because, you know, that just can't possibly be true. So in Python and in other programming languages, truth assertions are sometimes written as equals equals. So you may see code that says a equals equals c if you're testing whether a is equal to c. But in math notation, as we conventionally use it, like in these videos, the equal sign can be used for either assignments or for truth assertion. And so I try to make sure it's clear when I write an equal sign, whether we're assigning a value to a variable or whether we're asserting the truth of the equality of two values. Now let's dive more deeply into what the symbols in this equation means. The symbol here is the Greek alphabet alpha, and in this equation, alpha is also called the learning rate. The learning rate is usually a small positive number between zero and one, and it might be say 0.01. What alpha does is it basically controls how big of a step you take downhill. So if alpha is very large, then that corresponds to a very aggressive gradient descent procedure where you're trying to take huge steps downhill. And if alpha is very small, then you'd be taking small baby steps downhill. We'll come back later to delve more deeply into how to choose a good learning rate alpha. And finally, this term here, that's the derivative term of the cost function J. Let's not worry about the details of this derivative right now, but later on you get to see more about the derivative term. But for now, you can think of this derivative term that I drew a magenta box around as telling you in which direction you want to take your baby step. And in combination with the learning rate alpha, it also determines the size of the steps you want to take downhill. Now I do want to mention that derivatives come from calculus, and even if you aren't familiar with calculus, don't worry about it. Even without knowing any calculus, you'll be able to figure out all you need to know about this derivative term in this video and the next. One more thing. Remember your model has two parameters, not just W, but also B. So you also have an assignment operation to update the parameter B that looks very similar. B is assigned the O value of B minus the learning rate alpha times this slightly different derivative term D over dB of J of WB. So remember in the graph of the surface plot where you're taking baby steps until you get to the bottom of the valley? Well, for the gradient descent algorithm, you're going to repeat these two update steps until the algorithm converges. And by converges, I mean that you reach the point at a local minimum where the parameters W and B no longer change much with each additional step that you take. Now there's one more subtle detail about how to correctly implement gradient descent. You're going to update two parameters W and B, right? So this update takes place for both parameters W and B. One important detail is that for gradient descent, you want to simultaneously update W and B, meaning you want to update both parameters at the same time. What I mean by that is that in this expression, you're going to update W from the old W to a new W, and you're also updating B from its old value to a new value B. And the way to implement this is to compute the right side, computing this thing for W and B and simultaneously at the same time, update W and B to the new values. So let's take a look at what this means. Here's the correct way to implement gradient descent, which does a simultaneous update. This sets a variable tempW equal to that expression, which is W minus that term here. Let's also set another variable tempB to that, which is B minus that term. So you compute both right-hand sides, both updates, and store them into variables tempW and tempB. Then you copy the value of tempW into W, and you also copy the value of tempB into B. Now one thing you may notice is that this value of W is from before W gets updated. Here notice that the pre-update W is what goes into the derivative term over here, okay? In contrast, here's an incorrect implementation of gradient descent that does not do a simultaneous update. In this incorrect implementation, we compute tempW same as before, so far that's okay. And now here's where things start to differ. We then update W with the value in tempW before calculating the new value for the other parameter B. Next, we calculate tempB as B minus that term here. And finally, we update B with the value in tempB. The difference between the right-hand side and the left-hand side implementations is that if you look over here, this W has already been updated to this new value, and it's this updated W that actually goes into the cost function J of WB. It means that this term here on the right is not the same as this term over here that you see on the left. And that also means this tempB term on the right is not quite the same as the tempB term on the left, and thus this updated value for B on the right is not the same as this updated value for variable B on the left. The way that gradient descent is implemented in code, it actually turns out to be more natural to implement it the correct way with simultaneous updates. When you hear someone talk about gradient descent, they always mean the gradient descent where you perform a simultaneous update of the parameters. If however, you were to implement non-simultaneous updates, it turns out it will probably work more or less anyway, but doing it this way isn't really the correct way to implement it, it's actually some other algorithm with different properties. So I would advise you to just stick to the correct simultaneous update and not use this incorrect version on the right. So that's gradient descent. In the next video, we'll go into details of the derivative term, which you saw in this video, but that we didn't really talk about in detail. Calculus are part of calculus, and again, if you're not familiar with calculus, don't worry about it. You won't need to know calculus at all in order to complete this course or this specialization, and you'll have all the information you need in order to implement gradient descent. Coming up in the next video, we'll go over derivatives together, and you come away with the intuition and knowledge you need to be able to implement and apply gradient descent yourself. I think that'll be an exciting thing for you to know how to implement. So let's go on to the next video to see how to do that.
[{"start": 0.0, "end": 10.040000000000001, "text": " Let's take a look at how you can actually implement the gradient descent algorithm."}, {"start": 10.040000000000001, "end": 14.280000000000001, "text": " Let me write down the gradient descent algorithm."}, {"start": 14.280000000000001, "end": 15.4, "text": " Here it is."}, {"start": 15.4, "end": 25.240000000000002, "text": " On each step, w, the parameter, is updated to the old value of w minus alpha times this"}, {"start": 25.24, "end": 30.959999999999997, "text": " term d over dw of the cost function j of wb."}, {"start": 30.959999999999997, "end": 36.76, "text": " So what this expression is saying is update your parameter w by taking the current value"}, {"start": 36.76, "end": 44.44, "text": " of w and adjusting it a small amount, which is this expression on the right, minus alpha"}, {"start": 44.44, "end": 49.32, "text": " times this term over here."}, {"start": 49.32, "end": 54.519999999999996, "text": " So if you feel like there's a lot going on in this equation, it's okay."}, {"start": 54.52, "end": 55.52, "text": " Don't worry about it."}, {"start": 55.52, "end": 58.120000000000005, "text": " We'll unpack it together."}, {"start": 58.120000000000005, "end": 62.2, "text": " First, this equal notation here."}, {"start": 62.2, "end": 66.86, "text": " Notice I said we're assigning w a value using this equal sign."}, {"start": 66.86, "end": 71.36, "text": " So in this context, this equal sign is the assignment operator."}, {"start": 71.36, "end": 79.0, "text": " Specifically, in this context, if you write code that says a equals c, it means take the"}, {"start": 79.0, "end": 84.28, "text": " value of c and store it in your computer in the variable a."}, {"start": 84.28, "end": 89.96000000000001, "text": " If you write a equals a plus one, it means set the value of a to be equal to a plus one"}, {"start": 89.96000000000001, "end": 93.84, "text": " or increment the value of a by one."}, {"start": 93.84, "end": 101.68, "text": " So the assignment operator in coding is different than truth assertions in mathematics, where"}, {"start": 101.68, "end": 107.76, "text": " if I write a equals c, I'm asserting, that is, I am claiming that the values of a and"}, {"start": 107.76, "end": 110.56, "text": " c are equal to each other."}, {"start": 110.56, "end": 115.32000000000001, "text": " And hopefully I will never write a truth assertion a equals a plus one because, you know, that"}, {"start": 115.32000000000001, "end": 118.32000000000001, "text": " just can't possibly be true."}, {"start": 118.32000000000001, "end": 124.2, "text": " So in Python and in other programming languages, truth assertions are sometimes written as"}, {"start": 124.2, "end": 126.04, "text": " equals equals."}, {"start": 126.04, "end": 131.84, "text": " So you may see code that says a equals equals c if you're testing whether a is equal to"}, {"start": 131.84, "end": 132.84, "text": " c."}, {"start": 132.84, "end": 138.44, "text": " But in math notation, as we conventionally use it, like in these videos, the equal sign"}, {"start": 138.44, "end": 143.4, "text": " can be used for either assignments or for truth assertion."}, {"start": 143.4, "end": 148.0, "text": " And so I try to make sure it's clear when I write an equal sign, whether we're assigning"}, {"start": 148.0, "end": 155.56, "text": " a value to a variable or whether we're asserting the truth of the equality of two values."}, {"start": 155.56, "end": 160.56, "text": " Now let's dive more deeply into what the symbols in this equation means."}, {"start": 160.56, "end": 169.28, "text": " The symbol here is the Greek alphabet alpha, and in this equation, alpha is also called"}, {"start": 169.28, "end": 171.28, "text": " the learning rate."}, {"start": 171.28, "end": 176.22, "text": " The learning rate is usually a small positive number between zero and one, and it might"}, {"start": 176.22, "end": 179.92000000000002, "text": " be say 0.01."}, {"start": 179.92000000000002, "end": 186.1, "text": " What alpha does is it basically controls how big of a step you take downhill."}, {"start": 186.1, "end": 192.88, "text": " So if alpha is very large, then that corresponds to a very aggressive gradient descent procedure"}, {"start": 192.88, "end": 196.06, "text": " where you're trying to take huge steps downhill."}, {"start": 196.06, "end": 201.28, "text": " And if alpha is very small, then you'd be taking small baby steps downhill."}, {"start": 201.28, "end": 207.07999999999998, "text": " We'll come back later to delve more deeply into how to choose a good learning rate alpha."}, {"start": 207.07999999999998, "end": 213.64, "text": " And finally, this term here, that's the derivative term of the cost function J."}, {"start": 213.64, "end": 218.35999999999999, "text": " Let's not worry about the details of this derivative right now, but later on you get"}, {"start": 218.35999999999999, "end": 221.23999999999998, "text": " to see more about the derivative term."}, {"start": 221.23999999999998, "end": 226.44, "text": " But for now, you can think of this derivative term that I drew a magenta box around as telling"}, {"start": 226.44, "end": 230.07999999999998, "text": " you in which direction you want to take your baby step."}, {"start": 230.07999999999998, "end": 235.39999999999998, "text": " And in combination with the learning rate alpha, it also determines the size of the"}, {"start": 235.39999999999998, "end": 238.6, "text": " steps you want to take downhill."}, {"start": 238.6, "end": 244.12, "text": " Now I do want to mention that derivatives come from calculus, and even if you aren't"}, {"start": 244.12, "end": 247.48, "text": " familiar with calculus, don't worry about it."}, {"start": 247.48, "end": 251.04, "text": " Even without knowing any calculus, you'll be able to figure out all you need to know"}, {"start": 251.04, "end": 255.72, "text": " about this derivative term in this video and the next."}, {"start": 255.72, "end": 257.2, "text": " One more thing."}, {"start": 257.2, "end": 264.2, "text": " Remember your model has two parameters, not just W, but also B. So you also have an assignment"}, {"start": 264.2, "end": 268.76, "text": " operation to update the parameter B that looks very similar."}, {"start": 268.76, "end": 278.76, "text": " B is assigned the O value of B minus the learning rate alpha times this slightly different derivative"}, {"start": 278.76, "end": 283.28, "text": " term D over dB of J of WB."}, {"start": 283.28, "end": 288.8, "text": " So remember in the graph of the surface plot where you're taking baby steps until you get"}, {"start": 288.8, "end": 290.59999999999997, "text": " to the bottom of the valley?"}, {"start": 290.6, "end": 296.08000000000004, "text": " Well, for the gradient descent algorithm, you're going to repeat these two update steps"}, {"start": 296.08000000000004, "end": 298.52000000000004, "text": " until the algorithm converges."}, {"start": 298.52000000000004, "end": 304.24, "text": " And by converges, I mean that you reach the point at a local minimum where the parameters"}, {"start": 304.24, "end": 310.6, "text": " W and B no longer change much with each additional step that you take."}, {"start": 310.6, "end": 316.88, "text": " Now there's one more subtle detail about how to correctly implement gradient descent."}, {"start": 316.88, "end": 321.1, "text": " You're going to update two parameters W and B, right?"}, {"start": 321.1, "end": 326.62, "text": " So this update takes place for both parameters W and B."}, {"start": 326.62, "end": 334.6, "text": " One important detail is that for gradient descent, you want to simultaneously update"}, {"start": 334.6, "end": 340.28, "text": " W and B, meaning you want to update both parameters at the same time."}, {"start": 340.28, "end": 346.84, "text": " What I mean by that is that in this expression, you're going to update W from the old W to"}, {"start": 346.84, "end": 355.44, "text": " a new W, and you're also updating B from its old value to a new value B."}, {"start": 355.44, "end": 361.52, "text": " And the way to implement this is to compute the right side, computing this thing for W"}, {"start": 361.52, "end": 370.79999999999995, "text": " and B and simultaneously at the same time, update W and B to the new values."}, {"start": 370.79999999999995, "end": 374.4, "text": " So let's take a look at what this means."}, {"start": 374.4, "end": 380.32, "text": " Here's the correct way to implement gradient descent, which does a simultaneous update."}, {"start": 380.32, "end": 388.23999999999995, "text": " This sets a variable tempW equal to that expression, which is W minus that term here."}, {"start": 388.23999999999995, "end": 394.15999999999997, "text": " Let's also set another variable tempB to that, which is B minus that term."}, {"start": 394.15999999999997, "end": 399.4, "text": " So you compute both right-hand sides, both updates, and store them into variables tempW"}, {"start": 399.4, "end": 401.91999999999996, "text": " and tempB."}, {"start": 401.92, "end": 412.08000000000004, "text": " Then you copy the value of tempW into W, and you also copy the value of tempB into B."}, {"start": 412.08000000000004, "end": 420.96000000000004, "text": " Now one thing you may notice is that this value of W is from before W gets updated."}, {"start": 420.96000000000004, "end": 428.72, "text": " Here notice that the pre-update W is what goes into the derivative term over here, okay?"}, {"start": 428.72, "end": 434.8, "text": " In contrast, here's an incorrect implementation of gradient descent that does not do a simultaneous"}, {"start": 434.8, "end": 435.8, "text": " update."}, {"start": 435.8, "end": 444.12, "text": " In this incorrect implementation, we compute tempW same as before, so far that's okay."}, {"start": 444.12, "end": 447.24, "text": " And now here's where things start to differ."}, {"start": 447.24, "end": 454.72, "text": " We then update W with the value in tempW before calculating the new value for the other parameter"}, {"start": 454.72, "end": 455.72, "text": " B."}, {"start": 455.72, "end": 460.88000000000005, "text": " Next, we calculate tempB as B minus that term here."}, {"start": 460.88000000000005, "end": 465.84000000000003, "text": " And finally, we update B with the value in tempB."}, {"start": 465.84000000000003, "end": 469.52000000000004, "text": " The difference between the right-hand side and the left-hand side implementations is"}, {"start": 469.52000000000004, "end": 476.8, "text": " that if you look over here, this W has already been updated to this new value, and it's this"}, {"start": 476.8, "end": 483.52000000000004, "text": " updated W that actually goes into the cost function J of WB."}, {"start": 483.52, "end": 488.76, "text": " It means that this term here on the right is not the same as this term over here that"}, {"start": 488.76, "end": 491.35999999999996, "text": " you see on the left."}, {"start": 491.35999999999996, "end": 499.15999999999997, "text": " And that also means this tempB term on the right is not quite the same as the tempB term"}, {"start": 499.15999999999997, "end": 505.35999999999996, "text": " on the left, and thus this updated value for B on the right is not the same as this updated"}, {"start": 505.35999999999996, "end": 509.71999999999997, "text": " value for variable B on the left."}, {"start": 509.72, "end": 513.88, "text": " The way that gradient descent is implemented in code, it actually turns out to be more"}, {"start": 513.88, "end": 520.46, "text": " natural to implement it the correct way with simultaneous updates."}, {"start": 520.46, "end": 524.8000000000001, "text": " When you hear someone talk about gradient descent, they always mean the gradient descent"}, {"start": 524.8000000000001, "end": 528.96, "text": " where you perform a simultaneous update of the parameters."}, {"start": 528.96, "end": 535.88, "text": " If however, you were to implement non-simultaneous updates, it turns out it will probably work"}, {"start": 535.88, "end": 540.6, "text": " more or less anyway, but doing it this way isn't really the correct way to implement"}, {"start": 540.6, "end": 544.68, "text": " it, it's actually some other algorithm with different properties."}, {"start": 544.68, "end": 550.06, "text": " So I would advise you to just stick to the correct simultaneous update and not use this"}, {"start": 550.06, "end": 553.08, "text": " incorrect version on the right."}, {"start": 553.08, "end": 555.24, "text": " So that's gradient descent."}, {"start": 555.24, "end": 559.68, "text": " In the next video, we'll go into details of the derivative term, which you saw in this"}, {"start": 559.68, "end": 563.96, "text": " video, but that we didn't really talk about in detail."}, {"start": 563.96, "end": 568.24, "text": " Calculus are part of calculus, and again, if you're not familiar with calculus, don't"}, {"start": 568.24, "end": 569.5400000000001, "text": " worry about it."}, {"start": 569.5400000000001, "end": 575.0400000000001, "text": " You won't need to know calculus at all in order to complete this course or this specialization,"}, {"start": 575.0400000000001, "end": 580.36, "text": " and you'll have all the information you need in order to implement gradient descent."}, {"start": 580.36, "end": 585.2800000000001, "text": " Coming up in the next video, we'll go over derivatives together, and you come away with"}, {"start": 585.2800000000001, "end": 591.0, "text": " the intuition and knowledge you need to be able to implement and apply gradient descent"}, {"start": 591.0, "end": 592.32, "text": " yourself."}, {"start": 592.32, "end": 595.9200000000001, "text": " I think that'll be an exciting thing for you to know how to implement."}, {"start": 595.92, "end": 622.92, "text": " So let's go on to the next video to see how to do that."}]