Build a training loss monitor that detects divergence, oscillation and plateaus and recommends a learning rate

Quick Overview

Implement a function that reads a training loss history, labels it as diverged, oscillating, plateaued, healthy or too short, and returns a recommended learning rate. It tests NumPy windowing, NaN and infinity handling, and applying status rules in a fixed priority order.

Build a training loss monitor that detects divergence, oscillation and plateaus and recommends a learning rate

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `monitor_training(losses, lr, window=10, divergence_factor=2.0, oscillation_threshold=0.5, plateau_tol=1e-3)`. Diagnose the loss history and return a dict with the keys `status`, `lr` (the recommended learning rate), `oscillation_score` and `relative_improvement`. Every value except `status` is a Python `float`. The function follows these rules: - Scores use `L`, the last `window + 1` losses, and `d = np.diff(L)`. If `len(losses) < window + 1` or any loss is non-finite, both scores are `NaN`. - `oscillation_score` is the count of indices `i` with `d[i] * d[i+1] < 0`, divided by `window - 1`. - `relative_improvement` is `(L[0] - L[-1]) / max(abs(L[0]), 1e-12)`. - The status is the first match in this order: `"diverged"` (any non-finite loss, or `losses[-1] > divergence_factor * min(losses)`), `"insufficient_data"` (too short), `"oscillating"` (`oscillation_score >= oscillation_threshold`), `"plateau"` (`relative_improvement < plateau_tol`), else `"healthy"`. - The returned `lr` is the input `lr` times 0.1 for diverged and plateau, times 0.5 for oscillating, and times 1 for healthy and insufficient_data. Example: ```python monitor_training([5.0, 4.0, 3.0, 3.5, 3.2], lr=1.0, window=4) # d = [-1, -1, 0.5, -0.3] -> 2 sign changes / 3 # returns {'status': 'oscillating', 'lr': 0.5, 'oscillation_score': 0.6666666666666666, 'relative_improvement': 0.36} ``` ```hint Order of the checks The divergence test reads the whole history, while the two scores read only the tail. Work out which test has to run first so that a short run that has already blown up is still flagged. ``` ```hint NaN comparisons Any comparison with `NaN` is `False` in Python. Make sure no status branch can be reached with a `NaN` score unless that outcome is the one the rules ask for. ``` ### Constraints & Assumptions - `losses` is a non-empty 1-D sequence (a list or a NumPy array). - `window >= 2`, so `window - 1` is never zero. ### What a Strong Answer Covers - Computing both scores only on the last `window + 1` losses, and returning `NaN` for both when the history is too short or holds a non-finite value. - Applying the status rules in the stated priority, with divergence judged on the full history. - Mapping each status to its learning-rate factor and returning plain Python floats. - Edge cases: zero differences, a tail that starts at a loss of 0, exactly `window + 1` losses, and `inf` as well as `NaN`. - Time and space cost in terms of the history length and the window. ### Follow-up Questions - How would you keep a single noisy spike from triggering a divergence call? - How would you turn this into a stateful callback used during training? - Why might a plateau call for a higher learning rate, even though this monitor cuts it?

Overview: Implement a function that reads a training loss history, labels it as diverged, oscillating, plateaued, healthy or too short, and returns a recommended learning rate. It tests NumPy windowing, NaN and infinity handling, and applying status rules in a fixed priority order.

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

Implement monitor_training(losses, lr, window=10, divergence_factor=2.0, oscillation_threshold=0.5, plateau_tol=1e-3). Diagnose the loss history and return a dict with the keys status, lr (the recommended learning rate), oscillation_score and relative_improvement. Every value except status is a Python float.

The function follows these rules:

  • Scores use L , the last window + 1 losses, and d = np.diff(L) . If len(losses) < window + 1 or any loss is non-finite, both scores are NaN .
  • oscillation_score is the count of indices i with d[i] * d[i+1] < 0 , divided by window - 1 .
  • relative_improvement is (L[0] - L[-1]) / max(abs(L[0]), 1e-12) .
  • The status is the first match in this order: "diverged" (any non-finite loss, or losses[-1] > divergence_factor * min(losses) ), "insufficient_data" (too short), "oscillating" ( oscillation_score >= oscillation_threshold ), "plateau" ( relative_improvement < plateau_tol ), else "healthy" .
  • The returned lr is the input lr times 0.1 for diverged and plateau, times 0.5 for oscillating, and times 1 for healthy and insufficient_data.

Example:

monitor_training([5.0, 4.0, 3.0, 3.5, 3.2], lr=1.0, window=4)
# d = [-1, -1, 0.5, -0.3] -> 2 sign changes / 3
# returns
{'status': 'oscillating', 'lr': 0.5,
 'oscillation_score': 0.6666666666666666, 'relative_improvement': 0.36}

Constraints & Assumptions

  • losses is a non-empty 1-D sequence (a list or a NumPy array).
  • window >= 2 , so window - 1 is never zero.

What a Strong Answer Covers Guidance

  • Computing both scores only on the last window + 1 losses, and returning NaN for both when the history is too short or holds a non-finite value.
  • Applying the status rules in the stated priority, with divergence judged on the full history.
  • Mapping each status to its learning-rate factor and returning plain Python floats.
  • Edge cases: zero differences, a tail that starts at a loss of 0, exactly window + 1 losses, and inf as well as NaN .
  • Time and space cost in terms of the history length and the window.

Follow-up Questions Guidance

  • How would you keep a single noisy spike from triggering a divergence call?
  • How would you turn this into a stateful callback used during training?
  • Why might a plateau call for a higher learning rate, even though this monitor cuts it?