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

如何在tfds.load中实现80/10/10比例的训练/验证/测试集划分?

解决TensorFlow Datasets中colorectal_histology数据集80/10/10划分问题

要实现80%训练集、10%验证集、10%测试集的划分,你需要利用TFDS的分片语法来切分数据集的默认train拆分(因为colorectal_histology数据集本身只提供了train这一个原始拆分)。

正确的split参数设置

将split列表设置为以下内容:

split=['train[:80%]', 'train[80%:90%]', 'train[90%:]']
  • train[:80%]:取原始训练集的前80%作为新的训练集
  • train[80%:90%]:取原始训练集80%到90%之间的部分作为验证集
  • train[90%:]:取原始训练集最后10%的部分作为测试集

修改后的完整代码

import matplotlib.pyplot as plt
import tensorflow_datasets as tfds

def plot_example(x_raw, y_raw):
  fig, axes = plt.subplots(3, 3)
  i = 0
  for i in range(3):
    for j in range(3):
      imgplot = axes[i,j].imshow(x_raw[i*3 + j], cmap = 'bone')
      axes[i,j].set_title(y_raw[i*3 + j])
      axes[i,j].get_yaxis().set_visible(False)
      axes[i,j].get_xaxis().set_visible(False)
  fig.set_size_inches(18.5, 10.5, forward=True)

# 实现80%训练、10%验证、10%测试的划分
(ds_train, ds_val, ds_test), ds_info = tfds.load("colorectal_histology", 
                                           split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
                                           as_supervised=True, with_info=True)
df = tfds.as_dataframe(ds_train.shuffle(1000).take(1000), ds_info)

plot_example(df['image'], df['label'])
print(ds_info)

补充说明

默认情况下TFDS的分片规则是固定的,不会每次加载都随机变化。如果需要强制固定划分结果,可添加shuffle_files=False参数避免文件级别的随机打乱。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 23:50:28