smac.configspace package

class smac.configspace.CategoricalHyperparameter

Bases: ConfigSpace.hyperparameters.Hyperparameter

A categorical hyperparameter.

Its values are sampled from a set of values.

None is a forbidden value, please use a string constant instead and parse it in your own code, see here <https://github.com/automl/ConfigSpace/issues/159>_ for further details.

Example

>>> import ConfigSpace as CS
>>> import ConfigSpace.hyperparameters as CSH
>>> cs = CS.ConfigurationSpace(seed=1)
>>> cat_hp = CSH.CategoricalHyperparameter('cat_hp', choices=['red', 'green', 'blue'])
>>> cs.add_hyperparameter(cat_hp)
cat_hp, Type: Categorical, Choices: {red, green, blue}, Default: red
Parameters
  • name (str) – Name of the hyperparameter, with which it can be accessed

  • choices (list or tuple with str, float, int) – Collection of values to sample hyperparameter from

  • default_value (int, float, str, optional) – Sets the default value of the hyperparameter to a given value

  • meta (Dict, optional) – Field for holding meta data provided by the user. Not used by the configuration space.

  • weights ((list[float], optional)) – List of weights for the choices to be used (after normalization) as probabilities during sampling, no negative values allowed

_get_probabilities()
_inverse_transform()
_sample()
_transform()
_transform_scalar()
_transform_vector()
allow_greater_less_comparison()
check_default()
choices
compare()
compare_vector()
get_neighbors()
get_num_neighbors()
has_neighbors()

Check whether the given value is a legal value for the vector representation of this hyperparameter.

Parameters

value – the vector value to check

Returns

True if the given value is a legal vector value, otherwise False

Return type

bool

num_choices
probabilities
class smac.configspace.Configuration(configuration_space: ConfigSpace.configuration_space.ConfigurationSpace, values: Union[None, Dict[str, Union[str, float, int]]] = None, vector: Union[None, numpy.ndarray] = None, allow_inactive_with_values: bool = False, origin: Any = None, config_id: Optional[int] = None)

Bases: object

Class for a single configuration.

The Configuration object holds for all active hyperparameters a value. While the ConfigurationSpace stores the definitions for the hyperparameters (value ranges, constraints,…), a Configuration object is more an instance of it. Parameters of a Configuration object can be accessed and modified similar to python dictionaries (c.f. Guide).

Parameters
  • configuration_space (ConfigurationSpace) –

  • values (dict, optional) – A dictionary with pairs (hyperparameter_name, value), where value is a legal value of the hyperparameter in the above configuration_space

  • vector (np.ndarray, optional) – A numpy array for efficient representation. Either values or vector has to be given

  • allow_inactive_with_values (bool, optional) – Whether an Exception will be raised if a value for an inactive hyperparameter is given. Default is to raise an Exception. Default to False

  • origin (Any, optional) – Store information about the origin of this configuration. Default to None

  • config_id (int, optional) – Integer configuration ID which can be used by a program using the ConfigSpace package.

_populate_values() → None
get(item: unicode, default: Union[None, Any] = None) → Union[None, Any]

Return for a given hyperparameter name item the value of this hyperparameter. default if the hyperparameter name doesn’t exist.

Parameters
  • item (str) – Name of the desired hyperparameter

  • default (None, Any) –

Returns

Value of the hyperparameter

Return type

Any, None

get_array() → numpy.ndarray

Return the internal vector representation of the Configuration. All continuous values are scaled between zero and one.

Returns

The vector representation of the configuration

Return type

numpy.ndarray

get_dictionary() → Dict[str, Union[str, float, int]]

Return a representation of the Configuration in dictionary form.

Returns

Configuration as dictionary

Return type

dict

is_valid_configuration() → None

Check if the object is a valid Configuration. Raise an error if configuration is not valid.

keys() → List[str]

Cache the keys to speed up the process of retrieving the keys.

Returns

list of keys

Return type

list(str)

class smac.configspace.ConfigurationSpace(name: Optional[str] = None, seed: Optional[int] = None, meta: Optional[Dict] = None)

Bases: object

A collection-like object containing a set of hyperparameter definitions and conditions.

