klue_bert_layered / mymodel.py
winninglife's picture
initial commit
c344d9f
raw
history blame
15.6 kB
import re
import joblib
import pickle
import numpy as np
import pandas as pd
import tensorflow as tf
from typing import Optional, Union, Tuple
from gensim.models import Word2Vec
from transformers import BertTokenizer
from transformers import BertForSequenceClassification, Trainer, TrainingArguments, BertModel
from transformers.modeling_outputs import SequenceClassifierOutput
from torch.nn import MSELoss, CrossEntropyLoss, BCEWithLogitsLoss
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import torch.nn.functional as F
import torch
import time
from torch import nn
from transformers import Trainer
from transformers import AutoModel, AutoTokenizer
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
NUM_CLASSES = 3 # λΆ„λ₯˜ 클래슀 수
DROP_OUT = 0.3 # μ›ν•˜λŠ” dropout ν™•λ₯ 
class SentimentDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels=None):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
if self.labels:
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.encodings["input_ids"])
class CustomBertForSequenceClassification(BertForSequenceClassification):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.config = config
self.bert = BertModel(config)
classifier_dropout = (
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
)
self.dropout = nn.Dropout(classifier_dropout)
# ν•˜κΈ° λ°©μ‹μœΌλ‘œ λŒ€μ²΄ν•œλ‹€.
#self.classifier = nn.Linear(config.hidden_size, config.num_labels)
# https://github.com/KisuYang/EmotionX-KU/blob/master/models.py
self.linear_h = nn.Linear(config.hidden_size, 384)
self.linear_o = nn.Linear(384, config.num_labels)
self.selu = nn.SELU()
print("hidden_size:", config.hidden_size, "num_lables:", config.num_labels)
# Initialize weights and apply final processing
self.post_init()
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
token_type_ids: Optional[torch.Tensor] = None,
position_ids: Optional[torch.Tensor] = None,
head_mask: Optional[torch.Tensor] = None,
inputs_embeds: Optional[torch.Tensor] = None,
labels: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]:
r"""
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
"""
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# outputs[0]: batch_size(16), feature_size(38), hidden_size(768)
# outputs[1]: batch_size(16), hidden_size(768)
# BertModel 의 좜λ ₯쀑 Pooled Output 좜λ ₯을 μ·¨ν•œλ‹€.
pooled_output = outputs[1]
# Dropout 전에 https://github.com/KisuYang/EmotionX-KU/blob/master/models.py λ°©μ‹μœΌλ‘œ λ ˆμ΄μ–΄λ₯Ό μΆ”κ°€ν•œλ‹€.
pooled_output = self.selu(self.linear_h(pooled_output))
# Dropout 적용
pooled_output = self.dropout(pooled_output)
# Linear layerλ₯Ό ν†΅κ³Όμ‹œμΌœ num_labels 에 ν•΄λ‹Ήν•˜λŠ” 좜λ ₯을 μƒμ„±ν•œλ‹€.
#logits = self.classifier(pooled_output)
logits = self.linear_o(pooled_output)
loss = None
if labels is not None:
if self.config.problem_type is None:
if self.num_labels == 1:
self.config.problem_type = "regression"
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
self.config.problem_type = "single_label_classification"
else:
self.config.problem_type = "multi_label_classification"
if self.config.problem_type == "regression":
loss_fct = MSELoss()
if self.num_labels == 1:
loss = loss_fct(logits.squeeze(), labels.squeeze())
else:
loss = loss_fct(logits, labels)
elif self.config.problem_type == "single_label_classification":
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
elif self.config.problem_type == "multi_label_classification":
loss_fct = BCEWithLogitsLoss()
loss = loss_fct(logits, labels)
if not return_dict:
output = (logits,) + outputs[2:]
return ((loss,) + output) if loss is not None else output
return SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
def train_model(model_name, X_train, X_test, y_train, y_test, epochs=2, train_batch_size=8, eval_batch_size=16, use_emotion_x=False):
tokenizer = BertTokenizer.from_pretrained(model_name)
train_encodings = tokenizer(X_train, truncation=True, padding=True)
train_dataset = SentimentDataset(train_encodings, y_train)
test_encodings = tokenizer(X_test, truncation=True, padding=True)
test_dataset = SentimentDataset(test_encodings, y_test)
print(train_dataset[1]['input_ids'].shape)
print(train_dataset[1]['attention_mask'].shape)
training_args = TrainingArguments(
output_dir='./results', # output μ €μž₯ directory
num_train_epochs=epochs, # total number of training epochs
per_device_train_batch_size=train_batch_size, # batch size per device during training
per_device_eval_batch_size=eval_batch_size, # batch size per device during evaluation
warmup_steps = 500, # number of warmup steps for learning rate scheduler
weight_decay = 0.01, # weight decay 강도
logging_dir='./logs', # log μ €μž₯ directory
logging_steps=10,
do_eval=True
)
if use_emotion_x == True:
model = CustomBertForSequenceClassification.from_pretrained(model_name, num_labels=NUM_CLASSES).to('cuda')
else:
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=NUM_CLASSES).to('cuda')
trainer = Trainer(
model = model,
args = training_args,
train_dataset = train_dataset,
eval_dataset = test_dataset
)
s = time.time()
trainer.train()
trainer.evaluate(test_dataset)
prediction = trainer.predict(test_dataset)
y_logit = torch.tensor(prediction[0])
y_pred = F.softmax(y_logit, dim=-1).argmax(axis=1).numpy()
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
print(accuracy_score(y_test, y_pred))
return trainer , tokenizer
def test_trainer(trainer, tokenizer):
POSITIVE = 0
NEGATIVE = 1
NEUTRAL = 2
idx_to_label = {POSITIVE:'positive', NEGATIVE:'negative', NEUTRAL:'neutral'}
test_dict = {
'였늘 짜증 μ§€λŒ€λ‘œλ„€': NEGATIVE,
'톡μž₯이 ν……ν…… λΉ„μ—ˆμŒ': NEGATIVE,
'경제 사정이 μ’€ λ‚˜μ•„μ Έμ„œ μ’‹λ„€μš”': POSITIVE,
'κ΅­κ°€κ°„ 관계가 μ•…ν™”λ˜κ³  μžˆμ–΄μš”': NEGATIVE,
'ν•œκ΅­κ³Ό 일본은 사이가 μ•ˆμ’‹μ•„μš”.': NEGATIVE,
'μ‹€νŒ¨λŠ” μ„±κ³΅μ˜ μ–΄λ¨Έλ‹ˆμ΄λ‹€.': POSITIVE,
'날씨가 λ”°λœ»ν•΄μ„œ 마음이 νŽΈμ•ˆν•΄μš”.': POSITIVE,
'μ£Όλ¨Έλ‹ˆ 사정이 νŒŒμ‚° μ§μ „μž„' : NEGATIVE,
'λ„ˆλ¬΄ 걱정말고 νž˜λ‚΄!' : POSITIVE,
'μ•„ μ§„μ§œ! μ§œμ¦λ‚˜κ²Œ ꡴지말고 저리가!' : NEGATIVE,
'인생이 ν”Όκ³€ν•˜λ‹€.' : NEGATIVE,
'λ”°λœ»ν•œ 말씀 κ°μ‚¬ν•©λ‹ˆλ‹€.' :POSITIVE,
'바보같은 λ†ˆλ“€ ν•œμ‹¬ν•˜λ„€' :NEGATIVE,
'κ·Έ 말이 μ €λ₯Ό λ„ˆλ¬΄ νž˜λ“€κ²Œ ν•˜λ„€μš”' : NEGATIVE,
'μšΈμ§€λ§κ³  νž˜λ‚΄':POSITIVE,
'눈물이 λ©ˆμΆ”μ§ˆ μ•Šμ•„μš”':NEGATIVE,
'μƒˆλ‘œμš΄ 사μž₯λ‹˜μ€ 진취적인 뢄이라 κΈ°λŒ€κ°€ λœλ‹€':POSITIVE,
'였늘 할일이 νƒœμ‚°μ΄λ„€':NEUTRAL,
'할일이 λ„ˆλ¬΄ λ§Žμ§€λ§Œ κΎΈμ—­κΎΈμ—­ ν•˜κ³  μžˆμ–΄':NEUTRAL,
'λ°°κ°€ κ³ ν”„λ„€μš”':NEUTRAL,
'집에 κ°€κ³  μ‹Άλ„€μš”':NEUTRAL,
'μ½”μ½”μ•„ ν•œμž” ν•˜μ‹€λž˜μš”?':NEUTRAL,
'컴퓨터 λ°”κΏ”μ£Όμ„Έμš”.':NEUTRAL,
'ν•œλŒ€ λ§žμ„λž˜?': NEGATIVE,
'μ‹ λ‚˜λŠ” 여행을 μƒκ°ν•˜λ‹ˆ 기뢄이 μ’‹μŠ΅λ‹ˆλ‹€':POSITIVE,
'λ°°κ³ ν”ˆλ° λ°₯이 μ—†μ–΄μš”.':NEGATIVE,
'κ΅­κ°€ κ²½μ œκ°€ νŒŒνƒ„ λ‚˜λŠ” 쀑이닀.':NEGATIVE,
'λ„ˆλ•Œλ¬Έμ— λ‚΄κ°€ λ„ˆλ¬΄ νž˜λ“€μ–΄':NEGATIVE,
'κ·Έλž˜λ„ λ‹ˆκ°€ μžˆμ–΄μ„œ 닀행이야':POSITIVE,
'μ•”μšΈν•œ 경제 사정에도 μ—΄μ‹¬νžˆ ν•΄μ€˜μ„œ κ³ λ§ˆμ›Œμš”':POSITIVE,
'였늘 κΈ°λΆ„ μ§±μ΄μ—μš”':POSITIVE,
'λ„ˆλŠ” λŒ€μ²΄ ν•  쀄 μ•„λŠ”κ²Œ λ­λ‹ˆ?':NEGATIVE,
'μˆ™μ œκ°€ λ„ˆλ¬΄ μ–΄λ €μ›Œ λ―ΈμΉ˜κ² λ‹€':NEGATIVE,
'우리 νŒ€μ›λ“€ μ—΄μ‹¬νžˆ ν•΄μ€˜μ„œ μžλž‘μŠ€λŸ½μŠ΅λ‹ˆλ‹€':POSITIVE,
'Wow! μ˜ν™” μ§„μ§œ μž¬λ―Έμžˆλ„€':POSITIVE,
'γ… γ…  νž˜λ“€μ–΄ 죽을거 κ°™μ•„μš”':NEGATIVE,
'이번 μ—¬ν–‰μ½”μŠ€λŠ” 정말 ν™˜μƒμ μ΄λ„€μš”':POSITIVE,
'λ‹΅λ‹΅ν•œ μƒν™©μ΄μ§€λ§Œ λ„Œ 이겨낼 수 μžˆμ„κΊΌμ•Ό':POSITIVE,
'λ‹΅λ‹΅ν•œ μƒν™©μ΄μ§€λ§Œ λ„Œ 잘 ν•΄λ‚Ό 수 μžˆμ„κΊΌμ•Ό':POSITIVE,
'μ–Έμ œλ‚˜ 곁에 μžˆμ–΄μ€˜μ„œ 힘이 λ©λ‹ˆλ‹€.':POSITIVE,
'λͺΈμ΄ λ„ˆλ¬΄ μ•„νŒŒμ„œ 일이 손에 μ•ˆμž‘ν˜€μš”':NEGATIVE,
'λ„ˆ 정말 μž˜ν•œλ‹€ λ¦¬μŠ€νŽ™!':POSITIVE,
'μŠ¬ν”„μ§€λ§Œ κ΄œμ±¦μ•„':POSITIVE,
'κ°œλΉ‘μΉ˜λ„€ μ§„μ§œ':NEGATIVE,
'λΉ„κ°€ λ„ˆλ¬΄ 많이 μ™€μ„œ 집이 λ– λ‚΄λ €κ°”μ–΄μš”':NEGATIVE,
'햇빛이 μ¨μ¨ν•΄μ„œ 옷이 잘 마λ₯΄λ„€μš”':POSITIVE,
'AIκ³΅λΆ€λŠ” μ–΄λ ΅μ§€λ§Œ μž¬λ―Έμžˆμ–΄μš”':POSITIVE,
'널 μ–΄μ©Œλ©΄ 쒋냐? ν•œμˆ¨λ°–μ— μ•ˆλ‚˜μ˜¨λ‹€':NEGATIVE,
'λ„λŒ€μ²΄ 무슨 μƒκ°μœΌλ‘œ 이런 짓을 ν•œκ±°μ•Ό?':NEGATIVE,
'λ―Έμ›Œλ„ λ‹€μ‹œ ν•œλ²ˆ':POSITIVE,
'도움 말씀 κ°μ‚¬ν•©λ‹ˆλ‹€':POSITIVE,
'말도 μ•ˆλ˜λŠ” μ†Œλ¦¬ κ·Έλ§Œν•˜κ³  저리가':NEGATIVE,
'였늘 컀피챗 λΆ„μœ„κΈ° κ΅Ώ':POSITIVE,
'κΈ°λΆ„ λ‚˜λΉ μ„œ λ„ˆλž‘ μ–˜κΈ°ν•˜κΈ° μ‹«μ–΄':NEGATIVE,
'이 κ·Έλ¦Ό λ„ˆλ¬΄ λ§ˆμŒμ— λ“ λ‹€':POSITIVE,
'어이가 μ—†μ–΄μ„œ ν•  말이 μ—†μ–΄':NEGATIVE,
'λ™λ£Œ 직원이 퇴사 인사λ₯Ό ν–ˆλŠ”λ° μ”μ“Έν•œ 마음이 λ“œλ„€':NEGATIVE,
'νŒ€μ›μ΄ 아이디어 κ²€ν† λ₯Ό μš”μ²­ν–ˆλŠ”λ° λ„ˆλ¬΄ 쒋은 아이디어 κ°™μ•„. μ˜κ²¬μ„ λ¬Όμ–΄λ΄μ€˜μ„œ κ³ λ§ˆμ›Œ':POSITIVE,
'성격이 쒋은 νŒ€μ›λ“€κ³Ό ν•¨κ»˜ ν•  수 μžˆμ–΄μ„œ 닀행이야':POSITIVE,
'κΈˆμš”μΌλ§Œ 되면 기뢄이 μ’‹μ•„μ Έ':POSITIVE,
'벌써 μΌμš”μΌμ΄λΌλ‹ˆ μΆœκ·Όν•  μƒκ°ν•˜λ‹ˆ κΈ‰ λ‹€μš΄λœλ‹€.':NEGATIVE,
'μ§œμ¦λ‚˜λ‹ˆκΉŒ μ–˜κΈ°ν•˜μ§€λ§ˆ!':NEGATIVE,
'λ„ˆλ¬΄ 심심해.':NEUTRAL,
'λ˜‘λ˜‘ν•œ μ‚¬λžŒμ΄λž‘ λŒ€ν™”ν•˜λŠ”κ±΄ μ¦κ±°μ›Œμš”':POSITIVE,
'당신은 항상 μ›ƒλŠ” μ–Όκ΅΄μ΄μ–΄μ„œ λ§Œλ‚˜λ©΄ 기뢄이 μ’‹μ•„μ Έμš”':POSITIVE,
'연섀이 λ„ˆλ¬΄ λ”°λΆ„ν•΄μ„œ ν•˜ν’ˆμ΄ λ‚˜μ™€μš”':NEGATIVE,
'λ§›μžˆλŠ” 식당에 갈 생각을 ν•˜λ‹ˆ μ‹ λ‚˜μš”':POSITIVE,
'이런 ν›Œλ₯­ν•œ κ°•μ˜λ₯Ό λ“£κ²Œ λ˜μ„œ μ˜κ΄‘μž…λ‹ˆλ‹€.':POSITIVE,
'λ§Œλ‚˜λ΅™κ²Œ λ˜μ„œ λ°˜κ°‘μŠ΅λ‹ˆλ‹€.':POSITIVE,
'κ·Έ μ‚¬λžŒλ§Œ λ§Œλ‚˜λ©΄ 짜증이 λ‚˜μ„œ 보기가 μ‹«μ–΄':NEGATIVE,
'아이듀이 ν™œκΈ°μ°¨κ²Œ λ›°μ–΄λ…ΈλŠ” λͺ¨μŠ΅μ΄ 보기 μ’‹μ•„μš”':POSITIVE,
'ν•œμ‹¬ν•œ μ†Œλ¦¬μ’€ κ·Έλ§Œν•  수 μ—†μ–΄μš”?':NEGATIVE,
'웃기고 μžλΉ μ‘Œλ„€!':NEGATIVE,
'휴! μ‹­λ…„ κ°μˆ˜ν–ˆλ„€!':NEUTRAL,
'말같지도 μ•Šμ€ μ†Œλ¦¬ν•˜κ³  μžˆμ–΄! γ……γ…‚':NEGATIVE,
'μž…μ—μ„œ μš•μ΄ μžλ™μœΌλ‘œ λ‚˜μ˜¨λ‹€...':NEGATIVE,
'μž…λ§Œ μ—΄λ©΄ 거짓말이 μžλ™μœΌλ‘œ λ‚˜μ™€!':NEGATIVE,
'μ €κ±° 바보 아냐?':NEGATIVE,
'νž˜λ“€λ•Œ 곁에 μžˆμ–΄μ€˜μ„œ κ³ λ§ˆμ›Œ':POSITIVE,
'아이큐가 μ†Œμˆ«μ  μ΄ν•˜ κ°™μ•„':NEGATIVE,
'μ €λŸ° λͺ¨μ§€λ¦¬ κ°™μœΌλ‹ˆλΌκ³ ':NEGATIVE,
'지지리 λͺ»λ‚œ λ†ˆ':NEGATIVE,
'μ € 인간 λ•Œλ¬Έμ— λ‚΄κ°€ 제 λͺ…에 λͺ»μ‚΄κ²ƒ κ°™μ•„':NEGATIVE,
'μ € μƒˆλΌ μ£½μ—¬':NEGATIVE,
'λ„Œ 정말 μ²œμ‚¬κ°™μ•„':POSITIVE,
'당신이 μ’‹μ•„μš” 항상 곁에 μžˆμ–΄μ£Όμ„Έμš”':POSITIVE,
'꼴도 보기 μ‹«μœΌλ‹ˆ 썩 κΊΌμ Έ':NEGATIVE,
'μ•„ μ§„μ§œ λŒμ•„λ²„λ¦¬κ² λ„€':NEGATIVE,
'μ—­κ²¨μš΄ λ†ˆλ“€':NEGATIVE,
'μ €λŸ° 미인을 λ³΄λ‹ˆ μ•ˆκ΅¬κ°€ μ •ν™”λ˜λŠ” λŠλ‚Œμ΄μ•Ό':POSITIVE,
'μ•„μ˜€ γ……γ…‚ 눈 μ©λŠ”λ‹€':NEGATIVE,
'κΉμΉ˜μ§€λ§ˆ λ’€μ§ˆλž˜?':NEGATIVE,
'μ–Έμ œλ“  ν™˜μ˜μ΄μ—μš”':POSITIVE,
'쀘 νŒ¨λ²„λ¦¬κ³  μ‹Άλ„€ μ§„μ§œ':NEGATIVE,
'μ• κΈ°λ§Œ 보면 μ›ƒμŒμ΄ λ‚˜μ™€':POSITIVE,
'ν•˜λŠ” 짓 보면 μ €λŠ₯μ•„ κ°™μ•„':NEGATIVE,
'칭챙좍':NEGATIVE,
'왓더뻑':NEGATIVE,
'이 λΉ‘λŒ€κ°€λ¦¬μ•Ό':NEGATIVE,
'λŒλŒ€κ°€λ¦¬ μžμ‹':NEGATIVE,
'λ„ˆλ„ μžμ‹μ΄λΌκ³  낳은 λ‹ˆ μ—„λ§ˆκ°€ 뢈쌍':NEGATIVE,
'예쁜 κ³΅μ£Όλ‹˜μ΄μ—μš” μΆ•ν•˜ν•΄μš”':POSITIVE,
'μ”©μ”©ν•œ μ™•μžλ‹˜μ΄μ—μš”. μ’‹μœΌμ‹œκ² μ–΄μš”.':POSITIVE,
'얼씨ꡬ μ’‹λ‹€':POSITIVE,
'ν•˜λŠ˜μ΄ λ¬΄λ„ˆμ§€λŠ” 기뢄이야':NEGATIVE,
'ν•˜λŠ˜μ„ λ‚˜λŠ” 기뢄이야':POSITIVE,
'μ•„μžμ•„μž ν™”μ΄νŒ…!':POSITIVE,
'κ°œμƒˆλΌ':NEGATIVE,
'μ•„μ£Ό λ‚˜μ΄μŠ€':POSITIVE,
'닡도 μ—†λŠ” 인간듀':NEGATIVE,
'정말 μ—¬κΈ΄ μ €λŠ₯μ•„ 집단 κ°™μ•„':NEGATIVE,
'λ§Œλ‚˜μ„œ λ°˜κ°€μ›Œμš”. 정말 λ―ΈμΈμ΄μ‹œλ„€μš”':POSITIVE,
'당신이 κ·Έλ¦¬μ›Œμš”. λ³΄κ³ μ‹Άμ–΄μš”.':POSITIVE,
'λ°”λΌλ§Œ 봐도 μ›ƒμŒμ΄ λ‚˜μ™€μš”':POSITIVE,
'개 μ—΄λ°›λ„€':NEGATIVE,
'λ ˆμ•Œ λŒ€λ°• γ…‹γ…‹γ…‹':POSITIVE,
'λ„ˆλ¬΄ λ³΄κ³ μ‹Άμ—ˆμ–΄μš”. μ΄λ ‡κ²Œ λ§Œλ‚˜κ²Œλ˜μ„œ λ°˜κ°‘μŠ΅λ‹ˆλ‹€.':POSITIVE,
'μΉœκ΅¬μ•Ό μ‚¬λž‘ν•΄':POSITIVE,
'이 바보 μžμ‹μ•„':NEGATIVE,
'μ˜€λŠ˜μ€ 날씨가 μ°Έ μ’‹λ„€μš”. 기뢄이 μƒμΎŒν•΄μš”.':POSITIVE,
'μ†μƒν•΄μ„œ λ°₯이 μ•ˆλ„˜μ–΄κ°„λ‹€.': NEGATIVE,
'마음이 μšΈμ ν•΄μ„œ 길을 λ‚˜μ„°λ„€':NEGATIVE,
'μ˜€λŠ˜μ€ 인생 졜고의 λ‚ ': POSITIVE,
'이 ν›Œλ₯­ν•œ 일에 λ™μ°Έν•˜κ²Œ λ˜μ„œ μ˜κ΄‘μž…λ‹ˆλ‹€.':POSITIVE,
'λ‚˜ μ§‘μ—μ„œ μžλŠ” 쀑':NEUTRAL,
}
hit_cnt = 0
tot_cnt = len(test_dict)
for x, y in test_dict.items():
tokenized = tokenizer([x], truncation=True, padding=True)
pred = trainer.predict(SentimentDataset(tokenized))
logit = torch.tensor(pred[0])
result = F.softmax(logit, dim=-1).argmax(1).numpy()
if result[0] != y:
print(f"ERROR: {x} expected:{idx_to_label[y]} result:{idx_to_label[result[0]]}")
else:
hit_cnt += 1
print()
print(f"hit/total: {hit_cnt}/{tot_cnt}, rate: {hit_cnt/tot_cnt}")