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 Spring启动启用HTTPS   actionscript 3 java中的这个[“var”+“name”]   java只匹配给定集合中一个字符的一个匹配项   java Hibernate:防止角色表中出现多个相同的条目   javajersey+Spring注入servlet请求   java HtmlEditor javafx失去焦点   java Apache Wicket AjaxRequestTarget ListView组件未刷新或更新   mysql java。无法将lang.String转换为java。sql。时间戳   java将巨大的整数文件(在一行中)拆分为具有内存限制的已排序块   安卓如何完全关闭proguard?   安装Eclipse和Android SDK后的java“无AVD可用”消息   java动态显示图像视图   java在Spring中还有哪些WebsocketClient实现?   java Glassfish需要很长时间才能重新启动   使用Java简单串行连接器将pc与arduino连接   java如何在camel文件组件配置中结合readLockCheckInterval和maxMessagesPerPoll?   单击Android时的java预览图像   java如何将字节数组转换为ByteArrayOutputStream