Datasets documentation

データセットを理解する

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

データセットを理解する

データセットオブジェクトには、通常のDatasetと、✨IterableDataset✨の2種類があります。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はデータの列を含み、各列は異なるタイプのデータを持つことができます。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

IterableDataset は、load_dataset() で streaming パラメータを True に設定すると読み込まれます:

>>> 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 は、データセットを1つのサンプルずつ順次反復処理するため、データセット全体をダウンロードするのを待たずに使用を開始できます。すぐに使用したい大規模なデータセットに対して、非常に有用であることが想像できるでしょう。

インデックス作成

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を作成します。

次のステップ

これらの2種類のデータセットの違いについて詳しく知りたいですか?DatasetとIterableDatasetの違いの概念ガイドで詳しく学んでください。

これらのデータセットタイプをより実践的に学ぶには、ProcessガイドでDatasetの前処理方法や、StreamガイドでIterableDatasetの前処理方法を確認してください。

Update on GitHub