Skip to content

Documentation for RandomGridSearchOptimizer

Random grid search optimizer.

Searches for the best configuration by randomly testing n points of a grid of possible hyperparameters.

Examples:

1
2
3
4
5
6
7
my_pipe = Hyperpipe(name='rgrid_based_pipe',
                    optimizer='random_grid_search',
                    optimizer_params={'n_configurations': 50,
                                      'limit_in_minutes': 10},
                    ...
                    )
my_pipe.fit(X, y)

__init__(self, limit_in_minutes=None, n_configurations=25) special

Initialize the object.

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.

25
Source code in photonai/optimization/grid_search/grid_search.py
def __init__(self, limit_in_minutes: Union[float, None] = None, n_configurations: Union[int, None] = 25):
    """
    Initialize the object.

    Parameters:
        limit_in_minutes:
            Total time in minutes.

        n_configurations:
            Number of configurations to be calculated.

    """
    super(RandomGridSearchOptimizer, self).__init__()
    self._k = n_configurations
    self.n_configurations = self._k
    self.limit_in_minutes = limit_in_minutes
    self.start_time, self.end_time = None, None

next_config_generator(self)

Generator for new configs - ask method.

Returns:

Type Description
Generator

Yields the next config.

Source code in photonai/optimization/grid_search/grid_search.py
def next_config_generator(self) -> Generator:
    """
    Generator for new configs - ask method.

    Returns:
        Yields the next config.

    """
    if self.start_time is None and self.limit_in_minutes is not None:
        self.start_time = datetime.datetime.now()
        self.end_time = self.start_time + datetime.timedelta(minutes=self.limit_in_minutes)
    for parameters in super(RandomGridSearchOptimizer, self).next_config_generator():
        if self.limit_in_minutes is None or datetime.datetime.now() < self.end_time:
            yield parameters

prepare(self, pipeline_elements, maximize_metric)

Prepare 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/grid_search/grid_search.py
def prepare(self, pipeline_elements: list, maximize_metric: bool) -> None:
    """
    Prepare hyperparameter search.

    Parameters:
        pipeline_elements:
            List of all PipelineElements to create the hyperparameter space.

        maximize_metric:
            Boolean to distinguish between score and error.

    """
    super(RandomGridSearchOptimizer, self).prepare(pipeline_elements, maximize_metric)
    self.start_time = None
    self.n_configurations = self._k
    self.param_grid = list(self.param_grid)
    # create random order in list
    np.random.shuffle(self.param_grid)
    if self.n_configurations is not None:
        # k is maximal all grid items
        if self.n_configurations > len(self.param_grid):
            self.n_configurations = len(self.param_grid)
        self.param_grid = self.param_grid[0:self.n_configurations]