[tune] extend search space api docs (#10576)

Co-authored-by: Richard Liaw <rliaw@berkeley.edu>
This commit is contained in:
Kai Fricke
2020-09-04 18:39:51 -07:00
committed by GitHub
co-authored by Richard Liaw
parent c4c0857107
commit 2fac66650d
13 changed files with 274 additions and 137 deletions
+12 -43
View File
@@ -46,7 +46,6 @@ def easy_objective(config):
if __name__ == "__main__":
import argparse
from ax.service.ax_client import AxClient
parser = argparse.ArgumentParser()
parser.add_argument(
@@ -55,62 +54,32 @@ if __name__ == "__main__":
ray.init()
config = {
tune_kwargs = {
"num_samples": 10 if args.smoke_test else 50,
"config": {
"iterations": 100,
"x1": tune.uniform(0.0, 1.0),
"x2": tune.uniform(0.0, 1.0),
"x3": tune.uniform(0.0, 1.0),
"x4": tune.uniform(0.0, 1.0),
"x5": tune.uniform(0.0, 1.0),
"x6": tune.uniform(0.0, 1.0),
},
"stop": {
"timesteps_total": 100
}
}
parameters = [
{
"name": "x1",
"type": "range",
"bounds": [0.0, 1.0],
"value_type": "float", # Optional, defaults to "bounds".
"log_scale": False, # Optional, defaults to False.
},
{
"name": "x2",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x3",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x4",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x5",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x6",
"type": "range",
"bounds": [0.0, 1.0],
},
]
client = AxClient(enforce_sequential_optimization=False)
client.create_experiment(
parameters=parameters,
objective_name="hartmann6",
minimize=True, # Optional, defaults to False.
algo = AxSearch(
max_concurrent=4,
metric="hartmann6",
mode="min",
parameter_constraints=["x1 + x2 <= 2.0"], # Optional.
outcome_constraints=["l2norm <= 1.25"], # Optional.
)
algo = AxSearch(ax_client=client, max_concurrent=4)
scheduler = AsyncHyperBandScheduler(metric="hartmann6", mode="min")
tune.run(
easy_objective,
name="ax",
search_alg=algo,
scheduler=scheduler,
**config)
**tune_kwargs)
+4 -5
View File
@@ -35,16 +35,15 @@ if __name__ == "__main__":
args, _ = parser.parse_known_args()
ray.init()
space = {"width": (0, 20), "height": (-100, 100)}
config = {
tune_kwargs = {
"num_samples": 10 if args.smoke_test else 1000,
"config": {
"steps": 100,
"width": tune.uniform(0, 20),
"height": tune.uniform(-100, 100)
}
}
algo = BayesOptSearch(
space,
metric="mean_loss",
mode="min",
utility_kwargs={
@@ -58,4 +57,4 @@ if __name__ == "__main__":
name="my_exp",
search_alg=algo,
scheduler=scheduler,
**config)
**tune_kwargs)
+8 -14
View File
@@ -28,7 +28,6 @@ def easy_objective(config):
if __name__ == "__main__":
import argparse
from hyperopt import hp
parser = argparse.ArgumentParser()
parser.add_argument(
@@ -36,13 +35,6 @@ if __name__ == "__main__":
args, _ = parser.parse_known_args()
ray.init(configure_logging=False)
space = {
"width": hp.uniform("width", 0, 20),
"height": hp.uniform("height", -100, 100),
# This is an ignored parameter.
"activation": hp.choice("activation", ["relu", "tanh"])
}
current_best_params = [
{
"width": 1,
@@ -56,16 +48,18 @@ if __name__ == "__main__":
}
]
config = {
tune_kwargs = {
"num_samples": 10 if args.smoke_test else 1000,
"config": {
"steps": 100,
"width": tune.uniform(0, 20),
"height": tune.uniform(-100, 100),
# This is an ignored parameter.
"activation": tune.choice(["relu", "tanh"])
}
}
algo = HyperOptSearch(
space,
metric="mean_loss",
mode="min",
points_to_evaluate=current_best_params)
metric="mean_loss", mode="min", points_to_evaluate=current_best_params)
scheduler = AsyncHyperBandScheduler(metric="mean_loss", mode="min")
tune.run(easy_objective, search_alg=algo, scheduler=scheduler, **config)
tune.run(
easy_objective, search_alg=algo, scheduler=scheduler, **tune_kwargs)
+9 -11
View File
@@ -7,7 +7,7 @@ import time
import ray
from ray import tune
from ray.tune.schedulers import AsyncHyperBandScheduler
from ray.tune.suggest.optuna import OptunaSearch, param
from ray.tune.suggest.optuna import OptunaSearch
def evaluation_fn(step, width, height):
@@ -35,19 +35,17 @@ if __name__ == "__main__":
args, _ = parser.parse_known_args()
ray.init(configure_logging=False)
space = [
param.suggest_uniform("width", 0, 20),
param.suggest_uniform("height", -100, 100),
# This is an ignored parameter.
param.suggest_categorical("activation", ["relu", "tanh"])
]
config = {
tune_kwargs = {
"num_samples": 10 if args.smoke_test else 100,
"config": {
"steps": 100,
"width": tune.uniform(0, 20),
"height": tune.uniform(-100, 100),
# This is an ignored parameter.
"activation": tune.choice(["relu", "tanh"])
}
}
algo = OptunaSearch(space, metric="mean_loss", mode="min")
algo = OptunaSearch(metric="mean_loss", mode="min")
scheduler = AsyncHyperBandScheduler(metric="mean_loss", mode="min")
tune.run(easy_objective, search_alg=algo, scheduler=scheduler, **config)
tune.run(
easy_objective, search_alg=algo, scheduler=scheduler, **tune_kwargs)
+28 -4
View File
@@ -56,9 +56,34 @@ class AxSearch(Searcher):
use_early_stopped_trials: Deprecated.
max_concurrent (int): Deprecated.
Tune automatically converts search spaces to Ax's format:
.. code-block:: python
from ray import tune
from ray.tune.suggest.ax import AxSearch
config = {
"x1": tune.uniform(0.0, 1.0),
"x2": tune.uniform(0.0, 1.0)
}
def easy_objective(config):
for i in range(100):
intermediate_result = config["x1"] + config["x2"] * i
tune.report(score=intermediate_result)
ax_search = AxSearch(objective_name="score")
tune.run(
config=config,
easy_objective,
search_alg=ax_search)
If you would like to pass the search space manually, the code would
look like this:
.. code-block:: python
from ax.service.ax_client import AxClient
from ray import tune
from ray.tune.suggest.ax import AxSearch
@@ -72,9 +97,8 @@ class AxSearch(Searcher):
intermediate_result = config["x1"] + config["x2"] * i
tune.report(score=intermediate_result)
client = AxClient()
algo = AxSearch(space=parameters, objective_name="score")
tune.run(easy_objective, search_alg=algo)
ax_search = AxSearch(space=parameters, objective_name="score")
tune.run(easy_objective, search_alg=ax_search)
"""
+21 -7
View File
@@ -65,6 +65,24 @@ class BayesOptSearch(Searcher):
max_concurrent: Deprecated.
use_early_stopped_trials: Deprecated.
Tune automatically converts search spaces to BayesOptSearch's format:
.. code-block:: python
from ray import tune
from ray.tune.suggest.bayesopt import BayesOptSearch
config = {
"width": tune.uniform(0, 20),
"height": tune.uniform(-100, 100)
}
bayesopt = BayesOptSearch(metric="mean_loss", mode="min")
tune.run(my_func, config=config, search_alg=bayesopt)
If you would like to pass the search space manually, the code would
look like this:
.. code-block:: python
from ray import tune
@@ -74,8 +92,9 @@ class BayesOptSearch(Searcher):
'width': (0, 20),
'height': (-100, 100),
}
algo = BayesOptSearch(space, metric="mean_loss", mode="min")
tune.run(my_func, search_alg=algo)
bayesopt = BayesOptSearch(space, metric="mean_loss", mode="min")
tune.run(my_func, search_alg=bayesopt)
"""
# bayes_opt.BayesianOptimization: Optimization object
optimizer = None
@@ -336,11 +355,6 @@ class BayesOptSearch(Searcher):
"Grid search parameters cannot be automatically converted "
"to a BayesOpt search space.")
if resolved_vars:
raise ValueError(
"BayesOpt does not support fixed parameters. Please find a "
"different way to pass constants to your training function.")
def resolve_value(domain):
sampler = domain.get_sampler()
if isinstance(sampler, Quantized):
+46 -21
View File
@@ -42,27 +42,6 @@ class HyperOptSearch(Searcher):
pip install -U hyperopt
You will not be able to leverage Tune's default ``grid_search``
and random search primitives when using HyperOptSearch. You need to
use the `HyperOpt search space specification
<https://github.com/hyperopt/hyperopt/wiki/FMin>`_.
.. code-block:: python
space = {
'width': hp.uniform('width', 0, 20),
'height': hp.uniform('height', -100, 100),
'activation': hp.choice("activation", ["relu", "tanh"])
}
current_best_params = [{
'width': 10,
'height': 0,
'activation': 0, # The index of "relu"
}]
algo = HyperOptSearch(
space, metric="mean_loss", mode="min",
points_to_evaluate=current_best_params)
Parameters:
space (dict): HyperOpt configuration. Parameters will be sampled
@@ -88,6 +67,52 @@ class HyperOptSearch(Searcher):
max_concurrent: Deprecated.
use_early_stopped_trials: Deprecated.
Tune automatically converts search spaces to HyperOpt's format:
.. code-block:: python
config = {
'width': tune.uniform(0, 20),
'height': tune.uniform(-100, 100),
'activation': tune.choice(["relu", "tanh"])
}
current_best_params = [{
'width': 10,
'height': 0,
'activation': 0, # The index of "relu"
}]
hyperopt_search = HyperOptSearch(
metric="mean_loss", mode="min",
points_to_evaluate=current_best_params)
tune.run(trainable, config=config, search_alg=hyperopt_search)
If you would like to pass the search space manually, the code would
look like this:
.. code-block:: python
space = {
'width': hp.uniform('width', 0, 20),
'height': hp.uniform('height', -100, 100),
'activation': hp.choice("activation", ["relu", "tanh"])
}
current_best_params = [{
'width': 10,
'height': 0,
'activation': 0, # The index of "relu"
}]
hyperopt_search = HyperOptSearch(
space, metric="mean_loss", mode="min",
points_to_evaluate=current_best_params)
tune.run(trainable, search_alg=hyperopt_search)
"""
def __init__(
+21 -1
View File
@@ -60,7 +60,25 @@ class OptunaSearch(Searcher):
sampler (optuna.samplers.BaseSampler): Optuna sampler used to
draw hyperparameter configurations. Defaults to ``TPESampler``.
Example:
Tune automatically converts search spaces to Optuna's format:
.. code-block:: python
from ray.tune.suggest.optuna import OptunaSearch
config = {
"a": tune.uniform(6, 8)
"b": tune.uniform(10, 20)
}
optuna_search = OptunaSearch(
metric="loss",
mode="min")
tune.run(trainable, config=config, search_alg=optuna_search)
If you would like to pass the search space manually, the code would
look like this:
.. code-block:: python
@@ -76,6 +94,8 @@ class OptunaSearch(Searcher):
metric="loss",
mode="min")
tune.run(trainable, search_alg=optuna_search)
.. versionadded:: 0.8.8
"""