py_config_runner.utils¶
This module contains some basic helper methods that can be used inside running script.
- class py_config_runner.utils.ConfigObject(config_filepath, mutations=None, **kwargs)[source]¶
Lazy config object
- Parameters:
config_filepath (str | Path) – path to python configuration file
mutations (Mapping | None) – dict of mutations to apply to the configuration python file before loading. See example below.
kwargs (Any) – kwargs to pass to the config object. Note that for colliding keys retained value is the one from
config_filepath
.
Example:
config = ConfigObject("/path/to/baseline.py") print(config) # For example, configuration file contains params: seed, ... print(config.seed, config["seed"], config.get("seed"))
Example with mutations:
Let’s assume that configuration python file has
learning_rate = 0.01
which is used to configure an optimizer:# baseline.py configuration file learning_rate = 0.01 hp_params = [1.0, 0.9, 0.8] hp_dict = {"a": 0.5, "b": 0.75} optimizer = SGD(parameters, lr=learning_rate) model = MyModel(hp_params, hp_dict)
And we would like to override
learning_rate
,hp_params
andhp_dict
from the script using above configuration file and has also optimizer and model updated accordingly:# Script file using baseline.py configuration mutations={ "learning_rate": 0.05, "hp_params": 0.5 # here we can also change variable type "hp_dict": {"a": 0.1, "b": 0.2} } config = ConfigObject("/path/to/baseline.py", mutations=mutations) print(config) print(config.optimizer) # assert config.learning_rate == 0.05 # assert config.optimizer.lr == 0.05 # assert config.hp_params == 0.5 # assert config.hp_dict == {"a": 0.1, "b": 0.2}
Warning
Mutation can not be a class instance or other complex python object. The following wont work:
mutations={"model": MyModel()}
.