Python yacs库

      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())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值