A configuration space organizes all hyperparameters and its conditions as well as its forbidden clauses. Configurations can be sampled from this configuration space. As underlying data structure, the configuration space uses a tree-based approach to represent the conditions and restrictions between hyperparameters.

Parameters
  • name (str, optional) – Name of the configuration space

  • seed (int, optional) – random seed

  • meta (dict, optional) – Field for holding meta data provided by the user. Not used by the configuration space.

_add_edge(parent_node: ConfigSpace.hyperparameters.Hyperparameter, child_node: ConfigSpace.hyperparameters.Hyperparameter, condition: ConfigSpace.conditions.ConditionComponent) → None
_add_hyperparameter(hyperparameter: ConfigSpace.hyperparameters.Hyperparameter) → None
_check_condition(child_node: ConfigSpace.hyperparameters.Hyperparameter, condition: ConfigSpace.conditions.ConditionComponent) → None
_check_configuration_rigorous(configuration: ConfigSpace.configuration_space.Configuration, allow_inactive_with_values: bool = False) → None
_check_default_configuration() → ConfigSpace.configuration_space.Configuration
_check_edges(edges: List[Tuple[ConfigSpace.hyperparameters.Hyperparameter, ConfigSpace.hyperparameters.Hyperparameter]], values: List[Union[float, str, int]]) → None
_check_forbidden(vector: numpy.ndarray) → None
_check_forbidden_component(clause: ConfigSpace.forbidden.AbstractForbiddenComponent)
_create_tmp_dag() → ConfigSpace.nx.classes.digraph.DiGraph
_get_child_conditions_of(name: unicode) → List[ConfigSpace.conditions.AbstractCondition]
_get_children_of(name: unicode) → List[ConfigSpace.hyperparameters.Hyperparameter]
_get_parent_conditions_of(name: unicode) → List[ConfigSpace.conditions.AbstractCondition]
_get_parents_of(name: unicode) → List[ConfigSpace.hyperparameters.Hyperparameter]

Return the parent hyperparameters of a given hyperparameter.

Parameters

name (str) –

Returns

List with all parent hyperparameters

Return type

list

_sort_hyperparameters() → None
_update_cache()
add_condition(condition: ConfigSpace.conditions.ConditionComponent) → ConfigSpace.conditions.ConditionComponent

Add a condition to the configuration space. Check if adding the condition is legal:

  • The parent in a condition statement must exist

  • The condition must add no cycles

The internal array keeps track of all edges which must be added to the DiGraph; if the checks don’t raise any Exception, these edges are finally added at the end of the function.

Parameters

condition (Conditions) – Condition to add

Returns

Same condition as input

Return type

Conditions

add_conditions(conditions: List[ConfigSpace.conditions.ConditionComponent]) → List[ConfigSpace.conditions.ConditionComponent]

Add a list of conditions to the configuration space. They must be legal. Take a look at add_condition().

Parameters

conditions (list(Conditions)) – collection of conditions to add

Returns

Same as input conditions

Return type

list(Conditions)

add_configuration_space(prefix: unicode, configuration_space: ConfigurationSpace, delimiter: unicode = ':', parent_hyperparameter: ConfigSpace.hyperparameters.Hyperparameter = None)ConfigurationSpace

Combine two configuration space by adding one the other configuration space. The contents of the configuration space, which should be added, are renamed to prefix + delimiter + old_name.

Parameters
  • prefix (str) – The prefix for the renamed hyperparameter | conditions | forbidden clauses

  • configuration_space (ConfigurationSpace) – The configuration space which should be added

  • delimiter (str, optional) – Defaults to ‘:’

  • parent_hyperparameter (Hyperparameters, optional) – Adds for each new hyperparameter the condition, that parent_hyperparameter is active

Returns

The configuration space, which was added

Return type

ConfigurationSpace

add_forbidden_clause(clause: ConfigSpace.forbidden.AbstractForbiddenComponent) → ConfigSpace.forbidden.AbstractForbiddenComponent

Add a forbidden clause to the configuration space.

Parameters

clause (Forbidden clauses) – Forbidden clause to add

Returns

Same as input forbidden clause

Return type

Forbidden clauses

