Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEimer committed Apr 18, 2024
1 parent 93fe3cb commit 8405846
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 111 deletions.
64 changes: 64 additions & 0 deletions examples/configs/pbt_mlp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

defaults:
- _self_
- override hydra/sweeper: HyperHEBO


learning_rate: constant
learning_rate_init: 0.001
batch_size: 200
n_neurons: 10
n_layer: 1
solver: adam
activation: tanh

seed: 42
epochs: 10 # Default number of epochs

hydra:
sweeper:
budget: 10
optimizer_kwargs:
checkpoint_tf: true
load_tf: true
search_space:
hyperparameters:
n_layer:
type: uniform_int
lower: 1
upper: 5
default: ${n_layer}
n_neurons:
type: uniform_int
lower: 8
upper: 1024
log: true
default_value: ${n_neurons}
activation:
type: categorical
choices: [ logistic, tanh, relu ]
default_value: ${activation}
solver:
type: categorical
choices: [ lbfgs, sgd, adam ]
default_value: ${solver}
batch_size:
type: uniform_int
lower: 30
upper: 300
default_value: ${batch_size}
learning_rate:
type: categorical
choices: [ constant, invscaling, adaptive ]
default_value: ${learning_rate}
learning_rate_init:
type: uniform_float
lower: 0.0001
upper: 1
default_value: ${learning_rate_init}
log: true

run:
dir: ./tmp/${now:%Y-%m-%d}/${now:%H-%M-%S}
sweep:
dir: ./tmp/${now:%Y-%m-%d}/${now:%H-%M-%S}
2 changes: 1 addition & 1 deletion hydra_plugins/hyper_carp_s/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .config import HyperCARPSConfig

__all__ = ["HyperCARPSConfig"]
__all__ = ["HyperCARPSConfig"]
15 changes: 14 additions & 1 deletion hydra_plugins/hyper_carp_s/hyper_carp_s.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
"""CARP-S optimizer adapter for HyperSweeper."""

from __future__ import annotations

from carps.benchmarks.dummy_problem import DummyProblem

from hydra_plugins.hypersweeper import Info


class HyperCARPSAdapter:
"""CARP-S optimizer."""

def __init__(self, carps) -> None:
"""Initialize the optimizer."""
self.carps = carps

def ask(self):
"""Ask for the next configuration."""
carps_info = self.carps.ask()
info = Info(carps_info.config, carps_info.budget, None, carps_info.seed)
return info, False

def tell(self, info, value):
"""Tell the result of the configuration."""
self.smac.tell(info, value)


def make_carp_s(configspace, carps_args):
"""Make a CARP-S instance for optimization."""
problem = DummyProblem()
problem._configspace = configspace
optimizer = carps_args["optimizer"](problem)
return HyperCARPSAdapter(optimizer)
return HyperCARPSAdapter(optimizer)
2 changes: 1 addition & 1 deletion hydra_plugins/hyper_hebo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .config import HyperHEBOConfig

__all__ = ["HyperHEBOConfig"]
__all__ = ["HyperHEBOConfig"]
72 changes: 45 additions & 27 deletions hydra_plugins/hyper_hebo/hyper_hebo.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
"""HEBO in Hypersweeper."""

from __future__ import annotations

from collections import abc
import pandas as pd
import numpy as np

from hebo.optimizers.hebo import HEBO
from hebo.design_space.design_space import DesignSpace
import numpy as np
import pandas as pd
from ConfigSpace import Configuration, ConfigurationSpace
from ConfigSpace.hyperparameters import (CategoricalHyperparameter, Constant,
FloatHyperparameter, Hyperparameter,
IntegerHyperparameter,
OrdinalHyperparameter)
from hebo.design_space.design_space import DesignSpace
from hebo.optimizers.hebo import HEBO

from hydra_plugins.hypersweeper import Info


class HyperHEBOAdapter:
"""HEBO optimizer."""

