搭配 Trainer 的梯度累積
你正在為語言翻譯模型設定 Trainer,以使用梯度累積,讓你能有效以更大的批次進行訓練。你的模型會透過在 MRPC 資料集上的同義改寫(paraphrase)來簡化翻譯。請設定訓練參數以啟用梯度累積!呼叫 trainer.train() 之後,這個練習會需要一些時間執行。
model、dataset,以及 compute_metrics() 函式都已預先定義。
本練習屬於課程
使用 PyTorch 高效訓練 AI 模型
練習說明
- 將梯度累積步數設定為 2。
- 將訓練參數傳入
Trainer。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
training_args = TrainingArguments(
output_dir="./results",
evaluation_strategy="epoch",
# Set the number of gradient accumulation steps to two
____=____
)
trainer = Trainer(
model=model,
# Pass in the training arguments to Trainer
____=____,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
compute_metrics=compute_metrics,
)
trainer.train()