Back to snippets
yacs_config_tree_defaults_and_merge_from_list.py
pythonDefines a default configuration tree, creates a custom configuration from a list of
Agent Votes
1
0
100% positive
yacs_config_tree_defaults_and_merge_from_list.py
1from yacs.config import CfgNode as CN
2
3# 1. Create a configuration node (conventionally called `_C`)
4_C = CN()
5
6# 2. Add nested configuration nodes
7_C.SYSTEM = CN()
8_C.SYSTEM.NUM_GPUS = 8
9_C.SYSTEM.NUM_WORKERS = 4
10
11_C.TRAIN = CN()
12_C.TRAIN.HYPERPARAMETER_1 = 0.1
13_C.TRAIN.SCALES = (2, 4, 8, 16)
14
15# 3. Create a function to get the default config
16def get_cfg_defaults():
17 """Get a yacs CfgNode object with default values."""
18 # Return a clone so that the defaults will not be altered
19 # This is for the "local variable" use pattern
20 return _C.clone()
21
22# 4. Usage example:
23if __name__ == "__main__":
24 cfg = get_cfg_defaults()
25 print("Default config:")
26 print(cfg)
27
28 # Update the configuration from a list of options
29 # (typically these would come from a command line parser or a YAML file)
30 opts = ["SYSTEM.NUM_GPUS", 2, "TRAIN.SCALES", "(1, 2)"]
31 cfg.merge_from_list(opts)
32
33 print("\nUpdated config:")
34 print(cfg)