概述
它减少了当同一验证分数既用于选择设置又用于声称性能时可能出现的乐观评估。
深入探讨
Searching many model settings is itself a form of learning from data. Even when every candidate uses cross-validation, choosing the candidate with the best score favors settings that benefited from random variation in those validation folds. Reporting that winning score as a fresh performance estimate can therefore be optimistic. Nested cross-validation separates selection from evaluation. Start with an outer split. Set aside its test fold and conduct the entire search using only the outer training data. Inner cross-validation compares the candidate settings. Refit the selected configuration on the outer training data, evaluate it once on the outer test fold, and repeat this process for the other outer folds. The outer scores evaluate a procedure: the model family, search space, preprocessing and selection rule used together. Different outer folds may select different settings. That is expected, because their training data differ. The result is not a competition in which you simply deploy whichever outer-fold model achieved the highest test score. Preprocessing belongs inside the procedure. A scaler, imputer or feature selector fitted on all examples before splitting can leak information. Scikit-learn's Pipeline helps keep these operations attached to model fitting, while GridSearchCV can perform the inner search. The split strategy must also match the intended use. Repeated records from one customer may need group separation. Forecasting requires respect for time order. Nesting an unsuitable random split does not repair that design problem. After evaluating the fixed procedure, perform its selection step on the available development data and fit a final model. Preserve a separate test set if the project uses one. Repeatedly changing the procedure after inspecting outer scores can turn the outer evaluation into another tuning process.
现实世界的实施
研究人员在每个外部训练拆分内比较几种正则化强度。只有被选中的模型会在该拆分的外部测试折上进行评估。
一个假设的搜索使用五个外部折、三个内部折和十个候选设置。它执行150次内部候选拟合,并在每个外部折中对选定模型重新拟合一次,共计五次重新拟合。
一个模型使用每个客户的多条记录。当团队想要估计对未见客户的性能时,会在内部和外部拆分中保持客户分离。
分析师将 scikit-learn Pipeline 放入 GridSearchCV 并使用外层交叉验证程序评估该搜索。预处理在相关的训练折叠内学习。
嵌套交叉验证的未来
随着自动化模型搜索变得更容易启动,评估系统应当让调参与测试的界限清晰可见。一个有用的报告会记录完整的选择配方以及它被允许检查的数据。团队随后可以将改进与一个固定的流程进行比较,而不是在相同的测试反馈上反复优化。计算资源的限制仍然重要,尤其是对于昂贵的模型。恰当的做法是选择适合项目的评估设计并记录其局限性,而不是把额外的交叉验证层视为通用要求。
付诸实践
为什么这很重要
- 成本与预算. 多年来,架构决策决定着性能和运营成本。
- 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
- 质量控制. 更好的工程选择可以减少生产中的可靠性事故。
留意
- 优化一项基准测试可以隐藏更广泛的系统弱点。
- 基础设施和维护成本常常被低估。
- 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。
后续步骤
- 在实施之前定义延迟、质量和成本目标。
- 在实际负载和数据条件下进行基准测试。
- 仪器监控错误、漂移和用户影响。
- 在扩展之前准备回滚和事件响应路径。
不断探索
常见问题
什么是嵌套交叉验证?
嵌套交叉验证使用内部循环来选择模型的超参数,使用外部循环在留出的数据上评估得到的选择过程。它可以降低在同一验证分数既用于选择参数又用于声称性能时产生的乐观评估。
在一次外层拆分中,内层超参数搜索可以使用哪些数据?
外部测试折必须保持在选择过程之外,以便在留出数据上评估该过程。
为什么报告最佳内层验证分数会夸大模型性能?
选择过程使用验证结果,因此获胜分数并不是对该选择过程的独立评估。
在指南的示例中,五个外层折、三个内层折和十个候选设置需要多少次内层候选拟合?
将外部折、内部折和候选数量相乘:五乘以三乘以十等于一百五十。
在嵌套交叉验证开始之前,缩放器已在完整数据集上进行了拟合。这会留下什么问题?
嵌套并不能消除在划分之前引入的信息泄漏。预处理必须在相应的训练折内部完成。
不同的外层折选择了不同的超参数。这应该如何解读?
外层循环评估选择过程,其所选设置可能随训练样本而变化。