Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SioKCronin committed Sep 12, 2017
1 parent 97586c3 commit df807ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
9 changes: 9 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ These functions can be used as benchmark tests for assessing the performance of
algorithm.

* :mod:`pyswarms.utils.functions.single_obj` - single-objective test functions

Search
~~~~~~

These search methods can be used to compare the relative performance of hyperparameter value combinations in reducing a specified objective function.

* :mod:`pyswarms.utils.search.grid_search` - exhaustive search of optimal performance on selected objective function over cartesian products of provided hyperparameter values

* :mod:`pyswarms.utils.search.random_search` - search for optimal performance on selected objective function over combinations of randomly selected hyperparameter values within specified bounds for specified number of selection iterations
29 changes: 23 additions & 6 deletions pyswarms/utils/search/base_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, optimizer, n_particles, dimensions, options,
Attributes
----------
optimizer: PySwarms class
optimizer: pyswarms.single
either LocalBestPSO or GlobalBestPSO
n_particles : int
number of particles in the swarm.
Expand Down Expand Up @@ -63,7 +63,14 @@ def __init__(self, optimizer, n_particles, dimensions, options,
self.iters = iters

def generate_score(self, options):
"""Generates score for optimizer's performance on objective function."""
"""Generates score for optimizer's performance on objective function.
Parameters
----------
options: dict
a dict of 5 hyperparameter values ('c1', 'c2', 'w', 'k', 'p'
"""

#Intialize optimizer
f = self.optimizer(self.n_particles, self.dims, options,
Expand All @@ -74,7 +81,16 @@ def generate_score(self, options):

def search(self, maximum=False):
"""Compares optimizer's objective function performance scores
for all combinations of provided parameters."""
for all combinations of provided parameters.
Parameters
----------
maximum: bool
a bool defaulting to False, returning the minimum value for the
objective function. If set to True, will return the maximum value
for the objective function.
"""

#Assign parameter keys
params = self.options.keys()
Expand All @@ -85,11 +101,12 @@ def search(self, maximum=False):
#Calculate scores for all hyperparameter combinations
scores = [self.generate_score(i) for i in grid]

#Select optimization function
#Default behavior
idx, self.best_score = min(enumerate(scores), key=op.itemgetter(1))

#Catches the maximum bool flag
if maximum:
idx, self.best_score = max(enumerate(scores), key=op.itemgetter(1))
else:
idx, self.best_score = min(enumerate(scores), key=op.itemgetter(1))

#Return optimum hyperparameter value property from grid using index
self.best_options = op.itemgetter(idx)(grid)
Expand Down
22 changes: 16 additions & 6 deletions pyswarms/utils/search/grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
Hyperparameter grid search.
Compares the relative performance of hyperparameter value combinations in
reducing a specified objective function.
optimizing a specified objective function.
For each hyperparameter, user can provide either a single value or a list
of possible values, and their cartesian product is taken to produce a grid
of all possible combinations. These combinations are then tested to produce
a list of objective function scores. The default of the optimize method
returns the hyperparameters that yield the minimum score, yet maximum score
can also be evaluated.
of possible values. The cartesian products of these hyperparameters are taken
to produce a grid of all possible combinations. These combinations are then
tested to produce a list of objective function scores. The search method
default returns the minimum objective function score and hyperparameters that
yield the minimum score, yet maximum score can also be evaluated.
Parameters
----------
Expand Down Expand Up @@ -54,6 +54,15 @@ class GridSearch(SearchBase):
"""Exhaustive search of optimal performance on selected objective function
over all combinations of specified hyperparameter values."""

def __init__(self, optimizer, n_particles, dimensions, options,
objective_func, iters,bounds=None, velocity_clamp=None):
"""Initializes the paramsearch."""

# Assign attributes
super().__init__(optimizer, n_particles, dimensions, options,
objective_func, iters, bounds=bounds,
velocity_clamp=velocity_clamp)

def generate_grid(self):
"""Generates the grid of all hyperparameter value combinations."""

Expand All @@ -67,3 +76,4 @@ def generate_grid(self):

#Return list of dicts for all hyperparameter value combinations
return [dict(zip(*[params, list(x)])) for x in list_of_products]

18 changes: 10 additions & 8 deletions pyswarms/utils/search/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"""
Hyperparameter random search.
Compares the relative performance of randomly genreated hyperparameter value
combinations in reducing a specified objective function.
Compares the relative performance of combinations of randomly generated
hyperparameter values in optimizing a specified objective function.
User provides lists of bounds for the uniform random value generation of
'c1', 'c2', and 'w', and the random integer value generation of 'k'.
Combinations of values are generated for the number of iterations specified,
and the generated grid of combinations is used in the search method to find
the optimal parameters for the objective function. The default of the search
method returns the objective function score and hyperparameters that yield
the optimal parameters for the objective function. The search method default
returns the minimum objective function score and hyperparameters that yield
the minimum score, yet maximum score can also be evaluated.
Parameters
Expand Down Expand Up @@ -52,10 +52,12 @@
import random
from pyswarms.utils.search.base_search import SearchBase

from past.builtins import xrange

class RandomSearch(SearchBase):
"""Search of optimal performance on selected objective function
over combinations of hyperparameter values within specified bounds
for specified number of selection iterations."""
over combinations of randomly selected hyperparameter values
within specified bounds for specified number of selection iterations."""

def __init__(self, optimizer, n_particles, dimensions, options,
objective_func, iters, n_selection_iters,
Expand All @@ -82,7 +84,7 @@ def generate_grid(self):

#Remove 'p' to hold as a constant in the paramater combinations
p = options.pop('p')
params['p'] = [p for _ in range(self.n_selection_iters)]
params['p'] = [p for _ in xrange(self.n_selection_iters)]

#Assign generators based on parameter type
param_generators = {
Expand All @@ -103,5 +105,5 @@ def generate_grid(self):
'w': params['w'][i],
'k': params['k'][i],
'p': params['p'][i]}
for i in range(self.n_selection_iters)]
for i in xrange(self.n_selection_iters)]

0 comments on commit df807ff

Please sign in to comment.