1. 程式人生 > >機器學習演算法中如何選取超引數 學習速率 正則項係數 minibatch size

機器學習演算法中如何選取超引數 學習速率 正則項係數 minibatch size

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                       

本文是《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裡的所有樣本放在一個矩陣裡,利用線性代數庫來加速梯度的計算,這是工程實現中的一個優化方法。

那麼,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

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述