Batch_size问题
有關(guān)solver.prototxt中的各項(xiàng)解釋:http://www.cnblogs.com/denny402/p/5074049.html
solver算是caffe的核心的核心,它協(xié)調(diào)著整個(gè)模型的運(yùn)作。caffe程序運(yùn)行必帶的一個(gè)參數(shù)就是solver配置文件。運(yùn)行代碼一般為
# caffe train --solver=*_slover.prototxt在Deep Learning中,往往loss function是非凸的,沒有解析解,我們需要通過優(yōu)化方法來求解。solver的主要作用就是交替調(diào)用前向(forward)算法和后向(backward)算法來更新參數(shù),從而最小化loss,實(shí)際上就是一種迭代的優(yōu)化算法。
到目前的版本,caffe提供了六種優(yōu)化算法來求解最優(yōu)參數(shù),在solver配置文件中,通過設(shè)置type類型來選擇。
- Stochastic Gradient Descent (type: "SGD"),
 - AdaDelta (type: "AdaDelta"),
 - Adaptive Gradient (type: "AdaGrad"),
 - Adam (type: "Adam"),
 - Nesterov’s Accelerated Gradient (type: "Nesterov") and
 - RMSprop (type: "RMSProp")
 
?具體的每種方法的介紹,請(qǐng)看本系列的下一篇文章, 本文著重介紹solver配置文件的編寫。
Solver的流程:
1.?????設(shè)計(jì)好需要優(yōu)化的對(duì)象,以及用于學(xué)習(xí)的訓(xùn)練網(wǎng)絡(luò)和用于評(píng)估的測(cè)試網(wǎng)絡(luò)。(通過調(diào)用另外一個(gè)配置文件prototxt來進(jìn)行)
2.?????通過forward和backward迭代的進(jìn)行優(yōu)化來跟新參數(shù)。
3.?????定期的評(píng)價(jià)測(cè)試網(wǎng)絡(luò)。 (可設(shè)定多少次訓(xùn)練后,進(jìn)行一次測(cè)試)
4.?????在優(yōu)化過程中顯示模型和solver的狀態(tài)
在每一次的迭代過程中,solver做了這幾步工作:
1、調(diào)用forward算法來計(jì)算最終的輸出值,以及對(duì)應(yīng)的loss
2、調(diào)用backward算法來計(jì)算每層的梯度
3、根據(jù)選用的slover方法,利用梯度進(jìn)行參數(shù)更新
4、記錄并保存每次迭代的學(xué)習(xí)率、快照,以及對(duì)應(yīng)的狀態(tài)。
接下來,我們先來看一個(gè)實(shí)例:
net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_interval: 500 base_lr: 0.01 momentum: 0.9 type: SGD weight_decay: 0.0005 lr_policy: "inv" gamma: 0.0001 power: 0.75 display: 100 max_iter: 20000 snapshot: 5000 snapshot_prefix: "examples/mnist/lenet" solver_mode: CPU接下來,我們對(duì)每一行進(jìn)行詳細(xì)解譯:
net: "examples/mnist/lenet_train_test.prototxt"設(shè)置深度網(wǎng)絡(luò)模型。每一個(gè)模型就是一個(gè)net,需要在一個(gè)專門的配置文件中對(duì)net進(jìn)行配置,每個(gè)net由許多的layer所組成。每一個(gè)layer的具體配置方式可參考本系列文文章中的(2)-(5)。注意的是:文件的路徑要從caffe的根目錄開始,其它的所有配置都是這樣。
也可用train_net和test_net來對(duì)訓(xùn)練模型和測(cè)試模型分別設(shè)定。例如:
train_net: "examples/hdf5_classification/logreg_auto_train.prototxt" test_net: "examples/hdf5_classification/logreg_auto_test.prototxt"接下來第二行:
test_iter: 100這個(gè)要與test layer中的batch_size結(jié)合起來理解。mnist數(shù)據(jù)中測(cè)試樣本總數(shù)為10000,一次性執(zhí)行全部數(shù)據(jù)效率很低,因此我們將測(cè)試數(shù)據(jù)分成幾個(gè)批次來執(zhí)行,每個(gè)批次的數(shù)量就是batch_size。假設(shè)我們?cè)O(shè)置batch_size為100,則需要迭代100次才能將10000個(gè)數(shù)據(jù)全部執(zhí)行完。因此test_iter設(shè)置為100。執(zhí)行完一次全部數(shù)據(jù),稱之為一個(gè)epoch
test_interval: 500測(cè)試間隔。也就是每訓(xùn)練500次,才進(jìn)行一次測(cè)試。
base_lr: 0.01 lr_policy: "inv"gamma: 0.0001
power: 0.75
這四行可以放在一起理解,用于學(xué)習(xí)率的設(shè)置。只要是梯度下降法來求解優(yōu)化,都會(huì)有一個(gè)學(xué)習(xí)率,也叫步長(zhǎng)。base_lr用于設(shè)置基礎(chǔ)學(xué)習(xí)率,在迭代的過程中,可以對(duì)基礎(chǔ)學(xué)習(xí)率進(jìn)行調(diào)整。怎么樣進(jìn)行調(diào)整,就是調(diào)整的策略,由lr_policy來設(shè)置。
lr_policy可以設(shè)置為下面這些值,相應(yīng)的學(xué)習(xí)率的計(jì)算為:
-  
- - fixed: ?保持base_lr不變.
 - - step: ?如果設(shè)置為step,則還需要設(shè)置一個(gè)stepsize, ?返回 base_lr * gamma ^ (floor(iter / stepsize)),其中iter表示當(dāng)前的迭代次數(shù)
 - - exp: ? 返回base_lr * gamma ^ iter, iter為當(dāng)前迭代次數(shù)
 - - inv: ? ? 如果設(shè)置為inv,還需要設(shè)置一個(gè)power, 返回base_lr * (1 + gamma * iter) ^ (- power)
 - - multistep:?如果設(shè)置為multistep,則還需要設(shè)置一個(gè)stepvalue。這個(gè)參數(shù)和step很相似,step是均勻等間隔變化,而multistep則是根據(jù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stepvalue值變化
 - - poly: ?學(xué)習(xí)率進(jìn)行多項(xiàng)式誤差, 返回 base_lr (1 - iter/max_iter) ^ (power)
 - - sigmoid: 學(xué)習(xí)率進(jìn)行sigmod衰減,返回 base_lr ( 1/(1 + exp(-gamma * (iter - stepsize))))
 
 
multistep示例:
base_lr: 0.01 momentum: 0.9 weight_decay: 0.0005 # The learning rate policy lr_policy: "multistep" gamma: 0.9 stepvalue: 5000 stepvalue: 7000 stepvalue: 8000 stepvalue: 9000 stepvalue: 9500接下來的參數(shù):
momentum :0.9上一次梯度更新的權(quán)重,具體可參看下一篇文章。
type: SGD優(yōu)化算法選擇。這一行可以省掉,因?yàn)槟J(rèn)值就是SGD。總共有六種方法可選擇,在本文的開頭已介紹。
weight_decay: 0.0005權(quán)重衰減項(xiàng),防止過擬合的一個(gè)參數(shù)。
display: 100每訓(xùn)練100次,在屏幕上顯示一次。如果設(shè)置為0,則不顯示。
max_iter: 20000最大迭代次數(shù)。這個(gè)數(shù)設(shè)置太小,會(huì)導(dǎo)致沒有收斂,精確度很低。設(shè)置太大,會(huì)導(dǎo)致震蕩,浪費(fèi)時(shí)間。
snapshot: 5000 snapshot_prefix: "examples/mnist/lenet"快照。將訓(xùn)練出來的model和solver狀態(tài)進(jìn)行保存,snapshot用于設(shè)置訓(xùn)練多少次后進(jìn)行保存,默認(rèn)為0,不保存。snapshot_prefix設(shè)置保存路徑。
還可以設(shè)置snapshot_diff,是否保存梯度值,默認(rèn)為false,不保存。
也可以設(shè)置snapshot_format,保存的類型。有兩種選擇:HDF5 和BINARYPROTO ,默認(rèn)為BINARYPROTO
solver_mode: CPU設(shè)置運(yùn)行模式。默認(rèn)為GPU,如果你沒有GPU,則需要改成CPU,否則會(huì)出錯(cuò)。
有關(guān)train_val.prototxt中的batch_size與solver.prototxt中各項(xiàng)的關(guān)系:https://github.com/BVLC/caffe/issues/430#issuecomment-43961523
 
Regarding the batch_size=64 for training should be okay, although base_lr is linked to the batch_size, it allows some variability. Originally base_lr = 0.01 with batch_size=128, we have also used with batch_size=256 and still works. In theory when you reduce the batch_size by a factor of X then you should increase the base_lr by a factor of?sqrt(X), but Alex have used a factor of X (seehttp://arxiv.org/abs/1404.5997)
What you should change is the?stepsize?and?max_iter, accordingly to keep the same learning scheduling. If you divide the batch_size by X then you should multiply those by X.
Pay attention to the loss, if it doesn't go below 6.9 (which is basically random guessing) after 10k-20k iterations, then your training is not learning anything.
Training and testing share the data blobs and save quite a lot of memory:
 https://github.com/BVLC/caffe/pull/355
 
 https://www.zhihu.com/question/32673260
 
 
 
 
 
總結(jié)
以上是生活随笔為你收集整理的Batch_size问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 为什么黑咖啡可以减肥
 - 下一篇: 减肥期间可以喝茶吗