技術指南

巢狀交叉驗證

巢狀交叉驗證使用內部迴圈來選擇模型的設定,並使用外部迴圈在保留資料上評估所產生的選擇程序。

聆聽此指南AI 語音敘述 · ~3 min

本頁閱讀時間3分鐘
  1. 概述
  2. 深入探討
  3. 現實世界的實施
  4. 嵌套交叉驗證的未來
  5. 付諸實踐
  6. 不斷探索
  7. 常見問題

概述

它降低了因同時使用驗證分數來選擇設定與聲稱效能而產生的過度樂觀評估。

深入探討

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,並使用外層交叉驗證流程評估該搜尋。預處理在相關的訓練折疊內學習。

嵌套交叉驗證的未來

隨著自動化模型搜尋變得更容易啟動,評估系統應該讓調校與測試之間的界線變得清晰可見。一份有用的報告會記錄完整的選擇流程以及允許檢視的資料。團隊就能將改進與一個固定的程序比較,而不是一再針對相同的測試回饋進行優化。計算資源的限制仍然重要,尤其是對於昂貴的模型。適當的做法是選擇符合專案需求的評估設計,並說明其限制,而不是把額外的交叉驗證層級視為普遍必須的要求。

付諸實踐

為什麼這很重要

  • 成本與預算. 多年來,架構決策決定著效能和營運成本。
  • 更明確的決策. 技術教育幫助團隊選擇正確的堆疊,而不僅僅是最新的堆疊。
  • 品質管控. 更好的工程選擇可以減少生產中的可靠性事故。

留意

  • 優化一項基準測試可以隱藏更廣泛的系統弱點。
  • 基礎設施和維護成本常常被低估。
  • 隨著系統變得更加複雜,安全性和可觀察性差距可能會擴大。

後續步驟

  1. 在實施之前定義延遲、品質和成本目標。
  2. 在實際負載和資料條件下進行基準測試。
  3. 儀器監控錯誤、漂移和使用者影響。
  4. 在擴展之前準備回滾和事件回應路徑。

不斷探索

免費電子報

獲取每日 AI 簡報

每個工作日上午提供三則經過驗證的 AI 新聞,以淺顯易懂的英文撰寫。永久免費,無廣告。

每個工作日一封電子郵件。一鍵取消訂閱。我們絕不出售或分享您的地址。

測試一下自己

參加 Nested Cross-Validation 小測驗

每個答案皆有即時回饋,通過課程後可獲得具備可驗證 ID 的可分享證書。

開始測驗

支持免費的 AI 教育。AI Understanding 是一個 501(c)(3) 非營利組織——絕無廣告,絕無付費牆。 捐款

常見問題

什麼是 Nested Cross-Validation?

Nested cross-validation 會使用內部迴圈來挑選模型的設定,外部迴圈則在保留資料上評估所得到的選擇程序。它能降低在同時使用驗證分數來挑選設定與聲稱效能時可能產生的過度樂觀評估。

在一次外部切分中,內部超參數搜尋可以使用哪些資料?

外部測試折必須保留在選擇過程之外,以便在保留資料上評估該程序。

為什麼報告最佳內部驗證分數會高估模型效能?

選擇過程使用了驗證結果,因此最高分數並不是對該選擇過程的獨立評估。

五個外部折、三個內部折與十個候選設定,在指南範例中需要多少次內部候選擬合?

將外層折疊、內層折疊與候選值相乘:五乘三乘十等於 150。

在開始嵌套交叉驗證前,先在完整資料集上擬合比例因子。還剩下什麼問題?

嵌套不會撤銷在分割前引入的資訊洩漏。預處理必須在相關的訓練折疊內擬合。

不同的外層折疊會選擇不同的超參數。這應該如何詮釋?

外層迴圈評估選擇程序,其選定的設定可能因其訓練樣本而變化。