Learning-Rate Range Test With Smoothed Loss

Quick Overview

Implement a learning-rate range test that runs plain gradient descent over a geometric sweep of learning rates, smooths the loss with a bias-corrected moving average, and stops on divergence. It tests careful step ordering, numerical edge cases, and how to pick a training learning rate from the resulting curve.

Learning-Rate Range Test With Smoothed Loss

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `lr_range_test(params, loss_fn, grad_fn, lr_min, lr_max, num_steps, beta=0.98, diverge_factor=4.0)`. Run plain gradient descent on a copy of `params` with geometric learning rates `lr_i = lr_min * (lr_max/lr_min) ** (i/(num_steps-1))` for `i = 0, ..., num_steps-1`. Return `(lrs, losses, smoothed, suggested_lr)`: three `float64` 1-D arrays of recorded steps and a float. Each step `i` follows these rules, in order: 1. `loss = loss_fn(theta)`. If it is non-finite, stop without recording this step. 2. `avg = beta*avg + (1-beta)*loss` (`avg` starts at 0), `sm = avg / (1 - beta**(i+1))`. Record `lr`, `loss` and `sm`. 3. If `sm > diverge_factor * best` (strict), stop. The step stays recorded. 4. `best = min(best, sm)` (initially `inf`), then `theta -= lr * grad_fn(theta)`. `suggested_lr` is `lrs[argmin(smoothed)] / 10` (first minimum), or `nan` if nothing was recorded. Example, with `loss = 0.5*sum(theta**2)` and `grad = theta`: ```python lr_range_test([2.0], loss, grad, 0.1, 10.0, 3, beta=0.5) # lrs = [0.1, 1.0, 10.0] # losses = [2.0, 1.62, 0.0] # smoothed = [2.0, 1.31/0.75, 0.655/0.875] # about [2.0, 1.7467, 0.7486] # suggested_lr = 1.0 ``` ```hint Order of the checks The two stopping conditions differ in whether the current step ends up in the output. Trace the example step by step to confirm you record exactly the right entries. ``` ```hint The first step Work out what `diverge_factor * best` evaluates to before `best` has been updated, and what the bias-corrected value equals at `i = 0`. ``` ### Constraints & Assumptions - Raise `ValueError` if `num_steps < 2`, `lr_min <= 0`, `lr_max <= lr_min`, or `beta` is not in `[0, 1)`. - The input `params` must not be mutated. - `loss_fn` returns a scalar. `grad_fn` returns an array shaped like `params`. - The three returned arrays have dtype `float64`. ### What a Strong Answer Covers - A geometric schedule that follows the formula, starting at `lr_min` and ending at `lr_max` - A bias-corrected moving average of the loss, with recording, the divergence check, the `best` update and the gradient step in the stated order - Both stopping conditions: the non-finite step left out, the diverging step kept - Input validation and working on a copy so `params` is unchanged - The empty-run case and the first-minimum rule for `suggested_lr` ### Follow-up Questions - Why bias-correct the exponential moving average, and what goes wrong in the divergence check without it? - Why divide the learning rate at the smoothed-loss minimum by 10 before suggesting it? - How would you adapt this test to a real mini-batch training loop with an optimizer that keeps state? - The divergence rule compares against `diverge_factor * best`. What happens if the loss can be negative, and how would you change the rule?

Overview: Implement a learning-rate range test that runs plain gradient descent over a geometric sweep of learning rates, smooths the loss with a bias-corrected moving average, and stops on divergence. It tests careful step ordering, numerical edge cases, and how to pick a training learning rate from the resulting curve.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement lr_range_test(params, loss_fn, grad_fn, lr_min, lr_max, num_steps, beta=0.98, diverge_factor=4.0). Run plain gradient descent on a copy of params with geometric learning rates lr_i = lr_min * (lr_max/lr_min) ** (i/(num_steps-1)) for i = 0, ..., num_steps-1. Return (lrs, losses, smoothed, suggested_lr): three float64 1-D arrays of recorded steps and a float.

Each step i follows these rules, in order:

  1. loss = loss_fn(theta) . If it is non-finite, stop without recording this step.
  2. avg = beta*avg + (1-beta)*loss ( avg starts at 0), sm = avg / (1 - beta**(i+1)) . Record lr , loss and sm .
  3. If sm > diverge_factor * best (strict), stop. The step stays recorded.
  4. best = min(best, sm) (initially inf ), then theta -= lr * grad_fn(theta) .

suggested_lr is lrs[argmin(smoothed)] / 10 (first minimum), or nan if nothing was recorded.

Example, with loss = 0.5*sum(theta**2) and grad = theta:

lr_range_test([2.0], loss, grad, 0.1, 10.0, 3, beta=0.5)
# lrs          = [0.1, 1.0, 10.0]
# losses       = [2.0, 1.62, 0.0]
# smoothed     = [2.0, 1.31/0.75, 0.655/0.875]   # about [2.0, 1.7467, 0.7486]
# suggested_lr = 1.0

Constraints & Assumptions

  • Raise ValueError if num_steps < 2 , lr_min <= 0 , lr_max <= lr_min , or beta is not in [0, 1) .
  • The input params must not be mutated.
  • loss_fn returns a scalar. grad_fn returns an array shaped like params .
  • The three returned arrays have dtype float64 .

What a Strong Answer Covers Guidance

  • A geometric schedule that follows the formula, starting at lr_min and ending at lr_max
  • A bias-corrected moving average of the loss, with recording, the divergence check, the best update and the gradient step in the stated order
  • Both stopping conditions: the non-finite step left out, the diverging step kept
  • Input validation and working on a copy so params is unchanged
  • The empty-run case and the first-minimum rule for suggested_lr

Follow-up Questions Guidance

  • Why bias-correct the exponential moving average, and what goes wrong in the divergence check without it?
  • Why divide the learning rate at the smoothed-loss minimum by 10 before suggesting it?
  • How would you adapt this test to a real mini-batch training loop with an optimizer that keeps state?
  • The divergence rule compares against diverge_factor * best . What happens if the loss can be negative, and how would you change the rule?