def __init__(self, hebo, configspace, design_space):
"""Initialize the optimizer."""
self.hebo = hebo
self.configspace = configspace
self.design_space = design_space

def ask(self):
"""Ask for the next configuration."""
hebo_info = self.hebo.suggest(1)
config = HEBOcfg2ConfigSpacecfg(
hebo_suggestion=hebo_info, design_space=self.design_space, config_space=self.configspace
hebo_suggestion=hebo_info,
design_space=self.design_space,
config_space=self.configspace,
)
info = Info(config, None, None, None)
return info, False

def tell(self, info, value):
"""Tell the result of the configuration."""
cost = value.cost
suggestion = ConfigSpacecfg2HEBOcfg(info.config)

Expand All @@ -38,28 +51,30 @@ def tell(self, info, value):


def make_hebo(configspace, hebo_args):
"""Make a HEBO instance for optimization."""
hps_hebo = []
for _, v in configspace.items():
hps_hebo.append(configspaceHP2HEBOHP(v))
design_space = DesignSpace().parse(hps_hebo)
hebo = HEBO(space=design_space, **hebo_args)
return HyperHEBOAdapter(hebo, configspace, design_space)


# These functions were taken from the CARP-S project here: https://github.com/automl/CARP-S/blob/main/carps/optimizers/hebo.py#L23
def configspaceHP2HEBOHP(hp: Hyperparameter) -> dict:
"""Convert ConfigSpace hyperparameter to HEBO hyperparameter
def configspaceHP2HEBOHP(hp: Hyperparameter) -> dict: # noqa: PLR0911, N802
"""Convert ConfigSpace hyperparameter to HEBO hyperparameter.
Parameters
----------
hp : Hyperparameter
ConfigSpace hyperparameter
Returns
Returns:
-------
dict
HEBO hyperparameter
Raises
Raises:
------
NotImplementedError
If ConfigSpace hyperparameter is anything else than
Expand All @@ -69,13 +84,11 @@ def configspaceHP2HEBOHP(hp: Hyperparameter) -> dict:
if isinstance(hp, IntegerHyperparameter):
if hp.log:
return {"name": hp.name, "type": "pow_int", "lb": hp.lower, "ub": hp.upper}
else:
return {"name": hp.name, "type": "int", "lb": hp.lower, "ub": hp.upper}
elif isinstance(hp, FloatHyperparameter):
return {"name": hp.name, "type": "int", "lb": hp.lower, "ub": hp.upper}
elif isinstance(hp, FloatHyperparameter): # noqa: RET505
if hp.log:
return {"name": hp.name, "type": "pow", "lb": hp.lower, "ub": hp.upper}
else:
return {"name": hp.name, "type": "num", "lb": hp.lower, "ub": hp.upper}
return {"name": hp.name, "type": "num", "lb": hp.lower, "ub": hp.upper}
elif isinstance(hp, CategoricalHyperparameter):
return {"name": hp.name, "type": "cat", "categories": hp.choices}
elif isinstance(hp, OrdinalHyperparameter):
Expand All @@ -89,12 +102,17 @@ def configspaceHP2HEBOHP(hp: Hyperparameter) -> dict:
elif isinstance(hp, Constant):
return {"name": hp.name, "type": "cat", "categories": [hp.value]}
else:
raise NotImplementedError(f"Unknown hyperparameter type: {hp.__class__.__name__}")

