BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Optimizers16 of 18 in topic

255. Minimise a quadratic with AdamW, bias correction and a no-decay mask

HardOptimizers Pro
Asked atMeta

Real training configs do not decay every parameter: biases and norm gains are usually excluded. This problem runs AdamW on a known quadratic so every number can be checked, with a mask that marks which parameters get weight decay.

Write adamw_quadratic(A, b, x0, lr, steps, wd, decay_mask, b1=0.9, b2=0.999, eps=1e-8).
- f(x) = 0.5 * x @ A @ x - b @ x with A symmetric (d, d), b and x0 of length d, decay_mask a length-d list of 0/1.
- m and v start at zero. For step t = 1..steps:
g = A @ x - b
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)
x = x - lr * m_hat / (sqrt(v_hat) + eps) - lr * wd * decay_mask * x_old, where x_old is x before this step.
- Weight decay must NOT be added to g (that would be L2 regularisation, not AdamW).
Return (x_final as an array, losses) where losses is the list of f(x) after each step (length steps). steps = 0 returns (x0, []).

Functiondef adamw_quadratic(A, b, x0, lr, steps, wd, decay_mask, b1=0.9, b2=0.999, eps=1e-8)

Example 1

on the first step m_hat = g and v_hat = g ** 2, so each coordinate with nonzero gradient moves by almost exactly lr against the sign of its gradient.

Example 2

Input: A = [[3.0, 0.5, 0.0], [0.5, 1.0, 0.2], [0.0, 0.2, 0.5]], b = [1.0, -2.0, 0.5], x0 = [2.0, 2.0, -3.0], lr = 0.1, steps = 0, wd = 0.1, decay_mask = [1, 1, 0]

Output: ([2.0, 2.0, -3.0], [])

Example 3

Input: A = [[3.0, 0.5, 0.0], [0.5, 1.0, 0.2], [0.0, 0.2, 0.5]], b = [1.0, -2.0, 0.5], x0 = [2.0, 2.0, -3.0], lr = 0.1, steps = 1, wd = 0.0, decay_mask = [1, 1, 1]

Output: ([1.9, 1.9, -2.9], [13.3755])

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Use a 1-based t for the bias correction.

Hint 2

Compute the Adam part and the decay part from the same x_old, then evaluate f at the new x and append it.

Common mistakes
  • The buggy version adds wd * x into the gradient, which is L2 regularisation: the decay then gets divided by sqrt(v_hat) and its strength changes per parameter.
Understand the idea

Adam scales each step by running averages of the gradient (m) and squared gradient (v). Both start at zero, so early averages are too small; bias correction divides by 1 - beta^t to fix that. AdamW then shrinks weights directly by lr * wd * x, outside the adaptive scaling, and a mask lets biases and norm gains skip decay. Worked example in 1-D: f(x) = 0.5 * x^2, so g = x. x0 = 2, lr = 0.1, wd = 0.1, mask 1. Step 1: g = 2, m = 0.2, v = 0.004, m_hat = 0.2 / 0.1 = 2, v_hat = 0.004 / 0.001 = 4, so the Adam step is 0.1 * 2 / 2 = 0.1. Decay is 0.1 * 0.1 * 2 = 0.02. x = 2 - 0.1 - 0.02 = 1.88, loss = 0.5 * 1.88^2 = 1.7672. With mask 0, x = 1.9.

g = A x - b, update m and v, correct both, x = x - lr * m_hat / (sqrt(v_hat) + eps) - lr * wd * mask * x_old

Adding wd * x into g is L2, not AdamW. It is the default for transformers.

Topics
Optimizers
Companies1
  • Meta the topic appears in interview reports Machine Learning interview questions, page 10 (KatKat)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • AdamW with linear warmup and cosine decayHard
  • Compute Adafactor's factored second momentHard
  • Implement one step of the Adam optimizerMedium
  • Fine-tune on a memory budget: implement the Lion optimizer stepMedium
All Optimizers problems
Report
255 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def adamw_quadratic(A, b, x0, lr, steps, wd, decay_mask, b1=0.9, b2=0.999, eps=1e-8):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS