automl / ConfigSpace

Domain specific language for configuration spaces in Python/Cython. Useful for hyperparameter optimization and algorithm configuration.
https://automl.github.io/ConfigSpace/
Other
193 stars 89 forks source link

Question: How to define the nested config space? #339

Closed yiliu30 closed 10 months ago

yiliu30 commented 10 months ago

Following the example in advanced usage, if I need to generate the config for both model A and model B in a single Config, for example:

# one of the sampling configuration:
Configuration(
    Configuration_for_model_A(values={'model': 'A', 'alpha': 0.7799758081188035,}), 
    Configuration_for_model_B(values={'model': 'B', 'kernel': 'flooper', 'kernel_floops': 8,})

Could you provide guidance on implementing it? Thank you.

eddiebergman commented 10 months ago
from ConfigSpace import ConfigurationSpace

top_level = ConfigurationSpace({"model": ["A", "B"]})
model_a_space = ConfigurationSpace({"one": (1, 10), "two": ["red", "green", "blue"]})
model_b_space = ConfigurationSpace({"three": (0.0, 1.0), "four": ["purple", "black"]})

for value, space in [("A", model_a_space), ("B", model_b_space)]:
    top_level.add_configuration_space(
        prefix=f"model:{value}",
        configuration_space=space,
        parent_hyperparameter={"parent": top_level["model"], "value": value},
    )
print(top_level)

Configuration space object:
  Hyperparameters:
    model, Type: Categorical, Choices: {A, B}, Default: A
    model:A:one, Type: UniformInteger, Range: [1, 10], Default: 6
    model:A:two, Type: Categorical, Choices: {red, green, blue}, Default: red
    model:B:four, Type: Categorical, Choices: {purple, black}, Default: purple
    model:B:three, Type: UniformFloat, Range: [0.0, 1.0], Default: 0.5
  Conditions:
    model:A:one | model == 'A'
    model:A:two | model == 'A'
    model:B:four | model == 'B'
    model:B:three | model == 'B'
yiliu30 commented 10 months ago

Hi @eddiebergman, thanks for your quick reply. However, the above answer does not yield a sample including configuration for both A and B.

For example, the first two samples only include the configuration for model B.

for cfg in top_level.sample_configuration(2):
    print(cfg)

Configuration(values={
  'model': 'B',
  'model:B:four': 'purple',
  'model:B:three': 0.3936342825147082,
})
Configuration(values={
  'model': 'B',
  'model:B:four': 'purple',
  'model:B:three': 0.9810660152366504,
})
eddiebergman commented 10 months ago

Did you want to select a config from either A or B or for both A and B. You can control this with the parent_hyperparameter= value

yiliu30 commented 10 months ago

Did you want to select a config from either A or B or for both A and B. You can control this with the parent_hyperparameter= value

Hi, I want to select a config for both A and B.

eddiebergman commented 10 months ago
from ConfigSpace import ConfigurationSpace

top_level = ConfigurationSpace()
model_a_space = ConfigurationSpace({"one": (1, 10), "two": ["red", "green", "blue"]})
model_b_space = ConfigurationSpace({"three": (0.0, 1.0), "four": ["purple", "black"]})

for value, space in [("A", model_a_space), ("B", model_b_space)]:
    top_level.add_configuration_space(
        prefix=f"model:{value}",
        configuration_space=space,
    )
print(top_level)
yiliu30 commented 10 months ago

Thank you for your guidance, it's really helpful :)