deephyper.hpo.HpProblem#

class deephyper.hpo.HpProblem(config_space=None, seed: int | None = None)[source]#

Bases: object

Class to define an hyperparameter problem.

>>> from deephyper.hpo import HpProblem
>>> problem = HpProblem()
Parameters:
  • config_space (ConfigurationSpace, optional) – In case the HpProblem is defined from a

  • ConfigurationSpace.

Methods

add

Add a component to the configuration space.

add_condition

Add a condition to the problem.

add_conditions

Add a list of conditions to the problem.

add_forbidden_clause

Add a forbidden clause to the problem.

add_hyperparameter

Add an hyperparameter to the HpProblem.

add_hyperparameters

Add a list of hyperparameters.

check_configuration

Check if a configuration is valid.

sample

Sample a list of hyperparameter configuration.

set_constraint_fn

Set the constraint function.

set_sampling_fn

Set the sampling function.

set_seed

Set the random seed of the space.

to_json

Returns a dictionary of the space which can be saved as JSON.

Attributes

default_configuration

The default configuration as a dictionnary.

hyperparameter_names

The list of hyperparameters names.

space

The wrapped ConfigurationSpace object.

add(value, name=None, default_value=None) None[source]#

Add a component to the configuration space.

An added component can be an hyperparameter, a forbidden rule or a condition.

add_condition(condition)[source]#

Add a condition to the problem.

Add a condition to the HpProblem.

>>> from deephyper.hpo import HpProblem
>>> import ConfigSpace as cs
>>> problem = HpProblem()
>>> x = problem.add_hyperparameter((0.0, 10.0), "x")
>>> y = problem.add_hyperparameter((1e-4, 1.0), "y")
>>> problem.add_condition(cs.LessThanCondition(y, x, 1.0))
Parameters:

condition – A ConfigSpace condition.

add_conditions(conditions: list) None[source]#

Add a list of conditions to the problem.

Parameters:

conditions (list) – A list of ConfigSpace conditions.

add_forbidden_clause(clause)[source]#

Add a forbidden clause to the problem.

Add a forbidden clause to the HpProblem.

For example if we want to optimize \(\frac{1}{x}\) where \(x\) cannot be equal to 0:

>>> from deephyper.hpo import HpProblem
>>> import ConfigSpace as cs
>>> problem = HpProblem()
>>> x = problem.add_hyperparameter((0.0, 10.0), "x")
>>> problem.add_forbidden_clause(cs.ForbiddenEqualsClause(x, 0.0))
Parameters:

clause – a ConfigSpace forbidden clause.

add_hyperparameter(value, name: str = None, default_value=None) ConfigSpace.hyperparameters.Hyperparameter[source]#

Add an hyperparameter to the HpProblem.

Hyperparameters can be added to a HpProblem with a short syntax:

>>> problem.add_hyperparameter((0, 10), "discrete", default_value=5)
>>> problem.add_hyperparameter((0.0, 10.0), "real", default_value=5.0)
>>> problem.add_hyperparameter([0, 10], "categorical", default_value=0)

Sampling distributions can be provided:

>>> problem.add_hyperparameter((0.0, 10.0, "log-uniform"), "real", default_value=5.0)

It is also possible to use ConfigSpace Hyperparameters:

>>> import ConfigSpace.hyperparameters as csh
>>> csh_hp = csh.UniformIntegerHyperparameter(
...     name='uni_int', lower=10, upper=100, log=False)
>>> problem.add_hyperparameter(csh_hp)
Parameters:
  • value (tuple or list or ConfigSpace.Hyperparameter) – a valid hyperparametr description.

  • name (str) – The name of the hyperparameter to add.

  • default_value (float or int or str) – A default value for the corresponding hyperparameter.

Returns:

a ConfigSpace Hyperparameter object corresponding to the (value, name, default_value).

Return type:

ConfigSpace.Hyperparameter

add_hyperparameters(hp_list)[source]#

Add a list of hyperparameters.

It can be useful when a list of ConfigSpace.Hyperparameter are defined and we need to add them to the HpProblem.

Parameters:

hp_list (ConfigSpace.Hyperparameter) – a list of ConfigSpace hyperparameters.

Returns:

The list of added hyperparameters.

Return type:

list

check_configuration(parameters: dict, raise_if_not_valid: bool = True) bool[source]#

Check if a configuration is valid.

Parameters:
  • parameters (dict) – the configuration of parameters to test.

  • raise_if_not_valid (bool) – indicate if an error is raised if the configuration of parameters is invalid.

Raises:

ValueError – if the configuration is invalid.

property default_configuration#

The default configuration as a dictionnary.

property hyperparameter_names#

The list of hyperparameters names.

sample(size: int = 1, strict: bool = False, max_trials: int = 5, n_jobs: int = 1) list[dict][source]#

Sample a list of hyperparameter configuration.

Parameters:
  • size (int) – The number of configurations to sample.

  • strict (bool) – If the returned number of samples should be strictly equal to size. Defaults to False.

  • max_trials (int) – The maximum number of sampling trials. Defaults to 5.

  • n_jobs (int) – The number of concurrent threads for sampling flat search space.

Returns:

the list of sampled configurations.

Return type:

list[dict]

set_constraint_fn(fn: callable)[source]#

Set the constraint function.

Example

pb = HpProblem()
pb.add((0.0, 10.0), "x")
pb.add((0.0, 10.0), "y")

def constraint_fn(df: pd.DataFrame) -> pd.Series:
    accept = df["x"] + df["y"] >= 10
    return accept

pb.set_constraint_fn(constraint_fn)

samples = pb.sample(size=100)
df = pd.DataFrame(samples)
assert all(df["x"] + df["y"] >= 10)
set_sampling_fn(fn: callable)[source]#

Set the sampling function.

set_seed(seed: int)[source]#

Set the random seed of the space.

property space: ConfigSpace.ConfigurationSpace#

The wrapped ConfigurationSpace object.

to_json() dict[source]#

Returns a dictionary of the space which can be saved as JSON.