hpman的argparse扩展

hpargparse的Python项目详细描述


hpargparse

CircleCIcodecov

hpmanargparse扩展名

安装

python3 -m pip install hpargparse

简介

下面的示例位于examples/02-brief。在

main.py

^{pr2}$

结果:

$ ./main.py
weight decay is 1e-05
$ ./main.py --weight-decay 1e-4
weight decay is 0.0001
$ ./main.py --weight-decay 1e-4 --hp-list
weight_decay: 0.0001
$ ./main.py --weight-decay 1e-4 --hp-list detail
All hyperparameters:                                                                               
    ['weight_decay']                                                                               
                                              Details                                              
╔══════════════╦═══════╦════════╦═════════════════════════════════════════════════════════════════╗
║ name         ║ type  ║ value  ║ details                                                         ║
╠══════════════╬═══════╬════════╬═════════════════════════════════════════════════════════════════╣
║ weight_decay ║ float ║ 0.0001 ║ occurrence[0]:                                                  ║
║              ║       ║        ║   ./main.py:10                                                  ║
║              ║       ║        ║      5:                                                         ║
║              ║       ║        ║      6: import argparse                                         ║
║              ║       ║        ║      7:                                                         ║
║              ║       ║        ║      8:                                                         ║
║              ║       ║        ║      9: def func():                                             ║
║              ║       ║        ║ ==> 10:     weight_decay= _("weight_decay", 1e-5)              ║
║              ║       ║        ║     11:     print("weight decay is {}".format(weight_decay))    ║
║              ║       ║        ║     12:                                                         ║
║              ║       ║        ║     13:                                                         ║
║              ║       ║        ║     14: def main():                                             ║
║              ║       ║        ║     15:     parser= argparse.ArgumentParser()                  ║
╚══════════════╩═══════╩════════╩═════════════════════════════════════════════════════════════════╝
$ ./main.py -h
usage: main.py [-h][--weight-decay WEIGHT_DECAY][--hp-save HP_SAVE][--hp-load HP_LOAD][--hp-list [{detail,yaml,json}]][--hp-detail][--hp-serial-format {auto,yaml,pickle}][--hp-exit]

optional arguments:
  -h, --help            show this help message and exit
  --weight-decay WEIGHT_DECAY
                        (default: 1e-05)
  --hp-save HP_SAVE     Save hyperparameters to a file. The hyperparameters
                        are saved after processing of all other options
                        (default: None)
  --hp-load HP_LOAD     Load hyperparameters from a file. The hyperparameters
                        are loaded before any other options are processed
                        (default: None)
  --hp-list [{detail,yaml,json}]
                        List all available hyperparameters. If `--hp-list
                        detail` is specified, a verbose table will be print
                        (default: None)
  --hp-detail           Shorthand for --hp-list detail (default: False)
  --hp-serial-format {auto,yaml,pickle}
                        Format of the saved config file. Defaults to auto. It
                        can be set to override auto file type deduction.
                        (default: auto)
  --hp-exit             process all hpargparse actions and quit (default:
                        False)

hpcli:命令行工具

除了在代码中使用hpargparse.bind,我们还提供了一个命令行 工具hpcli为使用hpman的任何现有文件提供类似的功能。在

src.py

fromhpman.mimport__('num_channels',128)_('num_layers',50)

在shell中:

$ hpcli src.py
num_channels: 128
num_layers: 50
$ hpcli src.py --num-layers 101
num_channels: 128
num_layers: 101
$ hpcli src.py --num-layers 101 --hp-save config.yaml
num_channels: 128
num_layers: 101
$ hpcli src.py --num-layers 101 --hp-save config.yaml --hp-list detail
All hyperparameters:                                                   
    ['num_channels', 'num_layers']                                     
                                Details                                
╔══════════════╦══════╦═══════╦═══════════════════════════════════════╗
║ name         ║ type ║ value ║ details                               ║
╠══════════════╬══════╬═══════╬═══════════════════════════════════════╣
║ num_channels ║ int  ║ 128   ║ occurrence[0]:                        ║
║              ║      ║       ║   src.py:3                            ║
║              ║      ║       ║     1: from hpman.m import _          ║
║              ║      ║       ║     2:                                ║
║              ║      ║       ║ ==> 3: _("num_channels", 128)         ║
║              ║      ║       ║     4: _("num_layers", 50)            ║
║              ║      ║       ║     5:                                ║
╠══════════════╬══════╬═══════╬═══════════════════════════════════════╣
║ num_layers   ║ int  ║ 101   ║ occurrence[0]:                        ║
║              ║      ║       ║   src.py:4                            ║
║              ║      ║       ║     1: from hpman.m import _          ║
║              ║      ║       ║     2:                                ║
║              ║      ║       ║     3: _("num_channels", 128)         ║
║              ║      ║       ║ ==> 4: _("num_layers", 50)            ║
║              ║      ║       ║     5:                                ║
╚══════════════╩══════╩═══════╩═══════════════════════════════════════╝
$ hpcli src.py -h
usage: hpcli [-h][--num-channels NUM_CHANNELS][--num-layers NUM_LAYERS][--hp-save HP_SAVE][--hp-load HP_LOAD][--hp-list [{detail,yaml,json}]][--hp-detail][--hp-serial-format {auto,yaml,pickle}][--hp-exit]

