Implement a Percentile Pruner for Early Stopping of Hyperparameter Trials

Quick Overview

Implement a percentile pruner that stops weak hyperparameter trials early by comparing their intermediate scores with a cutoff computed from completed trials at the same step. It tests parameter validation, warmup and interval gating, mirroring the percentile for minimization, and careful handling of NaN values and ties.

Implement a Percentile Pruner for Early Stopping of Hyperparameter Trials

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `PercentilePruner`, which records intermediate scores through `report(trial_id, step, value)` and marks finished trials through `complete(trial_id)`. `should_prune(trial_id, step)` stops a trial whose score at `step` is worse than the percentile cutoff of completed trials at that same step, and `threshold(step)` returns that cutoff. The constructor is `PercentilePruner(percentile, n_startup_trials=5, n_warmup_steps=0, interval_steps=1, direction="maximize")`, and the pruner follows these rules: - The constructor raises `ValueError` unless `percentile` is a number from 0 to 100 inclusive, `n_startup_trials` and `n_warmup_steps` are ints of at least 0, `interval_steps` is an int of at least 1, and `direction` is `"maximize"` or `"minimize"`. - `report` overwrites any earlier value at the same step. Reporting to a completed trial raises `ValueError`. - `threshold(step)` is the linear-interpolated percentile (the `np.percentile` default) of completed trials' non-NaN values at `step`, using `100 - percentile` for minimize. It returns `nan` if there are no values. - `should_prune` returns `False` if the trial has no value at `step`, fewer than `n_startup_trials` trials are completed, `step < n_warmup_steps`, `(step - n_warmup_steps) % interval_steps != 0`, or the threshold is `nan`. - Otherwise a NaN value prunes. Maximize prunes when `value < threshold`, minimize when `value > threshold` (strict, so ties survive). - Running (not completed) trials never contribute to the reference set. Example: ```python # Trials 0..3 report 2.0, 4.0, 6.0, 8.0 at step 2 and then complete. p = PercentilePruner(25.0, n_startup_trials=0) q = PercentilePruner(25.0, n_startup_trials=0, direction="minimize") for pruner in (p, q): for tid, v in enumerate([2.0, 4.0, 6.0, 8.0]): pruner.report(tid, 2, v) pruner.complete(tid) p.threshold(2) # 3.5 p.threshold(7) # nan q.threshold(2) # 6.5 (75th percentile) q.report(10, 2, 7.0); q.should_prune(10, 2) # True q.report(11, 2, 6.5); q.should_prune(11, 2) # False ``` ```hint Minimize direction Run the minimize case of the example by hand. With `percentile=25` and lower scores better, ask which trials the cutoff should single out as the worst quarter, and check that your threshold lands there. ``` ```hint NaN in two places NaN can show up in the completed trials' values and in the running trial's value, and the rules treat the two differently. Decide where each one is handled and in which order the checks run. ``` ### What a Strong Answer Covers - Constructor validation of each parameter's type and range, including edge inputs such as NaN or a boolean passed as a number. - Storage that keeps running and completed trials apart, overwrites on re-report, and refuses reports to completed trials. - A threshold built from completed trials' non-NaN values at the exact step, with the percentile mirrored to `100 - percentile` for minimize. - The gating checks (missing value, startup count, warmup, interval, NaN threshold) in an order that matches the spec, and strict comparisons so ties survive. - The cost of `threshold` and `should_prune` as the number of completed trials grows. ### Follow-up Questions - How does this compare to a median pruner, and when would you pick a low percentile? - What happens if completed trials report at different steps than the running trial? - How would you make `threshold` efficient with thousands of completed trials?

Overview: Implement a percentile pruner that stops weak hyperparameter trials early by comparing their intermediate scores with a cutoff computed from completed trials at the same step. It tests parameter validation, warmup and interval gating, mirroring the percentile for minimization, and careful handling of NaN values and ties.

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

Implement PercentilePruner, which records intermediate scores through report(trial_id, step, value) and marks finished trials through complete(trial_id). should_prune(trial_id, step) stops a trial whose score at step is worse than the percentile cutoff of completed trials at that same step, and threshold(step) returns that cutoff.

The constructor is PercentilePruner(percentile, n_startup_trials=5, n_warmup_steps=0, interval_steps=1, direction="maximize"), and the pruner follows these rules:

  • The constructor raises ValueError unless percentile is a number from 0 to 100 inclusive, n_startup_trials and n_warmup_steps are ints of at least 0, interval_steps is an int of at least 1, and direction is "maximize" or "minimize" .
  • report overwrites any earlier value at the same step. Reporting to a completed trial raises ValueError .
  • threshold(step) is the linear-interpolated percentile (the np.percentile default) of completed trials' non-NaN values at step , using 100 - percentile for minimize. It returns nan if there are no values.
  • should_prune returns False if the trial has no value at step , fewer than n_startup_trials trials are completed, step < n_warmup_steps , (step - n_warmup_steps) % interval_steps != 0 , or the threshold is nan .
  • Otherwise a NaN value prunes. Maximize prunes when value < threshold , minimize when value > threshold (strict, so ties survive).
  • Running (not completed) trials never contribute to the reference set.

Example:

# Trials 0..3 report 2.0, 4.0, 6.0, 8.0 at step 2 and then complete.
p = PercentilePruner(25.0, n_startup_trials=0)
q = PercentilePruner(25.0, n_startup_trials=0, direction="minimize")
for pruner in (p, q):
    for tid, v in enumerate([2.0, 4.0, 6.0, 8.0]):
        pruner.report(tid, 2, v)
        pruner.complete(tid)

p.threshold(2)        # 3.5
p.threshold(7)        # nan

q.threshold(2)        # 6.5 (75th percentile)
q.report(10, 2, 7.0); q.should_prune(10, 2)   # True
q.report(11, 2, 6.5); q.should_prune(11, 2)   # False

What a Strong Answer Covers Guidance

  • Constructor validation of each parameter's type and range, including edge inputs such as NaN or a boolean passed as a number.
  • Storage that keeps running and completed trials apart, overwrites on re-report, and refuses reports to completed trials.
  • A threshold built from completed trials' non-NaN values at the exact step, with the percentile mirrored to 100 - percentile for minimize.
  • The gating checks (missing value, startup count, warmup, interval, NaN threshold) in an order that matches the spec, and strict comparisons so ties survive.
  • The cost of threshold and should_prune as the number of completed trials grows.

Follow-up Questions Guidance

  • How does this compare to a median pruner, and when would you pick a low percentile?
  • What happens if completed trials report at different steps than the running trial?
  • How would you make threshold efficient with thousands of completed trials?