# Momentum

Authors: Thomas Lee, Greta Gasswint, and Elizabeth Henning (SYSEN5800 Fall 2021)

## Introduction

Momentum is an extension to the gradient descent optimization algorithm that builds inertia in a search direction to overcome local minima and oscillation of noisy gradients. It is based on the same concept of momentum in physics. A classical example of the concept is a ball rolling down a hill that gathers enough momentum to overcome a plateau region and make it to a global minima instead of getting stuck at a local minima. Momentum adds history to the parameter updates of descent problems which significantly accelerates the optimization process. The amount of history included in the update equation is determined via a hyperparameter. This hyperparameter is a value ranging from 0 to 1, where a momentum value of 0 is equivalent to gradient descent without momentum. A higher momentum value means more gradients from the past (history) are considered.

Gradient descent also has several limitations which can be addressed using momentum application, including issues with noise in the oscillations and potential failure to solve due to non-convex functions having multiple local minima. Momentum addresses these limitations using exponentially weighted gradients, avoiding errors caused by iterations being equally weighted.

## Theory, methodology, and/or algorithmic discussions

### Algorithm

The main idea behind momentum is to compute an exponentially weighted average of the gradients and use that to update the weights. By taking past gradients into account, the steps to gradient descent become smoothed out, which can reduce the amount of oscillations seen in iterations (where the iterative solutions are going between above and below the true curve). 

In (stochastic) gradient descent without momentum, the update rule at each iteration is given by:

$\theta _{i}=\theta _{i-1}-\gamma *g_{i}$ Where:

• $\theta$ denotes the parameters to the cost function
• $g_{i}$ is the gradient indicating which direction to decrease the cost function by
• $\gamma$ is the hyperparameter representing the learning rate

In (stochastic) gradient descent with momentum, the update rule at each iteration is given by:

$b_{i}=\mu *b_{i-1}+g_{i}$ $\theta _{i}=\theta _{i-1}-\gamma *b_{i}$ Where:

• $\theta$ denotes the parameters to the cost function
• $g_{i}$ is the gradient indicating which direction to decrease the cost function by
• $\gamma$ is the hyperparameter representing the learning rate
• $b_{i}$ is the modified step direction term (as opposed to just using $g_{i}$ ) that incorporates momentum
• $\mu$ is a new hyperparameter that denotes the momentum constant

Momentum can be applied to other gradient descent variations such as batch gradient descent and mini-batch gradient descent. Regardless of the gradient descent variation, the momentum hyperparameter $\mu$ must be tuned in addition to the learning rate hyperparameter $\gamma$ . $\mu$ can be any value between 0 and 1. Generally, the larger the momentum value, the more past gradients must be taken into account, resulting in a smoother update. However, choosing a $\mu$ value that is too large will result in over smoothing. It is also possible for a large $\mu$ value to overshoot the target. As the $\mu$ value gets closer to 0, momentum behaves similarly to steepest descent. A value of 0 is essentially gradient descent without momentum and a momentum value of 0.9 tends to be a good default choice. Finding the optimal $\mu$ is a matter of trial and error to determine the best choice for a particular model such that the cost function is minimized.

With gradient descent, a weight update at time t is given by the learning rate and gradient at that exact moment. This means that the previous steps are not considered when searching for the next iteration's solution.

This results in two issues:

1. Unlike convex functions, a non-convex cost function can have many local minima's meaning the first local minima found is not guaranteed to be the global minima. At the local minima, the gradient of the cost function will be very small resulting in no weight updates. Because of this, gradient descent will get stuck and fail to find the global optimal solution.
2. Gradient descent can be noisy with many oscillations which results in a larger number of iterations needed to reach convergence.

Momentum is able to solve both of these issues buy using an exponentially weighted average of the gradients to update the weights at each iteration. This method also prevents gradients of previous iterations to be weighted equally. With an exponential weighted average, recent gradients are given more weight than previous gradients.

The graph to the right shows a comparison of gradient descent with and without momentum. Using momentum, the oscillations are significantly reduced and convergence to the minimum is achieved in fewer iterations.

## Numerical Example

To demonstrate the use of momentum in the context of gradient descent, minimize the following function:

$y=0.3*x^{4}-0.1*x^{3}-2*x^{2}-0.8*x$ ### SGD without Momentum Stochastic gradient descent without momentum stops at a local minimum.

Solving this objective function using stochastic gradient descent, starting at $\theta _{0}=-2.8$ and a learning rate of $\gamma =0.05$ , gives a final solution of $x=-1.59$ and $y=-1.46$ . This example uses python to construct a loss function and optimizer with PyTorch. The solver iterates through the following equations:

