vis4d.config.replicator

Replication methods to perform different parameters sweeps.

Functions

iterable_sampler(samples)

Creates a sampler from an iterable.

linspace_sampler(min_value, max_value[, n_steps])

Creates a linear space sampler.

logspace_sampler(min_exponent, max_exponent)

Creates a logarithmic space sampler.

replicate_config(configuration, sampling_args)

Function used to replicate a config.

iterable_sampler(samples)[source]

Creates a sampler from an iterable.

This fuction returns a method that returns a generator that iterates over all values provided in the ‘samples’ iterable.

Parameters:

samples (Iterable[Any]) – Iterable over which to sample.

Returns:

Function that

returns a generator which iterates over all elements in the given iterable.

Return type:

Callable[[], Generator[Any, None, None]]

linspace_sampler(min_value, max_value, n_steps=1)[source]

Creates a linear space sampler.

This fuction returns a method that returns a generator that iterates from min_value to max_value in n_steps.

Parameters:
  • min_value (float) – Lower value bound

  • max_value (float) – Upper value bound

  • n_steps (int, optional) – Number of steps. Defaults to 1.

Returns:

Function that

returns a generator which iterates from min to max in n_steps.

Return type:

Callable[[], Generator[float, None, None]]

logspace_sampler(min_exponent, max_exponent, n_steps=1, base=10)[source]

Creates a logarithmic space sampler.

This fuction returns a method that returns a generator that iterates from base^min_exponent to base^max_exponent in n_steps.

Parameters:
  • min_exponent (float) – Lower value bound

  • max_exponent (float) – Upper value bound

  • n_steps (int, optional) – Number of steps. Defaults to 1.

  • base (float) – Base value for exponential calculation. Defaults to 10.

Returns:

Function that

returns a generator which iterates from 10^min to 10^max in n_steps.

Return type:

Callable[[], Generator[float, None, None]]

replicate_config(configuration, sampling_args, method='grid', fstring='')[source]

Function used to replicate a config.

This function takes a ConfigDict and a dict with (key: generator) entries. It will yield, multiple modified config dicts assigned with different values defined in the sampling_args dictionary.

Example: >>> config = ConfigDict({“trainer”: {“lr”: 0.2, “bs”: 2}}) >>> replicated_config = replicate_config(config, >>> sampling_args = [(“trainer.lr”, linspace_sampler(0.01, 0.1, 3))], >>> method = “grid” >>> ) >>> for c in replicated_config: >>> print(c)

Will print:

trainer: bs: 2 lr: 0.01 trainer: bs: 2 lr: 0.055 trainer: bs: 2 lr: 0.1

NOTE, the config dict instance that will be returned will be mutable and continuously updated to preserve references. In the code above, executing >>> print(list(replicated_config)) Prints:

trainer: bs: 2 lr: 0.1 trainer: bs: 2 lr: 0.1 trainer: bs: 2 lr: 0.1

Please resolve the reference and copy the dict if you need a list: >>> print([c.copy_and_resolve_references() for c in replicated_config])

Parameters:
  • configuration (ConfigDict) – Configuration to replicate

  • sampling_args (dict[str, Callable[[], Any]]) – The queue, that contains (key, iterator) pairs where the iterator yields the values which should be assigned to the key.

  • method (str) – What replication method to use. Currently only ‘grid’ and ‘linear’ is supported. Grid combines the sampling arguments in a grid wise fashion ([1,2],[3,4] -> [1,3],[1,4],[2,3],[2,4]) whereas ‘linear’ will only select elements at the same index ([1,2],[3,4]->[1,3],[2,4]).

  • fstring (str) – Format string to use for the experiment name. Defaults to an empty string. The format string will be resolved with the values of the config dict. For example, if the config dict contains a key ‘trainer.lr’ with value 0.1, the format string ‘{trainer.lr}’ will be resolved to ‘0.1’.

Raises:

ValueError – if the replication method is unknown.

Return type:

Generator[ConfigDict, None, None]