当前位置: 首页 > 工具软件 > YACS > 使用案例 >

Python yacs库

慕宜民
2023-12-01

      yacs是一个python库用于定义和管理系统配置,比如那些通常可以在为科学实验设计的软件中找到的配置。这些“配置”通常包括用于训练机器学习模型的超参数

     之前有很多经典的工作用它进行参数配置,不过现在貌似开始有些过时

像这样

from yacs.config import CfgNode as CN
import yaml
import argparse

# define the default parameters and values
_C = CN()
_C.exp_group = 'default'
_C.exp_name = 'exp_name'
_C.description = '

_C.data = CN()
_C.data.batch_size = 16

# call this each time you want the default values
def get_cfg_defaults():
    return _C.clone()

"""
Then you use a .yaml file to specify parameters for each experiment

You can only include part of the parameters. Other parameters will
adopt the default values in the "_C.xxx" part above:

A sample yaml file content is like below. It only specifies two parameters:


exp_group: 'imagenet'
data:
    batch_size: 32

"""

# do this in your training code
# get the default values
cfg = get_cfg_defaults()

# update some values from the yaml file to overwrite default values
cfg.merge_from_file(config_fp)

# use this to take parameter values from the command line
# and overwrite the above
parser = argparse.ArgumentParser()
parser.add_argument(
    "opts",
    help="Override config options using the command-line",
    default=None,
    nargs=argparse.REMAINDER)
args = parser.parse_args()
cfg.merge_from_list(args.opts)

# dump all the parameters into a file to save as a record
with open('%s/config.yaml' % record_dir, 'w') as f:
    f.write(cfg.dump())

 类似资料: