시작하기무료로 시작하기

모델을 평가 모드로 설정하기

이제 언어 모델을 평가 모드로 설정할 차례입니다. 추론 시 모델이 평가 모드로 설정되지 않으면, 배치 정규화(batch normalization)나 드롭아웃(dropout) 같은 레이어가 모델 동작에 영향을 주어 번역 품질이 일관되지 않을 수 있습니다. 모델을 평가하는 루프를 완성해 보세요!

다음 데이터는 미리 불러와져 있습니다: model, eval_dataloader, accelerator, metric.

이 연습은 강의의 일부입니다

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)
코드 편집 및 실행