ML科普系列(一)训练集、测试集和验证集

训练集验证集测试集这三个名词在机器学习领域极其常见,但不少人并非特别清楚,尤为是后两个常常被人混用。网络

在有监督(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.

训练集

做用:估计模型学习

学习样本数据集,经过匹配一些参数来创建一个分类器。创建一种分类的方式,主要是用来训练模型的。测试

验证集

做用:肯定网络结构或者控制模型复杂程度的参数优化

对学习出来的模型,调整分类器的参数,如在神经网络中选择隐藏单元数。验证集还用来肯定网络结构或者控制模型复杂程度的参数。spa

测试集

做用:检验最终选择最优的模型的性能如何code

主要是测试训练好的模型的分辨能力(识别率等)orm

为什么须要划分

Ripley也谈到了这个问题:Why separate test and validation sets? blog

  • 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