add_forbidden_clauses(clauses: List[ConfigSpace.forbidden.AbstractForbiddenComponent]) → List[ConfigSpace.forbidden.AbstractForbiddenComponent]

Add a list of forbidden clauses to the configuration space.

Parameters

clauses (list(Forbidden clauses)) – Collection of forbidden clauses to add

Returns

Same as input clauses

Return type

list(Forbidden clauses)

add_hyperparameter(hyperparameter: ConfigSpace.hyperparameters.Hyperparameter) → ConfigSpace.hyperparameters.Hyperparameter

Add a hyperparameter to the configuration space.

Parameters

hyperparameter (Hyperparameters) – The hyperparameter to add

Returns

The added hyperparameter

Return type

Hyperparameters

add_hyperparameters(hyperparameters: List[ConfigSpace.hyperparameters.Hyperparameter]) → List[ConfigSpace.hyperparameters.Hyperparameter]

Add hyperparameters to the configuration space.

Parameters

hyperparameters (list(Hyperparameters)) – Collection of hyperparameters to add

Returns

List of added hyperparameters (same as input)

Return type

list(Hyperparameters)

check_configuration(configuration: ConfigSpace.configuration_space.Configuration) → None

Check if a configuration is legal. Raises an error if not.

Parameters

configuration (Configuration) – Configuration to check

check_configuration_vector_representation(vector: numpy.ndarray) → None

Raise error if configuration in vector representation is not legal.

Parameters

vector (np.ndarray) – Configuration in vector representation

generate_all_continuous_from_bounds(bounds: List[List[Any]]) → None

Generate UniformFloatHyperparameter from a list containing lists with lower and upper bounds. The generated hyperparameters are added to the configuration space.

Parameters

bounds (list[tuple([Any, Any])]) – List containing lists with two elements: lower and upper bound

get_active_hyperparameters(configuration: ConfigSpace.configuration_space.Configuration) → Set

Return a set of active hyperparameter for a given configuration.

Parameters

configuration (Configuration) – Configuration for which the active hyperparameter are returned

Returns

The set of all active hyperparameter

Return type

set(Configuration)

get_all_conditional_hyperparameters() → List[str]

Return a list with names of all conditional hyperparameters.

Returns

List with all conditional hyperparameter

Return type

list[Hyperparameters]

get_all_unconditional_hyperparameters() → List[str]

Return a list with names of unconditional hyperparameters.

Returns

List with all parent hyperparameters, which are not part of a condition

Return type

list[Hyperparameters]

get_child_conditions_of(name: Union[str, ConfigSpace.hyperparameters.Hyperparameter]) → List[ConfigSpace.conditions.AbstractCondition]

Return a list with conditions of all children of a given hyperparameter referenced by its name.

Parameters

name (str, Hyperparameters) – Hyperparameter or its name, for which conditions are requested

Returns

List with the conditions on the children of the given hyperparameter

Return type

list(Conditions)

get_children_of(name: Union[str, ConfigSpace.hyperparameters.Hyperparameter]) → List[ConfigSpace.hyperparameters.Hyperparameter]

Return a list with all children of a given hyperparameter.

Parameters

name (str, Hyperparameters) – Hyperparameter or its name, for which all children are requested

Returns

Children of the hyperparameter

Return type

list(Hyperparameters)

get_conditions() → List[ConfigSpace.conditions.AbstractCondition]

Return a list with all conditions from the configuration space.

Returns

Conditions of the configuration space

Return type

list(Conditions)

get_default_configuration() → ConfigSpace.configuration_space.Configuration

Return a configuration containing hyperparameters with default values.

Returns

Configuration with the set default values

Return type

Configuration

get_forbiddens() → List[ConfigSpace.forbidden.AbstractForbiddenComponent]

Return a list with all forbidden clauses from the configuration space.

Returns

List with the forbidden clauses

Return type

list(Forbidden clauses)

get_hyperparameter(name: unicode) → ConfigSpace.hyperparameters.Hyperparameter

Gives the hyperparameter from the configuration space given its name.

Parameters

name (str) – Name of the searched hyperparameter

Returns

Hyperparameter with the name name

Return type

Hyperparameters

get_hyperparameter_by_idx(idx: int) → unicode

