Datasets 文件

資料集特徵

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

資料集特徵

Features 定義了資料集的內部結構。它用於指定底層的序列化格式。不過,對你來說更有趣的是,Features 包含了關於從列名和型別到 ClassLabel 的所有高階資訊。你可以將 Features 視為資料集的骨架。

Features 的格式很簡單:dict[column_name, column_type]。它是一個由列名和列型別對組成的字典。列型別提供了廣泛的選項來描述你擁有的資料型別。

讓我們來看看 GLUE 基準測試中 MRPC 資料集的特徵

>>> from datasets import load_dataset
>>> dataset = load_dataset('nyu-mll/glue', 'mrpc', split='train')
>>> dataset.features
{'idx': Value('int32'),
 'label': ClassLabel(names=['not_equivalent', 'equivalent']),
 'sentence1': Value('string'),
 'sentence2': Value('string'),
}

Value 特徵告訴 🤗 Datasets

  • idx 的資料型別是 int32
  • sentence1sentence2 的資料型別是 string

🤗 Datasets 支援許多其他資料型別,例如 boolfloat32binary 等。

有關支援的資料型別的完整列表,請參閱 Value

ClassLabel 特徵告知 🤗 Datasets label 列包含兩個類別。這些類別被標記為 not_equivalentequivalent。標籤在資料集中以整數形式儲存。當你檢索標籤時,ClassLabel.int2str()ClassLabel.str2int() 會執行從整數值到標籤名稱的轉換,反之亦然。

如果你的資料型別包含物件列表,那麼你需要使用 List 特徵。還記得 SQuAD 資料集嗎?

>>> from datasets import load_dataset
>>> dataset = load_dataset('rajpurkar/squad', split='train')
>>> dataset.features
{'id': Value('string'),
 'title': Value('string'),
 'context': Value('string'),
 'question': Value('string'),
 'answers': {'text': List(Value('string')),
  'answer_start': List(Value('int32'))}}

answers 欄位是使用特徵字典構建的,因為它包含兩個子欄位 textanswer_start,它們分別是 stringint32 的列表。

請參閱 展平(flatten) 部分,瞭解如何將巢狀的子欄位提取為獨立的列。

陣列特徵型別對於建立各種大小的陣列很有用。你可以使用 Array2D 建立二維陣列,甚至可以使用 Array5D 建立五維陣列。

>>> features = Features({'a': Array2D(shape=(1, 3), dtype='int32')})

陣列型別還允許陣列的第一個維度是動態的。這對於處理可變長度的序列(例如句子)非常有用,而無需將輸入填充或截斷為統一的形狀。

>>> features = Features({'a': Array3D(shape=(None, 5, 2), dtype='int32')})

音訊特徵

音訊資料集有一列型別為 Audio,其中包含三個重要欄位

  • array:解碼後的音訊資料,表示為一維陣列。
  • path:下載的音訊檔案的路徑。
  • sampling_rate:音訊資料的取樣率。

當你載入一個音訊資料集並呼叫音訊列時,Audio 特徵會自動解碼並重取樣音訊檔案

>>> from datasets import load_dataset, Audio

>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train")
>>> dataset[0]["audio"]
<datasets.features._torchcodec.AudioDecoder object at 0x11642b6a0>

使用行索引先索引音訊資料集,然後再索引 audio 列 - dataset[0]["audio"] - 以避免解碼和重取樣資料集中的所有音訊檔案。否則,如果你的資料集很大,這可能是一個緩慢且耗時的過程。

使用 `decode=False`,Audio 型別只會提供音訊檔案的路徑或位元組,而不會將其解碼為 `torchcodec.AudioDecoder` 物件,

>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train").cast_column("audio", Audio(decode=False))
>>> dataset[0]
{'audio': {'bytes': None,
  'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav'},
 'english_transcription': 'I would like to set up a joint account with my partner',
 'intent_class': 11,
 'lang_id': 4,
 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav',
 'transcription': 'I would like to set up a joint account with my partner'}

影像特徵

影像資料集有一個型別為 Image 的列,它會從以位元組形式儲存的影像中載入 `PIL.Image` 物件

當你載入一個影像資料集並呼叫影像列時,Image 特徵會自動解碼影像檔案

>>> from datasets import load_dataset, Image

>>> dataset = load_dataset("AI-Lab-Makerere/beans", split="train")
>>> dataset[0]["image"]
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x500 at 0x125506CF8>

使用行索引先索引影像資料集,然後再索引 `image` 列 - `dataset[0]["image"]` - 以避免解碼資料集中的所有影像檔案。否則,如果你的資料集很大,這可能是一個緩慢且耗時的過程。

使用 `decode=False`,Image 型別只會提供影像檔案的路徑或位元組,而不會將其解碼為 `PIL.Image` 物件,

>>> dataset = load_dataset("AI-Lab-Makerere/beans", split="train").cast_column("image", Image(decode=False))
>>> dataset[0]["image"]
{'bytes': None,
 'path': '/Users/username/.cache/huggingface/datasets/downloads/extracted/772e7c1fba622cff102b85dd74bcce46e8168634df4eaade7bedd3b8d91d3cd7/train/healthy/healthy_train.265.jpg'}

根據資料集的不同,你可能會得到本地下載影像的路徑,或者如果資料集不是由單個檔案組成的,則會得到影像內容的位元組。

你還可以從 numpy 陣列定義一個影像資料集

>>> ds = Dataset.from_dict({"i": [np.zeros(shape=(16, 16, 3), dtype=np.uint8)]}, features=Features({"i": Image()}))

在這種情況下,numpy 陣列會被編碼為 PNG(如果畫素值的精度很重要,則為 TIFF)。

對於像 RGB 或 RGBA 這樣的多通道陣列,僅支援 uint8。如果使用更高的精度,你會收到一個警告,並且陣列會被向下轉型為 uint8。對於灰度影像,只要它與 `Pillow` 相容,你可以使用你想要的整數或浮點精度。如果你的影像整數或浮點精度太高,會顯示一個警告,在這種情況下陣列會被向下轉型:int64 陣列會被向下轉型為 int32,float64 陣列會被向下轉型為 float32。

< > 在 GitHub 上更新

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