[docs]classRandomSearchTuner(Tuner):"""Random Search Optimizer. Args: pipeline_hyperparameter_ranges (dict): a set of hyperparameter ranges corresponding to a pipeline's parameters with_replacement (bool): If false, only unique hyperparameters will be shown replacement_max_attempts (int): The maximum number of tries to get a unique set of random parameters. Only used if tuner is initalized with with_replacement=True random_seed (int): Seed for random number generator. Defaults to 0. Example: >>> tuner = RandomSearchTuner({'My Component': {'param a': [0.0, 10.0], 'param b': ['a', 'b', 'c']}}, random_seed=42) >>> proposal = tuner.propose() ... >>> assert proposal.keys() == {'My Component'} >>> assert proposal['My Component'] == {'param a': 3.7454011884736254, 'param b': 'c'} Determines points using a random search approach. >>> for each in range(7): ... print(tuner.propose()) {'My Component': {'param a': 7.3199394181140525, 'param b': 'b'}} {'My Component': {'param a': 1.5601864044243654, 'param b': 'a'}} {'My Component': {'param a': 0.5808361216819947, 'param b': 'c'}} {'My Component': {'param a': 6.011150117432089, 'param b': 'c'}} {'My Component': {'param a': 0.2058449429580245, 'param b': 'c'}} {'My Component': {'param a': 8.32442640800422, 'param b': 'a'}} {'My Component': {'param a': 1.8182496720710064, 'param b': 'a'}} """def__init__(self,pipeline_hyperparameter_ranges,with_replacement=False,replacement_max_attempts=10,random_seed=0,):super().__init__(pipeline_hyperparameter_ranges,random_seed=random_seed)self._space=Space(self._search_space_ranges)self._random_state=get_random_state(random_seed)self._with_replacement=with_replacementself._replacement_max_attempts=replacement_max_attemptsself._used_parameters=set()self._used_parameters.add(())self.curr_params=None
[docs]defadd(self,pipeline_parameters,score):"""Not applicable to random search tuner as generated parameters are not dependent on scores of previous parameters. Args: pipeline_parameters (dict): A dict of the parameters used to evaluate a pipeline score (float): The score obtained by evaluating the pipeline with the provided parameters """pass
[docs]defpropose(self):"""Generate a unique set of parameters. If tuner was initialized with ``with_replacement=True`` and the tuner is unable to generate a unique set of parameters after ``replacement_max_attempts`` tries, then ``NoParamsException`` is raised. Returns: dict: Proposed pipeline parameters """ifnotlen(self._search_space_ranges):returnself._convert_to_pipeline_parameters({})ifself._with_replacement:returnself._convert_to_pipeline_parameters(self._get_sample())elifnotself.curr_params:self.is_search_space_exhausted()params=self.curr_paramsself.curr_params=Nonereturnself._convert_to_pipeline_parameters(params)
[docs]defis_search_space_exhausted(self):"""Checks if it is possible to generate a set of valid parameters. Stores generated parameters in ``self.curr_params`` to be returned by ``propose()``. Returns: bool: If no more valid parameters exists in the search space, return False. Raises: NoParamsException: If a search space is exhausted, then this exception is thrown. """ifself._with_replacement:returnFalseelse:curr_params=()attempts=0whilecurr_paramsinself._used_parameters:ifattempts>=self._replacement_max_attempts:raiseNoParamsException("Cannot create a unique set of unexplored parameters. Try expanding the search space.",)returnTrueattempts+=1curr_params=self._get_sample()self._used_parameters.add(curr_params)self.curr_params=curr_paramsreturnFalse