blackboxopt.optimizers.hyperband
Hyperband (StagedIterationOptimizer)
Source code in blackboxopt/optimizers/hyperband.py
class Hyperband(StagedIterationOptimizer):
def __init__(
self,
search_space: ParameterSpace,
objective: Objective,
min_fidelity: float,
max_fidelity: float,
num_iterations: int,
eta: float = 3.0,
seed: int = None,
logger: logging.Logger = None,
):
"""Implementation of Hyperband as proposed in
Li, L., Jamieson, K., DeSalvo, G., Rostamizadeh, A., & Talwalkar, A. (2016).
Hyperband: A novel bandit-based approach to hyperparameter optimization.
arXiv preprint arXiv:1603.06560.
Args:
search_space: [description]
objective: [description]
min_fidelity: The smallest fidelity value that is still meaningful.
Must be strictly greater than zero!
max_fidelity: The largest fidelity value used during the optimization.
Must not be smaller than `min_fidelity`
num_iterations: [description]
eta: Scaling parameter to control the aggressiveness of Hyperband's racing.
seed: [description]
logger: [description]
"""
self.config_sampler = RandomSearchSampler(search_space)
self.min_fidelity = min_fidelity
self.max_fidelity = max_fidelity
self.eta = eta
super().__init__(
search_space=search_space,
objective=objective,
num_iterations=num_iterations,
seed=seed,
logger=logger,
)
def _create_new_iteration(self, iteration_index: int) -> StagedIteration:
"""Optimizer specific way to create a new
`blackboxopt.optimizer.staged.iteration.StagedIteration` object
"""
return create_hyperband_iteration(
iteration_index,
self.min_fidelity,
self.max_fidelity,
self.eta,
self.config_sampler,
self.objective,
self.logger,
)
generate_evaluation_specification(self)
inherited
Get next configuration and settings to evaluate.
Exceptions:
Type | Description |
---|---|
OptimizationComplete |
When the optimization run is finished, e.g. when the budget has been exhausted. |
OptimizerNotReady |
When the optimizer is not ready to propose a new evaluation specification. |
Source code in blackboxopt/optimizers/hyperband.py
def generate_evaluation_specification(self) -> EvaluationSpecification:
"""Get next configuration and settings to evaluate.
Raises:
OptimizationComplete: When the optimization run is finished, e.g. when the
budget has been exhausted.
OptimizerNotReady: When the optimizer is not ready to propose a new
evaluation specification.
"""
# check if any of the already active iterations returns a configuration and
# simply return that
for idx, iteration in enumerate(self.iterations):
es = iteration.generate_evaluation_specification()
if es is not None:
self.evaluation_uuid_to_iteration[str(es.optimizer_info["id"])] = idx
self.pending_configurations[str(es.optimizer_info["id"])] = es
return es
# if that didn't work, check if there another iteration can be started and then
# ask it for a configuration
if len(self.iterations) < self.num_iterations:
self.iterations.append(self._create_new_iteration(len(self.iterations)))
es = self.iterations[-1].generate_evaluation_specification()
self.evaluation_uuid_to_iteration[str(es.optimizer_info["id"])] = (
len(self.iterations) - 1
)
self.pending_configurations[str(es.optimizer_info["id"])] = es
return es
# check if the optimization is already complete or whether the optimizer is
# waiting for evaluation results -> raise corresponding error
if all([iteration.finished for iteration in self.iterations]):
raise OptimizationComplete
raise OptimizerNotReady
report(self, evaluations)
inherited
Report one or multiple evaluations to the optimizer.
All valid evaluations are processed. Faulty evaluations are not processed,
instead an EvaluationsError
is raised, which includes the problematic
evaluations with their respective Exceptions in the evaluations_with_errors
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evaluations |
Union[blackboxopt.evaluation.Evaluation, Iterable[blackboxopt.evaluation.Evaluation]] |
A single evaluated evaluation specifications, or an iterable of many. |
required |
Exceptions:
Type | Description |
---|---|
EvaluationsError |
Raised when an evaluation could not be processed. |
Source code in blackboxopt/optimizers/hyperband.py
def report(self, evaluations: Union[Evaluation, Iterable[Evaluation]]) -> None:
_evals = [evaluations] if isinstance(evaluations, Evaluation) else evaluations
call_functions_with_evaluations_and_collect_errors(
[super().report, _validate_optimizer_info_id, self._report],
_evals,
)