機器學習算法中怎樣選取超參數:學習速率、正則項系數、minibatch size


本文是《Neural networks and deep learning》概覽 中第三章的一部分,講機器學習算法中,怎樣選取初始的超參數的值。(本文會不斷補充)



學習速率(learning rate,η)

運用梯度下降算法進行優化時。權重的更新規則中,在梯度項前會乘以一個系數,這個系數就叫學習速率η。

以下討論在訓練時選取η的策略。

  • 固定的學習速率。

    假設學習速率太小,則會使收斂過慢。假設學習速率太大。則會導致代價函數振盪,例如以下圖所看到的。就下圖來說。一個比較好的策略是先將學習速率設置為0.25,然后在訓練到第20個Epoch時。學習速率改為0.025。

關於為什么學習速率太大時會振盪,看看這張圖就知道了。綠色的球和箭頭代表當前所處的位置,以及梯度的方向。學習速率越大,那么往箭頭方向前進得越多。假設太大則會導致直接跨過谷底到達還有一端,所謂“步子太大,邁過山谷”。

在實踐中。怎么粗略地確定一個比較好的學習速率呢?好像也僅僅能通過嘗試。你能夠先把學習速率設置為0.01,然后觀察training cost的走向。假設cost在減小。那你能夠逐步地調大學習速率,試試0.1,1.0….假設cost在增大,那就得減小學習速率,試試0.001,0.0001….經過一番嘗試之后,你能夠大概確定學習速率的合適的值。

為什么是依據training cost來確定學習速率。而不是依據validation accuracy來確定呢?這里直接引用一段話,有興趣能夠看看:

This all seems quite straightforward. However, using the training cost to pick η appears to contradict what I said earlier in this section, namely, that we’d pick hyper-parameters by evaluating performance using our held-out validation data. In fact, we’ll use validation accuracy to pick the regularization hyper-parameter, the mini-batch size, and network parameters such as the number of layers and hidden neurons, and so on. Why do things differently for the learning rate? Frankly, this choice is my personal aesthetic preference, and is perhaps somewhat idiosyncratic. The reasoning is that the other hyper-parameters are intended to improve the final classification accuracy on the test set, and so it makes sense to select them on the basis of validation accuracy. However, the learning rate is only incidentally meant to impact the final classification accuracy. It’s primary purpose is really to control the step size in gradient descent, and monitoring the training cost is the best way to detect if the step size is too big. With that said, this is a personal aesthetic preference. Early on during learning the training cost usually only decreases if the validation accuracy improves, and so in practice it’s unlikely to make much difference which criterion you use.



Early Stopping

所謂early stopping。即在每個epoch結束時(一個epoch即對全部訓練數據的一輪遍歷)計算 validation data的accuracy。當accuracy不再提高時,就停止訓練。這是非常自然的做法,由於accuracy不再提高了。訓練下去也沒用。另外。這樣做還能防止overfitting。

那么。怎么樣才算是validation accuracy不再提高呢?並非說validation accuracy一降下來。它就是“不再提高”,由於可能經過這個epoch后,accuracy減少了,可是隨后的epoch又讓accuracy升上去了,所以不能依據一兩次的連續減少就推斷“不再提高”。正確的做法是。在訓練的過程中。記錄最佳的validation accuracy,當連續10次epoch(或者很多其它次)沒達到最佳accuracy時,你能夠覺得“不再提高”,此時使用early stopping。

這個策略就叫“ no-improvement-in-n”,n即epoch的次數,能夠依據實際情況取10、20、30….



可變的學習速率

在前面我們講了怎么尋找比較好的learning rate,方法就是不斷嘗試。在一開始的時候,我們能夠將其設大一點。這樣就能夠使weights快一點發生改變,從而讓你看出cost曲線的走向(上升or下降)。進一步地你就能夠決定增大還是減小learning rate。

可是問題是,找出這個合適的learning rate之后。我們前面的做法是在訓練這個網絡的整個過程都使用這個learning rate。這顯然不是好的方法,在優化的過程中,learning rate應該是逐步減小的,越接近“山谷”的時候。邁的“步伐”應該越小。

在講前面那張cost曲線圖時,我們說能夠先將learning rate設置為0.25,到了第20個epoch時候設置為0.025。這是人工的調節。並且是在畫出那張cost曲線圖之后做出的決策。能不能讓程序在訓練過程中自己主動地決定在哪個時候減小learning rate?

答案是肯定的,並且做法非常多。

一個簡單有效的做法就是。當validation accuracy滿足 no-improvement-in-n規則時,本來我們是要early stopping的,可是我們能夠不stop,而是讓learning rate減半。之后讓程序繼續跑。

下一次validation accuracy又滿足no-improvement-in-n規則時,我們相同再將learning rate減半(此時變為原始learni rate的四分之中的一個)…繼續這個過程,直到learning rate變為原來的1/1024再終止程序。(1/1024還是1/512還是其它能夠依據實際確定)。【PS:也能夠選擇每一次將learning rate除以10,而不是除以2.】

A readable recent paper which demonstrates the benefits of variable learning rates in attacking MNIST.《Deep Big Simple Neural Nets Excel on HandwrittenDigit Recognition》



正則項系數(regularization parameter, λ)

正則項系數初始值應該設置為多少。好像也沒有一個比較好的准則。

建議一開始將正則項系數λ設置為0。先確定一個比較好的learning rate。

然后固定該learning rate。給λ一個值(比方1.0),然后依據validation accuracy。將λ增大或者減小10倍(增減10倍是粗調節,當你確定了λ的合適的數量級后,比方λ = 0.01,再進一步地細調節,比方調節為0.02,0.03,0.009之類。)

在《Neural Networks:Tricks of the Trade》中的第三章『A Simple Trick for Estimating the Weight Decay Parameter』中,有關於怎樣預計權重衰減項系數的討論,有基礎的讀者能夠看一下。



Mini-batch size

首先說一下採用mini-batch時的權重更新規則。

比方mini-batch size設為100。則權重更新的規則為:

也就是將100個樣本的梯度求均值。替代online learning方法中單個樣本的梯度值:

當採用mini-batch時,我們能夠將一個batch里的全部樣本放在一個矩陣里,利用線性代數庫來加速梯度的計算。這是project實現中的一個優化方法。

那么,size要多大?一個大的batch,能夠充分利用矩陣、線性代數庫來進行計算的加速,batch越小,則加速效果可能越不明顯。

當然batch也不是越大越好。太大了,權重的更新就會不那么頻繁。導致優化過程太漫長。所以mini-batch size選多少,不是一成不變的,依據你的數據集規模、你的設備計算能力去選。

The way to go is therefore to use some acceptable (but not necessarily optimal) values for the other hyper-parameters, and then trial a number of different mini-batch sizes, scaling η as above. Plot the validation accuracy versus time (as in, real elapsed time, not epoch!), and choose whichever mini-batch size gives you the most rapid improvement in performance. With the mini-batch size chosen you can then proceed to optimize the other hyper-parameters.



很多其它資料

LeCun在1998年的論文《Efficient BackProp》

Bengio在2012年的論文《Practical recommendations for gradient-based training of deep architectures》,給出了一些建議。包含梯度下降、選取超參數的具體細節。

以上兩篇論文都被收錄在了2012年的書《Neural Networks: Tricks of the Trade》里面,這本書里還給出了非常多其它的tricks。



轉載請注明出處:http://blog.csdn.net/u012162613/article/details/44265967


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM