Source code for olympus.hpo.random_search

from sspace import Space

from olympus.hpo.optimizer import HyperParameterOptimizer, WaitingForTrials, OptimizationIsDone
from olympus.hpo.fidelity import Fidelity
from olympus.utils import new_seed, compress_dict, decompress_dict


[docs]class RandomSearch(HyperParameterOptimizer): """Randomly samples sets of hyper parameter and return the best one Parameters ---------- count: int Number of configuration to sample Notes ----- .. image:: ../../docs/_static/hpo/rs_space.png """ def __init__(self, fidelity: Fidelity, count: int, space: Space, seed=0, pool_size=None, **kwargs): super(RandomSearch, self).__init__(fidelity, space, seed, **kwargs) self.count = count if pool_size is None: pool_size = self.count self.pool_size = pool_size
[docs] def suggest(self, **variables): if len(self.trials) < self.count: return self.sample(self.pool_size, **variables) if self.is_done(): raise OptimizationIsDone() raise WaitingForTrials()
[docs] def new_trials(self, trials): for trial in trials: trial.params[self.fidelity.name] = self.fidelity.max
[docs] def count_done(self): count = 0 for _, trial in self.trials.items(): if len(trial.objectives) > 0: count += 1 return count
[docs] def is_done(self): return self.count_done() == self.count
[docs] def info(self): return { 'unique_samples': self.count, 'total_epochs': self.fidelity.max * self.count, 'parallelism': self.count }
[docs] def load_state_dict(self, state): state = decompress_dict(state) super(RandomSearch, self).load_state_dict(state) self.count = state['count']
[docs] def state_dict(self, compressed=True): state = super(RandomSearch, self).state_dict(compressed=False) state['count'] = self.count if compressed: state = compress_dict(state) return state
[docs] def remaining(self): return self.count - self.count_done()
builders = { 'random_search': RandomSearch, }