python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參_第1頁
python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參_第2頁
python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參_第3頁
python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參_第4頁
python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參目錄前言使用步驟:常見規(guī)則使用config文件傳入超參數(shù)argparse中action的可選參數(shù)store_true

前言

argparse是深度學(xué)習(xí)項目調(diào)參時常用的python標(biāo)準(zhǔn)庫,使用argparse后,我們在命令行輸入的參數(shù)就可以以這種形式pythonfilename.py--lr1e-4--batch_size32來完成對常見超參數(shù)的設(shè)置。,一般使用時可以歸納為以下三個步驟

使用步驟:

創(chuàng)建ArgumentParser()對象調(diào)用add_argument()方法添加參數(shù)使用parse_args()解析參數(shù)在接下來的內(nèi)容中,我們將以實(shí)際操作來學(xué)習(xí)argparse的使用方法

importargparse

parser=argparse.ArgumentParser()#創(chuàng)建一個解析對象

parser.add_argument()#向該對象中添加你要關(guān)注的命令行參數(shù)和選項

args=parser.parse_args()#調(diào)用parse_args()方法進(jìn)行解析

常見規(guī)則

在命令行中輸入pythondemo.py-h或者pythondemo.py--help可以查看該python文件參數(shù)說明arg字典類似python字典,比如arg字典Namespace(integers=5)可使用arg.參數(shù)名來提取這個參數(shù)parser.add_argument(integers,type=str,nargs=+,help=傳入的數(shù)字)nargs是用來說明傳入的參數(shù)個數(shù),+表示傳入至少一個參數(shù),*表示參數(shù)可設(shè)置零個或多個,表示參數(shù)可設(shè)置零個或一個parser.add_argument(-n,--name,type=str,required=True,default=,help=名)required=True表示必須參數(shù),-n表示可以使用短選項使用該參數(shù)parser.add_argument(--test_action,default=False,action=store_true)store_true觸發(fā)時為真,不觸發(fā)則為假(test.py,輸出為False,test.py--test_action,輸出為True)

使用config文件傳入超參數(shù)

為了使代碼更加簡潔和模塊化,可以將有關(guān)超參數(shù)的操作寫在config.py,然后在train.py或者其他文件導(dǎo)入就可以。具體的config.py可以參考如下內(nèi)容。

importargparse

defget_options(parser=argparse.ArgumentParser()):

parser.add_argument('--workers',type=int,default=0,

help='numberofdataloadingworkers,youhadbetterputit'

'4timesofyourgpu')

parser.add_argument('--batch_size',type=int,default=4,help='inputbatchsize,default=64')

parser.add_argument('--niter',type=int,default=10,help='numberofepochstotrainfor,default=10')

parser.add_argument('--lr',type=float,default=3e-5,help='selectthelearningrate,default=1e-3')

parser.add_argument('--seed',type=int,default=118,help="randomseed")

parser.add_argument('--cuda',action='store_true',default=True,help='enablescuda')

parser.add_argument('--checkpoint_path',type=str,default='',

help='Pathtoloadaprevioustrainedmodelifnotempty(defaultempty)')

parser.add_argument('--output',action='store_true',default=True,help="showsoutput")

opt=parser.parse_args()

ifopt.output:

print(f'num_workers:{opt.workers}')

print(f'batch_size:{opt.batch_size}')

print(f'epochs(niters):{opt.niter}')

print(f'learningrate:{opt.lr}')

print(f'manual_seed:{opt.seed}')

print(f'cudaenable:{opt.cuda}')

print(f'checkpoint_path:{opt.checkpoint_path}')

returnopt

if__name__=='__main__':

opt=get_options()

$pythonconfig.py

num_workers:0

batch_size:4

epochs(niters):10

learningrate:3e-05

manual_seed:118

cudaenable:True

checkpoint_path:

隨后在train.py等其他文件,我們就可以使用下面的這樣的結(jié)構(gòu)來調(diào)用參數(shù)。

#導(dǎo)入必要庫

importconfig

opt=config.get_options()

manual_seed=opt.seed

num_workers=opt.workers

batch_size=opt.batch_size

lr=opt.lr

niters=opt.niters

checkpoint_path=opt.checkpoint_path

#隨機(jī)數(shù)的設(shè)置,保證復(fù)現(xiàn)結(jié)果

defset_seed(seed):

torch.manual_seed(seed)

torch.cuda.manual_seed_all(seed)

random.seed(seed)

np.random.seed(seed)

torch.backends.cudnn.benchmark=False

torch.backends.cudnn.deterministic=True

if__name__=='__main__':

set_seed(manual_seed)

forepochinrange(niters):

train(model,lr,batch_size,num_workers,checkpoint_path)

val(model,lr,batch_size,num_workers,checkpoint_path)

argparse中action的可選參數(shù)store_true

#test.py

importargparse

if__name__=='__main__':

parser=argparse.ArgumentParser()

parser.add_argument("--test_action",action='store_true')

args=parser.parse_args()

action_val=args.test_action

print(action_val)

以上面的代碼為例,若觸發(fā)test_action,則為True,否則為False:

$pythontest.py,輸出為False$pythontest.py--test_action,輸出為True

若在上面的代碼中加入default,設(shè)為False時:

parser.add_argument("--test_action",default='False',action='store_true')

$pythontest.py,輸出為False$pythontest.py--test_action,輸出為True

default設(shè)為True時:

parser.add_argument("--test_action"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論