Gradient Descent With Momentum (C2W2L06)

A short lecture explaining gradient descent with momentum: it computes an exponentially weighted average of recent gradients (Vdw, Vdb) and updates parameters using these smoothed gradients to reduce oscillation and accelerate convergence along shallow directions. The video gives geometric intuition (contours and a ball-rolling analogy), presents the update equations, discusses initialization and an alternate formulation (omitting the 1-β factor), and recommends common hyperparameter settings (β≈0.9; tune α).
Chapters
There's an algorithm called momentum or gradient descent with momentum that almost always works faster than the standard gradient descent algorithm in one sentence. The basic idea is to compute an exponentially weighted average of your gradients and then to use that gradient to update your weights instead. In this video, let's unpack that one sentence description and see how you can actually implement this. As the most of an example, let's say that you're trying to optimize a cost function which has contours like this. So the red dot denotes the position of the minimum. Maybe you start a gradient descent here, and if you take one iteration of gradient descent, either batch or, meaning batch gradient descent, maybe end up heading there, but now you're on the other side of this ellipse, you kind of. And if you take another step of gradient descent, maybe you end up doing that, and then another step, another step, and so on. And you see that gradient descent will, you know, sort of take a lot of steps, right? Just slowly oscillate, um, toward the minimum. And these up and down oscillations slows down gradient descent and prevents you from using a much larger learning rate. In particular, if you were to use a much larger learning rate, you might end up overshooting and end up diverging, like so. And so the need to prevent the oscillations from getting too big forces you to use a learning rate that's not itself too large. Another way of viewing this problem is that on the vertical axis, you want your learning to be a bit slower because you don't want those oscillations. But on the horizontal axis, you want faster learning, right? Because you want it to aggressively move from left to right toward that minimum, toward that red dot. So here's what you can do. If you implement gradient descent with momentum on each iteration, or more specifically, during iteration t, you would compute the usual derivatives DW db. I'll omit the superscript square bracket else. But you compute DW DB on the current mini batch. And if you're using batch gradient descent, then you know the current mini batch would be just your whole batch. And this works as well for batch gradient descent. So. So if your current mini batch is your entire training set, this works fine as well. And then what you do is you compute V dw to be beta v dw plus one minus beta dw. So this is similar to when we're previously computing V theta equals beta v theta plus 1 minus beta theta t. Right? So it's computing moving average of the derivatives for W. You're getting. And then you similarly compute V DB equals that plus 1 minus beta times DB. And then you would update your weights using W gets updated as W minus the learning rate times. Instead of updating it with dw with the derivative, you update it with V dw. And similarly B gets updated as B minus alpha times V db. So what this does is smooth out the steps of gradient descent. For example, let's say that the last few derivatives you computed were this, this, this, this, this. If you average out these gradients, you find that the oscillations in the vertical direction will tend to average out to something closer to zero. So in the vertical direction where you want to slow things down, this will average out positive and negative numbers, so the average will be closer to zero. Whereas on the horizontal direction, all the derivatives are pointing to the right in the horizontal direction. So the average in the horizontal direction will still be pretty big. So that's why with this algorithm, with a few iterations, you find that the gradient descent with momentum ends up eventually just taking steps that are much smaller oscillations in the vertical direction, but are more directed toward the horizontal to just moving quickly in the horizontal direction. And so this allows your algorithm to, you know, take a more straightforward path or less to damp out the oscillations in its path to the minimum. One intuition for this momentum, which works for some people, not for everyone, is that if you're trying to minimize, you know, a bowl shape function, right? This, this is really the contours of a bow. I guess I'm not very good at drawing, but you're trying to minimize this type of bow shaped function, then these derivative terms you can think of as providing acceleration to a ball that you're rolling downhill, and these momentum terms you can think of as representing the velocity. And so imagine that you have a bow and you take a ball and the derivative imparts acceleration to this little ball. It's a little ball that's rolling down this hill, right? And so it rolls faster and faster because of acceleration. And beta, because this number a little bit less than 1, this plays a role of friction and it prevents your ball from, you know, speeding up without limit. But so rather than gradient descent just taking every single step independently of all previous steps, now your little ball can roll downhill and gain momentum, but it can accelerate down this bow and therefore gain momentum. I find that this ball rolling down a bowl analogy, it seems to work for some people who enjoy physics intuitions, but it doesn't work for everyone. So if this analogy of a Ball rolling down a bowl doesn't work for you. Don't worry about it. Finally, let's look at some details on how you implement this. Here's the algorithm. And so you now have two hyperparameters, the learning rate alpha, as well as this parameter beta, which controls your exponentially weighted average. The most common value for beta is 0.9 averaging over the last 10 days temperature. So this is like averaging over the last 10 iterations gradients. And in practice beta equals 0.9 works very well. Uh, feel free to try different values and do some hyperparameter search, but 0.9 appears to be a pretty robust value. Well, and how about bias correction? Right, so do you wanna take V DW and VDB and divide it by one minus beta to the T? In practice, people don't usually do this because after just 10 iterations your moving average will have warmed up and is no longer a bias estimate. So in practice, I don't really see people bothering with bias correction when implementing gradient descent or momentum. And of course this process initialized with V DW equals 0. Note that this is a matrix of zeros with the same dimension as dw, which is the same dimension as W. And V DB is also initialized to a vector of zero. So the same dimension as db, which is in turn has the same dimension as B. Finally, I just mentioned that if you read the literature on gradient descent with momentum, often you see it with this term omitted, with this one minus beta term omitted. So you end up with V DW equals beta V DW plus dw. And the net effect of using this version in purple is that V DW ends up being scaled by a factor of one minus beta or really one over one minus beta. And so when you're performing these gradient descent updates, alpha just needs to change by corresponding value of or 1 over 1 minus beta. In practice, both of these will work just fine. It just affects what's the best value of the learning rate alpha. But I find that this particular formulation is is a little less intuitive because one impact of this is that if you end up tuning the hyperparameter beta, then this affects the scaling of VDW and VDB as well. And so you end up needing to retune the learning rate alpha as well. Maybe. Um, so I personally prefer the formulation that I have written here on the left rather than leaving out the one minus beta term. But so I tend to use the formula on the left printed formula with the 1 minus beta term. But for both versions having beta equals 0.9 is a common choice of hyperparameter. It's just that alpha the learning rate would need to be tuned differently for these two different versions. So that's it for gradient descent with momentum. This will almost always work better than the straightforward gradient descent algorithm without momentum. But there's still other things we could do to speed up your learning algorithm. Let's continue talking about these in the next couple videos.










