Configuration-Based#

Architecture Configuration#

archai.discrete_search.search_spaces.config.arch_config.build_arch_config(config_dict: Dict[str, Any]) ArchConfig[source]#

Build an ArchConfig object from a sampled config dictionary.

Parameters:

config_dict – Config dictionary

Returns:

ArchConfig object.

class archai.discrete_search.search_spaces.config.arch_config.ArchConfig(config_dict: Dict[str, dict | float | int | str])[source]#

Store architecture configs.

get_used_params() Dict[str, Dict | bool][source]#

Get the parameter usage tree.

Terminal nodes with value True represent architecture parameters that were used by calling ArchConfig.pick(param_name).

Returns:

Used parameters.

pick(param_name: str, default: Any | None = None, record_usage: bool | None = True) Any[source]#

Pick an architecture parameter, possibly recording its usage.

Parameters:
  • param_name – Architecture parameter name

  • default – Default value to return if parameter is not found. If None, an exception is raised.

  • record_usage – If this parameter should be recorded as ‘used’ in ArchConfig._used_params.

Returns:

Parameter value.

to_dict(remove_metadata_info: bool | None = False) OrderedDict[source]#

Convert ArchConfig object to an ordered dictionary.

Parameters:

remove_metadata_info – If keys used to store extra metadata should be removed.

Returns:

Ordered dictionary.

to_file(path: str) None[source]#

Save ArchConfig object to a file.

Parameters:

path – Path to save the file to.

classmethod from_file(path: str) ArchConfig[source]#

Load ArchConfig object from a file.

Parameters:

path – Path to load the file from.

Returns:

ArchConfig object.

class archai.discrete_search.search_spaces.config.arch_config.ArchConfigList(config: OrderedDict)[source]#

Store a list of architecture configs.

pick(param_name: str, record_usage: bool | None = True) None[source]#

Pick an architecture parameter, possibly recording its usage.

Parameters:
  • param_name – Architecture parameter name

  • default – Default value to return if parameter is not found. If None, an exception is raised.

  • record_usage – If this parameter should be recorded as ‘used’ in ArchConfig._used_params.

Returns:

Parameter value.

to_dict(remove_metadata_info: bool | None = False) OrderedDict[source]#

Convert ArchConfig object to an ordered dictionary.

Parameters:

remove_metadata_info – If keys used to store extra metadata should be removed.

Returns:

Ordered dictionary.

Architecture Parameter Tree#

class archai.discrete_search.search_spaces.config.arch_param_tree.ArchParamTree(config_tree: Dict[str, Any])[source]#

Tree of architecture parameters.

property num_archs: int#

Return the number of architectures in the search space.

to_dict(flatten: bool | None = False, deduplicate_params: bool | None = False, remove_constants: bool | None = False) OrderedDict[source]#

Convert the ArchParamTree to an ordered dictionary.

Parameters:
  • flatten – If the output dictionary should be flattened.

  • deduplicate_params – Removes duplicate architecture parameters.

  • remove_constants – Removes attributes that are not architecture params from the output dictionary.

Returns:

Ordered dictionary of architecture parameters.

sample_config(rng: Random | None = None) ArchConfig[source]#

Sample an architecture config from the search param tree.

Parameters:

rng – Random number generator used during sampling. If set to None, random.Random() is used.

Returns:

Sampled architecture config.

get_param_name_list() List[str][source]#

Get list of parameter names in the search space.

Returns:

List of parameter names.

encode_config(config: ArchConfig, track_unused_params: bool | None = True) List[float][source]#

Encode an ArchConfig object into a fixed-length vector of features.

This method should be used after the model object is created.

Parameters:
  • config – Architecture configuration.

  • track_unused_params – If track_unused_params=True, parameters not used during model creation (by calling config.pick) will be represented as float(“NaN”).

Returns:

List of features.

Discrete Choice#

class archai.discrete_search.search_spaces.config.discrete_choice.DiscreteChoice(choices: List[int | float | str], probabilities: List[float] | None = None, encode_strategy: str = 'auto')[source]#
encode(option: Any) List[float][source]#

Encodes the option into a numeric value or a one-hot encoding.

Parameters:

option (Any) – Option to encode.

Returns:

Encoded option.

Return type:

List[float]

random_sample(rng: Random | None = None) Any[source]#

Randomly samples a choice from the discrete set.

Parameters:

rng (Optional[Random], optional) – Random number generator.

Returns:

Randomly sampled choice.

Return type:

Any

Helpers#

archai.discrete_search.search_spaces.config.helpers.repeat_config(config_dict: Dict[str, Any], repeat_times: int | List[int], share_arch: bool | None = False) Dict[str, Any][source]#

