hpman的argparse扩展
hpargparse的Python项目详细描述
hpargparse
安装
python3 -m pip install hpargparse
简介
下面的示例位于examples/02-brief。在
main.py
结果:
$ ./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。在
这是一个充分发挥作用的例子,训练一个透镜使用
hpargparse
和hpman
协同管理超参数。在
我们强烈建议您围绕这个例子玩。在
示例:基础知识演练
现在我们逐一分解函数。在
下面的示例位于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
发展
- 安装要求:
pip install -r requirements.dev.txt -r requirements.txt
- 激活提交模板
git config commit.template .git-commit-template.txt
- 安装预提交挂钩
pre-commit install
- 项目
标签: