-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model.py
70 lines (50 loc) · 1.6 KB
/
test_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import timeit
start = timeit.default_timer()
from sklearn.neural_network import MLPClassifier
import json
from transformers import AutoTokenizer, AutoModel
from sklearn.metrics import f1_score, classification_report
import torch
from sklearn.model_selection import train_test_split
import re
import pickle
filename = 'finalized_model.sav'
# load the model from disk
def filter(text):
final_text = ''
final_text = text.replace("<br />"," ")
final_text = text.replace(" "," ")
return final_text
tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-128k-uncased")
bert = AutoModel.from_pretrained("dbmdz/bert-base-turkish-128k-uncased").to()
try:
def feature_extraction(text):
x = tokenizer.encode(filter(text))
with torch.no_grad():
x, _ = bert(torch.stack([torch.tensor(x)]).to())
return list(x[0][0].cpu().numpy())
except Exception as e:
print(e)
with open("/home/felix/PycharmProjects/turkishnlp/csvjson.json", 'r') as f:
data = json.load(f)
x = []
y = []
for i in data[:1000]:
try:
x.append(feature_extraction(i["Görüş"]))
if (i["Durum"] == "Tarafsız"):
y.append(1)
elif (i["Durum"] == "Olumsuz"):
y.append(0)
elif (i["Durum"] == "Olumlu"):
y.append(2)
except Exception as e:
print(e)
continue
x_train, x_test, y_train, y_test = train_test_split(x, y)
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(x_test , y_test)
print(result)
#Your statements here
stop = timeit.default_timer()
print('Time: ', stop - start)