Advanced Optimization: Understanding and Implementing ADAM
Introduction to ADAM
Hello! Today, we will explore the ADAM (Adaptive Moment Estimation) algorithm. This advanced optimization algorithm is a favorite among machine learning practitioners as it combines the advantages of two other extensions of Stochastic Gradient Descent (SGD): Root Mean Square Propagation (RMSprop) and Adaptive Gradient Algorithm (AdaGrad). Our primary focus today is understanding ADAM, and we will also build it from scratch in Python to optimize multivariable functions.
Understanding ADAM
Before we dive into ADAM, let us recall that classic gradient descent methods like SGD and even sophisticated versions like Momentum and RMSProp have some limitations. These limitations relate to sensitivity to learning rates, the issue of vanishing gradients, and the absence of individual adaptive learning rates for different parameters.
ADAM, a promising choice for an optimization algorithm, combines the merits of RMSProp and AdaGrad. It maintains a per-parameter learning rate adapted based on the average of recent magnitudes of the gradients for the weights (similar to RMSProp) and the average of recent gradients (like Momentum). This mechanism enables the algorithm to traverse quickly over the low gradient regions and slow down near the optimal points.
ADAM Mathematically
For ADAM, we modify the update rule of SGD, introducing two additional hyperparameters, beta1 and beta2. The hyperparameter beta1 controls the exponential decay rate for the first-moment estimates (similar to Momentum), while beta2 controls the exponential decay rate for the second-moment estimates (similar to RMSProp). The mathematical expression can be formulated as follows:
Here, m_t and v_t are estimates of the gradients' first moment (the mean) and the second moment (the uncentered variance), respectively, while grad represents the gradient. We also use an epsilon constant to maintain numerical stability and prevent division by zero, as in RMSProp.
ADAM in Python Code
Let's now consolidate the ADAM concept into Python code. We will define an ADAM function, which takes the gradients, the decay rates beta1 and beta2, a numerical constant epsilon, the learning rate, and previous estimates of m and v (initialized to 0) as input and returns the updated parameters, along with the updated m and v.
v and m are initialized with zeros and therefore they are biased towards zero at the start of the optimization, especially when the decay rates are small (beta1 and beta2 close to 1).
To counteract these biases, Adam also usually includes the correction terms m_hat and v_hat. These terms adjust m and v by an amount that lessens as the number of time steps increases:
Note that we still return plain m and v.
