systemy_dialogowe/evaluate.py

45 lines
1.6 KiB
Python

import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model
import tensorflow_addons as tfa
import numpy as np
#loading model
loaded_model = tf.keras.models.load_model('model')
#data load
acts=pd.read_csv('user_acts_one_hot.csv', index_col="Unnamed: 0")
acts=acts.drop(["Agent"],axis=1)
acts=acts.drop(["Act"],axis=1)
#tokenizer polbert
from transformers import *
tokenizer = BertTokenizer.from_pretrained("dkleczek/bert-base-polish-uncased-v1")
input_data = acts["text"].tolist()
encoded_input = tokenizer.batch_encode_plus(input_data, padding=True, truncation=True, return_tensors='tf')
dataset = tf.data.Dataset.from_tensor_slices({
'input_ids': encoded_input['input_ids'],
'attention_mask': encoded_input['attention_mask'],
'token_type_ids': encoded_input['token_type_ids']
}).batch(2)
# make predictions
predictions = loaded_model.predict(dataset)
for prediction in predictions:
predicted_classes = (predictions[prediction]> 0.5).astype("int32")
classes = ["ack","affirm","bye","hello","help","negate","null","repeat","reqalts","reqmore","restart","silence","thankyou","confirm","deny","inform","request"]
true_acts = acts.drop(acts.columns[0],axis=1)
true= true_acts.to_numpy()
results = abs(predicted_classes-true)
all=results.size
not_predicted = results.sum()
accuracy = (all-not_predicted)/all
from sklearn.metrics import f1_score
micro_f1 = f1_score(true, predicted_classes, average='micro')
macro_f1 = f1_score(true, predicted_classes, average='macro')
print(f"Accuracy: "+{accuracy})
print(f"micro f1 score : "+{micro_f1})
print(f"macro f1 score : "+{macro_f1})