訓練集、驗證集和測試集這三個名詞在機器學習領域極其常見,但很多人並不是特別清楚,尤其是后兩個經常被人混用。
在有監督(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
