1. 程式人生 > >訓練集,驗證集和測試集介紹和交叉驗證法介紹

訓練集,驗證集和測試集介紹和交叉驗證法介紹

訓練集驗證集測試集這三個名詞在機器學習領域極其常見,但很多人並不是特別清楚,尤其是後兩個經常被人混用。

在有監督(supervise)的機器學習中,資料集常被分成2~3個,即:訓練集(train set),驗證集(validation set),測試集(test set)。

Ripley, B.D(1996)在他的經典專著Pattern Recognition and Neural Networks中給出了這三個詞的定義。 

  • Training set: A set of examples used for learning, which is to fit the parameters [i.e., weights] of the classifier.
  • Validation set: A set of examples used to tune the parameters [i.e., architecture, not weights] of a classifier, for example to choose the number of hidden units in a neural network.
  • Test set: A set of examples used only to assess the performance [generalization] of a fully specified classifier.

訓練集

作用:估計模型

學習樣本資料集,通過匹配一些引數來建立一個分類器。建立一種分類的方式,主要是用來訓練模型的。

驗證集

作用:確定網路結構或者控制模型複雜程度的引數

對學習出來的模型,調整分類器的引數,如在神經網路中選擇隱藏單元數。驗證集還用來確定網路結構或者控制模型複雜程度的引數。

測試集

作用:檢驗最終選擇最優的模型的效能如何

主要是測試訓練好的模型的分辨能力(識別率等)

 

為何需要劃分

Ripley也談到了這個問題:Why separate test and validation sets? 

  • 1. The error rate estimate of the final model on validation data will be biased (smaller than the true error rate) since the validation set is used to select the final model. 
  • 2. After assessing the final model with the test set, YOU MUST NOT tune the model any further.

簡而言之,為了防止過度擬合。如果我們把所有資料都用來訓練模型的話,建立的模型自然是最契合這些資料的,測試表現也好。但換了其它資料集測試這個模型效果可能就沒那麼好了。就好像你給班上同學做校服,大家穿著都合適你就覺得按這樣做就對了,那給別的班同學穿呢?不合適的概率會高吧。總而言之訓練集和測試集相同的話,模型評估結果可能比實際要好。 

總結

顯然,training set是用來訓練模型或確定模型引數的,如ANN中權值等; validation set是用來做模型選擇(model selection),即做模型的最終優化及確定的,如ANN的結構;而 test set則純粹是為了測試已經訓練好的模型的推廣能力。當然,test set這並不能保證模型的正確性,他只是說相似的資料用此模型會得出相似的結果。但實際應用中,一般只將資料集分成兩類,即training set 和test set,大多數文章並不涉及validation set。

一個典型的劃分是訓練集佔總樣本的50%,而其它各佔25%,三部分都是從樣本中隨機抽取。

 

 

 

樣本少的時候,上面的劃分就不合適了。常用的是留少部分做測試集。然後對其餘N個樣本採用K折交叉驗證法。就是將樣本打亂,然後均勻分成K份,輪流選擇其中K-1份訓練,剩餘的一份做驗證,計算預測誤差平方和,最後把K次的預測誤差平方和再做平均作為選擇最優模型結構的依據。特別的K取N,就是留一法(leave one out)。

附上一段虛擬碼:

for each epoch
    for each training data instance
        propagate error through the network
        adjust the weights
        calculate the accuracy over training data
    for each validation data instance
        calculate the accuracy over the validation data
    if the threshold validation accuracy is met
        exit training
    else
        continue training

轉載自https://blog.csdn.net/cczx139/article/details/80266101