Hyperband Bracket Loop With Successive Halving
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `hyperband_brackets(max_resource, eta=3)`, returning a list of `(s, n, r)` tuples, and `hyperband(sample_config, evaluate, max_resource, eta=3, seed=0)`, which runs successive halving in every bracket and returns the global best. Let `s_max` be the largest integer with `eta**s_max <= max_resource`.
Requirements:
- Brackets run `s = s_max..0` with `n = ceil((s_max+1)*eta**s/(s+1))` (an int) and `r = max_resource/eta**s` (a float).
- Create one `rng = np.random.default_rng(seed)`. Each bracket draws `n` configs via `sample_config(rng)`. Ids are global, 0-based, in draw order.
- Rung `i = 0..s` evaluates the survivors in ascending id at `r_i = max_resource/eta**(s-i)`, appending `(s, i, id, r_i, float(loss))` to `history`. If `i < s`, keep the `len//eta` best by `(loss, id)`.
- The global best is the lowest loss across all evaluations. On ties, the earliest evaluation wins.
- Return `{"best_id", "best_config", "best_loss", "history", "configs"}`. A non-integer `eta`, `eta < 2`, or `max_resource < 1` raises `ValueError`.
Example:
```python
hyperband_brackets(81, 3)
# [(4, 81, 1.0), (3, 34, 3.0), (2, 15, 9.0), (1, 8, 27.0), (0, 5, 81.0)]
hyperband_brackets(10, 3)
# [(2, 9, 10/9), (1, 5, 10/3), (0, 3, 10.0)]
```
```hint Exact bracket math
Compute `s_max` and the ceiling in `n` with integer arithmetic. A floating-point logarithm can come out just below a whole number when `max_resource` is an exact power of `eta`.
```
```hint Where determinism breaks
List every place an ordering decision is made: the order survivors are evaluated in, the sort key for promotion, and the comparison that replaces the current best.
```
### Constraints & Assumptions
- `evaluate(config, r)` returns a finite loss, and lower is better.
- `configs` lists every sampled config, indexed by id.
- `sample_config` and `evaluate` are supplied by the caller; only `numpy` is needed for the generator.
### What a Strong Answer Covers
- An exact `s_max` and an `(s, n, r)` schedule that reproduces both examples.
- One shared generator across all brackets, with global ids assigned in draw order.
- Rung budgets, history records in the required shape, and promotion of `len//eta` survivors by `(loss, id)`, evaluated in ascending id.
- The tie rule for the global best and the input validation.
- Edge cases such as a run with a single bracket and why a rung never ends up empty.
### Follow-up Questions
- How would you parallelize Hyperband across many workers?
- How can evaluations reuse work from lower rungs?
- How does BOHB improve on random sampling here?
Overview: Implement the Hyperband bracket schedule and run successive halving in every bracket with a shared random generator, global config ids and deterministic tie-breaking. It tests exact bracket arithmetic, rung budgets, promotion rules, and how Hyperband trades many cheap trials against a few full-budget ones.