定义评估指标
您正在为一款视频会议应用开发实时语言翻译服务。为便于监控训练过程,您将定义准确率和 F1 分数这两个评估指标,用于衡量模型的整体表现。
evaluate 与 numpy(np)库已预先导入。
本练习是课程的一部分
使用 PyTorch 高效训练 AI 模型
练习说明
- 使用
evaluate库加载f1分数;accuracy已为您加载好。 - 将
eval_predictions解包为两个变量,以提取logits和labels。 - 将
logits转换为predictions。 - 基于
predictions和labels计算f1分数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
def compute_metrics(eval_predictions):
load_accuracy = evaluate.load("accuracy")
# Load the F1 score
load_f1 = evaluate.load("____")
# Extract logits and labels from eval_predictions
logits, ____ = eval_predictions
# Convert logits to predictions
predictions = np.____(logits, axis=-1)
accuracy = load_accuracy.compute(predictions=predictions, references=labels)["accuracy"]
# Compute the F1 score
f1 = ____.____(predictions=predictions, references=labels)["f1"]
return {"accuracy": accuracy, "f1": f1}