-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
104 lines (78 loc) · 3.2 KB
/
test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import torch
from torch.utils.data import DataLoader
from transformers import BertModel, BertTokenizer
from data import read_data
from model import Table
from utils import arg_parse, collate_fn, get_pred, f1_eval
def test():
args = arg_parse()
tokenizer = BertTokenizer.from_pretrained("bert-base-chinese")
model = BertModel.from_pretrained("bert-base-chinese")
test_features = read_data('./dataset/dev.json', tokenizer)
test_dataloader = DataLoader(test_features, batch_size=args.test_batch_size, shuffle=False, collate_fn=collate_fn,
drop_last=False)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Table(model, args)
model.to(device)
edi_checkpoint = torch.load(args.edi_checkpoint)
model.load_state_dict(edi_checkpoint["model_state_dict"])
results_all, labels_all = [], []
for data in test_dataloader:
model.eval()
input_ids, input_mask, table = data
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
with torch.no_grad():
_, results = model(input_ids, input_mask, table)
for i in range(len(table)):
results_all.append(get_pred(results[i]))
labels_all.append(get_pred(table[i]))
f = f1_eval(results_all, labels_all)
print(f)
edc_checkpoint = torch.load(args.edc_checkpoint)
model.load_state_dict(edc_checkpoint["model_state_dict"])
results_all, labels_all = [], []
for data in test_dataloader:
model.eval()
input_ids, input_mask, table = data
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
with torch.no_grad():
_, results = model(input_ids, input_mask, table)
for i in range(len(table)):
results_all.append(get_pred(results[i]))
labels_all.append(get_pred(table[i]))
f = f1_eval(results_all, labels_all)
print(f)
eaei_checkpoint = torch.load(args.eaei_checkpoint)
model.load_state_dict(eaei_checkpoint["model_state_dict"])
results_all, labels_all = [], []
for data in test_dataloader:
model.eval()
input_ids, input_mask, table = data
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
with torch.no_grad():
_, results = model(input_ids, input_mask, table)
for i in range(len(table)):
results_all.append(get_pred(results[i]))
labels_all.append(get_pred(table[i]))
f = f1_eval(results_all, labels_all)
print(f)
eaec_checkpoint = torch.load(args.eaec_checkpoint)
model.load_state_dict(eaec_checkpoint["model_state_dict"])
results_all, labels_all = [], []
for data in test_dataloader:
model.eval()
input_ids, input_mask, table = data
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
with torch.no_grad():
_, results = model(input_ids, input_mask, table)
for i in range(len(table)):
results_all.append(get_pred(results[i]))
labels_all.append(get_pred(table[i]))
f = f1_eval(results_all, labels_all)
print(f)
if __name__ == '__main__':
test()