Repeats an architecture config a variable number of times.

Parameters:
  • config_dict (Dict[str, Any]) – Config dictionary to repeat.

  • repeat_times (Union[int, List[int]]) – If an integer, the number of times to repeat the config will be treated as constant. If a list of integers, the number of times to repeat the config will be also considered an architecture parameter and will be sampled from the list.

  • share_arch (bool, optional) – Whether to share the architecture parameters across the repeated configs. Defaults to False.

Returns:

Config dictionary with the repeated config.

Return type:

Dict[str, Any]

Search Space#

class archai.discrete_search.search_spaces.config.search_space.ConfigSearchSpace(model_cls: Type[Module], arch_param_tree: ArchParamTree | Callable[[...], ArchParamTree], seed: int | None = None, mutation_prob: float = 0.3, track_unused_params: bool = True, unused_param_value: float = -1.0, hash_archid: bool = True, model_kwargs: Dict[str, Any] | None = None, builder_kwargs: Dict[str, Any] | None = None)[source]#
get_archid(arch_config: ArchConfig) str[source]#

Return the architecture identifier for the given architecture configuration.

Parameters:

arch_config – Architecture configuration.

Returns:

Architecture identifier.

save_arch(model: ArchaiModel, path: str) None[source]#

Save an architecture to a file without saving the weights.

Parameters:
  • model – Model’s architecture to save.

  • file_path – File path to save the architecture.

load_arch(path: str) ArchaiModel[source]#

Load from a file an architecture that was saved using SearchSpace.save_arch().

Parameters:

file_path – File path to load the architecture.

Returns:

Loaded model.

save_model_weights(model: ArchaiModel, path: str) None[source]#

Save the weights of a model.

Parameters:
  • model – Model to save the weights.

  • file_path – File path to save the weights.

load_model_weights(model: ArchaiModel, path: str) None[source]#

Load the weights (created with SearchSpace.save_model_weights()) into a model of the same architecture.

Parameters:
  • model – Model to load the weights.

  • file_path – File path to load the weights.

random_sample() ArchaiModel[source]#

Randomly sample an architecture from the search spaces.

Returns:

Sampled architecture.

mutate(model: ArchaiModel) ArchaiModel[source]#

Mutate an architecture from the search space.

This method should not alter the base model architecture directly, only generate a new one.

Parameters:

arch – Base model.

Returns:

Mutated model.

crossover(model_list: List[ArchaiModel]) ArchaiModel[source]#

Combine a list of architectures into a new one.

Parameters:

arch_list – List of architectures.

Returns:

Resulting model.

encode(model: ArchaiModel) ndarray[source]#

Encode an architecture into a fixed-length vector representation.

Parameters:

arch – Model from the search space.

Returns:

Fixed-length vector representation of arch.

Utilities#

archai.discrete_search.search_spaces.config.utils.flatten_dict(odict: Dict[str, Any]) dict[source]#

Flatten a nested dictionary into a single level dictionary.

Parameters:

odict – Nested dictionary.

Returns:

Flattened dictionary.

archai.discrete_search.search_spaces.config.utils.order_dict_keys(base_dict: OrderedDict, target_dict: Dict[str, Any]) OrderedDict[source]#

Order the keys of a target dictionary based on a base dictionary.

Parameters:
  • base_dict (OrderedDict[str, Any]) – Dictionary with the desired key order.

  • target_dict (Dict[str, Any]) – Dictionary to be ordered.

Returns:

Ordered version of target_dict dictionary.

Return type:

OrderedDict[str, Any]

archai.discrete_search.search_spaces.config.utils.replace_ptree_choices(config_tree: Dict | DiscreteChoice, repl_fn: Callable[[DiscreteChoice], Any]) OrderedDict[source]#

Replace all DiscreteChoice nodes in a tree with the output of a function.

Parameters:
  • config_tree – Tree with DiscreteChoice nodes.

  • repl_fn – Function to replace DiscreteChoice nodes.

Returns:

Replaced tree.

archai.discrete_search.search_spaces.config.utils.replace_ptree_pair_choices(query_tree: Dict | DiscreteChoice, aux_tree: Dict | Any, repl_fn: Callable[[DiscreteChoice, Any], Any]) OrderedDict[source]#

Replace all DiscreteChoice nodes in a tree with the output of a function and an auxilary tree.

Parameters:
  • query_tree – Tree with DiscreteChoice nodes.

  • aux_tree – Auxiliary tree with DiscreteChoice nodes.

  • repl_fn – Function that takes a query_node and an aux_node and returns a replacement for query_node.

Returns:

Replaced tree.