yacs库的用法

简介:
YACS是一个轻量级库,用于定义和管理系统配置,例如那些在为科学实验设计的软件中常见的配置。这些“配置”通常涵盖诸如用于训练机器学习模型的超参数或可配置模型超参数(诸如卷积神经网络的深度)之类的概念。 由于您正在进行科学研究,因此重复性至关重要,因此您需要一种可靠的方法来序列化实验配置。 YACS使用YAML作为简单的,人类可读的序列化格式。范式是:your code + a YACS config for experiment E (+ external dependencies + hardware + other nuisance terms …) = reproducible experiment E。虽然您可能无法控制所有内容,但至少可以控制代码和实验配置。 YACS随时为您提供帮助。

用法
YACS可以以各种灵活的方式使用。主要范例有两种:

配置为局部变量 (local variable)
配置为全局单例(global singleton)
你可以根据自己的需要选择使用哪一种模式,但我们推荐你使用local variable模式。

要在项目中使用YACS,首先要创建一个项目配置文件,通常称为config.py或defaults.py。 此文件是所有可配置选项的一站式参考点。 它应该有很好的文档记录,并为所有选项提供合理的默认值。

# my_project/config.py
from yacs.config import CfgNode as CN
 
#创建一个配置节点_C
_C = CN()
 
#在_C下创建新的配置节点_C.SYSTEM
#给_C.SYSTEM的属性配置默认值
_C.SYSTEM = CN()
# Number of GPUS to use in the experiment
_C.SYSTEM.NUM_GPUS = 8
# Number of workers for doing things
_C.SYSTEM.NUM_WORKERS = 4
 
_C.TRAIN = CN()
# A very important hyperparameter
_C.TRAIN.HYPERPARAMETER_1 = 0.1
# The all important scales for the stuff
_C.TRAIN.SCALES = (2, 4, 8, 16)
 
 
def get_cfg_defaults():
  """Get a yacs CfgNode object with default values for my_project."""
  # 克隆一份配置节点_C的信息返回,_C的信息不会改变
  # This is for the "local variable" use pattern
  return _C.clone()
 
# Alternatively, provide a way to import the defaults as
# a global singleton:
# cfg = _C  # users can `from config import cfg`

接下来,您需要创建YAML配置文件;通常你会为每个实验创建一个。每个配置文件仅重写该实验中需要更改的配置选项。

# my_project/experiment.yaml
SYSTEM:
  NUM_GPUS: 2
TRAIN:
  SCALES: (1, 2)

最后,您可以在你的实际项目代码使用配置。 在初始设置之后,最好通过调用freeze()方法将配置冻结以防止进一步修改。 如下图所示,配置选项可以通过导入cfg并直接访问它来使用全局选项集(globle singleton模式),或者可以复制cfg并将其作为参数传递(local variable模式)。

# my_project/main.py
 
import my_project
from config import get_cfg  # local variable usage pattern, or:
# from config import cfg  # global singleton usage pattern
 
 
if __name__ == "__main__":
  cfg = get_cfg_defaults()
  cfg.merge_from_file("experiment.yaml")
  cfg.freeze()
  print(cfg)
 
  # Example of using the cfg as global access to options
  if cfg.SYSTEM.NUM_GPUS > 0:
    my_project.setup_multi_gpu_support()
 
  model = my_project.create_model(cfg)

命令行重写
您也可以使用完全限定的键值对列表来更新CfgNode。 这样可以轻松地从命令行使用覆盖选项。 例如:

cfg.merge_from_file("experiment.yaml")
# Now override from a list (opts could come from the command line)
opts = ["SYSTEM.NUM_GPUS", 8, "TRAIN.SCALES", "(1, 2, 3, 4)"]
cfg.merge_from_list(opts)

建议采用以下原则:“相同的配置选项只使用一种方法。” 这个原则意味着如果在YACS配置对象中定义了一个选项,那么你的程序应该使用cfg.merge_from_list(opts)设置该配置选项,而不是通过定义–train-scales作为命令行参数来实现。 然后用来设置cfg.TRAIN.SCALES。

原文链接:https://blog.csdn.net/gefeng1209/article/details/90668882

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YACS 是一个强大的 PHP 脚本,可以让你维护一个动态的 Web 服务器。特性:- Runs on your own server, or on a shared web site- Post articles with web forms, by e-mail, or remotely (w:bloggar)- Embed images and photos in articles --automatic resize- Each section can be a weblog, a discussion board, a book of cooking recipes,etc, or even a plain list of articles- Overlay interface for PHP developers, to add extra functionality to articles,such as polls or cooking recipes- Display the content tree in Freemind- Comments, with quoting- Archives per week and per month- The home page is updated automatically on article publishing- Categories, sub-categories, etc. --Build your own Yahoo! or DMOZ...- Real-time meetings with community members- Private discussions and messages- Search on any word --text of articles is fully indexed- Multiple authors --actually, a community of contributors- Articles are visible only on publication after review by editors- Articles and sections can have dead-line to limit visibility over time- A straightforward control panel, and a set of configuration panels- File upload to articles , sections or categories- Attach links to articles, sections or categories- A comprehensive set of UBB-like codes are available to beautify your posts- Integrated support of TinyMCE and of FCKEditor- Fully customizable skins- Easy integration of Google Maps- Add a comprehensive web interface to existing collections of files- Support audio-on demand and video-on demand- Automatic web slideshow for shared photos- RSS syndication- Easy installation- XML-RPC interface (implementing the Blogger API and metaWeblog API) 标签:YACS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值