ray.tune.search.skopt.SkOptSearch#

class ray.tune.search.skopt.SkOptSearch(optimizer: Optional[skopt.optimizer.optimizer.Optimizer] = None, space: Optional[Union[List[str], Dict[str, Union[Tuple, List]]]] = None, metric: Optional[str] = None, mode: Optional[str] = None, points_to_evaluate: Optional[List[Dict]] = None, evaluated_rewards: Optional[List] = None, convert_to_python: bool = True)[source]#

Bases: ray.tune.search.searcher.Searcher

Uses Scikit Optimize (skopt) to optimize hyperparameters.

Scikit-optimize is a black-box optimization library. Read more here: https://scikit-optimize.github.io.

You will need to install Scikit-Optimize to use this module.

pip install scikit-optimize

This Search Algorithm requires you to pass in a skopt Optimizer object.

This searcher will automatically filter out any NaN, inf or -inf results.

Parameters
  • optimizer – Optimizer provided from skopt.

  • space – A dict mapping parameter names to valid parameters, i.e. tuples for numerical parameters and lists for categorical parameters. If you passed an optimizer instance as the optimizer argument, this should be a list of parameter names instead.

  • 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.

  • evaluated_rewards – If you have previously evaluated the parameters passed in as points_to_evaluate you can avoid re-running those trials by passing in the reward attributes as a list so the optimiser can be told the results without needing to re-compute the trial. Must be the same length as points_to_evaluate. (See tune/examples/skopt_example.py)

  • convert_to_python – SkOpt outputs numpy primitives (e.g. np.int64) instead of Python types. If this setting is set to True, the values will be converted to Python primitives.

Tune automatically converts search spaces to SkOpt’s format:

config = {
    "width": tune.uniform(0, 20),
    "height": tune.uniform(-100, 100)
}

current_best_params = [
    {
        "width": 10,
        "height": 0,
    },
    {
        "width": 15,
        "height": -20,
    }
]

skopt_search = SkOptSearch(
    metric="mean_loss",
    mode="min",
    points_to_evaluate=current_best_params)

tuner = tune.Tuner(
    trainable_function,
    tune_config=tune.TuneConfig(
        search_alg=skopt_search
    ),
    param_space=config
)
tuner.fit()

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

parameter_names = ["width", "height"]
parameter_ranges = [(0,20),(-100,100)]
current_best_params = [[10, 0], [15, -20]]

skopt_search = SkOptSearch(
    parameter_names=parameter_names,
    parameter_ranges=parameter_ranges,
    metric="mean_loss",
    mode="min",
    points_to_evaluate=current_best_params)

tuner = tune.Tuner(
    trainable_function,
    tune_config=tune.TuneConfig(
        search_alg=skopt_search
    ),
)
tuner.fit()
add_evaluated_point(parameters: Dict, value: float, error: bool = False, pruned: bool = False, intermediate_values: Optional[List[float]] = None)[source]#

Pass results from a point that has been evaluated separately.

This method allows for information from outside the suggest - on_trial_complete loop to be passed to the search algorithm. This functionality depends on the underlying search algorithm and may not be always available.

Parameters
  • parameters – Parameters used for the trial.

  • value – Metric value obtained in the trial.

  • error – True if the training process raised an error.

  • pruned – True if trial was pruned.

  • intermediate_values – List of metric values for intermediate iterations of the result. None if not applicable.

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.

The result is internally negated when interacting with Skopt so that Skopt Optimizers can β€œmaximize” this value, as it minimizes on default.

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()