Datasets 文件
過程
並獲得增強的文件體驗
開始使用
處理
🤗 Datasets 提供了許多工具來修改資料集的結構和內容。這些工具對於整理資料集、建立附加列、在特徵和格式之間轉換以及更多功能都很重要。
本指南將向您展示如何:
- 重新排序行並拆分資料集。
- 重新命名和刪除列,以及其他常見的列操作。
- 對資料集中的每個示例應用處理函式。
- 連線資料集。
- 應用自定義格式轉換。
- 儲存和匯出已處理的資料集。
有關處理其他資料集模式的更多詳細資訊,請參閱處理音訊資料集指南、處理影像資料集指南或處理文字資料集指南。
本指南中的示例使用 MRPC 資料集,但歡迎載入您選擇的任何資料集並進行操作!
>>> from datasets import load_dataset
>>> dataset = load_dataset("nyu-mll/glue", "mrpc", split="train")
本指南中的所有處理方法都返回一個新的 Dataset 物件。修改不是就地進行的。請注意不要覆蓋您以前的資料集!
排序、打亂、選擇、分割和分片
有幾個函式可以重新排列資料集的結構。這些函式對於僅選擇您想要的行、建立訓練和測試拆分以及將非常大的資料集分片為更小的塊很有用。
排序
使用 sort() 根據列值的數值對它們進行排序。提供的列必須與 NumPy 相容。
>>> dataset["label"][:10]
[1, 0, 1, 0, 1, 1, 0, 1, 0, 0]
>>> sorted_dataset = dataset.sort("label")
>>> sorted_dataset["label"][:10]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> sorted_dataset["label"][-10:]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
在底層,這會建立一個根據列值排序的索引列表。然後使用此索引對映來訪問底層 Arrow 表中的正確行。
打亂
shuffle() 函式隨機重新排列列值。如果您想要更多地控制用於打亂資料集的演算法,可以在此函式中指定 generator
引數以使用不同的 numpy.random.Generator
。
>>> shuffled_dataset = sorted_dataset.shuffle(seed=42)
>>> shuffled_dataset["label"][:10]
[1, 1, 1, 0, 1, 1, 1, 1, 1, 0]
打亂操作將索引列表 [0:len(my_dataset)]
打亂以建立索引對映。然而,一旦您的 Dataset 具有索引對映,速度可能會慢 10 倍。這是因為需要額外一步來使用索引對映獲取要讀取的行索引,更重要的是,您不再讀取連續的資料塊。要恢復速度,您需要使用 Dataset.flatten_indices() 將整個資料集重新寫入磁碟,這會移除索引對映。或者,您可以切換到 IterableDataset 並利用其快速近似打亂 IterableDataset.shuffle()
>>> iterable_dataset = dataset.to_iterable_dataset(num_shards=128)
>>> shuffled_iterable_dataset = iterable_dataset.shuffle(seed=42, buffer_size=1000)
選擇和過濾
在資料集中過濾行有兩種選擇:select() 和 filter()。
- select() 根據索引列表返回行。
>>> small_dataset = dataset.select([0, 10, 20, 30, 40, 50])
>>> len(small_dataset)
6
- filter() 返回符合指定條件的行。
>>> start_with_ar = dataset.filter(lambda example: example["sentence1"].startswith("Ar"))
>>> len(start_with_ar)
6
>>> start_with_ar["sentence1"]
['Around 0335 GMT , Tab shares were up 19 cents , or 4.4 % , at A $ 4.56 , having earlier set a record high of A $ 4.57 .',
'Arison said Mann may have been one of the pioneers of the world music movement and he had a deep love of Brazilian music .',
'Arts helped coach the youth on an eighth-grade football team at Lombardi Middle School in Green Bay .',
'Around 9 : 00 a.m. EDT ( 1300 GMT ) , the euro was at $ 1.1566 against the dollar , up 0.07 percent on the day .',
"Arguing that the case was an isolated example , Canada has threatened a trade backlash if Tokyo 's ban is not justified on scientific grounds .",
'Artists are worried the plan would harm those who need help most - performers who have a difficult time lining up shows .'
]
如果您設定 with_indices=True
,filter() 也可以按索引過濾。
>>> even_dataset = dataset.filter(lambda example, idx: idx % 2 == 0, with_indices=True)
>>> len(even_dataset)
1834
>>> len(dataset) / 2
1834.0
除非要保留的索引列表是連續的,否則這些方法也會在底層建立索引對映。
分割
train_test_split() 函式在您的資料集尚未具有訓練集和測試集時,可以建立它們。這允許您調整每個拆分中的相對比例或絕對樣本數量。在下面的示例中,使用 test_size
引數建立佔原始資料集 10% 的測試集。
>>> dataset.train_test_split(test_size=0.1)
{'train': Dataset(schema: {'sentence1': 'string', 'sentence2': 'string', 'label': 'int64', 'idx': 'int32'}, num_rows: 3301),
'test': Dataset(schema: {'sentence1': 'string', 'sentence2': 'string', 'label': 'int64', 'idx': 'int32'}, num_rows: 367)}
>>> 0.1 * len(dataset)
366.8
預設情況下,拆分是打亂的,但您可以設定 shuffle=False
以防止打亂。
分片
🤗 Datasets 支援分片,將非常大的資料集分成預定義數量的塊。在 shard() 中指定 num_shards
引數以確定將資料集分割成多少個分片。您還需要使用 index
引數提供要返回的分片。
例如,stanfordnlp/imdb 資料集有 25000 個示例。
>>> from datasets import load_dataset
>>> dataset = load_dataset("stanfordnlp/imdb", split="train")
>>> print(dataset)
Dataset({
features: ['text', 'label'],
num_rows: 25000
})
將資料集分片為四個塊後,第一個分片將只有 6250 個示例。
>>> dataset.shard(num_shards=4, index=0)
Dataset({
features: ['text', 'label'],
num_rows: 6250
})
>>> print(25000/4)
6250.0
重新命名、刪除、轉換和展平
以下函式允許您修改資料集的列。這些函式對於重新命名或刪除列、將列更改為一組新特徵以及展平巢狀列結構非常有用。
重新命名
當您需要重新命名資料集中的列時,使用 rename_column()。與原始列關聯的特徵實際上會移動到新的列名下,而不是僅僅就地替換原始列。
向 rename_column() 提供原始列的名稱和新的列名。
>>> dataset
Dataset({
features: ['sentence1', 'sentence2', 'label', 'idx'],
num_rows: 3668
})
>>> dataset = dataset.rename_column("sentence1", "sentenceA")
>>> dataset = dataset.rename_column("sentence2", "sentenceB")
>>> dataset
Dataset({
features: ['sentenceA', 'sentenceB', 'label', 'idx'],
num_rows: 3668
})
刪除
當您需要刪除一個或多個列時,請將要刪除的列名提供給 remove_columns() 函式。透過提供列名列表來刪除多個列。
>>> dataset = dataset.remove_columns("label")
>>> dataset
Dataset({
features: ['sentence1', 'sentence2', 'idx'],
num_rows: 3668
})
>>> dataset = dataset.remove_columns(["sentence1", "sentence2"])
>>> dataset
Dataset({
features: ['idx'],
num_rows: 3668
})
相反,select_columns() 選擇要保留的一個或多個列並刪除其餘列。此函式接受一個或一個列名列表。
>>> dataset
Dataset({
features: ['sentence1', 'sentence2', 'label', 'idx'],
num_rows: 3668
})
>>> dataset = dataset.select_columns(['sentence1', 'sentence2', 'idx'])
>>> dataset
Dataset({
features: ['sentence1', 'sentence2', 'idx'],
num_rows: 3668
})
>>> dataset = dataset.select_columns('idx')
>>> dataset
Dataset({
features: ['idx'],
num_rows: 3668
})
轉換
cast() 函式轉換一個或多個列的特徵型別。此函式接受您的新 Features 作為其引數。下面的示例演示如何更改 ClassLabel 和 Value 特徵。
>>> dataset.features
{'sentence1': Value('string'),
'sentence2': Value('string'),
'label': ClassLabel(names=['not_equivalent', 'equivalent']),
'idx': Value('int32')}
>>> from datasets import ClassLabel, Value
>>> new_features = dataset.features.copy()
>>> new_features["label"] = ClassLabel(names=["negative", "positive"])
>>> new_features["idx"] = Value("int64")
>>> dataset = dataset.cast(new_features)
>>> dataset.features
{'sentence1': Value('string'),
'sentence2': Value('string'),
'label': ClassLabel(names=['negative', 'positive']),
'idx': Value('int64')}
只有當原始特徵型別和新特徵型別相容時,才能進行轉換。例如,如果原始列只包含 1 和 0,您可以將特徵型別為 Value("int32")
的列轉換為 Value("bool")
。
使用 cast_column() 函式更改單個列的特徵型別。將列名及其新的特徵型別作為引數傳遞。
>>> dataset.features
{'audio': Audio(sampling_rate=44100, mono=True)}
>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))
>>> dataset.features
{'audio': Audio(sampling_rate=16000, mono=True)}
展平
有時,列可以是多種型別的巢狀結構。請看下面 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
欄位包含兩個子欄位:text
和 answer_start
。使用 flatten() 函式將子欄位提取到它們自己的獨立列中。
>>> flat_dataset = dataset.flatten()
>>> flat_dataset
Dataset({
features: ['id', 'title', 'context', 'question', 'answers.text', 'answers.answer_start'],
num_rows: 87599
})
請注意,子欄位現在已成為獨立的列:answers.text
和 answers.answer_start
。
對映
🤗 Datasets 的一些更強大的應用來自於使用 map() 函式。map() 的主要目的是加快處理函式的速度。它允許您獨立或批次地將處理函式應用於資料集中的每個示例。此函式甚至可以建立新的行和列。
在以下示例中,將資料集中的每個 sentence1
值都加上字首 'My sentence: '
。
首先建立一個函式,在每個句子的開頭新增 'My sentence: '
。該函式需要接受並輸出一個 dict
。
>>> def add_prefix(example):
... example["sentence1"] = 'My sentence: ' + example["sentence1"]
... return example
現在使用 map() 將 add_prefix
函式應用於整個資料集。
>>> updated_dataset = small_dataset.map(add_prefix)
>>> updated_dataset["sentence1"][:5]
['My sentence: Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .',
"My sentence: Yucaipa owned Dominick 's before selling the chain to Safeway in 1998 for $ 2.5 billion .",
'My sentence: They had published an advertisement on the Internet on June 10 , offering the cargo for sale , he added .',
'My sentence: Around 0335 GMT , Tab shares were up 19 cents , or 4.4 % , at A $ 4.56 , having earlier set a record high of A $ 4.57 .',
]
我們再來看一個示例,但這次您將使用 map() 刪除一列。當您刪除一列時,它僅在該示例提供給對映函式後才被刪除。這允許對映函式在使用列內容後再將其刪除。
在 map() 中使用 remove_columns
引數指定要刪除的列。
>>> updated_dataset = dataset.map(lambda example: {"new_sentence": example["sentence1"]}, remove_columns=["sentence1"])
>>> updated_dataset.column_names
['sentence2', 'label', 'idx', 'new_sentence']
🤗 Datasets 還有一個 remove_columns() 函式,它速度更快,因為它不復制剩餘列的資料。
如果您設定了 with_indices=True
,您也可以將 map() 與索引一起使用。下面的示例將索引新增到每個句子的開頭。
>>> updated_dataset = dataset.map(lambda example, idx: {"sentence2": f"{idx}: " + example["sentence2"]}, with_indices=True)
>>> updated_dataset["sentence2"][:5]
['0: Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .',
"1: Yucaipa bought Dominick 's in 1995 for $ 693 million and sold it to Safeway for $ 1.8 billion in 1998 .",
"2: On June 10 , the ship 's owners had published an advertisement on the Internet , offering the explosives for sale .",
'3: Tab shares jumped 20 cents , or 4.6 % , to set a record closing high at A $ 4.57 .',
'4: PG & E Corp. shares jumped $ 1.63 or 8 percent to $ 21.03 on the New York Stock Exchange on Friday .'
]
多程序
多程序處理透過在 CPU 上並行化程序來顯著加快處理速度。在 map() 中設定 num_proc
引數以設定要使用的程序數。
>>> updated_dataset = dataset.map(lambda example, idx: {"sentence2": f"{idx}: " + example["sentence2"]}, with_indices=True, num_proc=4)
如果您設定 with_rank=True
,map() 也可以與程序的 rank 一起工作。這類似於 with_indices
引數。對映函式中的 with_rank
引數位於 index
引數之後(如果它已存在)。
>>> import torch
>>> from multiprocess import set_start_method
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> from datasets import load_dataset
>>>
>>> # Get an example dataset
>>> dataset = load_dataset("fka/awesome-chatgpt-prompts", split="train")
>>>
>>> # Get an example model and its tokenizer
>>> model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B-Chat").eval()
>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B-Chat")
>>>
>>> def gpu_computation(batch, rank):
... # Move the model on the right GPU if it's not there already
... device = f"cuda:{(rank or 0) % torch.cuda.device_count()}"
... model.to(device)
...
... # Your big GPU call goes here, for example:
... chats = [[
... {"role": "system", "content": "You are a helpful assistant."},
... {"role": "user", "content": prompt}
... ] for prompt in batch["prompt"]]
... texts = [tokenizer.apply_chat_template(
... chat,
... tokenize=False,
... add_generation_prompt=True
... ) for chat in chats]
... model_inputs = tokenizer(texts, padding=True, return_tensors="pt").to(device)
... with torch.no_grad():
... outputs = model.generate(**model_inputs, max_new_tokens=512)
... batch["output"] = tokenizer.batch_decode(outputs, skip_special_tokens=True)
... return batch
>>>
>>> if __name__ == "__main__":
... set_start_method("spawn")
... updated_dataset = dataset.map(
... gpu_computation,
... batched=True,
... batch_size=16,
... with_rank=True,
... num_proc=torch.cuda.device_count(), # one process per GPU
... )
rank 的主要用例是在多個 GPU 上平行計算。這需要設定 multiprocess.set_start_method("spawn")
。如果您不這樣做,您將收到以下 CUDA 錯誤。
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method.
批處理
map() 函式支援處理批次的示例。透過設定 batched=True
來對批次進行操作。預設批次大小為 1000,但您可以使用 batch_size
引數進行調整。批處理支援有趣的應用程式,例如將長句子分成較短的塊和資料增強。
分割長示例
當示例過長時,您可能希望將它們分割成幾個較小的塊。首先建立一個函式,該函式:
將
sentence1
欄位分割成 50 個字元的塊。將所有塊堆疊在一起以建立新資料集。
>>> def chunk_examples(examples):
... chunks = []
... for sentence in examples["sentence1"]:
... chunks += [sentence[i:i + 50] for i in range(0, len(sentence), 50)]
... return {"chunks": chunks}
使用 map() 應用該函式。
>>> chunked_dataset = dataset.map(chunk_examples, batched=True, remove_columns=dataset.column_names)
>>> chunked_dataset[:10]
{'chunks': ['Amrozi accused his brother , whom he called " the ',
'witness " , of deliberately distorting his evidenc',
'e .',
"Yucaipa owned Dominick 's before selling the chain",
' to Safeway in 1998 for $ 2.5 billion .',
'They had published an advertisement on the Interne',
't on June 10 , offering the cargo for sale , he ad',
'ded .',
'Around 0335 GMT , Tab shares were up 19 cents , or',
' 4.4 % , at A $ 4.56 , having earlier set a record']}
請注意,現在句子被分割成較短的塊,並且資料集中的行數增加了。
>>> dataset
Dataset({
features: ['sentence1', 'sentence2', 'label', 'idx'],
num_rows: 3668
})
>>> chunked_dataset
Dataset({
features: ['chunks'],
num_rows: 10470
})
資料增強
map() 函式也可以用於資料增強。以下示例為句子中的遮蔽標記生成附加詞。
在 🤗 Transformers 的 FillMaskPipeline 中載入並使用 RoBERTA 模型。
>>> from random import randint
>>> from transformers import pipeline
>>> fillmask = pipeline("fill-mask", model="roberta-base")
>>> mask_token = fillmask.tokenizer.mask_token
>>> smaller_dataset = dataset.filter(lambda e, i: i<100, with_indices=True)
建立一個函式,隨機選擇句子中要遮蔽的單詞。該函式還應返回原始句子和 RoBERTA 生成的前兩個替換詞。
>>> def augment_data(examples):
... outputs = []
... for sentence in examples["sentence1"]:
... words = sentence.split(' ')
... K = randint(1, len(words)-1)
... masked_sentence = " ".join(words[:K] + [mask_token] + words[K+1:])
... predictions = fillmask(masked_sentence)
... augmented_sequences = [predictions[i]["sequence"] for i in range(3)]
... outputs += [sentence] + augmented_sequences
...
... return {"data": outputs}
使用 map() 將函式應用於整個資料集。
>>> augmented_dataset = smaller_dataset.map(augment_data, batched=True, remove_columns=dataset.column_names, batch_size=8)
>>> augmented_dataset[:9]["data"]
['Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .',
'Amrozi accused his brother, whom he called " the witness ", of deliberately withholding his evidence.',
'Amrozi accused his brother, whom he called " the witness ", of deliberately suppressing his evidence.',
'Amrozi accused his brother, whom he called " the witness ", of deliberately destroying his evidence.',
"Yucaipa owned Dominick 's before selling the chain to Safeway in 1998 for $ 2.5 billion .",
'Yucaipa owned Dominick Stores before selling the chain to Safeway in 1998 for $ 2.5 billion.',
"Yucaipa owned Dominick's before selling the chain to Safeway in 1998 for $ 2.5 billion.",
'Yucaipa owned Dominick Pizza before selling the chain to Safeway in 1998 for $ 2.5 billion.'
]
對於每個原始句子,RoBERTA 用三個替代詞增強了一個隨機詞。原始詞 distorting
被 withholding
、suppressing
和 destroying
補充。
非同步處理
非同步函式對於並行呼叫 API 端點非常有用,例如下載影像或呼叫模型端點。
您可以使用 async
和 await
關鍵字定義一個非同步函式,這是一個呼叫 Hugging Face 聊天模型的示例函式:
>>> import aiohttp
>>> import asyncio
>>> from huggingface_hub import get_token
>>> sem = asyncio.Semaphore(20) # max number of simultaneous queries
>>> async def query_model(model, prompt):
... api_url = f"https://api-inference.huggingface.co/models/{model}/v1/chat/completions"
... headers = {"Authorization": f"Bearer {get_token()}", "Content-Type": "application/json"}
... json = {"messages": [{"role": "user", "content": prompt}], "max_tokens": 20, "seed": 42}
... async with sem, aiohttp.ClientSession() as session, session.post(api_url, headers=headers, json=json) as response:
... output = await response.json()
... return {"Output": output["choices"][0]["message"]["content"]}
非同步函式並行執行,這大大加快了處理速度。如果按順序執行相同的程式碼,則會花費更多時間,因為它在等待模型響應時什麼也不做。通常建議在函式必須等待 API 響應時使用 async
/ await
,或者如果它下載資料並且可能需要一些時間。
請注意 Semaphore
的存在:它設定了可以並行執行的最大查詢數。建議在呼叫 API 時使用 Semaphore
以避免速率限制錯誤。
讓我們用它來呼叫 microsoft/Phi-3-mini-4k-instruct 模型,並要求它返回 Maxwell-Jia/AIME_2024 資料集中每個數學問題的主要主題。
>>> from datasets import load_dataset
>>> ds = load_dataset("Maxwell-Jia/AIME_2024", split="train")
>>> model = "microsoft/Phi-3-mini-4k-instruct"
>>> prompt = 'What is this text mainly about ? Here is the text:\n\n```\n{Problem}\n```\n\nReply using one or two words max, e.g. "The main topic is Linear Algebra".'
>>> async def get_topic(example):
... return await query_model(model, prompt.format(Problem=example['Problem']))
>>> ds = ds.map(get_topic)
>>> ds[0]
{'ID': '2024-II-4',
'Problem': 'Let $x,y$ and $z$ be positive real numbers that...',
'Solution': 'Denote $\\log_2(x) = a$, $\\log_2(y) = b$, and...,
'Answer': 33,
'Output': 'The main topic is Logarithms.'}
在這裡,Dataset.map() 非同步執行許多 get_topic
函式,因此它不必等待每個模型響應,這會按順序花費大量時間。
預設情況下,Dataset.map() 最多並行執行一千個對映函式,因此不要忘記使用 Semaphore
設定可以並行執行的最大 API 呼叫數,否則模型可能會返回速率限制錯誤或過載。對於高階用例,您可以在 datasets.config
中更改並行查詢的最大數量。
處理多個拆分
許多資料集都有拆分,可以使用 DatasetDict.map() 同時處理。例如,透過以下方式對訓練集和測試集中的 sentence1
欄位進行分詞:
>>> from datasets import load_dataset
# load all the splits
>>> dataset = load_dataset('nyu-mll/glue', 'mrpc')
>>> encoded_dataset = dataset.map(lambda examples: tokenizer(examples["sentence1"]), batched=True)
>>> encoded_dataset["train"][0]
{'sentence1': 'Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .',
'sentence2': 'Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .',
'label': 1,
'idx': 0,
'input_ids': [ 101, 7277, 2180, 5303, 4806, 1117, 1711, 117, 2292, 1119, 1270, 107, 1103, 7737, 107, 117, 1104, 9938, 4267, 12223, 21811, 1117, 2554, 119, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
}
分散式使用
當您在分散式設定中使用 map() 時,您還應該使用 torch.distributed.barrier。這可確保主程序執行對映,而其他程序載入結果,從而避免重複工作。
以下示例展示瞭如何使用 torch.distributed.barrier
同步程序:
>>> from datasets import Dataset
>>> import torch.distributed
>>> dataset1 = Dataset.from_dict({"a": [0, 1, 2]})
>>> if training_args.local_rank > 0:
... print("Waiting for main process to perform the mapping")
... torch.distributed.barrier()
>>> dataset2 = dataset1.map(lambda x: {"a": x["a"] + 1})
>>> if training_args.local_rank == 0:
... print("Loading results from main process")
... torch.distributed.barrier()
批處理
batch()
方法允許您將資料集中的樣本分組到批次中。當您想要為訓練或評估建立資料批次時,這特別有用,尤其是在使用深度學習模型時。
以下是使用 batch()
方法的示例:
>>> from datasets import load_dataset
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")
>>> batched_dataset = dataset.batch(batch_size=4)
>>> batched_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 .',
'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',
'if you sometimes like to go to the movies to have fun , wasabi is a good place to start .'],
'label': [1, 1, 1, 1]}
batch()
方法接受以下引數:
batch_size
(int
):每個批次中的樣本數量。drop_last_batch
(bool
,預設為False
):如果資料集大小不能被批次大小整除,是否丟棄最後一個不完整的批次。num_proc
(int
,可選,預設為None
):用於多程序處理的程序數。如果為 None,則不使用多程序處理。這可以顯著加快大型資料集的批處理速度。
請注意,Dataset.batch()
返回一個新 Dataset,其中每個項都是原始資料集中多個樣本的批次。如果您想批次處理資料,您應該直接使用批次 map(),它將函式應用於批次,但輸出資料集是非批次化的。
連線
如果它們共享相同的列型別,則可以連線單獨的資料集。使用 concatenate_datasets() 連線資料集。
>>> from datasets import concatenate_datasets, load_dataset
>>> stories = load_dataset("ajibawa-2023/General-Stories-Collection", split="train")
>>> stories = stories.remove_columns([col for col in stories.column_names if col != "text"]) # only keep the 'text' column
>>> wiki = load_dataset("wikimedia/wikipedia", "20220301.en", split="train")
>>> wiki = wiki.remove_columns([col for col in wiki.column_names if col != "text"]) # only keep the 'text' column
>>> assert stories.features.type == wiki.features.type
>>> bert_dataset = concatenate_datasets([stories, wiki])
只要資料集具有相同的行數,您也可以透過設定 axis=1
來水平連線兩個資料集。
>>> from datasets import Dataset
>>> stories_ids = Dataset.from_dict({"ids": list(range(len(stories)))})
>>> stories_with_ids = concatenate_datasets([stories, stories_ids], axis=1)
交錯
您還可以透過從每個資料集中交替獲取示例來將多個數據集混合在一起以建立新資料集。這被稱為*交錯*,由 interleave_datasets() 函式啟用。interleave_datasets() 和 concatenate_datasets() 都適用於常規 Dataset 和 IterableDataset 物件。有關如何交錯 IterableDataset 物件的示例,請參閱流式傳輸指南。
您可以為每個原始資料集定義取樣機率,以指定如何交錯資料集。在這種情況下,新資料集是透過從隨機資料集中逐個獲取示例來構建的,直到其中一個數據集用盡樣本。
>>> from datasets import Dataset, interleave_datasets
>>> seed = 42
>>> probabilities = [0.3, 0.5, 0.2]
>>> d1 = Dataset.from_dict({"a": [0, 1, 2]})
>>> d2 = Dataset.from_dict({"a": [10, 11, 12, 13]})
>>> d3 = Dataset.from_dict({"a": [20, 21, 22]})
>>> dataset = interleave_datasets([d1, d2, d3], probabilities=probabilities, seed=seed)
>>> dataset["a"]
[10, 11, 20, 12, 0, 21, 13]
您還可以指定 stopping_strategy
。預設策略 first_exhausted
是一種子取樣策略,即一旦其中一個數據集用盡樣本,資料集構建就會停止。您可以指定 stopping_strategy=all_exhausted
以執行過取樣策略。在這種情況下,資料集構建會在每個資料集中的所有樣本至少新增一次後停止。實際上,這意味著如果一個數據集耗盡,它將返回到該資料集的開頭,直到達到停止條件。請注意,如果沒有指定取樣機率,則新資料集將具有 max_length_datasets*nb_dataset
樣本。
>>> d1 = Dataset.from_dict({"a": [0, 1, 2]})
>>> d2 = Dataset.from_dict({"a": [10, 11, 12, 13]})
>>> d3 = Dataset.from_dict({"a": [20, 21, 22]})
>>> dataset = interleave_datasets([d1, d2, d3], stopping_strategy="all_exhausted")
>>> dataset["a"]
[0, 10, 20, 1, 11, 21, 2, 12, 22, 0, 13, 20]
格式
with_format() 函式即時應用自定義格式轉換。此函式替換任何先前指定的格式。例如,您可以使用此函式即時分詞和填充標記。分詞僅在訪問示例時應用。
例如,透過設定 type="torch"
來建立 PyTorch 張量:
>>> dataset = dataset.with_format(type="torch")
set_format() 函式也更改列的格式,只是它是就地執行的。
>>> dataset.set_format(type="torch")
如果您需要將資料集重置為原始格式,請將格式設定為 None
(或使用 reset_format())。
>>> dataset.format
{'type': 'torch', 'format_kwargs': {}, 'columns': [...], 'output_all_columns': False}
>>> dataset = dataset.with_format(None)
>>> dataset.format
{'type': None, 'format_kwargs': {}, 'columns': [...], 'output_all_columns': False}
張量格式
支援多種張量或陣列格式。通常建議使用這些格式,而不是手動將資料集的輸出轉換為張量或陣列,以避免不必要的資料複製並加快資料載入速度。
以下是支援的張量或陣列格式列表:
- NumPy:格式名稱為“numpy”,更多資訊請參閱使用 Datasets 與 NumPy
- PyTorch:格式名稱為“torch”,更多資訊請參閱使用 Datasets 與 PyTorch
- TensorFlow:格式名稱為“tensorflow”,更多資訊請參閱使用 Datasets 與 TensorFlow
- JAX:格式名稱為“jax”,更多資訊請參閱使用 Datasets 與 JAX
有關如何高效建立 TensorFlow 資料集的更多詳細資訊,請檢視使用 Datasets 與 TensorFlow 指南。
當資料集以張量或陣列格式格式化時,所有資料都格式化為張量或陣列(例如,PyTorch 不支援的字串等型別除外)。
>>> ds = Dataset.from_dict({"text": ["foo", "bar"], "tokens": [[0, 1, 2], [3, 4, 5]]})
>>> ds = ds.with_format("torch")
>>> ds[0]
{'text': 'foo', 'tokens': tensor([0, 1, 2])}
>>> ds[:2]
{'text': ['foo', 'bar'],
'tokens': tensor([[0, 1, 2],
[3, 4, 5]])}
表格格式
您可以使用資料框或表格格式來最佳化資料載入和資料處理,因為它們通常提供零複製操作和以低階語言編寫的轉換。
以下是支援的資料框或表格格式列表:
- Pandas:格式名稱為“pandas”,更多資訊請參閱使用 Datasets 與 Pandas
- Polars:格式名稱為“polars”,更多資訊請參閱使用 Datasets 與 Polars
- PyArrow:格式名稱為“arrow”,更多資訊請參閱使用 Datasets 與 PyArrow
當資料集以資料框或表格格式格式化時,每個資料集行或行批次都格式化為資料框或表格,並且資料集列格式化為系列或陣列。
>>> ds = Dataset.from_dict({"text": ["foo", "bar"], "label": [0, 1]})
>>> ds = ds.with_format("pandas")
>>> ds[:2]
text label
0 foo 0
1 bar 1
這些格式可以透過避免資料複製來更快地迭代資料,並且還可以在 map() 或 filter() 中實現更快的資料處理。
>>> ds = ds.map(lambda df: df.assign(upper_text=df.text.str.upper()), batched=True)
>>> ds[:2]
text label upper_text
0 foo 0 FOO
1 bar 1 BAR
自定義格式轉換
with_transform() 函式即時應用自定義格式轉換。此函式替換任何先前指定的格式。例如,您可以使用此函式即時分詞和填充標記。分詞僅在訪問示例時應用。
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
>>> def encode(batch):
... return tokenizer(batch["sentence1"], batch["sentence2"], padding="longest", truncation=True, max_length=512, return_tensors="pt")
>>> dataset = dataset.with_transform(encode)
>>> dataset.format
{'type': 'custom', 'format_kwargs': {'transform': <function __main__.encode(batch)>}, 'columns': ['idx', 'label', 'sentence1', 'sentence2'], 'output_all_columns': False}
還有一個 set_transform() 函式,它執行相同的功能,但會就地執行。
您還可以使用 with_transform() 函式進行 Features 的自定義解碼。
以下示例使用 pydub
包作為 torchcodec
解碼的替代方案。
>>> import numpy as np
>>> from pydub import AudioSegment
>>> audio_dataset_amr = Dataset.from_dict({"audio": ["audio_samples/audio.amr"]})
>>> def decode_audio_with_pydub(batch, sampling_rate=16_000):
... def pydub_decode_file(audio_path):
... sound = AudioSegment.from_file(audio_path)
... if sound.frame_rate != sampling_rate:
... sound = sound.set_frame_rate(sampling_rate)
... channel_sounds = sound.split_to_mono()
... samples = [s.get_array_of_samples() for s in channel_sounds]
... fp_arr = np.array(samples).T.astype(np.float32)
... fp_arr /= np.iinfo(samples[0].typecode).max
... return fp_arr
...
... batch["audio"] = [pydub_decode_file(audio_path) for audio_path in batch["audio"]]
... return batch
>>> audio_dataset_amr.set_transform(decode_audio_with_pydub)
儲存
資料集準備好後,您可以將其儲存為 Parquet 格式的 Hugging Face 資料集,然後使用 load_dataset() 稍後重新使用。
透過提供要儲存到的 Hugging Face 資料集倉庫的名稱到 push_to_hub() 來儲存您的資料集。
encoded_dataset.push_to_hub("username/my_dataset")
您可以使用多個程序並行上傳,這在您想要加快速度時特別有用。
dataset.push_to_hub("username/my_dataset", num_proc=8)
使用 load_dataset() 函式重新載入資料集(流式或非流式)。
from datasets import load_dataset
reloaded_dataset = load_dataset("username/my_dataset", streaming=True)
另外,您可以將其本地儲存為磁碟上的 Arrow 格式。與 Parquet 相比,Arrow 未壓縮,這使得重新載入速度更快,非常適合本地磁碟使用和臨時快取。但由於它更大且元資料較少,因此上傳/下載/查詢比 Parquet 慢,並且不太適合長期儲存。
使用 save_to_disk() 和 load_from_disk() 函式從磁碟重新載入資料集。
>>> encoded_dataset.save_to_disk("path/of/my/dataset/directory")
>>> # later
>>> from datasets import load_from_disk
>>> reloaded_dataset = load_from_disk("path/of/my/dataset/directory")
匯出
🤗 Datasets 也支援匯出,這樣您就可以在其他應用程式中使用您的資料集。下表顯示了當前支援的您可以匯出的檔案格式:
檔案型別 | 匯出方法 |
---|---|
CSV | Dataset.to_csv() |
JSON | Dataset.to_json() |
Parquet | Dataset.to_parquet() |
SQL | Dataset.to_sql() |
記憶體中的 Python 物件 | Dataset.to_pandas(), Dataset.to_polars() 或 Dataset.to_dict() |
例如,將資料集匯出為 CSV 檔案,如下所示:
>>> encoded_dataset.to_csv("path/of/my/dataset.csv")