Return the name of a hyperparameter from the configuration space given its id.

Parameters

idx (int) – Id of a hyperparameter

Returns

Name of the hyperparameter

Return type

str

get_hyperparameter_names() → List[str]

Return a list with all names of hyperparameter, which are contained in the configuration space object.

Returns

List of hyperparameter names

Return type

list(str)

get_hyperparameters() → List[ConfigSpace.hyperparameters.Hyperparameter]

Return a list with all the hyperparameter, which are contained in the configuration space object.

Returns

A list with all hyperparameters stored in the configuration space object

Return type

list(Hyperparameters)

get_idx_by_hyperparameter_name(name: unicode) → int

Return the id of a hyperparameter by its name.

Parameters

name (str) – Name of a hyperparameter

Returns

Id of the hyperparameter with name name

Return type

int

get_parent_conditions_of(name: Union[str, ConfigSpace.hyperparameters.Hyperparameter]) → List[ConfigSpace.conditions.AbstractCondition]

Return a list with conditions of all parents of a given hyperparameter.

Parameters

name (str, Hyperparameters) – Can either be the name of a hyperparameter or the hyperparameter object

Returns

List with all conditions on parent hyperparameters

Return type

List[Conditions]

get_parents_of(name: Union[str, ConfigSpace.hyperparameters.Hyperparameter]) → List[ConfigSpace.hyperparameters.Hyperparameter]

Return the parent hyperparameters of a given hyperparameter.

Parameters

name (str, Hyperparameters) – Can either be the name of a hyperparameter or the hyperparameter object

Returns

List with all parent hyperparameters

Return type

list[Conditions]

sample_configuration(size: int = 1) → Union[ConfigSpace.configuration_space.Configuration, List[ConfigSpace.configuration_space.Configuration]]

Sample size configurations from the configuration space object.

Parameters

size (int, optional) – Number of configurations to sample. Default to 1

Returns

A single configuration if size 1 else a list of Configurations

Return type

Configuration, List[Configuration]

seed(seed: int) → None

Set the random seed to a number.

Parameters

seed (int) – The random seed

class smac.configspace.Constant

Bases: ConfigSpace.hyperparameters.Hyperparameter

Representing a constant hyperparameter in the configuration space.

By sampling from the configuration space each time only a single, constant value will be drawn from this hyperparameter.

Parameters
  • name (str) – Name of the hyperparameter, with which it can be accessed

  • value (str, int, float) – value to sample hyperparameter from

  • meta (Dict, optional) – Field for holding meta data provided by the user. Not used by the configuration space.

_inverse_transform()
_sample()
_transform()
_transform_scalar()
_transform_vector()
get_neighbors()
get_num_neighbors()
has_neighbors()

Check whether the given value is a legal value for the vector representation of this hyperparameter.

Parameters

value – the vector value to check

Returns

True if the given value is a legal vector value, otherwise False

Return type

bool

value
class smac.configspace.InCondition

Bases: ConfigSpace.conditions.AbstractCondition

Hyperparameter child is conditional on the parent hyperparameter being in a set of values.

Example

>>> import ConfigSpace as CS
>>> import ConfigSpace.hyperparameters as CSH
>>> cs = CS.ConfigurationSpace(seed=1)
>>> a = CSH.UniformIntegerHyperparameter('a', lower=0, upper=10)
>>> b = CSH.UniformFloatHyperparameter('b', lower=1., upper=8., log=False)
>>> cs.add_hyperparameters([a, b])
[a, Type: UniformInteger, Range: [0, 10], Default: 5, b, Type: ...]

make b an active hyperparameter if a is in the set [1, 2, 3, 4]

>>> cond = CS.InCondition(b, a, [1, 2, 3, 4])
>>> cs.add_condition(cond)
b | a in {1, 2, 3, 4}
Parameters
  • child (Hyperparameters) – This hyperparameter will be sampled in the configspace, if the InCondition is satisfied

  • parent (Hyperparameters) – The hyperparameter, which has to satisfy the InCondition

  • values (list(str, float, int)) – Collection of values, which the parent is compared to

_evaluate()
values
vector_values
class smac.configspace.UniformFloatHyperparameter

