Datasets 文档
了解你的数据集
并获得增强的文档体验
开始使用
了解你的数据集
数据集对象有两种类型,一种是常规的 Dataset,另一种是 ✨ IterableDataset ✨。 Dataset 提供对行的快速随机访问,并通过内存映射,因此加载大型数据集也只占用相对较少的设备内存。但对于真正非常大的、甚至无法存储在磁盘或内存中的数据集,IterableDataset 允许你在数据集完全下载之前就可以访问和使用它!
本教程将向你展示如何加载和访问 Dataset 和 IterableDataset。
数据集
当你加载一个数据集的某个分片时,你将得到一个 Dataset 对象。你可以对 Dataset 对象执行许多操作,因此了解如何操作和与之交互存储在其中的数据非常重要。
本教程使用 rotten_tomatoes 数据集,但你可以随意加载任何你想要的数据集并进行跟随!
>>> from datasets import load_dataset
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")索引
一个 Dataset 包含数据列,每一列可以是不同类型的数据。索引 或轴标签用于从数据集中访问样本。例如,按行索引将返回数据集中一个样本的字典
# Get the first row in the dataset
>>> dataset[0]
{'label': 1,
'text': 'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .'}使用 - 操作符从数据集末尾开始
# Get the last row in the dataset
>>> dataset[-1]
{'label': 0,
'text': 'things really get weird , though not particularly scary : the movie is all portent and no content .'}按列名索引将返回该列所有值的列表
>>> dataset["text"]
['the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .',
'the gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson\'s expanded vision of j . r . r . tolkien\'s middle-earth .',
'effective but too-tepid biopic',
...,
'things really get weird , though not particularly scary : the movie is all portent and no content .']你可以结合行和列名索引来返回特定位置的特定值
>>> dataset[0]["text"]
'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .'索引顺序无关紧要。先按列名索引将返回一个 Column 对象,然后你可以像往常一样使用行索引来索引它
>>> import time
>>> start_time = time.time()
>>> text = dataset[0]["text"]
>>> end_time = time.time()
>>> print(f"Elapsed time: {end_time - start_time:.4f} seconds")
Elapsed time: 0.0031 seconds
>>> start_time = time.time()
>>> text = dataset["text"][0]
>>> end_time = time.time()
>>> print(f"Elapsed time: {end_time - start_time:.4f} seconds")
Elapsed time: 0.0042 seconds切片
切片返回数据集的一个切片——或子集——这对于一次查看多行很有用。要对数据集进行切片,请使用 : 操作符指定一个位置范围。
# Get the first three rows
>>> dataset[:3]
{'label': [1, 1, 1],
'text': ['the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .',
'the gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson\'s expanded vision of j . r . r . tolkien\'s middle-earth .',
'effective but too-tepid biopic']}
# Get rows between three and six
>>> dataset[3:6]
{'label': [1, 1, 1],
'text': ['if you sometimes like to go to the movies to have fun , wasabi is a good place to start .',
"emerges as something rare , an issue movie that's so honest and keenly observed that it doesn't feel like one .",
'the film provides some great insight into the neurotic mindset of all comics -- even those who have reached the absolute top of the game .']}IterableDataset
当你设置 load_dataset() 中的 streaming 参数为 True 时,将加载 IterableDataset
>>> from datasets import load_dataset
>>> iterable_dataset = load_dataset("ethz/food101", split="train", streaming=True)
>>> for example in iterable_dataset:
... print(example)
... break
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F0681F5C520>, 'label': 6}你也可以从现有的 Dataset 创建一个 IterableDataset,但这比流式处理模式更快,因为数据集是从本地文件流式传输的
>>> from datasets import load_dataset
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")
>>> iterable_dataset = dataset.to_iterable_dataset()IterableDataset 每次只迭代数据集的一个样本,所以你不需要等待整个数据集下载完成就可以使用它。正如你可以想象的那样,这对于你想立即使用的大型数据集非常有用!
索引
IterableDataset 的行为与常规的 Dataset 不同。你无法对 IterableDataset 中的样本进行随机访问。相反,你应该迭代它的元素,例如,通过调用 next(iter()) 或使用 for 循环来返回 IterableDataset 中的下一个项目
>>> next(iter(iterable_dataset))
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F0681F59B50>,
'label': 6}
>>> for example in iterable_dataset:
... print(example)
... break
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F7479DE82B0>, 'label': 6}但是 IterableDataset 支持列索引,它将返回一个列值的可迭代对象
>>> next(iter(iterable_dataset["label"]))
6创建子集
你可以使用 IterableDataset.take() 返回包含特定数量样本的数据集子集
# Get first three examples
>>> list(iterable_dataset.take(3))
[{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F7479DEE9D0>,
'label': 6},
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at 0x7F7479DE8190>,
'label': 6},
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x383 at 0x7F7479DE8310>,
'label': 6}]但与 切片 不同的是,IterableDataset.take() 创建一个新的 IterableDataset。
后续步骤
对这两种数据集类型的区别感兴趣?在 Dataset 和 IterableDataset 之间的区别 的概念指南中了解更多。
要更深入地了解这些数据集类型,请查看 处理 指南,了解如何预处理 Dataset,或者 流式处理 指南,了解如何预处理 IterableDataset。
在 GitHub 上更新