-
大小: 28KB文件類型: .m金幣: 1下載: 0 次發布日期: 2021-05-11
- 語言: Matlab
- 標簽: matconvnet??cnn_train.m??
資源簡介
對matconvnet中用于cnn訓練的cnn_train.m文件進行了詳細的注釋。后續會進行更新,并且會把相關的函數也進行注釋
代碼片段和文件信息
%調用cnn_train:??
%?[?net?info?]?=?cnn_train(net?imdb?@getBatch?opts.train?‘val‘?find(imdb.images.set?==?3))?;??
%%
%主體函數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實現,可以根據getBatch函數不同用于不同的數據集和任務
%????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根據
%?the?terms?of?the?BSD?license?(see?the?COPYING?file).
addpath(fullfile(vl_rootnn?‘examples‘));%添加examples的路徑
opts.expDir?=?fullfile(‘data‘‘exp‘)?;%選擇保存路徑
opts.continue?=?true?;%每次重啟都是接著上次訓練狀態開始
opts.batchSize?=?256?;%選擇初始化批大小為256
opts.numSubBatches?=?1?;%選擇子批的個數為1(不劃分子批)
opts.train?=?[]?;%初始化訓練集索引為空
opts.val?=?[]?;%初始化驗證集索引為空
opts.gpus?=?[]?;%選擇gpu
opts.epochSize?=?inf;%inf無窮大量,
opts.prefetch?=?false?;%選擇是否預讀取下一批次的樣本,初始為否
opts.numEpochs?=?300?;%選擇epoch數量
opts.learningRate?=?0.001?;
opts.weightDecay?=?0.0005?;
opts.solver?=?[]?;??%?Empty?array?means?use?the?default?SGD?solver使用默認的SGDsolver訓練
[opts?varargin]?=?vl_argparse(opts?varargin)?;%調用函數修改默認參數配置
%對結構體opts中的內容,用varargin進行更新,opts中沒有的元素復制到varargin中
if?~isempty(opts.solver)%如果opts.solver不是空集
??assert(isa(opts.solver?‘function_handle‘)?&&?nargout(opts.solver)?==?2...%?isa判斷輸入內容是否為指定類的對象,是的話返回true
????‘Invalid?solver;?expected?a?function?handle?with?two?outputs.‘)?;%assert如果cond是false則引發錯誤并且返回信息。
%如果不為空,則當opts.solver是函數句柄并且輸出的參數數目為2時才能繼續
??%?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系統的緩存目錄。選擇內存映射文件
opts.profile?=?false?;%用于觀察每句程序的耗時
opts.parameterServer.method?=?‘mmap‘?;
opts.parameterServer.prefix?=?‘mcn‘?;%詞頭
opts.conserveMemory?=?true?;%保存內存
opts.backPropDepth?=?+inf?;%bp算法的深度
opts.sync?=?false?;%同步
opts.cudnn?=?true?;
opts.errorFunction?=?‘multiclass‘?;%多類誤差函數
opts.errorLabels?=?{}?;%初始化錯誤標簽為空,誤差的類別,如top1error
opts.plotDiagnostics?=?false?;%是否繪制診斷信息
opts.plotStatistics?=?true;%是否繪制過程統計信息
opts.postEpochFn?=?[]?;??%?postEpochFn(netparamsstate)?called?after?each?epoch;?can?return?a?new?learning?rate?0?to?stop?[]?for?no?change
%每次之后可以更換學習速率
opts?=?vl_argparse(opts?varargin)?;%調用函數修改默認參數配置
%%
%初始化準備工作
if?~exist(opts.expDir?‘dir‘)?mkdir(opts.expDir)?;?end%如果不存在保存路徑,就創建它
if?isempty(opts.train)?opts.train?=?find(i
- 上一篇:數據包絡法DEA)matlab代碼
- 下一篇:最優化 外點罰函數 有matlab程序
評論
共有 條評論