huggingfaceから自然言語処理でベンチマークによく用いられるデータセット (数は本記事公開時点で98) を容易に利用するためのライブラリ nlp が公開されました。 本記事ではこのライブラリの特徴と利用方法をご紹介します。

目次

特徴

nlpの特徴を引用します。

  • Build-in interoperability with Numpy, Pandas, PyTorch and Tensorflow 2
  • Lightweight and fast with a transparent and pythonic API
  • Strive on large datasets: nlp naturally frees the user from RAM memory limitation, all datasets are memory-mapped on drive by default.
  • Smart caching: never wait for your data to process several times

まとめると以下の通りです。

  • PyTorchやTensorFlowなど異なるフレームワークでも利用しやすい
  • いろいろなデータや評価関数を統一的なAPIで提供している
  • 大きなデータでも極力メモリ消費を減らすようにディスクから直接メモリに読み込む形式にする
  • データの前処理結果をキャッシュして同じ計算を省略する

TensorflowではTensorflow Datasetsというデータのエコシステムがありますが、データの形式がTensorflowを前提としています。 一方で、nlpではフレームワークに依らない形式でデータを扱えるのと、評価関数も提供している点が主な違いです。

インストール

pip install nlp

主な利用方法

データはキャッシュされていなければ、ダウンロードされます。 その後、pyarrowを使ってディスクから直接メモリに読み込めるような形式で保存されます。

import nlp

# Print all the available datasets
print(dataset.id for dataset in nlp.list_datasets())

# Load a dataset and print the first examples in the training set
squad_dataset = nlp.load_dataset('squad')
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5.00k/5.00k [00:00<00:00, 250kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2.24k/2.24k [00:00<00:00, 587kB/s]
Downloading and preparing dataset squad/plain_text (download: 33.51 MiB, generated: 85.75 MiB, total: 119.27 MiB) to /Users/takuya/.cache/huggingface/datasets/squad/plain_text/1.0.0...
Downloading: 30.3MB [00:00, 48.3MB/s]
Downloading: 4.85MB [00:00, 21.0MB/s]
Dataset squad downloaded and prepared to /Users/takuya/.cache/huggingface/datasets/squad/plain_text/1.0.0. Subsequent calls will reuse this data.

引数のsplitはデフォルトではallになっており、学習データ、開発データ、評価データのすべて (データごとに用意されているすべて) を返します。

print(squad_dataset.keys())
dict_keys(['train', 'validation'])

具体的なデータは以下のように取得します。

print(squad_dataset['train'][0])
{'id': '5733be284776f41900661182', 'title': 'University_of_Notre_Dame', 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', 'answers': {'text': ['Saint Bernadette Soubirous'], 'answer_start': [515]}}

利用可能なデータは以下のようにして列挙できます。

for dataset in nlp.list_datasets():
    print(dataset.id)
aeslc
ai2_arc
anli
billsum
blimp
blog_authorship_corpus
boolq
break_data
cfq
civil_comments
cmrc2018
cnn_dailymail
coarse_discourse
com_qa
commonsense_qa
coqa
cornell_movie_dialog
cos_e
cosmos_qa
crime_and_punish
csv
definite_pronoun_resolution
discofuse
drop
empathetic_dialogues
eraser_multi_rc
esnli
event2Mind
flores
fquad
gap
gigaword
...

本記事公開時点で98のデータセットが公開されています。

おわり

本記事では自然言語処理のベンチマークでよく用いられるデータセットを用意に利用できるライブラリnlpを紹介しました。 nlpという名前は他のライブラリでも使われる可能性があり、予期せぬバグの原因となるため、nlpから名前を変更するような議論がされているようです。 検索もしにくいですし、個人的には変えたほうが良いかなと思います。


関連記事






最近の記事