BERT 모델 평가하기
BERT 토크나이저로 샘플 리뷰를 토크나이즈했으니, 이제 PyBooks의 샘플로 BERT 모델을 평가해 보겠습니다. 추가로, 새로운 데이터에 대한 감성 예측도 평가할 거예요.
다음이 미리 임포트되어 있습니다: BertTokenizer, BertForSequenceClassification, torch.
학습된 model 인스턴스도 미리 로드되어 있어요. 이제 새로운 데이터 샘플로 테스트해 보겠습니다.
이 연습은 강의의 일부입니다
PyTorch로 배우는 텍스트 딥러닝
연습 안내
- 평가용 텍스트를 토크나이즈하고 PyTorch 텐서를 반환하도록 준비하세요.
- 출력 logits를 0과 1 사이의 확률로 변환하세요.
- 확률에서 감성을 표시하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
text = "I had an awesome day!"
# Tokenize the text and return PyTorch tensors
input_eval = tokenizer(____, return_tensors=____, truncation=True, padding=True, max_length=32)
outputs_eval = model(**input_eval)
# Convert the output logits to probabilities
predictions = torch.nn.functional.____(outputs_eval.____, dim=-1)
# Display the sentiments
predicted_label = ____ if torch.____(predictions) > 0 else ____
print(f"Text: {text}\nSentiment: {predicted_label}")