optional arguments:
  -h, --help            show this help message and exit
  --num-channels NUM_CHANNELS
                        (default: 128)
  --num-layers NUM_LAYERS
                        (default: 50)
  --hp-save HP_SAVE     Save hyperparameters to a file. The hyperparameters
                        are saved after processing of all other options
                        (default: None)
  --hp-load HP_LOAD     Load hyperparameters from a file. The hyperparameters
                        are loaded before any other options are processed
                        (default: None)
  --hp-list [{detail,yaml,json}]
                        List all available hyperparameters. If `--hp-list
                        detail` is specified, a verbose table will be print
                        (default: yaml)
  --hp-detail           Shorthand for --hp-list detail (default: False)
  --hp-serial-format {auto,yaml,pickle}
                        Format of the saved config file. Defaults to auto. It
                        can be set to override auto file type deduction.
                        (default: auto)
  --hp-exit             process all hpargparse actions and quit (default:
                        False)

这可能是检查代码中超参数的便捷工具。在

示例:深度学习实验

此示例位于examples/01-nn-training。在

这是一个充分发挥作用的例子,训练一个透镜使用 hpargparsehpman协同管理超参数。在

我们强烈建议您围绕这个例子玩。在

示例:基础知识演练

现在我们逐一分解函数。在

下面的示例位于examples/00-basic。在

lib.py

fromhpman.mimport_defadd():return_("a",1)+_("b",2)defmult():return_("a")*_("b")

main.py

#!/usr/bin/env python3importargparseimporthpargparsefromhpman.mimport_importosBASE_DIR=os.path.dirname(os.path.realpath(__file__))defmain():parser=argparse.ArgumentParser()# ... do whatever you wantparser.add_argument(dest="predefined_arg")# analyze everything in this directory_.parse_file(BASE_DIR)# <-- IMPORTANT# bind will monkey_patch parser.parse_args to do its jobhpargparse.bind(parser,_)# <-- IMPORTANT# parse args and set the valuesargs=parser.parse_args()# ... do whatever you want nextimportlibprint("a = {}".format(_.get_value("a")))print("b = {}".format(_.get_value("b")))print("lib.add() = {}".format(lib.add()))print("lib.mult() = {}".format(lib.mult()))if__name__=="__main__":main()

帮助

$ ./main.py -h
usage: main.py [-h][--a A][--b B][--hp-save HP_SAVE][--hp-load HP_LOAD][--hp-list [{detail,yaml,json}]][--hp-detail][--hp-serial-format {auto,yaml,pickle}][--hp-exit]
               predefined_arg

positional arguments:
  predefined_arg

optional arguments:
  -h, --help            show this help message and exit
  --a A                 (default: 1)
  --b B                 (default: 2)
  --hp-save HP_SAVE     Save hyperparameters to a file. The hyperparameters
                        are saved after processing of all other options
                        (default: None)
  --hp-load HP_LOAD     Load hyperparameters from a file. The hyperparameters
                        are loaded before any other options are processed
                        (default: None)
  --hp-list [{detail,yaml,json}]
                        List all available hyperparameters. If `--hp-list
                        detail` is specified, a verbose table will be print
                        (default: None)
  --hp-detail           Shorthand for --hp-list detail (default: False)
  --hp-serial-format {auto,yaml,pickle}
                        Format of the saved config file. Defaults to auto. It
                        can be set to override auto file type deduction.
                        (default: auto)
  --hp-exit             process all hpargparse actions and quit (default:
                        False)

从命令行参数设置超参数

$ ./main.py some_thing --a 3 --b 5a=3b=5
lib.add()=8
lib.mult()=15

列出所有超参数

$ ./main.py some_arg --hp-list
a: 1
b: 2

。。。以及细节:

$ ./main.py some_arg --hp-list detail
All hyperparameters:                                                                               
    ['a', 'b']                                                                                     
                                              Details                                              
