微調前的資料前處理

使用 Llama 3 進行微調

Francesca Donadoni

Curriculum Manager, DataCamp

使用資料集來進行微調

  • 資料品質 是關鍵

  • 訓練集(Training Set)

    • 用於模型訓練
    • 佔大多數資料

資料集含訓練集的示意圖。

使用 Llama 3 進行微調

使用資料集來進行微調

  • 資料品質 是關鍵

  • 訓練集(Training Set)

    • 用於模型訓練
    • 佔大多數資料
  • 驗證集(Validation Set)
    • 用於挑選最佳模型版本

訓練集與驗證集示意圖。

使用 Llama 3 進行微調

使用資料集來進行微調

  • 資料品質 是關鍵

  • 訓練集(Training Set)

    • 用於模型訓練
    • 佔大多數資料
  • 驗證集(Validation Set)
    • 用於挑選最佳模型版本
  • 測試集(Test Set)
    • 用於評估模型效能

訓練集、驗證集與測試集示意圖。

使用 Llama 3 進行微調

使用 datasets 函式庫準備資料

 

  • Datasets 函式庫
  • 前處理
  • 分割
  • 載入
  • 記憶體管理

資料流程示意圖:資料集流入 datasets 函式庫,顯示 3 個綠色方塊(前處理、載入/資料管理、整合),箭頭指向輸出(已準備好的資料)。

使用 Llama 3 進行微調

載入客服資料集

from datasets import load_dataset

ds = load_dataset( 'bitext/Bitext-customer-support-llm-chatbot-training-dataset',
split="train"
)
print(ds.column_names)
['flags', 'instruction', 'category', 'intent', 'response']
使用 Llama 3 進行微調

快速查看資料

import pprint
pprint.pprint(ds[0])
{'category': 'ORDER',
 'flags': 'B',
 'instruction': 'question about cancelling order {{Order Number}}',
 'intent': 'cancel_order',
 'response': "I've understood you have a question regarding canceling order "
             "{{Order Number}}, and I'm here to provide you with the "
             'information you need. Please go ahead and ask your question, and '
             "I'll do my best to assist you."}
使用 Llama 3 進行微調

篩選資料集

from datasets import load_dataset, Dataset

ds = load_dataset(
    'bitext/Bitext-customer-support-llm-chatbot-training-dataset',
    split="train")

print(ds.shape)
(26872, 5)
first_thousand_points = ds[:1000]

ds = Dataset.from_dict(first_thousand_points)
使用 Llama 3 進行微調

資料集前處理

def merge_example(row):

row['conversation'] = f"Query: {row['instruction']}\nResponse: {row['response']}" return row
ds = ds.map(merge_example)
print(ds[0]['conversation'])
Query: question about cancelling order {{Order Number}}
Response: I've understood you have a question regarding canceling order {{Order Number}}, 
and I'm here to provide you with the information you need. Please go ahead and ask your 
question, and I'll do my best to assist you.
使用 Llama 3 進行微調

儲存前處理後的資料集

ds.save_to_disk("preprocessed_dataset")
Saving the dataset (1/1 shards): 100%
26872/26872 [00:00<00:00, 383823.33 examples/s]
from datasets import load_from_disk
ds_preprocessed = load_from_disk("preprocessed_dataset")
使用 Llama 3 進行微調

在 TorchTune 中使用 Hugging Face 資料集

 

  • 可搭配 TorchTune 使用 Hugging Face 資料集
  • 設定資料集路徑與組態

 

tune run full_finetune_single_device --config llama3/8B_full_single_device \
dataset=preprocessed_dataset dataset.split=train
使用 Llama 3 進行微調

一起來練習吧!

使用 Llama 3 進行微調

Preparing Video For Download...