Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Change back to old convention on n_random_starts
Browse files Browse the repository at this point in the history
  • Loading branch information
betatim committed Jun 11, 2017
1 parent e1108e9 commit cdae44a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 147 deletions.
70 changes: 28 additions & 42 deletions skopt/optimizer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import copy
import inspect
import numbers
import warnings
from collections import Iterable

import numpy as np
Expand All @@ -19,7 +18,7 @@


def base_minimize(func, dimensions, base_estimator,
n_calls=100, n_initial_points=10, n_random_starts=None,
n_calls=100, n_random_starts=10,
acq_func="EI", acq_optimizer="lbfgs",
x0=None, y0=None, random_state=None, verbose=False,
callback=None, n_points=10000, n_restarts_optimizer=5,
Expand Down Expand Up @@ -54,14 +53,9 @@ def base_minimize(func, dimensions, base_estimator,
* `n_calls` [int, default=100]:
Maximum number of calls to `func`.
* `n_initial_points` [int, default=10]:
Number of evaluations of `func` with initialization points
before approximating it with `base_estimator`. Points provided as
`x0` count as initialization points. If len(x0) < n_initial_points
additional points are sampled at random.
* `n_random_starts` [int, default=10]:
DEPRECATED, use `n_initial_points` instead.
Number of evaluations of `func` with random points before
approximating it with `base_estimator`.
* `acq_func` [string, default=`"EI"`]:
Function to minimize over the posterior distribution. Can be either
Expand Down Expand Up @@ -168,60 +162,52 @@ def base_minimize(func, dimensions, base_estimator,
"n_jobs": n_jobs}
acq_func_kwargs = {"xi": xi, "kappa": kappa}

if n_random_starts is not None:
warnings.warn(("n_random_starts has been renamed to "
"n_initial_points."),
DeprecationWarning)
n_initial_points = n_random_starts

optimizer = Optimizer(dimensions, base_estimator,
n_initial_points=n_initial_points,
acq_func=acq_func, acq_optimizer=acq_optimizer,
random_state=random_state,
acq_optimizer_kwargs=acq_optimizer_kwargs,
acq_func_kwargs=acq_func_kwargs)

# Initialize with provided points (x0 and y0) and/or random points
if x0 is None:
x0 = []
elif not isinstance(x0[0], (list, tuple)):
x0 = [x0]

if not isinstance(x0, list):
raise ValueError("`x0` should be a list, but got %s" % type(x0))

if n_random_starts == 0 and not x0:
raise ValueError("Either set `n_random_starts` > 0,"
" or provide `x0`")

if isinstance(y0, Iterable):
y0 = list(y0)
elif isinstance(y0, numbers.Number):
y0 = [y0]

# is the budget for calling `func` large enough?
required_calls = n_random_starts + (len(x0) if not y0 else 0)
if n_calls < required_calls:
raise ValueError(
"Expected `n_calls` >= %d, got %d" % (required_calls, n_calls))

# Number of points the user wants to evaluate before it makes sense to
# fit a surrogate model
n_initial_points = n_random_starts + len(x0)
optimizer = Optimizer(dimensions, base_estimator,
n_initial_points=n_initial_points,
acq_func=acq_func, acq_optimizer=acq_optimizer,
random_state=random_state,
acq_optimizer_kwargs=acq_optimizer_kwargs,
acq_func_kwargs=acq_func_kwargs)

if optimizer.space.n_dims == 1:
assert all(isinstance(p, Iterable) for p in x0)

if not all(len(p) == optimizer.space.n_dims for p in x0):
raise RuntimeError("Optimization space (%s) and initial points in x0 "
"use inconsistent dimensions." % optimizer.space)

if not isinstance(x0, list):
raise ValueError("`x0` should be a list, but got %s" % type(x0))

# how many times do we need to call `func` to evaluate all points in `x0`
# or to have at least `n_initial_points` values
if y0 is None:
n_init_func_calls = max(n_initial_points, len(x0))
else:
n_init_func_calls = n_initial_points - len(y0)

if n_calls < n_init_func_calls:
raise ValueError(
"Expected `n_calls` >= %d, got %d" % (n_init_func_calls,
n_calls))

if n_initial_points == 0 and not x0:
raise ValueError("Either set `n_initial_points` > 0,"
" or provide `x0`")

callbacks = check_callback(callback)
if verbose:
callbacks.append(VerboseCallback(
n_init=n_init_func_calls, n_random=n_initial_points,
n_init=len(x0) if not y0 else 0,
n_random=n_random_starts,
n_total=n_calls))

# setting the scope for these variables
Expand Down
12 changes: 3 additions & 9 deletions skopt/optimizer/forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def forest_minimize(func, dimensions, base_estimator="ET", n_calls=100,
n_random_starts=None, n_initial_points=10, acq_func="EI",
n_random_starts=10, acq_func="EI",
x0=None, y0=None, random_state=None, verbose=False,
callback=None, n_points=10000, xi=0.01, kappa=1.96,
n_jobs=1):
Expand Down Expand Up @@ -65,14 +65,9 @@ def forest_minimize(func, dimensions, base_estimator="ET", n_calls=100,
* `n_calls` [int, default=100]:
Number of calls to `func`.
* `n_initial_points` [int, default=10]:
Number of evaluations of `func` with initialization points
before approximating it with `base_estimator`. Points provided as
`x0` count as initialization points. If len(x0) < n_initial_points
additional points are sampled at random.
* `n_random_starts` [int, default=10]:
DEPRECATED, use `n_initial_points` instead.
Number of evaluations of `func` with random points before
approximating it with `base_estimator`.
* `acq_func` [string, default=`"LCB"`]:
Function to minimize over the forest posterior. Can be either
Expand Down Expand Up @@ -169,7 +164,6 @@ def forest_minimize(func, dimensions, base_estimator="ET", n_calls=100,
return base_minimize(func, dimensions, base_estimator,
n_calls=n_calls, n_points=n_points,
n_random_starts=n_random_starts,
n_initial_points=n_initial_points,
x0=x0, y0=y0, random_state=random_state,
acq_func=acq_func,
xi=xi, kappa=kappa, verbose=verbose,
Expand Down
12 changes: 3 additions & 9 deletions skopt/optimizer/gbrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def gbrt_minimize(func, dimensions, base_estimator=None,
n_calls=100, n_random_starts=None, n_initial_points=10,
n_calls=100, n_random_starts=10,
acq_func="EI", acq_optimizer="auto",
x0=None, y0=None, random_state=None, verbose=False,
callback=None, n_points=10000, xi=0.01, kappa=1.96,
Expand Down Expand Up @@ -53,14 +53,9 @@ def gbrt_minimize(func, dimensions, base_estimator=None,
* `n_calls` [int, default=100]:
Number of calls to `func`.
* `n_initial_points` [int, default=10]:
Number of evaluations of `func` with initialization points
before approximating it with `base_estimator`. Points provided as
`x0` count as initialization points. If len(x0) < n_initial_points
additional points are sampled at random.
* `n_random_starts` [int, default=10]:
DEPRECATED, use `n_initial_points` instead.
Number of evaluations of `func` with random points before
approximating it with `base_estimator`.
* `acq_func` [string, default=`"LCB"`]:
Function to minimize over the forest posterior. Can be either
Expand Down Expand Up @@ -148,7 +143,6 @@ def gbrt_minimize(func, dimensions, base_estimator=None,
return base_minimize(func, dimensions, base_estimator,
n_calls=n_calls, n_points=n_points,
n_random_starts=n_random_starts,
n_initial_points=n_initial_points,
x0=x0, y0=y0, random_state=random_state, xi=xi,
kappa=kappa, acq_func=acq_func, verbose=verbose,
callback=callback, acq_optimizer="sampling")
12 changes: 3 additions & 9 deletions skopt/optimizer/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def gp_minimize(func, dimensions, base_estimator=None,
n_calls=100, n_random_starts=None, n_initial_points=10,
n_calls=100, n_random_starts=10,
acq_func="gp_hedge", acq_optimizer="lbfgs", x0=None, y0=None,
random_state=None, verbose=False, callback=None,
n_points=10000, n_restarts_optimizer=5, xi=0.01, kappa=1.96,
Expand Down Expand Up @@ -76,14 +76,9 @@ def gp_minimize(func, dimensions, base_estimator=None,
* `n_calls` [int, default=100]:
Number of calls to `func`.
* `n_initial_points` [int, default=10]:
Number of evaluations of `func` with initialization points
before approximating it with `base_estimator`. Points provided as
`x0` count as initialization points. If len(x0) < n_initial_points
additional points are sampled at random.
* `n_random_starts` [int, default=10]:
DEPRECATED, use `n_initial_points` instead.
Number of evaluations of `func` with random points before
approximating it with `base_estimator`.
* `acq_func` [string, default=`"EI"`]:
Function to minimize over the gaussian prior. Can be either
Expand Down Expand Up @@ -252,7 +247,6 @@ def gp_minimize(func, dimensions, base_estimator=None,
acq_func=acq_func,
xi=xi, kappa=kappa, acq_optimizer=acq_optimizer, n_calls=n_calls,
n_points=n_points, n_random_starts=n_random_starts,
n_initial_points=n_initial_points,
n_restarts_optimizer=n_restarts_optimizer,
x0=x0, y0=y0, random_state=random_state, verbose=verbose,
callback=callback, n_jobs=n_jobs)

0 comments on commit cdae44a

Please sign in to comment.