開始使用免費開始

將模型設為評估模式

你已經準備好把語言模型切換到評估模式。若在推論時未啟用評估模式,像是 batch normalization 與 dropout 等層會改變模型行為,導致翻譯品質不一致。請建立迴圈來評估模型!

部分資料已預先載入:modeleval_dataloaderacceleratormetric

本練習屬於課程

使用 PyTorch 高效訓練 AI 模型

檢視課程

練習說明

  • 在對資料集進行批次迴圈之前,先將模型設為評估模式。
  • 使用 Accelerator 的 .gather_for_metrics() 方法,跨裝置彙整 predictionslabels,以計算評估指標。
  • 在最後計算評估指標。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

metric = evaluate.load("glue", "mrpc")

# Set the model in evaluation mode
____.____()
for step, batch in enumerate(eval_dataloader):
    with torch.no_grad():
        outputs = model(**batch)
    predictions = outputs.logits.argmax(dim=-1)
    # Aggregate values across devices
    predictions, references = ____.____((predictions, batch["labels"]))
    metric.add_batch(predictions=predictions, references=references)
# Compute the evaluation metric
eval_metric = metric.____()
print(eval_metric)
編輯並執行程式碼