optunity.solvers.GridSearch module

class optunity.solvers.GridSearch.GridSearch(**kwargs)[source]

Bases: optunity.solvers.util.Solver

Please refer to Grid Search for more information about this algorithm.

Exhaustive search over the Cartesian product of parameter tuples. Returns x (the tuple which maximizes f) and its score f(x).

>>> s = GridSearch(x=[1,2,3], y=[-1,0,1])
>>> best_pars, _ = s.optimize(lambda x, y: x*y)
>>> best_pars['x']
3
>>> best_pars['y']
1

Initializes the solver with a tuple indicating parameter values.

>>> s = GridSearch(x=[1,2], y=[3,4])
>>> s.parameter_tuples['x']
[1, 2]
>>> s.parameter_tuples['y']
[3, 4]
static assign_grid_points(lb, ub, density)[source]

Assigns equally spaced grid points with given density in [ub, lb]. The bounds are always used. density must be at least 2.

Parameters:
  • lb (float) – lower bound of resulting grid
  • ub (float) – upper bound of resulting grid
  • density (int) – number of points to use
>>> s = GridSearch.assign_grid_points(1.0, 2.0, 3)
>>> s 
[1.0, 1.5, 2.0]
desc_brief = 'finds optimal parameter values on a predefined grid'
desc_full = ['Retrieves the best parameter tuple on a predefined grid.', ' ', 'This function requires the grid to be specified via named arguments:', '- names :: argument names', '- values :: list of grid coordinates to test', ' ', 'The solver performs evaluation on the Cartesian product of grid values.', 'The number of evaluations is the product of the length of all value vectors.']
maximize(f, pmap=<built-in function map>)

Maximizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

minimize(f, pmap=<built-in function map>)

Minimizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

name = 'grid search'
optimize(f, maximize=True, pmap=<built-in function map>)[source]

Optimizes f.

Parameters:
  • f (callable) – the objective function
  • maximize (boolean) – do we want to maximizes?
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

parameter_tuples

Returns the possible values of every parameter.

static suggest_from_box(num_evals, **kwargs)[source]

Creates a GridSearch solver that uses less than num_evals evaluations within given bounds (lb, ub). The bounds are first tightened, resulting in new bounds covering 99% of the area.

The resulting solver will use an equally spaced grid with the same number of points in every dimension. The amount of points that is used is per dimension is the nth root of num_evals, rounded down, where n is the number of hyperparameters.

>>> s = GridSearch.suggest_from_box(30, x=[0, 1], y=[-1, 0], z=[-1, 1])
>>> s['x'] 
[0.005, 0.5, 0.995]
>>> s['y'] 
[-0.995, -0.5, -0.005]
>>> s['z'] 
[-0.99, 0.0, 0.99]

Verify that we can effectively make a solver from box.

>>> s = GridSearch.suggest_from_box(30, x=[0, 1], y=[-1, 0], z=[-1, 1])
>>> solver = GridSearch(**s)
optunity.solvers.GridSearch.nth_root(val, n)[source]