$g_{i}=\nabla \theta _{i}f_{i}(\theta _{i-1})=1.2*\theta _{i-1}^{3}-0.3*\theta _{i-1}^{2}-4*\theta _{i-1}-0.8$ $\theta _{i}=\theta _{i-1}-\gamma *g_{i}$ Iteration $g_{i}$ $\theta _{i}$ 1 -18.294399999999 -1.885279999999999
2 -2.3661413363687 -1.766972933181563
3 -1.2889636339668 -1.702524751483223
4 -0.78138469777962 -1.663455516594242

The graph to the right depicts how the stochastic gradient descent problem will stop at a local minimum instead of finding the global minimum of the function. The magenta line represents the objective function and the blue line shows the points at each iteration. The following table lists the solution values for $x$ and $y$ for each iteration.

x y
1 -2.800 7.194
2 -1.885 -1.140
3 -1.766 -1.354
4 -1.702 -1.421
5 -1.663 -1.446
... ...
99 -1.586 -1.464

### SGD with Momentum Stochastic gradient descent with momentum stops at the global minimum.

By adding momentum, the solver is able to overcome the hill past the local minimum and settle in the global maximum. The following example uses a momentum value of $\mu =0.7$ and the same parameters as the previous example, with learning rate of $\gamma =0.05$ and starting point of $\theta _{0}=-2.8$ . The solver iterates through the following equations:

$g_{i}=\nabla \theta _{i}f_{i}(\theta _{i-1})=1.2*\theta _{i-1}^{3}-0.3*\theta _{i-1}^{2}-4*\theta _{i-1}-0.8$ $b_{i}=\mu *b_{i-1}+g_{i}$ $\theta _{i}=\theta _{i-1}-\gamma *b_{i}$ The values of $b_{i}$ use momentum and information from previous iterations. For the first iteration, $b_{0}=0$ . Therefore, the first iteration will have the same solution as the example without momentum.

Iteration $g_{i}$ $b_{i}$ $\theta _{i}$ 1 -18.29439999999999 0 -1.88527999999999
2 -2.366141336368740 -15.1722213363687 -1.12666893318156
3 1.609651754221803 -9.01090318123631 -0.67612377411974
4 1.396449498127179 -9.22410543733093 -0.21491850225320

Note that momentum values less than 0.7 are too low and are unable to overcome the hill past the local minimum. The following table lists the values for $x$ and $y$ for each iteration after starting at $\theta _{0}=-2.8$ . Using momentum, this solver gives the correct solution for the global minimum at $x=2.04$ and $y=-5.61$ , as shown in the graph.

x y
1 -2.800 7.194
2 -1.885 -1.140
3 -1.126 -1.011
4 -0.676 -0.279
... ...
99 2.042 -5.608

## Applications

Momentum is widely used in the machine learning community for optimizing non-convex functions such as deep neural networks. Empirically, momentum methods outperform traditional stochastic gradient descent approaches. In deep learning, SGD is widely prevalent and is the underlying basis for many optimizers such as Adam, Adadelta, RMSProp, etc. which already utilize momentum to reduce computation speed. The momentum extension for optimization algorithms is available in many popular machine learning frameworks such as PyTorch, tensor flow, and scikit-learn. Generally, any problem that can be solved with stochastic gradient descent can benefit from the application of momentum. These are often unconstrained optimization problems. Some common SGD applications where momentum may be applied are ridge and logistic regression and support vector machines. Classification problems including those relating to cancer diagnosis and image determination  can also have reduced run times when momentum is implemented. In the case of medical diagnoses, this increased computation speed can directly benefit patients through faster diagnosis times and higher accuracy of diagnosis within the neural network.

Momentum is also very useful in objective functions that have a large amount of curvature. These are areas where the gradients changes a lot. Since momentum leverages an exponential weighted average, smaller steps will be taken in these regions that have higher curvature resulting in dampened oscillations.

Overall, momentum may have farther reaching applicability to accelerating second order methods and deeper connections to other methods, such as the ellipsoid algorithm.

## Conclusion

Momentum improves on gradient descent by reducing oscillatory effects and acting as an accelerator for optimization problem solving. Additionally, it finds the global (and not just local) optimum. Because of these advantages, momentum is commonly used in machine learning and has broad applications to all optimizers through SGD. Though the hyperparameters for momentum must be chosen with care and requires some trial and error, it ultimately addresses common issues seen in gradient descent problems. As deep learning continues to advance, momentum application will allow models and problems to be trained and solved faster than methods without.