def test_sample_types_conditional(self): import ConfigSpace as cs import numpy as np from deephyper.evaluator import Evaluator from deephyper.problem import HpProblem from deephyper.search.hps import CBO problem = HpProblem() # choices choice = problem.add_hyperparameter( name="choice", value=["choice1", "choice2"], ) # integers x1_int = problem.add_hyperparameter(name="x1_int", value=(1, 10)) x2_int = problem.add_hyperparameter(name="x2_int", value=(1, 10)) # conditions cond_1 = cs.EqualsCondition(x1_int, choice, "choice1") cond_2 = cs.EqualsCondition(x2_int, choice, "choice2") problem.add_condition(cond_1) problem.add_condition(cond_2) def run(config): if config["choice"] == "choice1": assert np.issubdtype(type(config["x1_int"]), np.integer) else: assert np.issubdtype(type(config["x2_int"]), np.integer) return 0 create_evaluator = lambda: Evaluator.create(run, method="serial") CBO(problem, create_evaluator(), random_state=42, surrogate_model="DUMMY").search(10)
Problem = HpProblem(seed=45) func = Problem.add_hyperparameter(name="func", value=["f1", "f2"]) # f1 variables f1_x = Problem.add_hyperparameter(name="f1_x", value=(0.0, 1.0, "uniform")) f1_y = Problem.add_hyperparameter(name="f1_y", value=(0.0, 1.0, "uniform")) # f2 variables f2_x = Problem.add_hyperparameter(name="f2_x", value=(0.0, 1.0, "uniform")) f2_y = Problem.add_hyperparameter(name="f2_y", value=(0.0, 1.0, "uniform")) cond_f1_x = cs.EqualsCondition(f1_x, func, "f1") cond_f1_y = cs.EqualsCondition(f1_y, func, "f1") Problem.add_condition(cond_f1_x) Problem.add_condition(cond_f1_y) cond_f2_x = cs.EqualsCondition(f2_x, func, "f2") cond_f2_y = cs.EqualsCondition(f2_y, func, "f2") Problem.add_condition(cond_f2_x) Problem.add_condition(cond_f2_y) # Definition of the function which runs the model def run(param_dict): func = param_dict["func"] if func == "f1": return param_dict["f1_x"] + param_dict["f1_y"] else:
classifier = Problem.add_hyperparameter( name="classifier", value=["RandomForest", "Logistic", "AdaBoost", "KNeighbors", "MLP", "SVC", "XGBoost"], ) # n_estimators n_estimators = Problem.add_hyperparameter( name="n_estimators", value=(1, 2000, "log-uniform") ) cond_n_estimators = cs.OrConjunction( cs.EqualsCondition(n_estimators, classifier, "RandomForest"), cs.EqualsCondition(n_estimators, classifier, "AdaBoost"), ) Problem.add_condition(cond_n_estimators) # max_depth max_depth = Problem.add_hyperparameter(name="max_depth", value=(2, 100, "log-uniform")) cond_max_depth = cs.EqualsCondition(max_depth, classifier, "RandomForest") Problem.add_condition(cond_max_depth) # n_neighbors n_neighbors = Problem.add_hyperparameter(name="n_neighbors", value=(1, 100)) cond_n_neighbors = cs.EqualsCondition(n_neighbors, classifier, "KNeighbors") Problem.add_condition(cond_n_neighbors)
Problem.add_dim('units2', (1, 64)) # int in range 1-64 Problem.add_dim('dropout1', (0.0, 1.0)) # float in range 0-1 Problem.add_dim('dropout2', (0.0, 1.0)) # float in range 0-1 Problem.add_dim('batch_size', (5, 500)) # int in range 5-500 Problem.add_dim('log10_learning_rate', (-5.0, 0.0)) # float lr range from 10^-5 to 1 # one of ['relu', ..., ] Problem.add_dim('activation', ['relu', 'elu', 'selu', 'tanh']) optimizer = Problem.add_dim('optimizer', [ 'Adam', 'RMSprop', 'SGD', 'Nadam', 'Adagrad' ]) # Only vary momentum if optimizer is SGD momentum = Problem.add_dim("momentum", (0.5, 0.9)) Problem.add_condition(cs.EqualsCondition(momentum, optimizer, "SGD")) # Add a starting point to try first Problem.add_starting_point( units1=16, units2=32, dropout1=0.0, dropout2=0.0, batch_size=16, activation='relu', optimizer='SGD', log10_learning_rate=-3.0, momentum=0.5, )
"RandomForest", "Linear", "AdaBoost", "KNeighbors", "MLP", "SVR", "XGBoost" ], ) # n_estimators n_estimators = problem_autosklearn1.add_hyperparameter(name="n_estimators", value=(1, 2000, "log-uniform")) cond_n_estimators = cs.OrConjunction( cs.EqualsCondition(n_estimators, regressor, "RandomForest"), cs.EqualsCondition(n_estimators, regressor, "AdaBoost"), ) problem_autosklearn1.add_condition(cond_n_estimators) # max_depth max_depth = problem_autosklearn1.add_hyperparameter(name="max_depth", value=(2, 100, "log-uniform")) cond_max_depth = cs.EqualsCondition(max_depth, regressor, "RandomForest") problem_autosklearn1.add_condition(cond_max_depth) # n_neighbors n_neighbors = problem_autosklearn1.add_hyperparameter(name="n_neighbors", value=(1, 100)) cond_n_neighbors = cs.EqualsCondition(n_neighbors, regressor, "KNeighbors")
criterion = Problem.add_hyperparameter( name="criterion", value=["friedman_mse", "mse", "mae", "gini", "entropy"], default_value="gini", ) # GradientBoosting loss = Problem.add_hyperparameter(name="loss", value=["deviance", "exponential"]) learning_rate = Problem.add_hyperparameter(name="learning_rate", value=(0.01, 1.0)) subsample = Problem.add_hyperparameter(name="subsample", value=(0.01, 1.0)) gradient_boosting_hp = [loss, learning_rate, subsample] for hp_i in gradient_boosting_hp: Problem.add_condition( cs.EqualsCondition(hp_i, classifier, "GradientBoosting")) forbidden_criterion_rf = cs.ForbiddenAndConjunction( cs.ForbiddenEqualsClause(classifier, "RandomForest"), cs.ForbiddenInClause(criterion, ["friedman_mse", "mse", "mae"]), ) Problem.add_forbidden_clause(forbidden_criterion_rf) forbidden_criterion_gb = cs.ForbiddenAndConjunction( cs.ForbiddenEqualsClause(classifier, "GradientBoosting"), cs.ForbiddenInClause(criterion, ["gini", "entropy"]), ) Problem.add_forbidden_clause(forbidden_criterion_gb) if __name__ == "__main__": print(Problem)