Skip to content

Documentation for GridSearchOptimizer

Grid search optimizer.

Searches for the best configuration by iteratively testing a grid of possible hyperparameter combinations.

Examples:

1
2
3
4
5
my_pipe = Hyperpipe(name='grid_based_pipe',
                    optimizer='grid_search',
                    ...
                    )
my_pipe.fit(X, y)
Source code in photonai/optimization/grid_search/grid_search.py
class GridSearchOptimizer(PhotonSlaveOptimizer):
    """Grid search optimizer.

    Searches for the best configuration by iteratively
    testing a grid of possible hyperparameter combinations.

    Example:
        ``` python
        my_pipe = Hyperpipe(name='grid_based_pipe',
                            optimizer='grid_search',
                            ...
                            )
        my_pipe.fit(X, y)
        ```

    """
    def __init__(self):
        """Initialize the object."""
        self.param_grid = []
        self.pipeline_elements = None
        self.parameter_iterable = None
        self.ask = self.next_config_generator()

    def prepare(self, pipeline_elements: list, maximize_metric: bool) -> None:
        """
        Creates a grid from a list of PipelineElements.
        Hyperparameters can be accessed via pipe_element.hyperparameters.

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

            maximize_metric:
                Boolean to distinguish between score and error.

        """
        self.pipeline_elements = pipeline_elements
        self.ask = self.next_config_generator()
        self.param_grid = create_global_config_grid(self.pipeline_elements)
        logger.info("Grid Search generated " + str(len(self.param_grid)) + " configurations")

    def next_config_generator(self) -> Generator:
        """
        Generator for new configs - ask method.

        Returns:
            Yields the next config.

        """
        for parameters in self.param_grid:
            yield parameters

__init__(self) special

Initialize the object.

Source code in photonai/optimization/grid_search/grid_search.py
def __init__(self):
    """Initialize the object."""
    self.param_grid = []
    self.pipeline_elements = None
    self.parameter_iterable = None
    self.ask = self.next_config_generator()

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.

    """
    for parameters in self.param_grid:
        yield parameters

prepare(self, pipeline_elements, maximize_metric)

Creates a grid from a list of PipelineElements. Hyperparameters can be accessed via pipe_element.hyperparameters.

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:
    """
    Creates a grid from a list of PipelineElements.
    Hyperparameters can be accessed via pipe_element.hyperparameters.

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

        maximize_metric:
            Boolean to distinguish between score and error.

    """
    self.pipeline_elements = pipeline_elements
    self.ask = self.next_config_generator()
    self.param_grid = create_global_config_grid(self.pipeline_elements)
    logger.info("Grid Search generated " + str(len(self.param_grid)) + " configurations")