-
大小: 28KB文件類型: .m金幣: 1下載: 0 次發(fā)布日期: 2021-05-11
- 語言: Matlab
- 標(biāo)簽: matconvnet??cnn_train.m??
資源簡介
對matconvnet中用于cnn訓(xùn)練的cnn_train.m文件進(jìn)行了詳細(xì)的注釋。后續(xù)會進(jìn)行更新,并且會把相關(guān)的函數(shù)也進(jìn)行注釋
代碼片段和文件信息
%調(diào)用cnn_train:??
%?[?net?info?]?=?cnn_train(net?imdb?@getBatch?opts.train?‘val‘?find(imdb.images.set?==?3))?;??
%%
%主體函數(shù)cnn_train
function?[net?stats]?=?cnn_train(net?imdb?getBatch?varargin)
%CNN_TRAIN??An?example?implementation?of?SGD?for?training?CNNs
%????CNN_TRAIN()?is?an?example?learner?implementing?stochastic
%????gradient?descent?with?momentum?to?train?a?CNN.?It?can?be?used
%????with?different?datasets?and?tasks?by?providing?a?suitable
%????getBatch?function.
%???????帶有動量功能的SGD實現(xiàn),可以根據(jù)getBatch函數(shù)不同用于不同的數(shù)據(jù)集和任務(wù)
%????The?function?automatically?restarts?after?each?training?epoch?by
%????checkpointing.
%???????每次epoch之后重啟
%????The?function?supports?training?on?CPU?or?on?one?or?more?GPUs
%????(specify?the?list?of?GPU?IDs?in?the?‘gpus‘?option).
%?Copyright?(C)?2014-16?Andrea?Vedaldi.
%?All?rights?reserved.
%
%?This?file?is?part?of?the?VLFeat?library?and?is?made?available?under根據(jù)
%?the?terms?of?the?BSD?license?(see?the?COPYING?file).
addpath(fullfile(vl_rootnn?‘examples‘));%添加examples的路徑
opts.expDir?=?fullfile(‘data‘‘exp‘)?;%選擇保存路徑
opts.continue?=?true?;%每次重啟都是接著上次訓(xùn)練狀態(tài)開始
opts.batchSize?=?256?;%選擇初始化批大小為256
opts.numSubBatches?=?1?;%選擇子批的個數(shù)為1(不劃分子批)
opts.train?=?[]?;%初始化訓(xùn)練集索引為空
opts.val?=?[]?;%初始化驗證集索引為空
opts.gpus?=?[]?;%選擇gpu
opts.epochSize?=?inf;%inf無窮大量,
opts.prefetch?=?false?;%選擇是否預(yù)讀取下一批次的樣本,初始為否
opts.numEpochs?=?300?;%選擇epoch數(shù)量
opts.learningRate?=?0.001?;
opts.weightDecay?=?0.0005?;
opts.solver?=?[]?;??%?Empty?array?means?use?the?default?SGD?solver使用默認(rèn)的SGDsolver訓(xùn)練
[opts?varargin]?=?vl_argparse(opts?varargin)?;%調(diào)用函數(shù)修改默認(rèn)參數(shù)配置
%對結(jié)構(gòu)體opts中的內(nèi)容,用varargin進(jìn)行更新,opts中沒有的元素復(fù)制到varargin中
if?~isempty(opts.solver)%如果opts.solver不是空集
??assert(isa(opts.solver?‘function_handle‘)?&&?nargout(opts.solver)?==?2...%?isa判斷輸入內(nèi)容是否為指定類的對象,是的話返回true
????‘Invalid?solver;?expected?a?function?handle?with?two?outputs.‘)?;%assert如果cond是false則引發(fā)錯誤并且返回信息。
%如果不為空,則當(dāng)opts.solver是函數(shù)句柄并且輸出的參數(shù)數(shù)目為2時才能繼續(xù)
??%?Call?without?input?arguments?to?get?default?options
??opts.solverOpts?=?opts.solver()?;
end
opts.momentum?=?0.9?;
opts.saveSolverState?=?true?;
opts.nesterovUpdate?=?false?;
opts.randomSeed?=?0?;
opts.memoryMapFile?=?fullfile(tempdir?‘matconvnet.bin‘)?;%tempdir系統(tǒng)的緩存目錄。選擇內(nèi)存映射文件
opts.profile?=?false?;%用于觀察每句程序的耗時
opts.parameterServer.method?=?‘mmap‘?;
opts.parameterServer.prefix?=?‘mcn‘?;%詞頭
opts.conserveMemory?=?true?;%保存內(nèi)存
opts.backPropDepth?=?+inf?;%bp算法的深度
opts.sync?=?false?;%同步
opts.cudnn?=?true?;
opts.errorFunction?=?‘multiclass‘?;%多類誤差函數(shù)
opts.errorLabels?=?{}?;%初始化錯誤標(biāo)簽為空,誤差的類別,如top1error
opts.plotDiagnostics?=?false?;%是否繪制診斷信息
opts.plotStatistics?=?true;%是否繪制過程統(tǒng)計信息
opts.postEpochFn?=?[]?;??%?postEpochFn(netparamsstate)?called?after?each?epoch;?can?return?a?new?learning?rate?0?to?stop?[]?for?no?change
%每次之后可以更換學(xué)習(xí)速率
opts?=?vl_argparse(opts?varargin)?;%調(diào)用函數(shù)修改默認(rèn)參數(shù)配置
%%
%初始化準(zhǔn)備工作
if?~exist(opts.expDir?‘dir‘)?mkdir(opts.expDir)?;?end%如果不存在保存路徑,就創(chuàng)建它
if?isempty(opts.train)?opts.train?=?find(i
評論
共有 條評論