Adam and AdamW are optimization algorithms used in machine learning to update the parameters of a model during training. Both algorithms are based on the Adam optimizer, which uses adaptive learning rates for each parameter, but they differ in how they handle weight decay (also known as L2 regularization).
Adam with weight decay incorporates weight decay into the parameter updates by adding a weight decay term to the gradient before it is used to update the parameters. Specifically, the weight decay term is added to the first moment estimate in the Adam algorithm. The resulting update rule for a parameter θ_i at time step t is as follows:
θ_i(t)=θ_i(t−1)−η
where η is the learning rate, m^_i(t) is the bias-corrected first moment estimate, is the bias-corrected second raw moment estimate, is a small constant to prevent division by zero, and is the weight decay coefficient.
AdamW, on the other hand, separates the weight decay term from the gradient updates. The weight decay term is applied directly to the parameters before the gradient update step. The resulting update rule for a parameter θ_i at time step t is as follows:
θ_i(t)=(1−
where η is the learning rate, m^_i(t) is the bias-corrected first moment estimate, is the bias-corrected second raw moment estimate, is a small constant to prevent division by zero, and is the weight decay coefficient.
In summary, the main difference between Adam with weight decay and AdamW is that in Adam with weight decay, the weight decay term is added to the gradient updates, while in AdamW, the weight decay term is applied directly to the parameters before the gradient update step. This separation of the weight decay term in AdamW can lead to more stable training and better generalization performance, especially when using large batch sizes.