Gradient Descent with Momentum, RMSProp, and Adam
Momentum, RMSProp, and Adam are considered adaptive optimization algorithms because they change the learning rate during training in a way that is more responsive to the topology of the cost function's landscape, compared to the more rigid update rules of basic gradient descent variations. It helps accelerate the gradient descent algorithm, which is crucial for training neural networks efficiently. Fundamentally, they do this by using an exponentially weighted averages of the gradients rather than just the current one. This way, it smooths out the noise in the steps taken towards the minimum of a cost function.
Exponentially Weighted Moving Averages
We should first go over exponentially weighted averages. If we have time-series data like the temperature of days throughout the year:
If we plot data like this, we will find it quite noisy. Now let's compute the exponentially weighted averages using:
For example, say and we want to compute :
If we plot this, the values will represent averages over, heuristically, entries. So, the plot will be much smoother than the original plot.
- will average last 10 entries
- will average last 50 entries
- will average last 2 entries
This heuristic comes from the fact that when :
More generally, it takes about 10 days to decay to about 1/3. So we are averaging over roughly 10 entries. This is a good rule of thumb to keep in mind. Similarly, you can approximate the number of days using:
With high value of beta, the plot you get is much smoother because you're now averaging over more days of temperature. So, the curve is less wavy & smoother, but on the flip side the curve has now shifts further to the right because you're now averaging over a much larger window of temperatures. And by averaging over a larger window, this formula, this exponentially weighted average formula adapts more slowly when the temperature changes i.e. we have more latency. And the reason for that is when Beta 0.98 then it's giving a lot of weight to the previous value and a much smaller weight, just 0.02, to whatever you're seeing right now.
By averaging only over two days temperature you're averaging over much shorter window. The resulting data will be much more noisy, and also much more susceptible to outliers. But this adapts much more quickly when the temperature changes.
Understanding exponentially weighted averages
Intuition. Say we were calculating up until :
We can expand and rewrite the equation as as sum:
We can see clearly now that this is just a weighted sum with different weights on different 's, where the weights decay exponentially, and the weights add up to 1. So in general:
This calculation takes very little memory to calculate. This has efficiency - just a storage of one real number. We can implement this algorithm with more accurate results using a moving window. But the code is more efficient and faster using the exponentially weighted averages algorithm.
Bias Correction in Exponentially Weighted Averages
Initially, because , the exponentially weighted average is biased toward zero, which can affect early iterations. To correct this, you can apply bias correction:
As becomes larger, becomes close to 1. Bias correction works to make the denominator large near and like when is large. So, to compare, the old equation:
New Equation with Bias Correction:
When is small, is inflated and when is large, the denominator just becomes almost 1 and there is no effect of it.
Although, this will only affect the initial stages of learning. I have found that I'm better off just setting to be equal with and not bother with bias correction.
Gradient descent with momentum
When we apply momentum to gradient descent, we're aiming to reduce the erratic movements and focus on the consistent direction of progress. The momentum algorithm almost always works faster than standard gradient descent. The simple idea is to calculate the exponentially weighted averages for your gradients and then update your weights with the new values. This averages out the oscillations in the gradients (esp in vertical direction) i.e. In direction we don't want. i.e. from blue to red. By damping these oscialltions there is a more straightforward path to the minimum.
Generally, you will set and to 0, and then on each iteration , once you have computed your gradients and on the current mini-batch, you will compute a moving average for the derivatives as:
And then update the parameters as follows:
Derivatives can be thought as acceleration and velocity, and beta is analogous to friction. Because mini-batch gradient descent makes a parameter update after seeing just a subset of examples, the direction of the update has some variance, and so the path taken by mini-batch gradient descent will "oscillate" toward convergence. Using momentum can reduce these oscillations.
Using momentum can often lead to:
- Faster convergence.
- Smoother descent paths with fewer oscillations.
- Better navigation through tricky areas like saddle points.
It's a valuable tool in the machine learning optimization toolbox, often resulting in better performance than standard gradient descent. However, choosing the right hyperparameter value is critical and typically requires some experimentation. A common default is . In practice as-well, many opt to skip bias correction after setting the initial value of equal to the first gradient.
RMSprop
Stands for Root mean square prop. Developed by Geoffrey Hinton, this algorithm speeds up the gradient descent with momentum by dividing the learning rate by an exponentially decaying average of squared gradients.
Generally, you will set and to 0, and then on each iteration , once you have computed your gradients and on the current mini-batch, you will compute:
For clarity, the superscript 2 denotes element-wise squaring. This keeps an exponentially weighted average of the squares of the derivatives. RMSprop then updates the weights as follows:
's are small so squares are even smaller. 's however are normally larger so squares are larger. Then when we are updating our parameters, we divide by a small value for , so the updates are larger, and is divided by a large term so is updated less. RMSprop will essentially make the cost function move slower on the direction and faster on the direction in the following diagram:
This damps out more in the vertical direction. Therefore, you can use a bigger learning rate without it diverging. We also ensure more numerical stability by ensuring that is not zero by adding a small value epsilon (e.g. ) to it.
Also just to note, the vertical and horizontal and directions are in reality in a very high dimensional space of parameters. But the intuition is the same.
Adam optimization algorithm
Stands for Adaptive Moment Estimation. Adam optimization and RMSprop are among the optimization algorithms that worked very well with a lot of NN architectures; it is proven to be effective on a lot of learning algorithm architectures. Adam optimization simply puts RMSprop and momentum together.
Similarly to before, you will start of by setting you will set , , and to 0. Then, once you have computed your gradients and on the current mini-batch, you will compute the momemtum and RMSprop terms as follows:
In the typical implementation of Adam, we do use bias correction:
Then we update the parameters as follows:
This algorithm combines the effect of gradient descent with momentum with the effects of gradient descent with RMSprop. It is currently recommended to use Adam optimization instead of RMSprop, because it works well in practice.
Hyperparameters for Adam
- : Needs to be tuned
- : parameter of momentum - 0.9 is recommended by default
- : parameter of RMSprop - 0.999 is recommended by default
- : is recommended by default.