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

模型训练评估分文件:测试数据应重新拆分还是复用原拆分集?

模型训练与评估的数据集拆分问题

问题背景

现有train.py实现模型训练逻辑,通过train_test_split划分数据集后训练模型,并保存权重到weights/目录,代码如下:

x_train, x_test, y_train, y_test = train_test_split(x, y)
model = compile()
model.fit(x_train, y_train)
model.save_weights("weights/")

另有evaluate.py负责加载训练好的权重评估模型性能,当前代码会再次执行train_test_split划分数据集:

x_train, x_test, y_train, y_test = train_test_split(x, y)
model = compile()
model.load_weights("weights/")
model.evaluate(x_test, y_test)

疑问

evaluate.py中重新执行train_test_split是否正确?是否应该复用train.py中已拆分好的测试集?


解答

结论:evaluate.py中重新执行train_test_split是错误的,必须复用train.py中拆分好的测试集

原因:

  • train_test_split默认采用随机方式划分数据集,两次执行会生成不同的训练/测试集组合。如果用新拆分的测试集评估模型,相当于用模型完全没接触过的陌生数据测试,无法准确反映模型在训练阶段对应测试集上的泛化能力,评估结果不具备参考价值和可比性。
  • 在标准机器学习流程中,训练集和测试集必须严格分离且测试集固定不变,这样才能保证每次评估的基准一致,方便后续模型迭代优化时的性能对比验证。

修正后的代码

train.py

x_train, x_test, y_train, y_test = train_test_split(x, y)
# 保存训练时拆分好的测试集
np.save("x_test.npy", x_test) 
np.save("y_test.npy", y_test) 
model = compile()
model.fit(x_train, y_train)
model.save_weights("weights/")

evaluate.py

# 加载训练阶段保存的测试集
x_test = np.load("x_test.npy")
y_test = np.load("y_test.npy")
model = compile()
model.load_weights("weights/")
model.evaluate(x_test, y_test)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 11:35:13