-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_models.py
More file actions
54 lines (36 loc) · 1.67 KB
/
eval_models.py
File metadata and controls
54 lines (36 loc) · 1.67 KB
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
import torch
from pandas import DataFrame
from Configuration import Configuration
from Evaluation.metrics import eval_model
from Parsing.parser_utils import parse_args, ensembleParser, holdout
from Training.NERCRFClassifier import NERCRFClassifier
def outputs(results, error_dict):
if isinstance(results, DataFrame):
print(results)
else:
print(results.getvalue())
if error_dict is not None:
print(error_dict)
if __name__ == '__main__':
args, _ = parse_args()
conf = Configuration(args)
conf.show_parameters(["bert"])
if (args.datasets is None) or (args.models is None) or len(args.datasets) != 2 or len(args.models) != 2:
raise Exception("Define datasets and models in the same order!")
paths = args.datasets
models = args.models
(handler_a, handler_b), unified_dt = ensembleParser(paths[0], paths[1])
_, _, df_test = holdout(unified_dt)
modelA = NERCRFClassifier(conf.bert, handler_a.id2label)
modelA.load_state_dict(torch.load(models[0]))
modelB = NERCRFClassifier(conf.bert, handler_b.id2label)
modelB.load_state_dict(torch.load(models[1]))
if conf.cuda:
modelA = modelA.to(conf.gpu)
modelB = modelB.to(conf.gpu)
output_results, error_dict_A = eval_model(modelA, df_test[["sentences", "labels_a"]], conf, handler_a,
result=args.eval, return_dict=True)
outputs(output_results, error_dict_A)
output_results, error_dict_B = eval_model(modelB, df_test[["sentences", "labels_b"]], conf, handler_b,
result=args.eval, return_dict=True)
outputs(output_results, error_dict_B)