資料集文件

了解您的資料集

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

了解您的資料集

資料集物件主要有兩種型別:常規的 Dataset,以及 ✨ IterableDataset ✨。Dataset 提供對資料列的快速隨機存取,並透過記憶體映射 (memory-mapping) 技術,即使是載入大型資料集也只需要相對較少的裝置記憶體。但對於大到無法放入磁碟或記憶體的超大型資料集,IterableDataset 允許您在不需等待完全下載的情況下,直接存取並使用資料集!

本教學將向您展示如何載入與存取 DatasetIterableDataset

Dataset

當您載入資料集分割區 (split) 時,將會得到一個 Dataset 物件。您可以使用 Dataset 物件進行許多操作,這就是為什麼學習如何處理與互動其中儲存的資料如此重要。

本教學使用的是 rotten_tomatoes 資料集,但您可以自由載入任何您感興趣的資料集並跟著操作!

>>> from datasets import load_dataset

>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")

索引 (Indexing)

Dataset 包含資料欄 (columns),每一欄可以是不同型別的資料。索引 (index) 或稱軸標籤,用於從資料集中存取範例。例如,透過列索引將會回傳該資料集範例的字典。

# 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

切片 (Slicing)

切片會回傳資料集的片段或子集,這對於同時查看多個列很有用。若要對資料集進行切片,請使用 : 運算子來指定位置範圍。

# 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 會逐一迭代資料集中的範例,因此您不必等待整個資料集下載完成即可開始使用。如您所想,這對於想要立即使用的大型資料集非常實用!

索引 (Indexing)

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

建立子集 (Creating a subset)

您可以使用 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}]

但與切片 (slicing) 不同,IterableDataset.take() 會建立一個新的 IterableDataset

後續步驟

想了解更多關於這兩種資料集型別之間的差異嗎?請參閱DatasetIterableDataset 的差異》概念指南以瞭解更多資訊。

若要更深入實作這些資料集型別,請查看《處理 (Process)》指南以了解如何預處理 Dataset,或是查看《串流 (Stream)》指南以了解如何預處理 IterableDataset

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.