Package: hpargparse
hpargparseAnargparseextension forhpmanInstallationpython3-mpipinstallhpargparseBrief IntroductionThe following example lies inexamples/02-brief.main.py#!/usr/bin/env python3fromhpman.mimport_importhpargparseimportargparsedeffunc():weight_decay=_("weight_decay",1e-5)print("weight decay is{}".format(weight_decay))defmain():parser=argparse.ArgumentParser()_.parse_file(__file__)hpargparse.bind(parser,_)parser.parse_args()func()if__name__=="__main__":main()results in:$./main.py
weightdecayis1e-05
$./main.py--weight-decay1e-4
weightdecayis0.0001
$./main.py--weight-decay1e-4--hp-list
weight_decay:0.0001
$./main.py--weight-decay1e-4--hp-listdetail
Allhyperparameters:['weight_decay']Details╔══════════════╦═══════╦════════╦═════════════════════════════════════════════════════════════════╗
║name║type║value║details║
╠══════════════╬═══════╬════════╬═════════════════════════════════════════════════════════════════╣
║weight_decay║float║0.0001║occurrence[0]:║
║║║║./main.py:10║
║║║║5:║
║║║║6:importargparse║
║║║║7:║
║║║║8:║
║║║║9:deffunc():║
║║║║==>10:weight_decay=_("weight_decay",1e-5)║
║║║║11:print("weight decay is {}".format(weight_decay))║
║║║║12:║
║║║║13:║
║║║║14:defmain():║
║║║║15:parser=argparse.ArgumentParser()║
╚══════════════╩═══════╩════════╩═════════════════════════════════════════════════════════════════╝
$./main.py-h
usage:main.py[-h][--weight-decayWEIGHT_DECAY][--hp-saveHP_SAVE][--hp-loadHP_LOAD][--hp-list[{detail,yaml,json}]][--hp-detail][--hp-serial-format{auto,yaml,pickle}][--hp-exit]optionalarguments:-h,--helpshowthishelpmessageandexit--weight-decayWEIGHT_DECAY(default:1e-05)--hp-saveHP_SAVESavehyperparameterstoafile.Thehyperparametersaresavedafterprocessingofallotheroptions(default:None)--hp-loadHP_LOADLoadhyperparametersfromafile.Thehyperparametersareloadedbeforeanyotheroptionsareprocessed(default:None)--hp-list[{detail,yaml,json}]Listallavailablehyperparameters.If`--hp-listdetail`isspecified,averbosetablewillbeprint(default:None)--hp-detailShorthandfor--hp-listdetail(default:False)--hp-serial-format{auto,yaml,pickle}Formatofthesavedconfigfile.Defaultstoauto.Itcanbesettooverrideautofiletypededuction.(default:auto)--hp-exitprocessallhpargparseactionsandquit(default:False)hpcli: The Command Line ToolBesides usinghpargparse.bindin you code, we also come with a command line
toolhpclito provide similar functions to any existing file using hpman.src.pyfromhpman.mimport__('num_channels',128)_('num_layers',50)In shell:$hpclisrc.py
num_channels:128num_layers:50$hpclisrc.py--num-layers101num_channels:128num_layers:101$hpclisrc.py--num-layers101--hp-saveconfig.yaml
num_channels:128num_layers:101$hpclisrc.py--num-layers101--hp-saveconfig.yaml--hp-listdetail
Allhyperparameters:['num_channels','num_layers']Details╔══════════════╦══════╦═══════╦═══════════════════════════════════════╗
║name║type║value║details║
╠══════════════╬══════╬═══════╬═══════════════════════════════════════╣
║num_channels║int║128║occurrence[0]:║
║║║║src.py:3║
║║║║1:fromhpman.mimport_║
║║║║2:║
║║║║==>3:_("num_channels",128)║
║║║║4:_("num_layers",50)║
║║║║5:║
╠══════════════╬══════╬═══════╬═══════════════════════════════════════╣
║num_layers║int║101║occurrence[0]:║
║║║║src.py:4║
║║║║1:fromhpman.mimport_║
║║║║2:║
║║║║3:_("num_channels",128)║
║║║║==>4:_("num_layers",50)║
║║║║5:║
╚══════════════╩══════╩═══════╩═══════════════════════════════════════╝
$hpclisrc.py-h
usage:hpcli[-h][--num-channelsNUM_CHANNELS][--num-layersNUM_LAYERS][--hp-saveHP_SAVE][--hp-loadHP_LOAD][--hp-list[{detail,yaml,json}]][--hp-detail][--hp-serial-format{auto,yaml,pickle}][--hp-exit]optionalarguments:-h,--helpshowthishelpmessageandexit--num-channelsNUM_CHANNELS(default:128)--num-layersNUM_LAYERS(default:50)--hp-saveHP_SAVESavehyperparameterstoafile.Thehyperparametersaresavedafterprocessingofallotheroptions(default:None)--hp-loadHP_LOADLoadhyperparametersfromafile.Thehyperparametersareloadedbeforeanyotheroptionsareprocessed(default:None)--hp-list[{detail,yaml,json}]Listallavailablehyperparameters.If`--hp-listdetail`isspecified,averbosetablewillbeprint(default:yaml)--hp-detailShorthandfor--hp-listdetail(default:False)--hp-serial-format{auto,yaml,pickle}Formatofthesavedconfigfile.Defaultstoauto.Itcanbesettooverrideautofiletypededuction.(default:auto)--hp-exitprocessallhpargparseactionsandquit(default:False)This could be a handy tool to inspect the hyperparameters in your code.Example: Deep Learning ExperimentThis example lies inexamples/01-nn-training.It is a fully functional example of training a LeNet on MNIST usinghpargparseandhpmancollaboratively to manage hyperparameters.Wehighly suggestyou playing around this example.Example: Basics WalkthroughNow we break down the functions one-by-one.The following example lies inexamples/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()Help$./main.py-h
usage:main.py[-h][--aA][--bB][--hp-saveHP_SAVE][--hp-loadHP_LOAD][--hp-list[{detail,yaml,json}]][--hp-detail][--hp-serial-format{auto,yaml,pickle}][--hp-exit]predefined_arg

