使用 Hugging Face 進行模型微調

使用 Llama 3 進行微調

Francesca Donadoni

Curriculum Manager, DataCamp

進行微調需要哪些要素?

  1. 語言模型+tokenizer(如 LLama 系列的 TinyLLama-v0)
  2. 訓練資料集(Bitext 客服資料集
  3. 訓練參數
  4. 執行微調(來自 TRLSFTTrainer
  5. 評估基準或資料集

訓練流程。訓練需要訓練參數、資料與模型,並產生微調後模型。評估使用微調後模型與評估資料集。

使用 Llama 3 進行微調

使用 Auto 類別載入模型與 tokenizer 的方法

model_name="Maykeye/TinyLLama-v0"

model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

tokenizer.pad_token = tokenizer.eos_token
1 https://huggingface.co/docs/transformers/main/en/model_doc/auto
使用 Llama 3 進行微調

用 TrainingArguments 定義訓練參數

training_arguments = TrainingArguments(

per_device_train_batch_size=1,
learning_rate=2e-3,
max_grad_norm=0.3,
max_steps=200,
... gradient_accumulation_steps=2, save_steps=10,
)
1 https://huggingface.co/docs/transformers/v4.40.1/en/main_classes/trainer#transformers.TrainingArguments
使用 Llama 3 進行微調

用 SFTTrainer 設定訓練流程

trainer = SFTTrainer(

model=model, tokenizer=tokenizer,
train_dataset=dataset, dataset_text_field='conversation',
max_seq_length=250,
args=training_arguments
)
使用 Llama 3 進行微調

解讀 SFTTrainer 的微調結果

trainer.train()
TrainOutput(global_step=200, training_loss=1.9401231002807617,
            metrics={'train_runtime': 142.5501, 
                     'train_samples_per_second': 2.806,
                     'train_steps_per_second': 1.403, 
                     'total_flos': 1461265827840.0, 
                     'train_loss': 1.9401231002807617, 
                     'epoch': 2.0})
使用 Llama 3 進行微調

用 ROUGE-1 評估訓練後模型的方法

  • ROUGE-1:參考答案與生成文字的詞重疊比例
import evaluate

rouge = evaluate.load('rouge')
predictions = ["hello there", "general kenobi"] references = ["hello there", "master yoda"]
results = rouge.compute(predictions=predictions, references=references) print(results)
{'rouge1': 0.5, 'rouge2': 0.5, 'rougeL': 0.5, 'rougeLsum': 0.5}
1 https://huggingface.co/spaces/evaluate-metric/rouge
使用 Llama 3 進行微調

如何使用 ROUGE-1 分數

  1. evaluation_dataset 使用評估集
def generate_predictions_and_reference(dataset):
    predictions = []
    references = []
    for row in dataset:
        inputs = tokenizer.encode(row["instruction"], return_tensors="pt")

outputs = model.generate(inputs)
decoded_outputs = tokenizer.decode(outputs[0, inputs.shape[1]:], skip_special_tokens = True)
references += [row["response"]] predictions += [decoded_outputs] return references, predictions
使用 Llama 3 進行微調

如何在評估集上執行 ROUGE-1

references, predictions = generate_predictions_and_reference(evaluation_dataset)
rouge = evaluate.load('rouge')
results = rouge.compute(predictions=predictions, references=references)
print(results)
使用 Llama 3 進行微調

微調 vs 未微調

已微調

{'rouge1': 0.22425812699023645,
 'rouge2': 0.039502543246449,
 'rougeL': 0.1501513006868983,
 'rougeLsum': 0.18685597710721613}

未微調

{'rouge1': 0.1310928764315105,
 'rouge2': 0.04581654122835097,
 'rougeL': 0.08415351421221628,
 'rougeLsum': 0.1224749866097021}
使用 Llama 3 進行微調

一起來練習吧!

使用 Llama 3 進行微調

Preparing Video For Download...