utils.sweep

Sweep hyperparameter or argument search spaces.

class utils.sweep.Sweep(search_space: dict, num_combinations: int = None)

Reads a search_space dictionary and generates appropriate parameter combinations.

Parameters:

search_space (dict) – Dictionaries of argument keys and their possible values (or scipy distributions and parameters), grouped by top-level keys specifying the sweep strategy to be used with each. Search spaces are typically stored in the sweep key of a Component configuration dictionary.

Search space parameters are expected to be found under one of four top-level keys:

1. Fixed: The argument takes the same value across all branches. E.g., if settings[“fixed”][“resample”]=100, resample will always take the value 100.

2. Zipped: The values of the arguments listed are iterated over such that the Nth value of any argument is matched with the Nth value of other arguments. Combinations are repeated as many times as it takes to reach the number of combinations dictated by construction or by specification. E.g., if settings[“zipped”][“freq_band”] = [[5, 10], [30, 50]] and … [“resample”] = [50, 100], then self.combinations will have freq_band = [5, 10] with resample = 50 in half the branches, and freq_band = [30, 50] with resample = 100 in the other half.

3. Grid: The values of the keys listed are iterated over such that any value combination is equally represented in the resulting dictionary. Combinations are repeated as many times as it takes to reach the number of combinations dictated by construction or by specification. E.g., if settings[“zipped”][“freq_band”] = [[5, 10], [30, 50]] and …[“resample”] = [50, 100], then self.combinations will have four qualitatively distinct groups, with (freq_band, resample) taking one of the following combined values: ([5, 10], 50), ([5, 10], 100), ([30, 50], 50), ([30, 50], 100).

4. Random: The values of the parameter keys listed are drawn from a scipy.stats distribution given under the key “method”, with the arguments “args”. E.g., if settings[“random”][“resample”][“method”]=”uniform” and …[“args”] = [x, y], then resample values will be drawn from U(x, x+y).

Example

Define a search space and generate a list of parameter combination dictionaries:

>>> s = Sweep(search_space={"fixed": {"a": 4}, "grid": {"b": [6, 7], "c": [8, 9]}}, num_combinations=4)
>>> print(s())
[{'a': 4, 'b': 6, 'c': 8}, {'a': 4, 'b': 6, 'c': 9}, {'a': 4, 'b': 7, 'c': 8}, {'a': 4, 'b': 7, 'c': 9}]

See also

Scipy Distributions

For a list of supported probability distributions and their arguments.

count_combinations()

Counts search space configurations and sets num_combinations if not provided or invalid.

Enforces the following contract w.r.t. the calculated number of combinations (NoC):

1. If NoC was not provided by the user, the calculated value is the minimal one ensuring that every unique parameter combination can be represented.

2. If NoC was provided, the calculated value will be either equal to or greater than the requested number. The calculated value will be greater iff the user’s value will result in an imbalance (i.e., if not every parameter combination can be represented with that NoC). In such cases, the user should intentionally sample from the larger combination dictionary in a way they see fit. Generally, users should keep balancing considerations in mind when designing heterogeneous layers.

static extract_combinations(parameters: dict, mode: str) list[dict]

Extracts all parameter combinations from parameters based on the traversal mode given.

Parameters:
  • parameters (dict) – Dictionary, possibly loaded from YAML, containing parameters and their levels.

  • mode (str) – Combination strategy, “grid” if all parameter permutations should be tested or “zipped” for a linear pass.

See also

Sweep

For a full documentation of parameter sweep strategies.

generate_combinations() list[dict]

Determine the argument combinations defining this sweep, update the combinations attribute, and return its value for potential use by the calling function.

heterogenize(obj: Any, unravel: bool = True)

Initializes attribute tensors heterogeneously based on a given sweep search dictionary.

Parameters:
  • obj (Component) – Sapicore engine component whose parameters will be diversified (currently, either Ensemble or Synapse).

  • unravel (bool) – When the property is a 2D tensor, dictates whether combination values should be assigned to individual elements (True, default) or overwrite entire rows (False).