[Python] ConfigSpace 여러 기능 사용해보기

2020. 8. 20. 21:20분석 Python/구현 및 자료

import ConfigSpace as CS

config_space = CS.ConfigurationSpace()
config_space.add_hyperparameter(
    CS.UniformIntegerHyperparameter('hidden_size_1', lower=50, upper=100))
config_space.add_hyperparameter(
    CS.UniformIntegerHyperparameter('hidden_size_2', lower=10, upper=50))
# config_space.add_hyperparameter(
#     CS.UniformIntegerHyperparameter('hidden_size_3', lower=10, upper=100))
config_space.add_hyperparameter(
    CS.UniformFloatHyperparameter("lr", lower=1e-6,upper =1e-2,log=True))
config_space.add_hyperparameter(
    CS.UniformFloatHyperparameter("weight_decay", lower=1e-7,upper =1e-3,log=True))
config_space.add_hyperparameter(
    CS.CategoricalHyperparameter('activation', choices=['selu', 'leaky_relu', 'elu',"prelu","gelu"]))
config_space.sample_configuration()

 

Integer hyperparameters and float hyperparameters

config_space = CS.ConfigurationSpace()
config_space.add_hyperparameter(
    CS.UniformFloatHyperparameter(name='C', lower=-1, upper=1,log=False))
config_space.add_hyperparameter(
    CS.UniformIntegerHyperparameter(name='max_iter', lower=10, upper=100,))
config_space.sample_configuration()

Categorical hyperparameters and conditions

config_space = CS.ConfigurationSpace(seed=1234)
kernel_type = CS.CategoricalHyperparameter(
        name='kernel_type', choices=['linear', 'poly', 'rbf', 'sigmoid'])
degree = CS.UniformIntegerHyperparameter(
        'degree', lower=2, upper=4, default_value=2)
coef0 = CS.UniformFloatHyperparameter(
        name='coef0', lower=0, upper=1, default_value=0.0)
gamma = CS.UniformFloatHyperparameter(
        name='gamma', lower=1e-5, upper=1e2, default_value=1, log=True)
config_space.add_hyperparameters([kernel_type, degree, coef0, gamma])
config_space.sample_configuration()

 

cond_1 = CS.EqualsCondition(degree, kernel_type, 'poly')
cond_2 = CS.OrConjunction(CS.EqualsCondition(coef0, kernel_type, 'poly'),
                          CS.EqualsCondition(coef0, kernel_type, 'sigmoid'))
cond_3 = CS.OrConjunction(CS.EqualsCondition(gamma, kernel_type, 'rbf'),
                          CS.EqualsCondition(gamma, kernel_type, 'poly'),
                          CS.EqualsCondition(gamma, kernel_type, 'sigmoid'))
config_space.add_conditions([cond_2])

kernel_type이 poly일 때나 sigmoid일 때 coef를 사용함.

config_space.sample_configuration()

Forbidden clauses

config_space = CS.ConfigurationSpace()
penalty = CS.CategoricalHyperparameter(
        name="penalty", choices=["l1", "l2"], default_value="l2")
loss = CS.CategoricalHyperparameter(
        name="loss", choices=["hinge", "squared_hinge"], default_value="squared_hinge")
dual = CS.Constant("dual", "False")
config_space.add_hyperparameters([penalty, loss, dual])
config_space.sample_configuration()

 

 

 

penalty_and_loss = CS.ForbiddenAndConjunction(
        CS.ForbiddenEqualsClause(penalty, "l1"),
        CS.ForbiddenEqualsClause(loss, "hinge")
    )
constant_penalty_and_loss = CS.ForbiddenAndConjunction(
        CS.ForbiddenEqualsClause(dual, "False"),
        CS.ForbiddenEqualsClause(penalty, "l2"),
        CS.ForbiddenEqualsClause(loss, "hinge")
    )
penalty_and_dual = CS.ForbiddenAndConjunction(
        CS.ForbiddenEqualsClause(dual, "False"),
        CS.ForbiddenEqualsClause(penalty, "l1")
    )
config_space.add_forbidden_clauses([penalty_and_loss,
                          constant_penalty_and_loss,
                          penalty_and_dual])
  • penalty가 l1 이면서 loss가 hinge 인 것은 금지
  • dual이 False면서 penalty가 l2 이면서 loss가 hinge 인 것은 금지
  • dual이 False면서, penalty가 l1 이면 금지
config_space.sample_configuration()

 

 

 

 

https://automl.github.io/ConfigSpace/master/User-Guide.html#st-example-integer-hyperparameters-and-float-hyperparameters

https://automl.github.io/ConfigSpace/master/API-Doc.html#hyperparameters

728x90