You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

深度学习模型验证数据使用方案咨询:三种流程孰优孰劣?

深度学习中验证数据的正确使用方式

你的判断是对的——第一种和第三种流程是正确的,第二种流程存在严重问题,以下是具体分析:

各流程细节解析

第一种:手动拆分训练/验证集

这是行业内最规范的做法之一,完全明确了三个数据集的职责边界:

  1. 先将全量数据拆分为训练集+测试集(测试集占20%),测试集全程不参与任何训练相关环节,仅用于最终的泛化能力评估
  2. 再从训练集中拆分出独立的验证集(占训练集的20%,对应全量数据的16%)
  3. 训练时用训练集拟合模型,用验证集监控过拟合、触发早停逻辑
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, shuffle=True, stratify=y)

X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42, stratify=y_train)

# Training the model and saving the history
history = model.fit(X_train, y_train, epochs=150,
                    batch_size=128, validation_data=(X_val, y_val), callbacks=[early_stopping])

这种方式的优势是验证集与测试集完全独立,最终测试集给出的评估结果能真实反映模型在未知数据上的泛化能力。

第二种:用测试集充当验证集

这是绝对错误的操作:
将测试集作为验证集使用时,模型在训练过程中会间接“学习”到测试集的特征——比如早停机制会基于测试集的性能调整训练周期,本质上让模型拟合了测试集数据。最终用测试集评估的结果会严重高估模型的泛化能力,彻底失去了测试集作为“最终客观裁判”的意义。

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, shuffle=True, stratify=y)

# Training the model and saving the history
history = model.fit(X_train, y_train, epochs=150,
                    batch_size=128, validation_data=(X_test, y_test), callbacks=[early_stopping])

第三种:用validation_split自动拆分

这也是正确的做法,核心逻辑和第一种一致:
Keras的validation_split=0.2会自动从训练集的末尾拆分出20%的数据作为验证集。需要注意的是,如果训练集在train_test_split后未打乱,要确保数据分布均匀(可手动shuffle,或依赖fit默认的shuffle=True参数在每次epoch前打乱训练集)。

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, shuffle=True, stratify=y)

# Training the model and saving the history
history = model.fit(X_train, y_train, epochs=150,
                    batch_size=128, validation_split=0.2, callbacks=[early_stopping])

内容的提问来源于stack exchange,提问作者xavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 15:45:54