Equations · Kingma and Ba, “Adam: A Method for Stochastic Optimization” (2015)
The Adam optimizer update rule, explained
Adam keeps a running average of the gradient and of its square for every parameter, and steps by their ratio. All operations on vectors are elementwise.
Term by term
The gradient of the loss on this step's mini-batch, at the current parameters. is the step number.
A moving average of gradients (the "first moment"). by default: each step keeps 90% of the old average.
A moving average of squared gradients (the "second moment"), with . squares each entry.
Bias correction: and start at 0 and are too small early on; dividing by fixes that. Here really is a power: to the .
The step size (learning rate), by default.
The step direction: average gradient divided by its typical size, per parameter. prevents division by zero.
A small worked example
First step, one parameter, gradient : and . Corrected: , . Step: . The first step has size however large the gradient is.
In code
import numpy as np
def adam_step(theta, g, m, v, t, alpha=1e-3, b1=0.9, b2=0.999, eps=1e-8):
m = b1 * m + (1 - b1) * g
v = b2 * v + (1 - b2) * g**2
m_hat = m / (1 - b1**t)
v_hat = v / (1 - b2**t)
return theta - alpha * m_hat / (np.sqrt(v_hat) + eps), m, vEvery symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)