本文是《使用PaddleNLP進(jìn)行惡意網(wǎng)頁(yè)識(shí)別》系列第二篇,該系列持續(xù)更新中……系列目錄
PaddleNLP文檔:如何自定義數(shù)據(jù)集from paddlenlp.datasets import load_dataset
通過(guò)使用PaddleNLP提供的 load_dataset() , MapDataset 和 IterDataset 。任何人都可以方便的定義屬于自己的數(shù)據(jù)集。
從本地文件創(chuàng)建數(shù)據(jù)集?
從本地文件創(chuàng)建數(shù)據(jù)集時(shí),我們 推薦 根據(jù)本地?cái)?shù)據(jù)集的格式給出讀取function并傳入 load_dataset() 中創(chuàng)建數(shù)據(jù)集。
以 waybill_ie 快遞單信息抽取任務(wù)中的數(shù)據(jù)為例:
```python from paddlenlp.datasets import load_dataset
def read(data_path): with open(data_path, 'r', encoding='utf-8') as f: # 跳過(guò)列名 next(f) for line in f: words, labels = line.strip('\n').split('\t') words = words.split('\002') labels = labels.split('\002') yield {'tokens': words, 'labels': labels}
data_path為read()方法的參數(shù)
map_ds = load_dataset(read, data_path='train.txt',lazy=False) iter_ds = load_dataset(read, data_path='train.txt',lazy=True) ```
我們推薦將數(shù)據(jù)讀取代碼寫成生成器(generator)的形式,這樣可以更好的構(gòu)建 MapDataset 和 IterDataset 兩種數(shù)據(jù)集。同時(shí)我們也推薦將單條數(shù)據(jù)寫成字典的格式,這樣可以更方便的監(jiān)測(cè)數(shù)據(jù)流向。
事實(shí)上,MapDataset 在絕大多數(shù)時(shí)候都可以滿足要求。一般只有在數(shù)據(jù)集過(guò)于龐大無(wú)法一次性加載進(jìn)內(nèi)存的時(shí)候我們才考慮使用 IterDataset 。任何人都可以方便的定義屬于自己的數(shù)據(jù)集。
注解
需要注意的是,只有從 DatasetBuilder 初始化的數(shù)據(jù)集具有將數(shù)據(jù)中的label自動(dòng)轉(zhuǎn)為id的功能(詳細(xì)條件參見(jiàn) 如何貢獻(xiàn)數(shù)據(jù)集)。
像上例中的自定義數(shù)據(jù)集需要在自定義的convert to feature方法中添加label轉(zhuǎn)id的功能。
自定義數(shù)據(jù)讀取function中的參數(shù)可以直接以關(guān)鍵字參數(shù)的的方式傳入 load_dataset() 中。而且對(duì)于自定義數(shù)據(jù)集,lazy 參數(shù)是 必須 傳入的。
歡迎光臨 愛(ài)鋒貝 (http://m.7gfy2te7.cn/) | Powered by Discuz! X3.4 |