ray.tune.search.zoopt.ZOOptSearch#

class ray.tune.search.zoopt.ZOOptSearch(algo: str = 'asracos', budget: Optional[int] = None, dim_dict: Optional[Dict] = None, metric: Optional[str] = None, mode: Optional[str] = None, points_to_evaluate: Optional[List[Dict]] = None, parallel_num: int = 1, **kwargs)[source]#

Bases: ray.tune.search.searcher.Searcher

A wrapper around ZOOpt to provide trial suggestions.

ZOOptSearch is a library for derivative-free optimization. It is backed by the ZOOpt package. Currently, Asynchronous Sequential RAndomized COordinate Shrinking (ASRacos) is implemented in Tune.

To use ZOOptSearch, install zoopt (>=0.4.1): pip install -U zoopt.

Tune automatically converts search spaces to ZOOpt”s format:

from ray import tune
from ray.tune.search.zoopt import ZOOptSearch

"config": {
    "iterations": 10,  # evaluation times
    "width": tune.uniform(-10, 10),
    "height": tune.uniform(-10, 10)
}

zoopt_search_config = {
    "parallel_num": 8,  # how many workers to parallel
}

zoopt_search = ZOOptSearch(
    algo="Asracos",  # only support Asracos currently
    budget=20,  # must match `num_samples` in `tune.TuneConfig()`.
    dim_dict=dim_dict,
    metric="mean_loss",
    mode="min",
    **zoopt_search_config
)

tuner = tune.Tuner(
    my_objective,
    tune_config=tune.TuneConfig(
        search_alg=zoopt_search,
        num_samples=20
    ),
    run_config=air.RunConfig(
        name="zoopt_search",
        stop={"timesteps_total": 10}
    ),
    param_space=config
)
tuner.fit()

If you would like to pass the search space manually, the code would look like this:

from ray import tune
from ray.tune.search.zoopt import ZOOptSearch
from zoopt import ValueType

dim_dict = {
    "height": (ValueType.CONTINUOUS, [-10, 10], 1e-2),
    "width": (ValueType.DISCRETE, [-10, 10], False),
    "layers": (ValueType.GRID, [4, 8, 16])
}

"config": {
    "iterations": 10,  # evaluation times
}

zoopt_search_config = {
    "parallel_num": 8,  # how many workers to parallel
}

zoopt_search = ZOOptSearch(
    algo="Asracos",  # only support Asracos currently
    budget=20,  # must match `num_samples` in `tune.TuneConfig()`.
    dim_dict=dim_dict,
    metric="mean_loss",
    mode="min",
    **zoopt_search_config
)

tuner = tune.Tuner(
    my_objective,
    tune_config=tune.TuneConfig(
        search_alg=zoopt_search,
        num_samples=20
    ),
    run_config=air.RunConfig(
        name="zoopt_search",
        stop={"timesteps_total": 10}
    ),
)
tuner.fit()
Parameters
  • algo – To specify an algorithm in zoopt you want to use. Only support ASRacos currently.

  • budget – Number of samples.

  • dim_dict – Dimension dictionary. For continuous dimensions: (continuous, search_range, precision); For discrete dimensions: (discrete, search_range, has_order); For grid dimensions: (grid, grid_list). More details can be found in zoopt package.

  • metric – The training result objective value attribute. If None but a mode was passed, the anonymous metric _metric will be used per default.

  • mode – One of {min, max}. Determines whether objective is minimizing or maximizing the metric attribute.

  • points_to_evaluate – Initial parameter suggestions to be run first. This is for when you already have some good parameters you want to run first to help the algorithm make better suggestions for future parameters. Needs to be a list of dicts containing the configurations.

  • parallel_num – How many workers to parallel. Note that initial phase may start less workers than this number. More details can be found in zoopt package.

set_search_properties(metric: Optional[str], mode: Optional[str], config: Dict, **spec) bool[source]#

Pass search properties to searcher.

This method acts as an alternative to instantiating search algorithms with their own specific search spaces. Instead they can accept a Tune config through this method. A searcher should return True if setting the config was successful, or False if it was unsuccessful, e.g. when the search space has already been set.

Parameters
  • metric – Metric to optimize

  • mode – One of [β€œmin”, β€œmax”]. Direction to optimize.

  • config – Tune config dict.

  • **spec – Any kwargs for forward compatiblity. Info like Experiment.PUBLIC_KEYS is provided through here.

suggest(trial_id: str) Optional[Dict][source]#

Queries the algorithm to retrieve the next set of parameters.

Parameters

trial_id – Trial ID used for subsequent notifications.

Returns

Configuration for a trial, if possible.

If FINISHED is returned, Tune will be notified that no more suggestions/configurations will be provided. If None is returned, Tune will skip the querying of the searcher for this step.

Return type

dict | FINISHED | None

on_trial_complete(trial_id: str, result: Optional[Dict] = None, error: bool = False)[source]#

Notification for the completion of trial.

save(checkpoint_path: str)[source]#

Save state to path for this search algorithm.

Parameters

checkpoint_path – File where the search algorithm state is saved. This path should be used later when restoring from file.

Example:

search_alg = Searcher(...)

tuner = tune.Tuner(
    cost,
    tune_config=tune.TuneConfig(
        search_alg=search_alg,
        num_samples=5
    ),
    param_space=config
)
results = tuner.fit()

search_alg.save("./my_favorite_path.pkl")

Changed in version 0.8.7: Save is automatically called by Tuner().fit(). You can use Tuner().restore() to restore from an experiment directory such as /ray_results/trainable.

restore(checkpoint_path: str)[source]#

Restore state for this search algorithm

Parameters

checkpoint_path – File where the search algorithm state is saved. This path should be the same as the one provided to β€œsave”.

Example:

search_alg.save("./my_favorite_path.pkl")

search_alg2 = Searcher(...)
search_alg2 = ConcurrencyLimiter(search_alg2, 1)
search_alg2.restore(checkpoint_path)
tuner = tune.Tuner(
    cost,
    tune_config=tune.TuneConfig(
        search_alg=search_alg2,
        num_samples=5
    ),
)
tuner.fit()