コード例 #1
0
def string2config_space(space_desc: str):
    line_list = space_desc.split('\n')
    cur_line = 2
    cs = ConfigurationSpace()
    status = 'hp'
    hp_list = list()
    while cur_line != len(line_list) - 1:
        line_content = line_list[cur_line]
        if line_content == '  Conditions:':
            hp_dict = {hp.name: hp for hp in hp_list}
            status = 'cond'
        elif line_content == '  Forbidden Clauses:':
            status = 'bid'
        else:
            if status == 'hp':
                hp = string2hyperparameter(line_content)
                hp_list.append(hp)
                cs.add_hyperparameter(hp)
            elif status == 'cond':
                cond = string2condition(line_content, hp_dict)
                cs.add_condition(cond)
            else:
                forbid = string2forbidden(line_content, hp_dict)
                cs.add_forbidden_clause(forbid)
        cur_line += 1
    return cs
コード例 #2
0
use_degree = InCondition(child=degree, parent=kernel, values=["poly"])
use_coef0 = InCondition(child=coef0, parent=kernel, values=["poly", "sigmoid"])
cs.add_conditions([use_degree, use_coef0])

# This also works for parameters that are a mix of categorical and values from a range of numbers
# For example, gamma can be either "auto" or a fixed float
gamma = CategoricalHyperparameter(
    "gamma", ["auto", "value"],
    default_value="auto")  # only rbf, poly, sigmoid
gamma_value = UniformFloatHyperparameter("gamma_value",
                                         0.0001,
                                         8,
                                         default_value=1)
cs.add_hyperparameters([gamma, gamma_value])
# We only activate gamma_value if gamma is set to "value"
cs.add_condition(InCondition(child=gamma_value, parent=gamma,
                             values=["value"]))
# And again we can restrict the use of gamma in general to the choice of the kernel
cs.add_condition(
    InCondition(child=gamma, parent=kernel, values=["rbf", "poly", "sigmoid"]))

# Example call of the function
# It returns: Status, Cost, Runtime, Additional Infos
def_value = svm_from_cfg(cs.get_default_configuration())
print("Default Value: %.2f" % (def_value))

# Optimize, using a SMAC-object
print("Optimizing! Depending on your machine, this might take a few minutes.")
bo = BayesianOptimization(svm_from_cfg,
                          cs,
                          max_runs=30,
                          time_limit_per_trial=30,