-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
520 lines (447 loc) · 19.9 KB
/
metric.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import os
import re
import json
from collections import defaultdict, Counter
def parse_answer(text):
text = text.split("\n")[0].strip().lower()
if text != "" and text[-1] == ".":
return text[:-1]
return text
def parse_local_answer(text, alias=None):
text = parse_answer(text)
if alias is not None:
if text in alias:
text = alias[0]
if "unknown" in text:
return "unknown"
else:
return text
def parse_tendency_answer(text):
# text = text.strip()
text = text.split("\n")[0].strip()
if "(" in text:
pattern = re.compile("\(([ABC])\)")
spans = pattern.findall(text)
if len(spans) != 1:
return text
else:
return spans[0]
elif "." in text:
return text.split(".")[0]
else:
return text
def get_question(item):
if "\n\n" not in item["instance"]["input"]["text"]:
if len(item["instance"]["input"]["text"].split("\n")) == 3:
event, question, _ = item["instance"]["input"]["text"].split("\n")
else: # ft
event = "NA"
question, _ = item["instance"]["input"]["text"].split("\n")
else:
input = item["instance"]["input"]["text"].split("\n\n")[-1]
if len(input.split("\n")) == 3:
event, question, _ = input.split("\n")
else: # serac
event = "NA"
question, _ = input.split("\n")
return question.replace("Question: ", "")
class MetricForFact():
def __init__(self, predictions, labels, data, locality_data=None) -> None:
self.predictions = predictions
self.labels = labels
self.data = data
self.locality_data = locality_data
self.metrics = {}
self.properties = self.get_properties()
@staticmethod
def load_file(answer_file):
predictions, labels = [], []
data = json.load(open(answer_file))
for item in data["request_states"]:
predictions.append(item["request"]["result"]["completions"][0]["text"])
labels.append(item["instance"]["answer"]["alias"] + [item["instance"]["answer"]["name"]])
if item["instance"]["answer"]["id"] == "Q30":
labels[-1].append("United States")
return data, predictions, labels
def get_properties(self):
properties = defaultdict(list)
for item in self.data["request_states"]:
properties["question_types"].append(item["instance"]["question_type"])
properties["event_types"].append(item["instance"]["event_type"])
properties["local"].append(item["instance"]["local"])
properties["event"].append(item["instance"]["event"])
return properties
def compute_acc(self):
counter = Counter()
# overall
for i, (pred, label) in enumerate(zip(self.predictions, self.labels)):
if self.properties["local"][i]: continue
question_type = self.properties["question_types"][i]
correct = 0
# unknown
if "unknown" in label:
counter["unknown_total"] += 1
if "unknown" in parse_answer(pred):
counter["unknown_correct"] += 1
correct = 1
else:
counter["known_total"] += 1
if parse_answer(pred) in [parse_answer(_label) for _label in label]:
counter["known_correct"] += 1
correct = 1
# question type
if question_type == 0:
counter["0_total"] += 1
counter["0_correct"] += correct
elif question_type == 1:
if "unknown" not in label:
counter["1_total"] += 1
counter["1_correct"] += correct
else:
raise ValueError
# overall
counter["overall_total"] += 1
counter["overall_correct"] += correct
for type in ["unknown", "known", "0", "1", "overall"]:
counter[f"{type}_acc"] = counter[f"{type}_correct"]/ counter[f"{type}_total"]
self.metrics["acc"] = counter
print(counter)
def compute_event_acc(self):
counter = Counter()
flag_event = "NA"
correct = 1
# overall
for i, (pred, label) in enumerate(zip(self.predictions, self.labels)):
if self.properties["local"][i]: continue
if self.properties["event"][i] != flag_event:
if flag_event != "NA":
counter["overall_total"] += 1
counter["overall_correct"] += correct
else:
pass
flag_event = self.properties["event"][i]
correct = 1
if "unknown" in label:
if "unknown" in parse_answer(pred):
correct = 1 * correct
else:
correct = 0
else:
if parse_answer(pred) in [parse_answer(_label) for _label in label]:
correct = 1 * correct
else:
correct = 0
counter["overall_total"] += 1
counter["overall_correct"] += correct
counter["overall_acc"] = counter["overall_correct"] / counter["overall_total"]
self.metrics["event_acc"] = counter
print(counter)
def get_input_pred(self, data):
input2pred = {}
all_alias = {}
for item in data["request_states"]:
if not item["instance"]["local"]: continue
if "\n\n" in item["instance"]["input"]["text"]: # SERAC
item["instance"]["input"]["text"] = item["instance"]["input"]["text"].split("\n\n")[-1]
if "Event:" in item["instance"]["input"]["text"]: # SERAC
question = "\n".join(item["instance"]["input"]["text"].split("\n")[1:])
else:
question = item["instance"]["input"]["text"]
assert question not in input2pred
input2pred[question] = item["request"]["result"]["completions"][0]["text"]
all_alias[question] = item["instance"]["answer"]["alias"]
return input2pred, all_alias
def compute_locality(self):
input2pred, all_alias = self.get_input_pred(self.data)
counter = Counter()
assert len(input2pred) == len(self.locality_data["request_states"])
for item in self.locality_data["request_states"]:
assert item["instance"]["input"]["text"] in input2pred
assert item["instance"]["local"]
alias = all_alias[item["instance"]["input"]["text"]]
answer_after_editing = input2pred[item["instance"]["input"]["text"]]
answer_before_editing = item["request"]["result"]["completions"][0]["text"]
counter["overall_total"] += 1
if parse_local_answer(answer_after_editing, alias) == parse_local_answer(answer_before_editing, alias):
counter["overall_same"] += 1
counter["overall_consistency"] = counter["overall_same"] / counter["overall_total"]
print(counter)
self.metrics["locality"] = counter
def dump_metrics(self, save_path):
with open(save_path, "w") as f:
json.dump(self.metrics, f, indent=4)
class MetricForTendency():
def __init__(self, predictions, labels, data, locality_data=None) -> None:
self.predictions = predictions
self.labels = labels
self.data = data
self.locality_data = locality_data
self.metrics = {}
self.properties = self.get_properties()
@staticmethod
def load_file(answer_file):
predictions, labels = [], []
data = json.load(open(answer_file))
for item in data["request_states"]:
predictions.append(item["request"]["result"]["completions"][0]["text"])
labels.append(item["instance"]["answer"])
return data, predictions, labels
def get_properties(self):
properties = defaultdict(list)
for item in self.data["request_states"]:
properties["event_types"].append(item["instance"]["event_type"])
properties["local"].append(item["instance"]["local"])
properties["question_type"].append(item["instance"].get("question_type", "NA"))
properties["event"].append(item["instance"]["event"])
return properties
def compute_acc(self):
counter = Counter()
for i, (pred, label) in enumerate(zip(self.predictions, self.labels)):
if self.properties["local"][i]:
continue
counter["overall_total"] += 1
if parse_tendency_answer(pred) == parse_tendency_answer(label):
counter["overall_correct"] += 1
else:
print(pred, label)
counter["overall_acc"] = counter["overall_correct"] / (counter["overall_total"] + 1e-10)
self.metrics["acc"] = counter
print(self.metrics)
def compute_event_acc(self):
counter = Counter()
flag_event = "NA"
correct = 1
for i, (pred, label) in enumerate(zip(self.predictions, self.labels)):
if self.properties["local"][i]: continue
if self.properties["event"][i] != flag_event:
if flag_event != "NA":
counter["overall_total"] += 1
counter["overall_correct"] += correct
else:
pass
flag_event = self.properties["event"][i]
correct = 1
if parse_tendency_answer(pred) == parse_tendency_answer(label):
correct = 1 * correct
else:
correct = 0
counter["overall_total"] += 1
counter["overall_correct"] += correct
counter["overall_acc"] = counter["overall_correct"] / counter["overall_total"]
self.metrics["event_acc"] = counter
print(counter)
def get_input_pred(self, data):
input2pred = []
for item in data["request_states"]:
if not item["instance"]["local"]: continue
if "\n\n" in item["instance"]["input"]["text"]: # SERAC
item["instance"]["input"]["text"] = item["instance"]["input"]["text"].split("\n\n")[-1]
if "Event:" in item["instance"]["input"]["text"]: # SERAC
question = "\n".join(item["instance"]["input"]["text"].split("\n")[1:])
else:
question = item["instance"]["input"]["text"]
input2pred.append([question, item["request"]["result"]["completions"][0]["text"]])
return input2pred
def compute_locality(self):
input2pred = self.get_input_pred(self.data)
counter = Counter()
assert len(input2pred) == len(self.locality_data["request_states"])
for i, item in enumerate(self.locality_data["request_states"]):
assert item["instance"]["local"]
# import pdb; pdb.set_trace()
assert item["instance"]["input"]["text"].split("\n")[0] == input2pred[i][0].split("\n")[0]
answer_after_editing = input2pred[i][1]
answer_before_editing = item["request"]["result"]["completions"][0]["text"]
counter["overall_total"] += 1
if parse_local_answer(answer_after_editing) == parse_local_answer(answer_before_editing):
counter["overall_same"] += 1
counter["overall_consistency"] = counter["overall_same"] / counter["overall_total"]
print(counter)
self.metrics["locality"] = counter
def dump_metrics(self, save_path):
with open(save_path, "w") as f:
json.dump(self.metrics, f, indent=4)
class MetricForAutoEval():
def __init__(self, data) -> None:
self.data = data
self.metrics = {}
@staticmethod
def load_file(answer_file):
data = json.load(open(answer_file))
return data
def compute_score(self):
# not local; reliablity
counter = Counter() # not local
total = 0
for item in self.data["request_states"]:
if item["instance"]["local"]:
continue
total += 1
if item["scores"]["overall"] == 5:
counter["full_mark"] += 1
if item["scores"]["accuracy"] == 5:
counter["acc_full_mark"] += 1
if item["scores"]["coherence"] == 5:
counter["cohere_full_mark"] += 1
if item["scores"]["comprehensive"] == 5:
counter["compre_full_mark"] += 1
for key in item["scores"]:
counter[key] += item["scores"][key]
for key in counter:
counter[key] /= total
counter["total"] = total
self.metrics["score"] = counter
print(self.metrics)
def compute_event_score(self):
# not local; reliablity
counter = Counter() # not local
flag_event = "NA"
flag = 1
counter_per_event = Counter()
for item in self.data["request_states"]:
if item["instance"]["local"]: continue
if item["instance"]["event"] != flag_event:
if flag_event != "NA":
for key in counter_per_event:
counter_per_event[key] /= counter_per_event["total"]
assert int(counter_per_event["total"]) == 1 or len(counter_per_event) == 0
for key in counter_per_event:
counter[key] += counter_per_event[key]
counter["full_mark"] += flag
else:
pass
flag_event = item["instance"]["event"]
counter_per_event = Counter()
flag = 1
if item["scores"]["overall"] != 5:
flag = 0
if item["scores"]["overall"] == -1: continue
for key in item["scores"]:
counter_per_event[key] += item["scores"][key]
counter_per_event["total"] += 1
for key in counter_per_event:
counter[key] += counter_per_event[key]
print(counter)
total = counter["total"]
for key in counter:
counter[key] /= total
self.metrics["event_score"] = counter
def dump_metrics(self, save_path):
with open(save_path, "w") as f:
json.dump(self.metrics, f, indent=4)
if __name__ == "__main__":
# Factual Knowledge
models = ["gpt-4", "gpt-3.5", "gemini-pro"]
files = [
("fact.json", "metrics_icl.json"),
("fact_bm25.json", "metrics_bm25.json"),
("fact_e5.json", "metrics_e5.json"),
("fact_serac.json", "metrics_serac.json")
]
for model in models:
for file in files:
io_dir = f"data/processed/fact/{model}"
answer_file = os.path.join(io_dir, file[0])
data, predictions, labels = MetricForFact.load_file(answer_file)
local_file = os.path.join(io_dir, "fact_local.json")
locality_data, _, _ = MetricForFact.load_file(local_file)
metric = MetricForFact(predictions, labels, data, locality_data)
metric.compute_acc()
metric.compute_event_acc()
metric.compute_locality()
metric.dump_metrics(os.path.join(io_dir, file[1]))
models = ["gpt-j-6b", "tulu-v2-7b", "Mistral-7B-Instruct-v0.2"]
files = [
("fact_predictions.json", "metrics_icl.json"),
("fact_bm25_predictions.json", "metrics_bm25.json"),
("fact_e5_predictions.json", "metrics_e5.json"),
("fact_serac_predictions.json", "metrics_serac.json"),
("fact_ft_predictions.json", "metrics_ft.json")
]
for model in models:
for file in files:
io_dir = f"open-source/output/{model}"
answer_file = os.path.join(io_dir, file[0])
data, predictions, labels = MetricForFact.load_file(answer_file)
local_file = os.path.join(io_dir, "fact_local.json")
locality_data, _, _ = MetricForFact.load_file(local_file)
metric = MetricForFact(predictions, labels, data, locality_data)
metric.compute_acc()
metric.compute_event_acc()
metric.compute_locality()
metric.dump_metrics(os.path.join(io_dir, file[1]))
# Tendency: Multiple choice
models = ["gpt-4", "gpt-3.5", "gemini-pro"]
files = [
("tendency_mc_predictions.json", "metrics_mc_icl.json"),
("tendency_mc_bm25_predictions.json", "metrics_mc_bm25.json"),
("tendency_mc_e5_predictions.json", "metrics_mc_e5.json"),
("tendency_mc_serac_predictions.json", "metrics_mc_serac.json"),
]
for model in models:
for file in files:
io_dir = f"data/processed/tendency/{model}/examiner"
answer_file = os.path.join(io_dir, file[0])
data, predictions, labels = MetricForTendency.load_file(answer_file)
local_file = os.path.join(io_dir, "tendency_mc_local.json")
locality_data, _, _ = MetricForTendency.load_file(local_file)
metric = MetricForTendency(predictions, labels, data, locality_data)
metric.compute_acc()
metric.compute_event_acc()
metric.compute_locality()
metric.dump_metrics(os.path.join(io_dir, file[1]))
models = ["tulu-v2-7b", "Mistral-7B-Instruct-v0.2", "gpt-j-6b"]
files = [
("tendency_mc_predictions.json", "metrics_mc_icl.json"),
("tendency_mc_bm25_predictions.json", "metrics_mc_bm25.json"),
("tendency_mc_e5_predictions.json", "metrics_mc_e5.json"),
("tendency_mc_serac_predictions.json", "metrics_mc_serac.json"),
("tendency_mc_ft_predictions.json", "metrics_mc_ft.json")
]
for model in models:
for file in files:
io_dir = f"open-source/output/{model}"
answer_file = os.path.join(io_dir, file[0])
data, predictions, labels = MetricForTendency.load_file(answer_file)
local_file = os.path.join(io_dir, "tendency_mc_local_predictions.json")
locality_data, _, _ = MetricForTendency.load_file(local_file)
metric = MetricForTendency(predictions, labels, data, locality_data)
metric.compute_acc()
metric.compute_event_acc()
metric.compute_locality()
metric.dump_metrics(os.path.join(io_dir, file[1]))
# Tendency: Open-ended Generation
# GPT-4 scored scores for all models
models = ["gpt-4", "gpt-3.5", "gemini-pro"]
files = [
("tendency_gen_exam.json", "metrics_gen_icl.json"),
("tendency_gen_bm25_exam.json", "metrics_gen_bm25.json"),
("tendency_gen_e5_exam.json", "metrics_gen_e5.json"),
("tendency_gen_serac_exam.json", "metrics_gen_serac.json"),
]
for model in models:
for file in files:
io_dir = f"data/processed/tendency/{model}/examiner"
answer_file = os.path.join(io_dir, file[0])
data = MetricForAutoEval.load_file(answer_file)
metric = MetricForAutoEval(data)
metric.compute_score()
metric.compute_event_score()
metric.dump_metrics(os.path.join(io_dir, file[1]))
models = ["tulu-v2-7b", "Mistral-7B-Instruct-v0.2", "gpt-j-6b"]
files = [
("tendency_gen_exam.json", "metrics_gen_icl.json"),
("tendency_gen_bm25_exam.json", "metrics_gen_bm25.json"),
("tendency_gen_e5_exam.json", "metrics_gen_e5.json"),
("tendency_gen_serac_exam.json", "metrics_gen_serac.json"),
("tendency_gen_ft_exam.json", "metrics_gen_ft.json")
]
for model in models:
for file in files:
io_dir = f"open-source/output/{model}/examiner"
answer_file = os.path.join(io_dir, file[0])
data = MetricForAutoEval.load_file(answer_file)
metric = MetricForAutoEval(data)
metric.compute_score()
metric.compute_event_score()
metric.dump_metrics(os.path.join(io_dir, file[1]))