-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_shift.py
145 lines (118 loc) · 5.16 KB
/
eval_shift.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import argparse
import os
import torch
from tqdm import tqdm
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
from models import VariationalBert, VariationalElectra
from run_squad import get_dataloader, to_list
from squad_metrics import (SquadResult, compute_predictions_logits,
squad_evaluate)
from squad_utils import convert_examples_to_features, read_squad_examples
def run(args):
ckpt = torch.load(args.ckpt_file, map_location="cpu")
state_dict = ckpt["state_dict"]
model_args = ckpt["args"]
if args.baseline:
model = AutoModelForQuestionAnswering.from_pretrained(model_args.bert_model)
else:
if model_args.electra:
model = VariationalElectra(model_args)
else:
model = VariationalBert(model_args)
model.load_state_dict(state_dict)
device = torch.cuda.current_device()
args.device = device
model = model.to(device)
tokenizer = AutoTokenizer.from_pretrained(model_args.bert_model)
examples = read_squad_examples(args.dev_file,
is_training=False,
debug=args.debug)
features = convert_examples_to_features(examples, tokenizer,
args.max_seq_length,
args.doc_stride,
args.max_query_length,
is_training=False)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
eval_dataloader = get_dataloader(args, features, is_training=False)
# Eval!
all_results = []
for batch in tqdm(eval_dataloader, desc="Evaluating", position=3, leave=False):
model.eval()
batch = tuple(t.to(args.device) for t in batch)
with torch.no_grad():
inputs = {
"input_ids": batch[0],
"attention_mask": batch[1],
"token_type_ids": batch[2],
}
feature_indices = batch[3]
outputs = model(**inputs)
start_logits, end_logits = outputs[0], outputs[1]
outputs = (start_logits, end_logits)
for i, feature_index in enumerate(feature_indices):
eval_feature = features[feature_index.item()]
unique_id = int(eval_feature.unique_id)
output = [to_list(output[i]) for output in outputs]
start_logits, end_logits = output
result = SquadResult(unique_id, start_logits, end_logits)
all_results.append(result)
prefix = ""
# Compute predictions
output_prediction_file = os.path.join(
args.output_dir, "predictions_{}.json".format(prefix))
output_nbest_file = os.path.join(
args.output_dir, "nbest_predictions_{}.json".format(prefix))
if args.version_2_with_negative:
output_null_log_odds_file = os.path.join(
args.output_dir, "null_odds_{}.json".format(prefix))
else:
output_null_log_odds_file = None
predictions = compute_predictions_logits(
examples,
features,
all_results,
args.n_best_size,
args.max_answer_length,
args.do_lower_case,
output_prediction_file,
output_nbest_file,
output_null_log_odds_file,
args.verbose_logging,
args.version_2_with_negative,
args.null_score_diff_threshold,
tokenizer,
)
# Compute the F1 and exact scores.
results = squad_evaluate(examples, predictions)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
output_file = os.path.join(args.output_dir, "{}_metrics.txt".format(args.data))
f = open(output_file, "w")
for k,v in results.items():
print("{}: {:.4f}".format(k,v ))
f.write("{}: {:.4f}\n".format(k, v))
f.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--test_folder", type=str, default="shift-data")
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--doc_stride", type=int, default=128)
parser.add_argument("--max_seq_length", type=int, default=384)
parser.add_argument("--max_query_length", type=int, default=64)
parser.add_argument("--bert_model", type=str, default="bert-base-uncased")
parser.add_argument("--ckpt_file", type=str, required=True)
parser.add_argument("--seed", type=int, default=1004)
parser.add_argument("--version_2_with_negative", action="store_true")
parser.add_argument("--n_best_size", type=int, default=20)
parser.add_argument("--verbose_logging", action="store_true")
parser.add_argument("--max_answer_length", type=int, default=30)
parser.add_argument("--null_score_diff_threshold", type=float, default=0.0)
parser.add_argument("--output_dir", type=str, required=True)
parser.add_argument("--baseline", action="store_true")
args = parser.parse_args()
args.do_lower_case = True
for file_name in os.listdir(args.test_folder):
args.data = file_name.replace("_v1.0.json","")
args.dev_file = os.path.join(args.test_folder, file_name)
run(args)