PigeonColumba compressa

Lesson 4 of 10

The Adam optimizer

Plain gradient descent uses one learning rate for every parameter. Adam keeps two running averages per parameter, the gradient (m, momentum) and the squared gradient (v), and steps by m / sqrt(v): large where the gradient is consistent, small where it is noisy or huge.

For step t = 1, 2, ...:

  1. m = b1 * m + (1 - b1) * g
  2. v = b2 * v + (1 - b2) * g * g
  3. m_hat = m / (1 - b1**t), v_hat = v / (1 - b2**t) (bias correction: both start at 0)
  4. p -= lr * m_hat / (sqrt(v_hat) + eps)

Implement it as a class that updates arrays in place, like lab/nn.py:Adam (which also adds decoupled weight decay).

Your code

Where this is used