deephyper.hpo.HpProblem#
- class deephyper.hpo.HpProblem(config_space=None, seed: int | None = None)[source]#
Bases:
objectClass to define an hyperparameter problem.
>>> from deephyper.hpo import HpProblem >>> problem = HpProblem()
- Parameters:
config_space (ConfigurationSpace, optional) – In case the
HpProblemis defined from aConfigurationSpace.
Methods
Add a component to the configuration space.
Add a condition to the problem.
Add a list of conditions to the problem.
Add a forbidden clause to the problem.
Add an hyperparameter to the
HpProblem.Add a list of hyperparameters.
Check if a configuration is valid.
Sample a list of hyperparameter configuration.
Set the constraint function.
Set the sampling function.
Set the random seed of the space.
Returns a dictionary of the space which can be saved as JSON.
Attributes
The default configuration as a dictionnary.
The list of hyperparameters names.
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
HpProblemwith 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:
- Returns:
a ConfigSpace
Hyperparameterobject 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.Hyperparameterare defined and we need to add them to theHpProblem.- Parameters:
hp_list (ConfigSpace.Hyperparameter) – a list of ConfigSpace hyperparameters.
- Returns:
The list of added hyperparameters.
- Return type:
- check_configuration(parameters: dict, raise_if_not_valid: bool = True) bool[source]#
Check if a configuration is valid.
- Parameters:
- 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 toFalse.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:
- 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)
- property space: ConfigSpace.ConfigurationSpace#
The wrapped ConfigurationSpace object.