Created
July 19, 2021 08:25
-
-
Save vene/e9a8e584163f2962749f598e809f0f40 to your computer and use it in GitHub Desktop.
Experimental config with files & CLI using only OmegaConf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example input and output. | |
$ python conf.py seed=42 lr=.1 | |
project: ??? | |
seed: 42 | |
lr: 0.1 | |
epochs: ??? | |
p_drop: 0.5 | |
baseconf: null | |
$ echo "\ | |
project: myproj | |
seed: 42 | |
lr: .01 | |
epochs: 100" > my.conf | |
$ python conf.py baseconf=my.conf | |
project: myproj | |
seed: 42 | |
lr: 0.01 | |
epochs: 100 | |
p_drop: 0.5 | |
baseconf: my.conf | |
$ python conf.py baseconf=my.conf seed=1 | |
project: myproj | |
seed: 1 | |
lr: 0.01 | |
epochs: 100 | |
p_drop: 0.5 | |
baseconf: my.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# author: vlad niculae <[email protected]> | |
# license: 2-clause BSD | |
from dataclasses import dataclass | |
from omegaconf import OmegaConf | |
from typing import Optional | |
@dataclass | |
class MyConfigSchema: | |
project: str # project name (wandb) | |
seed: int | |
lr: float | |
epochs: int | |
p_drop: float = 0.5 | |
baseconf: Optional[str] = None # path to config file | |
def load_config(show=False): | |
conf = OmegaConf.from_cli() | |
# merge with base config yaml from disk. | |
# cli flags take priority. | |
if 'baseconf' in conf: | |
baseconf = OmegaConf.load(conf.baseconf) | |
conf = OmegaConf.merge(baseconf, conf) | |
# validate against schema | |
schema = OmegaConf.structured(MyConfigSchema) | |
conf = OmegaConf.merge(schema, conf) | |
if show: | |
print(OmegaConf.to_yaml(conf)) | |
conf = OmegaConf.to_container(conf) | |
return conf | |
if __name__ == '__main__': | |
conf = load_config(show=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment