-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
65 lines (55 loc) · 2.01 KB
/
evaluate.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
import torch
from Dataset import Dataset
import sqlite3
import pandas as pd
import torch
def test(model, test_data):
test = Dataset(test_data)
test_dataloader = torch.utils.data.DataLoader(test, batch_size=1)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
if use_cuda:
model = model.cuda()
total_acc_test = 0
with torch.no_grad():
for test_input, test_label in test_dataloader:
test_label = test_label.to(device)
mask = test_input['attention_mask'].to(device)
input_id = test_input['input_ids'].squeeze(1).to(device)
output = model(input_id, mask)
acc = (output.argmax(dim=1) == test_label).sum().item()
total_acc_test += acc
print(f'Test Accuracy: {total_acc_test / len(test_data): .3f}')
if __name__ == "__main__":
con = sqlite3.connect('articles.db')
cursorObj = con.cursor()
def getData(query):
data=cursorObj.execute(query)
res = data.fetchall()
labels, texts = zip(*res)
return pd.DataFrame({'type':labels, 'text':texts})
print("Loading test dataset... ")
title5 = getData("select type, title from test")
head5 = getData("select type, head from test")
title4 = getData("select type, title from test where not type = 'society'")
head4 = getData("select type, head from test where not type = 'society'")
print("Load Model title_5...")
model = torch.load('./model/title_5.pt')
print("Testing title_5...")
test(model, title5)
del model
print("Load Model head_5...")
model = torch.load('./model/head_5.pt')
print("Testing head_5...")
test(model, head5)
del model
print("Load Model title_4...")
model = torch.load('./model/title_4.pt')
print("Testing title4...")
test(model, title4)
del model
print("Load Model head_4...")
model = torch.load('./model/head_4.pt')
print("Testing head_4...")
test(model, head4)
del model