positionalarguments:predefined_arg

optionalarguments:-h,--helpshowthishelpmessageandexit--aA(default:1)--bB(default:2)--hp-saveHP_SAVESavehyperparameterstoafile.Thehyperparametersaresavedafterprocessingofallotheroptions(default:None)--hp-loadHP_LOADLoadhyperparametersfromafile.Thehyperparametersareloadedbeforeanyotheroptionsareprocessed(default:None)--hp-list[{detail,yaml,json}]Listallavailablehyperparameters.If`--hp-listdetail`isspecified,averbosetablewillbeprint(default:None)--hp-detailShorthandfor--hp-listdetail(default:False)--hp-serial-format{auto,yaml,pickle}Formatofthesavedconfigfile.Defaultstoauto.Itcanbesettooverrideautofiletypededuction.(default:auto)--hp-exitprocessallhpargparseactionsandquit(default:False)Set Hyperparameters from Command Line Arguments$./main.pysome_thing--a3--b5a=3b=5lib.add()=8lib.mult()=15List All Hyperparameters$./main.pysome_arg--hp-list
a:1b:2... and details:$./main.pysome_arg--hp-listdetail
Allhyperparameters:['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:defadd():║
║║║║7:# define a hyperparameter on-the-fly with defaults            ║║║║║==>8:return_("a",1)+_("b",2)║
║║║║9:║
║║║║10:║
║║║║11:defmult():║
║║║║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:defmult():║
║║║║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:defadd():║
║║║║7:# define a hyperparameter on-the-fly with defaults            ║║║║║==>8:return_("a",1)+_("b",2)║
║║║║9:║
║║║║10:║
║║║║11:defmult():║
║║║║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:defmult():║
║║║║12:# reuse a pre-defined hyperparameters                         ║║║║║==>13:return_("a")*_("b")║
║║║║14:║
╚══════╩══════╩═══════╩═══════════════════════════════════════════════════════════════════════════╝Save/Load from/to YAML file# save to yaml file$./main.pysome_arg--hp-save/tmp/config.yaml--hp-exit

$cat/tmp/config.yamla:1b:2# load from yaml file$catconfig_modified.yaml
a:123b:456$./main.pysome_arg--hp-loadconfig_modified.yaml--hp-list
a:123b:456DevelopmentInstall requirements:pipinstall-rrequirements.dev.txt-rrequirements.txtActivate git commit templategitconfigcommit.template.git-commit-template.txtInstall pre-commit hookpre-commitinstall