Datasets 文档
了解你的数据集
并获得增强的文档体验
开始使用
了解你的数据集
有两种类型的数据集对象:一种是常规的 Dataset,另一种是 ✨ IterableDataset ✨。Dataset 提供了对行的快速随机访问和内存映射,因此即使加载大型数据集也只占用相对较少的设备内存。但对于那些甚至无法装入磁盘或内存的超大型数据集,IterableDataset 允许您在数据集完全下载完之前就访问和使用它!
本教程将向您展示如何加载和访问 Dataset 和 IterableDataset。
Dataset
当您加载一个数据集分割时,您会得到一个 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 上更新