Documentation for RandomSearchOptimizer
Random search optimizer.
Searches for the best configuration by randomly testing hyperparameter combinations without any grid.
Source code in photonai/optimization/random_search/random_search.py
class RandomSearchOptimizer(PhotonSlaveOptimizer):
"""Random search optimizer.
Searches for the best configuration by randomly
testing hyperparameter combinations without any grid.
"""
def __init__(self, limit_in_minutes: Union[float, None] = None, n_configurations: Union[int, None] = 10):
"""
Initialize the object.
One of limit_in_minutes or n_configurations must differ from None.
Parameters:
limit_in_minutes:
Total time in minutes.
n_configurations:
Number of configurations to be calculated.
"""
self.pipeline_elements = None
self.parameter_iterable = None
self.ask = self.next_config_generator()
self.n_configurations = None
if not limit_in_minutes or limit_in_minutes <= 0:
self.limit_in_minutes = None
else:
self.limit_in_minutes = limit_in_minutes
self.start_time = None
self.end_time = None
if not n_configurations or n_configurations <= 0:
self.n_configurations = None
else:
self.n_configurations = n_configurations
self.k_configutration = 0 # use k++ until k==n: break
if self.n_configurations is None and self.limit_in_minutes is None:
msg = "No stopping criteria for RandomSearchOptimizer."
logger.error(msg)
raise ValueError(msg)
def prepare(self, pipeline_elements: list, maximize_metric: bool) -> None:
"""
Initializes grid free random hyperparameter search.
Parameters:
pipeline_elements:
List of all PipelineElements to create the hyperparameter space.
maximize_metric:
Boolean to distinguish between score and error.
"""
self.start_time = None
self.pipeline_elements = pipeline_elements
self.ask = self.next_config_generator()
def next_config_generator(self) -> Generator:
"""
Generator for new configs - ask method.
Returns:
Yields the next config.
"""
while True:
self.k_configutration += 1
new_config = True
if self.limit_in_minutes:
if self.start_time is None:
self.start_time = datetime.datetime.now()
self.end_time = self.start_time + datetime.timedelta(minutes=self.limit_in_minutes)
if datetime.datetime.now() >= self.end_time:
new_config = False
if self.n_configurations:
if self.k_configutration >= self.n_configurations + 1:
new_config = False
if not new_config:
return
_ = (yield self._generate_config())
def _generate_config(self):
config = {}
for p_element in self.pipeline_elements:
for h_key, h_value in p_element.hyperparameters.items():
if isinstance(h_value, list):
config[h_key] = random.choice(h_value)
else:
config[h_key] = h_value.get_random_value()
return config
__init__(self, limit_in_minutes=None, n_configurations=10)
special
Initialize the object. One of limit_in_minutes or n_configurations must differ from None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit_in_minutes |
Optional[float] |
Total time in minutes. |
None |
n_configurations |
Optional[int] |
Number of configurations to be calculated. |
10 |
Source code in photonai/optimization/random_search/random_search.py
def __init__(self, limit_in_minutes: Union[float, None] = None, n_configurations: Union[int, None] = 10):
"""
Initialize the object.
One of limit_in_minutes or n_configurations must differ from None.
Parameters:
limit_in_minutes:
Total time in minutes.
n_configurations:
Number of configurations to be calculated.
"""
self.pipeline_elements = None
self.parameter_iterable = None
self.ask = self.next_config_generator()
self.n_configurations = None
if not limit_in_minutes or limit_in_minutes <= 0:
self.limit_in_minutes = None
else:
self.limit_in_minutes = limit_in_minutes
self.start_time = None
self.end_time = None
if not n_configurations or n_configurations <= 0:
self.n_configurations = None
else:
self.n_configurations = n_configurations
self.k_configutration = 0 # use k++ until k==n: break
if self.n_configurations is None and self.limit_in_minutes is None:
msg = "No stopping criteria for RandomSearchOptimizer."
logger.error(msg)
raise ValueError(msg)
next_config_generator(self)
Generator for new configs - ask method.
Returns:
Type | Description |
---|---|
Generator |
Yields the next config. |
Source code in photonai/optimization/random_search/random_search.py
def next_config_generator(self) -> Generator:
"""
Generator for new configs - ask method.
Returns:
Yields the next config.
"""
while True:
self.k_configutration += 1
new_config = True
if self.limit_in_minutes:
if self.start_time is None:
self.start_time = datetime.datetime.now()
self.end_time = self.start_time + datetime.timedelta(minutes=self.limit_in_minutes)
if datetime.datetime.now() >= self.end_time:
new_config = False
if self.n_configurations:
if self.k_configutration >= self.n_configurations + 1:
new_config = False
if not new_config:
return
_ = (yield self._generate_config())
prepare(self, pipeline_elements, maximize_metric)
Initializes grid free random hyperparameter search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline_elements |
list |
List of all PipelineElements to create the hyperparameter space. |
required |
maximize_metric |
bool |
Boolean to distinguish between score and error. |
required |
Source code in photonai/optimization/random_search/random_search.py
def prepare(self, pipeline_elements: list, maximize_metric: bool) -> None:
"""
Initializes grid free random hyperparameter search.
Parameters:
pipeline_elements:
List of all PipelineElements to create the hyperparameter space.
maximize_metric:
Boolean to distinguish between score and error.
"""
self.start_time = None
self.pipeline_elements = pipeline_elements
self.ask = self.next_config_generator()