def HEBOcfg2ConfigSpacecfg(
hebo_suggestion: pd.DataFrame, design_space: DesignSpace, config_space: ConfigurationSpace
raise NotImplementedError(
f"Unknown hyperparameter type: {hp.__class__.__name__}"
)


def HEBOcfg2ConfigSpacecfg( # noqa: N802
hebo_suggestion: pd.DataFrame,
design_space: DesignSpace,
config_space: ConfigurationSpace,
) -> Configuration:
"""Convert HEBO config to ConfigSpace config
"""Convert HEBO config to ConfigSpace config.
Parameters
----------
Expand All @@ -105,12 +123,12 @@ def HEBOcfg2ConfigSpacecfg(
config_space : ConfigurationSpace
ConfigSpace configuration space
Returns
Returns:
-------
Configuration
Config in ConfigSpace format
Raises
Raises:
------
ValueError
If HEBO config is more than 1
Expand All @@ -128,21 +146,21 @@ def HEBOcfg2ConfigSpacecfg(
hyp[k] = hp_k.sequence[hyp[k]]
return Configuration(configuration_space=config_space, values=hyp)

def ConfigSpacecfg2HEBOcfg(config: Configuration) -> pd.DataFrame:
"""Convert ConfigSpace config to HEBO suggestion

def ConfigSpacecfg2HEBOcfg(config: Configuration) -> pd.DataFrame: # noqa: N802
"""Convert ConfigSpace config to HEBO suggestion.
Parameters
----------
config : Configuration
Configuration
Returns
Returns:
-------
pd.DataFrame
Configuration in HEBO format, e.g.
x1 x2
0 2.817594 0.336420
"""
config_dict = dict(config)
rec = pd.DataFrame(config_dict, index=[0])
return rec
return pd.DataFrame(config_dict, index=[0])
71 changes: 2 additions & 69 deletions hydra_plugins/hyper_pbt/hyper_pbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ConfigSpace.hyperparameters import (NormalIntegerHyperparameter,
UniformIntegerHyperparameter)

from hydra_plugins.hypersweeper import HypersweeperSweeper, Info
from hydra_plugins.hypersweeper import Info


class PBT:
Expand Down Expand Up @@ -112,71 +112,4 @@ def tell(self, info, result):

def make_pbt(configspace, pbt_args):
"""Make a PBT instance for optimization."""
return PBT(**pbt_args)


class HyperPBTSweeper(HypersweeperSweeper):
"""Hydra Sweeper for PBT."""

def __init__(
self,
global_config,
global_overrides,
launcher,
optimizer_kwargs,
budget_arg_name,
save_arg_name,
load_arg_name,
n_trials,
cs,
seeds=False,
slurm=False,
slurm_timeout=10,
max_parallelization=0.1,
job_array_size_limit=100,
max_budget=None,
deterministic=True,
base_dir=False,
min_budget=None,
wandb_project=False,
wandb_entity=False,
wandb_tags=None,
maximize=False,
):
"""Initialize the Hypersweeper with PBT as the optimizer."""
if wandb_tags is None:
wandb_tags = ["pbt"]
super().__init__(
global_config=global_config,
global_overrides=global_overrides,
launcher=launcher,
make_optimizer=make_pbt,
optimizer_kwargs=optimizer_kwargs,
budget_arg_name=budget_arg_name,
save_arg_name=save_arg_name,
load_arg_name=load_arg_name,
n_trials=n_trials,
cs=cs,
seeds=seeds,
slurm=slurm,
slurm_timeout=slurm_timeout,
max_parallelization=max_parallelization,
job_array_size_limit=job_array_size_limit,
max_budget=max_budget,
deterministic=deterministic,
base_dir=base_dir,
min_budget=min_budget,
wandb_project=wandb_project,
wandb_entity=wandb_entity,
wandb_tags=wandb_tags,
maximize=maximize,
)
# Save and load target functions
self.checkpoint_tf = True
self.load_tf = True

# Provide HP info to PBT
self.optimizer.categorical_hps = self.categorical_hps
self.optimizer.continuous_hps = self.continuous_hps
self.optimizer.continuous_hps = self.continuous_hps
self.optimizer.hp_bounds = self.hp_bounds
return PBT(configspace, **pbt_args)
3 changes: 3 additions & 0 deletions hydra_plugins/hyper_rs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .config import HyperRSConfig

__all__ = ["HyperRSConfig"]
Loading

0 comments on commit 8405846

Please sign in to comment.