我是靠谱客的博主 忧郁大叔,这篇文章主要介绍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())

最后

以上就是忧郁大叔最近收集整理的关于Python yacs库的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部