Bases: ConfigSpace.hyperparameters.FloatHyperparameter

A float hyperparameter.

Its values are sampled from a uniform distribution with values from lower to upper.

Example

>>> import ConfigSpace as CS
>>> import ConfigSpace.hyperparameters as CSH
>>> cs = CS.ConfigurationSpace(seed=1)
>>> uniform_float_hp = CSH.UniformFloatHyperparameter('uni_float', lower=10,
...                                                   upper=100, log=False)
>>> cs.add_hyperparameter(uniform_float_hp)
uni_float, Type: UniformFloat, Range: [10.0, 100.0], Default: 55.0
Parameters
  • name (str) – Name of the hyperparameter, with which it can be accessed

  • lower (int, floor) – Lower bound of a range of values from which the hyperparameter will be sampled

  • upper (int, float) – Upper bound

  • default_value (int, float, optional) – Sets the default value of a hyperparameter to a given value

  • q (int, float, optional) – Quantization factor

  • log (bool, optional) – If True, the values of the hyperparameter will be sampled on a logarithmic scale. Default to False

  • meta (Dict, optional) – Field for holding meta data provided by the user. Not used by the configuration space.

_inverse_transform()
_sample()
_transform_scalar()
_transform_vector()
check_default()
get_neighbors()

Check whether the given value is a legal value for the vector representation of this hyperparameter.

Parameters

value – the vector value to check

Returns

True if the given value is a legal vector value, otherwise False

Return type

bool

to_integer()
class smac.configspace.UniformIntegerHyperparameter

Bases: ConfigSpace.hyperparameters.IntegerHyperparameter

An integer hyperparameter.

Its values are sampled from a uniform distribution with bounds lower and upper.

Example

>>> import ConfigSpace as CS
>>> import ConfigSpace.hyperparameters as CSH
>>> cs = CS.ConfigurationSpace(seed=1)
>>> uniform_integer_hp = CSH.UniformIntegerHyperparameter(name='uni_int', lower=10,
...                                                       upper=100, log=False)
>>> cs.add_hyperparameter(uniform_integer_hp)
uni_int, Type: UniformInteger, Range: [10, 100], Default: 55
Parameters
  • name (str) – Name of the hyperparameter with which it can be accessed

  • lower (int) – Lower bound of a range of values from which the hyperparameter will be sampled

  • upper (int) – upper bound

  • default_value (int, optional) – Sets the default value of a hyperparameter to a given value

  • q (int, optional) – Quantization factor

  • log (bool, optional) – If True, the values of the hyperparameter will be sampled on a logarithmic scale. Defaults to False

  • meta (Dict, optional) – Field for holding meta data provided by the user. Not used by the configuration space.

_inverse_transform()
_sample()
_transform_scalar()
_transform_vector()
check_default()
get_neighbors()
get_num_neighbors()
has_neighbors()

Check whether the given value is a legal value for the vector representation of this hyperparameter.

Parameters

value – the vector value to check

Returns

True if the given value is a legal vector value, otherwise False

Return type

bool

smac.configspace.convert_configurations_to_array(configs: List[ConfigSpace.configuration_space.Configuration]) → numpy.ndarray[source]

Impute inactive hyperparameters in configurations with their default.

Necessary to apply an EPM to the data.

Parameters

configs (List[Configuration]) – List of configuration objects.

Returns

Return type

np.ndarray

smac.configspace.get_one_exchange_neighbourhood()

Return all configurations in a one-exchange neighborhood.

The method is implemented as defined by: Frank Hutter, Holger H. Hoos and Kevin Leyton-Brown Sequential Model-Based Optimization for General Algorithm Configuration In Proceedings of the conference on Learning and Intelligent Optimization(LION 5)

Parameters
  • configuration (Configuration) – for this Configuration object num_neighbors neighbors are computed

  • seed (int) – Sets the random seed to a fixed value

  • num_neighbors ((int, optional)) – number of configurations, which are sampled from the neighbourhood of the input configuration

  • stdev ((float, optional)) – The standard deviation is used to determine the neigbours of UniformFloatHyperparameter and UniformIntegerHyperparameter.

Returns

It contains configurations, with values being situated around the given configuration.

Return type

Generator