Adam Optimizer With Bias Correction

Quick Overview

Implement one step of the Adam optimizer in NumPy, with lazily created first and second moment buffers, a step counter and bias correction. It tests the exact order of the update, numerical details such as where epsilon goes, and how the optimizer behaves in its first few steps.

Adam Optimizer With Bias Correction

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `Adam.step(params, grads)`, which applies one Adam update and returns the new parameters as a NumPy array. The optimizer is constructed as `Adam(lr=0.001, beta1=0.9, beta2=0.999, eps=1e-8)` and keeps per-element first and second moment estimates (`self.m`, `self.v`) and a step counter `self.t` across calls. The update rule for one call: ```python t += 1 m = beta1 * m + (1 - beta1) * g v = beta2 * v + (1 - beta2) * g**2 m_hat = m / (1 - beta1**t) v_hat = v / (1 - beta2**t) params_new = params - lr * m_hat / (sqrt(v_hat) + eps) ``` Example: ```python opt = Adam(lr=0.1) opt.step(np.array([1.0, -2.0]), np.array([0.5, -4.0])) # -> array([0.9, -1.9]) (approximately: the first step moves each param by lr * sign(g)) # opt.t == 1 ``` ```hint Check the first step by hand Work out `m_hat` and `v_hat` at `t = 1` starting from zero moments. The ratio between them explains why the example moves both parameters by the same amount even though one gradient is eight times larger. ``` ```hint Shapes and copies The moment buffers have to match any input shape, including 2-D, and the constructor does not know that shape. Also check what `np.asarray` returns for an input that is already a float array before you write anything in place. ``` ### Constraints & Assumptions - `params` and `grads` have the same shape (any shape, including 2-D). The output has that shape. - Lazily initialise `m` and `v` to zeros on the first call. `t` increments before bias correction. - Do not mutate the input `params` array. Return a new array. - `eps` is added after the square root of `v_hat`. - On the first call, a zero gradient leaves that parameter unchanged. On later calls a zero gradient can still move it, because `m` carries momentum from earlier steps. ### What a Strong Answer Covers - Lazy, shape-matched initialization of both moment buffers and the step counter. - The order inside a step: when `t` changes, how both moments update, the bias correction, and where eps goes. - Why the first step has size close to `lr` for every element, whatever the gradient's magnitude. - Returning a new array without modifying the caller's `params`. - Per-step time and memory cost. ### Follow-up Questions - Why is bias correction needed, and what happens early in training without it? - How would you extend this to AdamW with decoupled weight decay? - How would you support multiple parameter tensors (for example a dict of layers)?

Overview: Implement one step of the Adam optimizer in NumPy, with lazily created first and second moment buffers, a step counter and bias correction. It tests the exact order of the update, numerical details such as where epsilon goes, and how the optimizer behaves in its first few steps.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement Adam.step(params, grads), which applies one Adam update and returns the new parameters as a NumPy array. The optimizer is constructed as Adam(lr=0.001, beta1=0.9, beta2=0.999, eps=1e-8) and keeps per-element first and second moment estimates (self.m, self.v) and a step counter self.t across calls.

The update rule for one call:

t += 1
m = beta1 * m + (1 - beta1) * g
v = beta2 * v + (1 - beta2) * g**2
m_hat = m / (1 - beta1**t)
v_hat = v / (1 - beta2**t)
params_new = params - lr * m_hat / (sqrt(v_hat) + eps)

Example:

opt = Adam(lr=0.1)
opt.step(np.array([1.0, -2.0]), np.array([0.5, -4.0]))
# -> array([0.9, -1.9])  (approximately: the first step moves each param by lr * sign(g))
# opt.t == 1

Constraints & Assumptions

  • params and grads have the same shape (any shape, including 2-D). The output has that shape.
  • Lazily initialise m and v to zeros on the first call. t increments before bias correction.
  • Do not mutate the input params array. Return a new array.
  • eps is added after the square root of v_hat .
  • On the first call, a zero gradient leaves that parameter unchanged. On later calls a zero gradient can still move it, because m carries momentum from earlier steps.

What a Strong Answer Covers Guidance

  • Lazy, shape-matched initialization of both moment buffers and the step counter.
  • The order inside a step: when t changes, how both moments update, the bias correction, and where eps goes.
  • Why the first step has size close to lr for every element, whatever the gradient's magnitude.
  • Returning a new array without modifying the caller's params .
  • Per-step time and memory cost.

Follow-up Questions Guidance

  • Why is bias correction needed, and what happens early in training without it?
  • How would you extend this to AdamW with decoupled weight decay?
  • How would you support multiple parameter tensors (for example a dict of layers)?