Skip to content

Instantly share code, notes, and snippets.

@vene
Created July 19, 2021 08:25
Show Gist options
  • Save vene/e9a8e584163f2962749f598e809f0f40 to your computer and use it in GitHub Desktop.
Save vene/e9a8e584163f2962749f598e809f0f40 to your computer and use it in GitHub Desktop.
Experimental config with files & CLI using only OmegaConf
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
# author: vlad niculae <vlad@vene.ro>
# 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