# Machine Learning Optimisation Using Gradient Descent

In machine learning (ML), gradients refer to vectors that show the direction of maximum increase of a cost function. A widely used optimisation method for complex machine learning and deep learning models is gradient descent. During the optimisation process, the accuracy of parameter adjustments are measured by evaluating the cost function at each step. The model will adjust its parameters until the cost function approaches zero, thereby refining itself further.

There are two main stages involved in the iterative process of the gradient descent method:

1. The gradient can be used to calculate the first-order derivative of the function at a specific point.
2. To move in the opposite direction of the incline, you need to alter your course.

## Learning pace

To locate the minimum value of a given function, the gradient descent process is implemented by multiplying the function’s gradient by a parameter called the learning rate, and then subtracting the resulting value from the current position. The success of this method relies heavily on the appropriate selection of the learning rate parameter; if set too low, the model may not be able to reach the optimal solution in a given number of iterations, whereas if set too high, the model may either fail to converge or even diverge.

As shown in the accompanying illustration, a lower learning rate (left) results in a slower convergence rate for the model, whereas a higher learning rate (right) leads to quicker convergence. This clearly implies that selecting a suitable learning rate is crucial if you wish to achieve the most accurate model possible.

There are three different variations of the gradient descent method, each tailored to specific situations based on the available data volume and preferred balance between speed and accuracy. These implementations are:

Firstly, batch gradient descent: This method calculates gradients for the entire dataset as a single batch, making it slow and impractical for data sets that are too large for random access memory (RAM). Nevertheless, it can deliver outstanding outcomes for smaller data sets because of the reduced instability of the parameter updates.

Secondly, stochastic gradient descent: This method employs random samples from the training data to evaluate the gradient, giving the potential for much faster processing times than batch gradient descent (BGD). However, this also introduces a significant amount of noise and instability to the model’s parameters. To tackle this issue, decreasing the learning rate may help, but this will require additional iterations and training time.

Thirdly, mini-batch gradient descent: This technique combines the strengths of stochastic gradient descent and batch gradient descent into a more streamlined process. Mini-batch gradient descent adjusts the model’s parameters using subsets of the training data. The main advantage of this approach is its ability to combine the resiliency of stochastic gradient descent with the efficiency of batch gradient descent, making it a perfect option in any situation where both speed and accuracy are essential.

## Algorithms for Gradient Descent Optimization

### The Momentum Method

The momentum method is used to help the stochastic gradient descent algorithm make progress in the desired direction, reducing oscillations. This is achieved by including the update vector from the previous iteration. The final outcome is obtained by multiplying the step factor with a second constant, typically around 0.9.

The Adagrad optimizer is highly effective in processing sparse data due to its ability to make small adjustments to the weights based on feature usage. Adagrad assigns multiple learning rates to each parameter update at each time step, prioritizing less frequent features with higher learning rates and more frequent features with lower learning rates. One of the primary benefits of Adagrad is that it negates the need to manually adjust the learning rate. However, it is worth noting that the learning rate can drop to almost zero if there is no new information for the model to process.

### RMSprop

While RMSprop, gradient descent with momentum, and Adagrad exhibit some similarities, the RMSprop optimizer employs a unique method for parameter adjustment. RMSprop reduces vertical oscillations and facilitates larger horizontal steps. The algorithm also employs a decaying moving average to prioritize recent measured gradients. Additionally, RMSprop has an adjustable learning rate that varies during the training process rather than remaining a fixed hyperparameter.

Adam is a cutting-edge optimization algorithm that combines the advantageous features of Adagrad and RMSprop. By calculating the average of the first and second momentums (variance), Adam has proven to be effective in adjusting learning rates. Its consistently superior performance has made it a top choice for both machine learning and deep learning applications, and it is frequently the default algorithm for many skilled practitioners. Adam’s remarkable success has earned it a reputation as a reliable and highly recommended optimization method.

Although there are many benefits associated with the Adam optimizer, it has been found not to converge to a simple optimization procedure. The AMSGrad optimization method was developed to address this issue, but it has been found to offer no improvement over Adam.  