Version: Next

Optuna Sweeper plugin

PyPI PyPI - License PyPI - Python Version PyPI - DownloadsExampleExample

This plugin enables Hydra applications to utilize Optuna for the optimization of the parameters of experiments.

Installation#

pip install hydra-optuna-sweeper --upgrade

Usage#

Please set hydra/sweeper to optuna in your config file.

defaults:
- override hydra/sweeper: optuna

Alternatively, add hydra/sweeper=optuna option to your command line.

The default configuration is here.

Example#

We include an example in this directory. example/sphere.py implements a simple benchmark function to be minimized.

You can discover the Optuna sweeper parameters with:

python example/sphere.py hydra/sweeper=optuna --cfg hydra -p hydra.sweeper
# @package hydra.sweeper
_target_: hydra_plugins.hydra_optuna_sweeper.optuna_sweeper.OptunaSweeper
optuna_config:
direction: minimize
storage: null
study_name: sphere
n_trials: 20
n_jobs: 1
sampler: TPESampler
seed: 123
search_space:
x:
type: float
low: -5.5
high: 5.5
step: 0.5
y:
type: categorical
choices:
- -5
- 0
- 5

The function decorated with @hydra.main() returns a float which we want to minimize, the minimum is 0 and reached for:

x: 0
y: 0

To run optimization, clone the code and run the following command in the plugins/hydra_optuna_sweeper directory:

python example/sphere.py --multirun

You can also override the search space parametrization:

python example/sphere.py --multirun 'x=interval(-5.0, 5.0)' 'y=interval(0, 10)'

Search space configuration#

This plugin supports Optuna's distributions to configure search spaces. They can be defined either through commandline override or config file.

Configuring through commandline override#

Hydra provides a override parser that support rich syntax. Please refer to OverrideGrammer/Basic and OverrideGrammer/Extended for details.

Interval override#

By default, interval is converted to UniformDistribution. You can use IntUniformDistribution, LogUniformDistribution or IntLogUniformDistribution by casting the interval to int and tagging it with log.

Example for interval override
python example/sphere.py --multirun 'x=int(interval(-5.0, 5.0))' 'y=tag(log, interval(1, 10))'

The output is as follows:

[HYDRA] Study name: sphere
[HYDRA] Storage: None
[HYDRA] Sampler: TPESampler
[HYDRA] Direction: minimize
[HYDRA] Launching 1 jobs locally
[HYDRA] #0 : x=-3 y=1.6859762540733367
[HYDRA] Launching 1 jobs locally
[HYDRA] #1 : x=1 y=5.237816870668193
...
[HYDRA] Best parameters: {'x': 0, 'y': 1.0929184723430116}
[HYDRA] Best value: 1.1944707871885822

Range override#

range is converted to IntUniformDistribution. If you apply shuffle to range, CategoricalDistribution is used instead.

Example for range override
python example/sphere.py --multirun 'x=range(-5.0, 5.0)' 'y=shuffle(range(-5, 5))'

The output is as follows:

[HYDRA] Study name: sphere
[HYDRA] Storage: None
[HYDRA] Sampler: TPESampler
[HYDRA] Direction: minimize
[HYDRA] Launching 1 jobs locally
[HYDRA] #0 : x=-3 y=-1
[HYDRA] Launching 1 jobs locally
[HYDRA] #1 : x=1 y=0
...
[HYDRA] Best parameters: {'x': 1, 'y': 0}
[HYDRA] Best value: 1

Choice override#

choice is converted to CategoricalDistribution.

Example for choice override
python example/sphere.py --multirun 'x=choice(-5.0, 0.0, 5.0)' 'y=choice(0, 1, 2, 3, 4, 5)'

The output is as follows:

[HYDRA] Study name: sphere
[HYDRA] Storage: None
[HYDRA] Sampler: TPESampler
[HYDRA] Direction: minimize
[HYDRA] Launching 1 jobs locally
[HYDRA] #0 : x=5.0 y=5
[HYDRA] Launching 1 jobs locally
[HYDRA] #1 : x=5.0 y=2
...
[HYDRA] Best parameters: {'x': 0.0, 'y': 0}
[HYDRA] Best value: 0.0

Configuring through config file#

Int parameters#

int parameters can be defined with the following fields:

  • type: int
  • low: lower bound
  • high: upper bound
  • step: discretization step (optional)
  • log: if true, space is converted to the log domain

If log is false, the parameter is mapped to IntUniformDistribution. Otherwise, the parameter is mapped to IntLogUniformDistribution. Please note that step can not be set if log=true.

Float parameters#

float parameters can be defined with the following fields:

  • type: float
  • low: lower bound
  • high: upper bound
  • step: discretization step
  • log: if true, space is converted to the log domain

If log is false, the parameter is mapped to UniformDistribution or DiscreteUniformDistribution depending on the presence or absence of the step field, respectively. Otherwise, the parameter is mapped to LogUniformDistribution. Please note that step can not be set if log=true.

Categorical parameters#

categorical parameters can be defined with the following fields:

  • type: categorical
  • choices: a list of parameter value candidates

The parameters are mapped to CategoricalDistribution.

Last updated on by Omry Yadan