186. yacs 使用记录
yacs库,用于为一个系统构建config文件
Install
1 | $ pip install yacs |
Import
1 | from yacs.config import CfgNode as CN |
Usage
create config node
需要创建CN()
这个作为容器来装载我们的参数,这个容器可以嵌套
1 | from yacs.config import CfgNode as CN |
API reference
use __C
as created config file
1. clone()
return a copy config file, so the defaults will not be altered
1 | def get_cfg_defaults(): |
2. clear()
clear your config file, you will get None
as the result
1 | print(__C.clear()) # None |
3. merge_from_file()
对于不同的实验,你有不同的超参设置,所以你可以使用yaml文件来管理不同的configs,然后使用merge_from_file()
这个方法,这个会比较每个experiments特有的config和默认参数的区别,会将默认参数与特定参数不同的部分,用特定参数覆盖。
1 | __C.merge_from_file("./test_config.yaml") |
Addition:
- 你需要merge的yaml文件中,不能有default参数中不存在的参数,不然会报错,但是可以比default中设定的参数少,比如default文件中有name参数,这是不需要特定改动的,你可以在yaml中不设置name这个key。
1 | from yacs.config import CfgNode as CN |
4. merge_from_list()
可以用list
来传递参数
1 | from yacs.config import CfgNode as CN |
other details are the same as merge_from_file
5. merge_from_other_cfg()
the same as merge_from_file
and merge_from_list
, the only difference is that the merged file is also a CfgNode
class
6. freeze()
freeze the configs, and you can not change the value after this operation
1 | from yacs.config import CfgNode as CN |
7. defrost()
reverse operation of freeze()
1 | from yacs.config import CfgNode as CN |