╔══════╦══════╦═══════╦═══════════════════════════════════════════════════════════════════════════╗
║ name ║ type ║ value ║ details                                                                   ║
╠══════╬══════╬═══════╬═══════════════════════════════════════════════════════════════════════════╣
║ a    ║ int  ║ 1     ║ occurrence[0]:                                                            ║
║      ║      ║       ║   /data/project/hpargparse/examples/00-basic/lib.py:8                     ║
║      ║      ║       ║      3: # for more usecases, please refer to hpman's document             ║
║      ║      ║       ║      4:                                                                   ║
║      ║      ║       ║      5:                                                                   ║
║      ║      ║       ║      6: def add():                                                        ║
║      ║      ║       ║      7:     # define a hyperparameter on-the-fly with defaults            ║
║      ║      ║       ║ ==>  8:     return _("a", 1) + _("b", 2)                                  ║
║      ║      ║       ║      9:                                                                   ║
║      ║      ║       ║     10:                                                                   ║
║      ║      ║       ║     11: def mult():                                                       ║
║      ║      ║       ║     12:     # reuse a pre-defined hyperparameters                         ║
║      ║      ║       ║     13:     return _("a") * _("b")                                        ║
║      ║      ║       ║ occurrence[1]:                                                            ║
║      ║      ║       ║   /data/project/hpargparse/examples/00-basic/lib.py:13                    ║
║      ║      ║       ║      8:     return _("a", 1) + _("b", 2)                                  ║
║      ║      ║       ║      9:                                                                   ║
║      ║      ║       ║     10:                                                                   ║
║      ║      ║       ║     11: def mult():                                                       ║
║      ║      ║       ║     12:     # reuse a pre-defined hyperparameters                         ║
║      ║      ║       ║ ==> 13:     return _("a") * _("b")                                        ║
║      ║      ║       ║     14:                                                                   ║
╠══════╬══════╬═══════╬═══════════════════════════════════════════════════════════════════════════╣
║ b    ║ int  ║ 2     ║ occurrence[0]:                                                            ║
║      ║      ║       ║   /data/project/hpargparse/examples/00-basic/lib.py:8                     ║
║      ║      ║       ║      3: # for more usecases, please refer to hpman's document             ║
║      ║      ║       ║      4:                                                                   ║
║      ║      ║       ║      5:                                                                   ║
║      ║      ║       ║      6: def add():                                                        ║
║      ║      ║       ║      7:     # define a hyperparameter on-the-fly with defaults            ║
║      ║      ║       ║ ==>  8:     return _("a", 1) + _("b", 2)                                  ║
║      ║      ║       ║      9:                                                                   ║
║      ║      ║       ║     10:                                                                   ║
║      ║      ║       ║     11: def mult():                                                       ║
║      ║      ║       ║     12:     # reuse a pre-defined hyperparameters                         ║
║      ║      ║       ║     13:     return _("a") * _("b")                                        ║
║      ║      ║       ║ occurrence[1]:                                                            ║
║      ║      ║       ║   /data/project/hpargparse/examples/00-basic/lib.py:13                    ║
║      ║      ║       ║      8:     return _("a", 1) + _("b", 2)                                  ║
║      ║      ║       ║      9:                                                                   ║
║      ║      ║       ║     10:                                                                   ║
║      ║      ║       ║     11: def mult():                                                       ║
║      ║      ║       ║     12:     # reuse a pre-defined hyperparameters                         ║
║      ║      ║       ║ ==> 13:     return _("a") * _("b")                                        ║
║      ║      ║       ║     14:                                                                   ║
╚══════╩══════╩═══════╩═══════════════════════════════════════════════════════════════════════════╝

保存/加载自/到YAML文件

# save to yaml file
$ ./main.py some_arg --hp-save /tmp/config.yaml --hp-exit

$ cat /tmp/config.yaml 
a: 1
b: 2# load from yaml file
$ cat config_modified.yaml
a: 123
b: 456

$ ./main.py some_arg --hp-load config_modified.yaml --hp-list
a: 123
b: 456

发展

  1. 安装要求:
pip install -r requirements.dev.txt -r requirements.txt
  1. 激活提交模板
git config commit.template .git-commit-template.txt
  1. 安装预提交挂钩
pre-commit install

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将cassandra中的行数据转换为与列相关的嵌套json   java如何使用jcr XPath在jcr:content/@jcr:data中搜索?   java在使用openCV进行安卓开发时如何利用手机的广角镜头   java解析扩展了接口,结束了一个潜在的无限循环   位置服务的@Override方法中存在java Android应用程序错误   java本地线程的用途和需求是什么   具有左右子访问的java节点树遍历   java验证JsonWebToken签名   JUL日志处理程序中的java日志记录   嵌入式Java读取给定时间段的串行数据。   java有没有办法从多个URL获取多个图像?   java线程通过等待intent阻止自己发送intent   java Spring MVC解析多部分内容请求   java JPA/Hibernate静态元模型属性未填充NullPointerException   java格式错误的字符(需要引号,得到I)~正在处理   java为什么PrintWriter对象抛出FileNotFoundException?   java Neo4j未正确保存标签   java IE不加载图像