上文提到,到目前為止,caffe總共提供了六種優化方法:
- 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"
)
Solver就是用來使loss最小化的優化方法。對於一個數據集D,需要優化的目標函數是整個數據集中所有數據loss的平均值。
其中,fW(x(i))計算的是數據x(i)上的loss, 先將每個單獨的樣本x的loss求出來,然后求和,最后求均值。 r(W)是正則項(weight_decay),為了減弱過擬合現象。
如果采用這種Loss 函數,迭代一次需要計算整個數據集,在數據集非常大的這情況下,這種方法的效率很低,這個也是我們熟知的梯度下降采用的方法。
在實際中,通過將整個數據集分成幾批(batches), 每一批就是一個mini-batch,其數量(batch_size)為N<<|D|,此時的loss 函數為:
有了loss函數后,就可以迭代的求解loss和梯度來優化這個問題。在神經網絡中,用forward pass來求解loss,用backward pass來求解梯度。
在caffe中,默認采用的Stochastic Gradient Descent(SGD)進行優化求解。后面幾種方法也是基於梯度的優化方法(like SGD),因此本文只介紹一下SGD。其它的方法,有興趣的同學,可以去看文獻原文。
1、Stochastic gradient descent(SGD)
隨機梯度下降(Stochastic gradient descent)是在梯度下降法(gradient descent)的基礎上發展起來的,梯度下降法也叫最速下降法,具體原理在網易公開課《機器學習》中,吳恩達教授已經講解得非常詳細。SGD在通過負梯度和上一次的權重更新值Vt的線性組合來更新W,迭代公式如下:
其中, 是負梯度的學習率(base_lr),
是上一次梯度值的權重(momentum),用來加權之前梯度方向對現在梯度下降方向的影響。這兩個參數需要通過tuning來得到最好的結果,一般是根據經驗設定的。如果你不知道如何設定這些參數,可以參考相關的論文。
在深度學習中使用SGD,比較好的初始化參數的策略是把學習率設為0.01左右(base_lr: 0.01),在訓練的過程中,如果loss開始出現穩定水平時,對學習率乘以一個常數因子(gamma),這樣的過程重復多次。
對於momentum,一般取值在0.5--0.99之間。通常設為0.9,momentum可以讓使用SGD的深度學習方法更加穩定以及快速。
關於更多的momentum,請參看Hinton的《A Practical Guide to Training Restricted Boltzmann Machines》。
實例:
base_lr: 0.01 lr_policy: "step" gamma: 0.1 stepsize: 1000 max_iter: 3500 momentum: 0.9
lr_policy設置為step,則學習率的變化規則為 base_lr * gamma ^ (floor(iter / stepsize))
即前1000次迭代,學習率為0.01; 第1001-2000次迭代,學習率為0.001; 第2001-3000次迭代,學習率為0.00001,第3001-3500次迭代,學習率為10-5
上面的設置只能作為一種指導,它們不能保證在任何情況下都能得到最佳的結果,有時候這種方法甚至不work。如果學習的時候出現diverge(比如,你一開始就發現非常大或者NaN或者inf的loss值或者輸出),此時你需要降低base_lr的值(比如,0.001),然后重新訓練,這樣的過程重復幾次直到你找到可以work的base_lr。
2、AdaDelta
AdaDelta是一種”魯棒的學習率方法“,是基於梯度的優化方法(like SGD)。
具體的介紹文獻:
M. Zeiler ADADELTA: AN ADAPTIVE LEARNING RATE METHOD. arXiv preprint, 2012.
示例:
net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_interval: 500 base_lr: 1.0 lr_policy: "fixed" momentum: 0.95 weight_decay: 0.0005 display: 100 max_iter: 10000 snapshot: 5000 snapshot_prefix: "examples/mnist/lenet_adadelta" solver_mode: GPU type: "AdaDelta" delta: 1e-6
從最后兩行可看出,設置solver type為Adadelta時,需要設置delta的值。
3、AdaGrad
自適應梯度(adaptive gradient)是基於梯度的優化方法(like SGD)
具體的介紹文獻:
Duchi, E. Hazan, and Y. Singer. Adaptive Subgradient Methods for Online Learning and Stochastic Optimization. The Journal of Machine Learning Research, 2011.
示例:
net: "examples/mnist/mnist_autoencoder.prototxt" test_state: { stage: 'test-on-train' } test_iter: 500 test_state: { stage: 'test-on-test' } test_iter: 100 test_interval: 500 test_compute_loss: true base_lr: 0.01 lr_policy: "fixed" display: 100 max_iter: 65000 weight_decay: 0.0005 snapshot: 10000 snapshot_prefix: "examples/mnist/mnist_autoencoder_adagrad_train" # solver mode: CPU or GPU solver_mode: GPU type: "AdaGrad"
4、Adam
是一種基於梯度的優化方法(like SGD)。
具體的介紹文獻:
D. Kingma, J. Ba. Adam: A Method for Stochastic Optimization. International Conference for Learning Representations, 2015.
5、NAG
Nesterov 的加速梯度法(Nesterov’s accelerated gradient)作為凸優化中最理想的方法,其收斂速度非常快。
具體的介紹文獻:
I. Sutskever, J. Martens, G. Dahl, and G. Hinton. On the Importance of Initialization and Momentum in Deep Learning. Proceedings of the 30th International Conference on Machine Learning, 2013.
示例:
net: "examples/mnist/mnist_autoencoder.prototxt" test_state: { stage: 'test-on-train' } test_iter: 500 test_state: { stage: 'test-on-test' } test_iter: 100 test_interval: 500 test_compute_loss: true base_lr: 0.01 lr_policy: "step" gamma: 0.1 stepsize: 10000 display: 100 max_iter: 65000 weight_decay: 0.0005 snapshot: 10000 snapshot_prefix: "examples/mnist/mnist_autoencoder_nesterov_train" momentum: 0.95 # solver mode: CPU or GPU solver_mode: GPU type: "Nesterov"
6、RMSprop
RMSprop是Tieleman在一次 Coursera課程演講中提出來的,也是一種基於梯度的優化方法(like SGD)
具體的介紹文獻:
T. Tieleman, and G. Hinton. RMSProp: Divide the gradient by a running average of its recent magnitude. COURSERA: Neural Networks for Machine Learning.Technical report, 2012.
示例:
net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_interval: 500 base_lr: 1.0 lr_policy: "fixed" momentum: 0.95 weight_decay: 0.0005 display: 100 max_iter: 10000 snapshot: 5000 snapshot_prefix: "examples/mnist/lenet_adadelta" solver_mode: GPU type: "RMSProp" rms_decay: 0.98
最后兩行,需要設置rms_decay值。