TensorFlow Datasets格式专用构建器详解
概述
TensorFlow Datasets(TFDS)提供了一系列格式专用的数据集构建器(Dataset Builders),这些构建器是tfds.core.GeneratorBasedBuilder
的子类,能够为特定数据格式处理大部分数据处理工作。本文将详细介绍几种主要的格式专用构建器及其使用方法。
基于tf.data.Dataset的构建器
基本概念
当需要从tf.data.Dataset
格式的数据创建TFDS数据集时,可以使用tfds.dataset_builders.TfDataBuilder
。这种构建器特别适合以下两种场景:
- 在类似笔记本的环境中创建实验性数据集
- 在代码中定义数据集构建器
笔记本环境中的使用示例
假设在笔记本中已经加载了数据并转换为tf.data.Dataset
格式,并应用了各种转换(如map、filter等),现在想要存储这些数据以便与团队成员共享或在其他笔记本中加载。
import tensorflow as tf
import tensorflow_datasets as tfds
# 创建训练和测试数据集
my_ds_train = tf.data.Dataset.from_tensor_slices({"number": [1, 2, 3]})
my_ds_test = tf.data.Dataset.from_tensor_slices({"number": [4, 5]})
# 定义构建器
builder = tfds.dataset_builders.TfDataBuilder(
name="my_dataset",
config="single_number",
version="1.0.0",
split_datasets={
"train": my_ds_train,
"test": my_ds_test,
},
features=tfds.features.FeaturesDict({
"number": tfds.features.Scalar(dtype=tf.int64),
}),
description="包含单个数字的数据集"
)
# 存储为TFDS数据集
builder.download_and_prepare()
数据集加载
存储后,可以通过以下方式加载数据集:
ds_test = tfds.load("my_dataset/single_number", split="test")
版本更新
当数据集发生变化时,可以轻松存储为新版本:
# 对数据进行转换
def add_one(example):
example["number"] += 1
return example
my_ds_train_v2 = my_ds_train.map(add_one)
my_ds_test_v2 = my_ds_test.map(add_one)
# 创建新版本构建器
builder_v2 = tfds.dataset_builders.TfDataBuilder(
name="my_dataset",
config="single_number",
version="1.1.0",
# ...其他参数...
)
builder_v2.download_and_prepare()
Croissant构建器
Croissant格式简介
Croissant是一种用于机器学习数据集的高级格式,它将元数据、资源文件描述、数据结构和默认ML语义组合到单个文件中,使数据集更易于查找、使用和支持工具。
使用示例
import tensorflow_datasets as tfds
builder = tfds.dataset_builders.CroissantBuilder(
jsonld="path/to/metadata.json",
file_format='array_record',
)
builder.download_and_prepare()
ds = builder.as_data_source()
CoNLL构建器
CoNLL格式简介
CoNLL是一种常用的文本标注数据格式,每行通常包含一个标记及其语言注释,空行表示句子边界。
构建器实现
from tensorflow_datasets.core.dataset_builders.conll import conll_dataset_builder_utils as conll_lib
import tensorflow_datasets.public_api as tfds
class MyCoNNLDataset(tfds.dataset_builders.ConllDatasetBuilder):
VERSION = tfds.core.Version('1.0.0')
BUILDER_CONFIGS = [conll_lib.CONLL_2003_CONFIG]
def _info(self) -> tfds.core.DatasetInfo:
return self.create_dataset_info(
# 填写数据集信息
)
def _split_generators(self, dl_manager):
path = dl_manager.download_and_extract('https://data-url')
return {
'train': self._generate_examples(path=path/'train.txt'),
'test': self._generate_examples(path=path/'test.txt')
}
CoNLL-U构建器
CoNLL-U格式简介
CoNLL-U是CoNLL的增强版,支持多词标记等特性。每行包含一个标记及其注释,字段间用制表符分隔。
构建器实现
from tensorflow_datasets.core.dataset_builders.conll import conllu_dataset_builder_utils as conllu_lib
import tensorflow_datasets.public_api as tfds
class MyCoNNLUDataset(tfds.dataset_builders.ConllUDatasetBuilder):
VERSION = tfds.core.Version('1.0.0')
BUILDER_CONFIGS = [
conllu_lib.get_universal_morphology_config(
language='en',
features=conllu_lib.UNIVERSAL_DEPENDENCIES_FEATURES,
)
]
def _info(self) -> tfds.core.DatasetInfo:
return self.create_dataset_info(
# 填写数据集信息
)
def _split_generators(self, dl_manager):
path = dl_manager.download_and_extract('https://data-url')
return {
'train': self._generate_examples(path=path/'train.txt')
}
总结
TensorFlow Datasets提供的格式专用构建器大大简化了特定格式数据集的创建过程。无论是从内存中的tf.data.Dataset
创建数据集,还是处理标准化的文本标注格式如CoNLL和CoNLL-U,这些构建器都提供了便捷的解决方案。开发者可以根据具体需求选择合适的构建器,快速实现数据集的创建和共享。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考