Implement one RMSProp optimizer step with optional momentum in NumPy

Quick Overview

Write a single RMSProp update in NumPy with an optional momentum buffer, returning new parameters and a new state dict without mutating the inputs. It tests the squared-gradient moving average, where epsilon goes, shape checks, and how RMSProp compares with Adam.

Implement one RMSProp optimizer step with optional momentum in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `init_rmsprop_state(params)` and `rmsprop_step(params, grads, state, lr=0.01, rho=0.9, eps=1e-8, momentum=0.0)` in NumPy. The step applies one RMSProp update and returns `(new_params, new_state)` without mutating its inputs. The functions must behave as follows: - State is a dict: `square_avg` and `momentum_buffer` (zeros shaped like `params`), and `step` (an int that starts at 0 and goes up by 1 per call). - Update `s = rho*s + (1-rho)*g²`, then `u = g / (sqrt(s) + eps)`, with eps outside the root. - If `momentum > 0`: `buf = momentum*buf + u` and `p -= lr*buf`. Otherwise `p -= lr*u` and the buffer is passed through unchanged. - `state=None` behaves like a freshly initialized state. - A shape mismatch between `params` and `grads` raises `ValueError`. Example: ```python params = np.array([1.0, -2.0]) grads = np.array([0.5, -1.0]) new_params, new_state = rmsprop_step(params, grads, None, lr=0.1, rho=0.9) # new_state["square_avg"] -> [0.025, 0.1] # new_params -> [1.0 - 0.1*0.5/sqrt(0.025), -2.0 + 0.1*1.0/sqrt(0.1)] # ~= [0.6838, -1.6838] # new_state["step"] -> 1 ``` ```hint Side effects Count every place where your code could write into an array the caller still holds: `params`, and each array inside `state`. ``` ### Constraints & Assumptions - Inputs may be arrays of any shape, including scalars and matrices. All arithmetic is elementwise. - Return float arrays, even when the inputs are lists or integer arrays. ### What a Strong Answer Covers - The squared-gradient moving average and the scaled step, with eps added outside the square root and no bias correction. - Both momentum branches, including the buffer passing through unchanged when `momentum` is 0. - A new parameter array and a new state dict on every call, with `step` incremented and the inputs left untouched. - Handling `state=None` and rejecting mismatched shapes with `ValueError`. ### Follow-up Questions - How does RMSProp differ from Adam? - What does the centered RMSProp variant change? - Why is the first step so large, and what does that look like in the example?

Overview: Write a single RMSProp update in NumPy with an optional momentum buffer, returning new parameters and a new state dict without mutating the inputs. It tests the squared-gradient moving average, where epsilon goes, shape checks, and how RMSProp compares with Adam.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement init_rmsprop_state(params) and rmsprop_step(params, grads, state, lr=0.01, rho=0.9, eps=1e-8, momentum=0.0) in NumPy. The step applies one RMSProp update and returns (new_params, new_state) without mutating its inputs.

The functions must behave as follows:

  • State is a dict: square_avg and momentum_buffer (zeros shaped like params ), and step (an int that starts at 0 and goes up by 1 per call).
  • Update s = rho*s + (1-rho)*g² , then u = g / (sqrt(s) + eps) , with eps outside the root.
  • If momentum > 0 : buf = momentum*buf + u and p -= lr*buf . Otherwise p -= lr*u and the buffer is passed through unchanged.
  • state=None behaves like a freshly initialized state.
  • A shape mismatch between params and grads raises ValueError .

Example:

params = np.array([1.0, -2.0])
grads = np.array([0.5, -1.0])
new_params, new_state = rmsprop_step(params, grads, None, lr=0.1, rho=0.9)
# new_state["square_avg"] -> [0.025, 0.1]
# new_params -> [1.0 - 0.1*0.5/sqrt(0.025), -2.0 + 0.1*1.0/sqrt(0.1)]
#            ~= [0.6838, -1.6838]
# new_state["step"] -> 1

Constraints & Assumptions

  • Inputs may be arrays of any shape, including scalars and matrices. All arithmetic is elementwise.
  • Return float arrays, even when the inputs are lists or integer arrays.

What a Strong Answer Covers Guidance

  • The squared-gradient moving average and the scaled step, with eps added outside the square root and no bias correction.
  • Both momentum branches, including the buffer passing through unchanged when momentum is 0.
  • A new parameter array and a new state dict on every call, with step incremented and the inputs left untouched.
  • Handling state=None and rejecting mismatched shapes with ValueError .

Follow-up Questions Guidance

  • How does RMSProp differ from Adam?
  • What does the centered RMSProp variant change?
  • Why is the first step so large, and what does that look like in the example?