PIXELBANKv8.2.1
Menu

Adam Optimizer Step

Implement a single step of the Adam optimizer, widely used in deep learning.

Adam (Adaptive Moment Estimation) combines momentum and RMSprop, adapting learning rates per-parameter:

Algorithm (single step at time tt):

  1. Compute gradient gtg_t at current parameters
  2. Update biased first moment: mt=β1mt1+(1β1)gtm_t = \beta_1 m_{t-1} + (1-\beta_1) g_t
  3. Update biased second moment: vt=β2vt1+(1β2)gt2v_t = \beta_2 v_{t-1} + (1-\beta_2) g_t^2
  4. Bias correction: m^t=mt1β1t\hat{m}_t = \frac{m_t}{1-\beta_1^t}, v^t=vt1β2t\hat{v}_t = \frac{v_t}{1-\beta_2^t}
  5. Update parameters: θt=θt1αm^tv^t+ϵ\theta_t = \theta_{t-1} - \alpha \frac{\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}

Typical hyperparameters: α=0.001\alpha=0.001, β1=0.9\beta_1=0.9, β2=0.999\beta_2=0.999, ϵ=108\epsilon=10^{-8}

Example:

Input:
params = [1.0, 2.0]
grads = [0.1, 0.2]
m = [0.0, 0.0]
v = [0.0, 0.0]
t = 1
lr = 0.001
beta1 = 0.9
beta2 = 0.999
Output:
{'params': [0.999, 1.999], 'm': [0.01, 0.02], 'v': [0.00001, 0.00004]}
Reasoning:

Step 1: Update first moment (m) m1=0.9×0+0.1×[0.1,0.2]=[0.01,0.02]m_1 = 0.9 \times 0 + 0.1 \times [0.1, 0.2] = [0.01, 0.02]

Step 2: Update second moment (v) v1=0.999×0+0.001×[0.01,0.04]=[0.00001,0.00004]v_1 = 0.999 \times 0 + 0.001 \times [0.01, 0.04] = [0.00001, 0.00004]

Step 3: Bias correction m^1=[0.01,0.02]10.91=[0.1,0.2]\hat{m}_1 = \frac{[0.01, 0.02]}{1-0.9^1} = [0.1, 0.2] v^1=[0.00001,0.00004]10.9991=[0.01,0.04]\hat{v}_1 = \frac{[0.00001, 0.00004]}{1-0.999^1} = [0.01, 0.04]

Step 4: Parameter update θ1=[1,2]0.001×[0.1,0.2][0.01,0.04]+ϵ\theta_1 = [1, 2] - 0.001 \times \frac{[0.1, 0.2]}{\sqrt{[0.01, 0.04]} + \epsilon} θ1=[1,2]0.001×[0.1,0.2][0.1,0.2]\theta_1 = [1, 2] - 0.001 \times \frac{[0.1, 0.2]}{[0.1, 0.2]} θ1=[1,2]0.001×[1,1]=[0.999,1.999]\theta_1 = [1, 2] - 0.001 \times [1, 1] = [0.999, 1.999]

Constraints:

  • params: Current parameter values (list)
  • grads: Gradients at current params (list)
  • m, v: First and second moment estimates (lists)
  • t: Current timestep (int >= 1)
  • Return: Dict with new 'params', 'm', 'v'
  • Round to 6 decimal places
Editor

Test Results

0/0
Run code to see test results.