One-Cycle Learning Rate and Momentum Schedule
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `one_cycle(...)` returning `(lr, momentum)` as Python floats. Phase 1 anneals lr from `initial_lr` to `max_lr` and momentum from `max_momentum` to `base_momentum`. Phase 2 anneals lr to `min_lr` and momentum back to `max_momentum`. Also implement `one_cycle_curve(...)` returning two NumPy arrays of shape `(total_steps,)` for steps `0..total_steps-1`.
```python
def one_cycle(step, total_steps, max_lr, pct_start=0.3, div_factor=25.0,
final_div_factor=1e4, base_momentum=0.85, max_momentum=0.95,
anneal="cos"):
... # returns (lr, momentum)
def one_cycle_curve(total_steps, max_lr, pct_start=0.3, div_factor=25.0,
final_div_factor=1e4, base_momentum=0.85,
max_momentum=0.95, anneal="cos"):
... # returns (lrs, momentums)
```
The schedule is defined by these quantities and formulas:
```python
initial_lr = max_lr / div_factor
min_lr = initial_lr / final_div_factor
end1 = pct_start * total_steps - 1.0 # float, not rounded
end2 = total_steps - 1
# phase 1 if step <= end1: pct = step / end1 (pct = 1.0 if end1 == 0)
# phase 2 otherwise: pct = (step - end1) / (end2 - end1)
# cos: end + (start - end) / 2 * (1 + cos(pi * pct))
# linear: start + (end - start) * pct
```
Examples (values rounded):
```python
one_cycle(0, 10, 1.0) # (0.04, 0.95)
one_cycle(1, 10, 1.0) # (0.52, 0.9)
one_cycle(2, 10, 1.0) # (1.0, 0.85) peak
one_cycle(5, 10, 1.0) # (0.61126, 0.88887)
one_cycle(5, 10, 1.0, anneal="linear") # (0.57143, 0.89286)
one_cycle(9, 10, 1.0) # (4e-06, 0.95)
one_cycle(0, 4, 0.5, pct_start=0.25) # (0.5, 0.85)
```
With 10 steps and the default `pct_start`, phase 1 ends at step 2, so steps 0 to 2 warm up and steps 3 to 9 anneal. In the last example `end1` is exactly 0, so step 0 is already the peak.
```hint Start from the boundaries
Compute `end1` and `end2` as floats and decide which phase a step falls in before you compute any interpolation fraction.
```
```hint Edge inputs
Find the input that makes the phase 1 fraction divide by zero, and check that `one_cycle_curve` rejects bad arguments even when it would never call `one_cycle`.
```
### Constraints & Assumptions
- `anneal` is `"cos"` or `"linear"`, otherwise raise `ValueError`.
- Raise `ValueError` if `total_steps` is not an integer ≥ 2, `pct_start` is not in (0, 1), or `step` is outside `[0, total_steps)`.
- `max_lr`, `div_factor`, `final_div_factor` are positive finite floats.
### What a Strong Answer Covers
- Phase boundaries kept as floats and the test that assigns a step to a phase
- Both annealing formulas, with lr and momentum moving in opposite directions in each phase
- The degenerate case where phase 1 ends at step 0
- Validation in both functions, including `one_cycle_curve` called with too few steps
- The cost of one value and of the full curve
### Follow-up Questions
- Why lower momentum while the learning rate rises?
- How would you choose `max_lr` for a model you have not trained before?
- What has to be saved to resume training mid-schedule, and what happens if `total_steps` changes after a restart?
Overview: Implement the one-cycle schedule, which warms the learning rate up to a peak and then anneals it far below its starting value while momentum moves the opposite way, using cosine or linear interpolation. Tests phase-boundary arithmetic, a divide-by-zero edge case, and input validation across two functions.