-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_utils.py
551 lines (496 loc) · 22.5 KB
/
eval_utils.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env python3
# Copyright (c) # Copyright (c) ACL 2024, Natural Language Reasoning and Structured Explanations Workshop
import json
import re
import argparse
import csv
from utils import (
extract_if_correct, get_rationale, read_jsonl, read_json, approx_eq, approx_in, approx_overlap, tokenize,
extract_nums, find_nums, find_formula, extract_answer, test_answer)
import numpy as np
import pandas as pd
from constants import (
DEFAULT_INPUT_ANN_PATH,
DEFAULT_INPUT_GEN_PATH,
DEFAULT_INPUT_RES_PATH,
DEFAULT_PROMPT_PATH,
DEFAULT_OUTPUT_PATH,
DATASETS,
STR_GEN_STOP,
DICT_STR_SPLIT_RATIONALE
)
def util_gsm8k(file_path, num_test):
# file contains a list of dict with keys: question, answer, example_result, ground_truth, query, ans_*
dataset_name = "gsm8k"
data = read_jsonl(DEFAULT_INPUT_ANN_PATH + dataset_name +
"/grade_school_math/data/test.jsonl")
for i in range(len(data)):
item = data[i]
ans = extract_if_correct(item['answer'])
rationale, _ = item['answer'].split('####')
rationale = rationale.strip().split("\n")
# rationale = item['answer'].strip().split(". ")
item['rationale'] = rationale
item['answer'] = ans
# processing the formula to get leaf nums and non_leaf_nums
leaf_nums = []
non_leaf_nums = []
for step in rationale:
if step.count("<<") == 0:
# no formula in this step - treat the last num as non_leaf_num
nums = find_nums(step)
if not nums:
continue
for num in nums[:-1]:
if num in leaf_nums or num in non_leaf_nums:
continue
# this num is a new leaf num
leaf_nums.append(num)
non_leaf_nums.append(nums[-1])
elif step.count("<<") == 1:
formula = find_formula(step)
left, right = formula.split("=")
left_nums = find_nums(left)
# when "X/Y" is only num on the left, split them
if len(left_nums) == 1 and "/" in left_nums[0]:
left_nums = left_nums[0].split("/")
for num in left_nums:
if num in leaf_nums or num in non_leaf_nums:
continue
# this num is a new leaf num
leaf_nums.append(num)
non_leaf_nums.append(right)
else:
# assert no step with >1 formulas
assert False
# ------
item['leaf_nums'] = leaf_nums
item['non_leaf_nums'] = non_leaf_nums
data[i] = item
# dictionary maps question to id
question2id = dict()
for i in range(len(data)):
item = data[i]
question2id[item['question']] = i
dict_output_path = DEFAULT_INPUT_GEN_PATH + "gsm8k_question2id.json"
with open(dict_output_path, 'w') as outfile:
json.dump(question2id, outfile)
outfile.write('\n')
print(f"Saved gsm8k question2id dict in {dict_output_path}")
list_questionid = []
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
non_leaf_coverage_l, non_leaf_precision_l, non_leaf_F1_l = [], [], []
accuracy_l = []
bins_non_leaf_coverage = dict()
bins_non_leaf_precision = dict()
bins_non_leaf_F1 = dict()
bins_ans_accuracy = dict()
count_no_nonleaf = 0
for index in range(len(result)):
predicted_item = result[index]
data_entry = data[question2id[predicted_item['question']]]
assert data_entry['question'] == predicted_item['question']
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
# result[index][key_[4:]] = predicted_item[key_]
predicted_rationale, raw_ans = get_rationale(
predicted_item[key_])
setting_name = key_[4:]
# raw_ans = extract_answer(predicted_item[key_], dataset_name)
break
else:
predicted_rationale = predicted_item['example_result'].split('\nA: ')[
0]
# raw_ans = predicted_item['example_result']
raw_ans = str(extract_answer(
predicted_item['example_result'], dataset_name))
setting_name = "example_result"
# get rid of in-token ",", artifact for long numbers
predicted_rationale_tokens = predicted_rationale.split()
for i in range(len(predicted_rationale_tokens)):
token = predicted_rationale_tokens[i]
if "," in token[1:-1]:
token = token[0] + token[1:-1].replace(",", "") + token[-1]
predicted_rationale_tokens[i] = token
predicted_rationale = " ".join(predicted_rationale_tokens)
predicted_steps = predicted_rationale.split(
DICT_STR_SPLIT_RATIONALE[STR_GEN_STOP])
predicted_nonleaf_nums = set()
for step in predicted_steps:
if '=' in step:
nums = find_nums(step)
if nums:
predicted_nonleaf_nums.add(nums[-1])
nums = find_nums(predicted_steps[-1])
if nums:
predicted_nonleaf_nums.add(nums[-1])
non_leaf_nums = set(data_entry['non_leaf_nums'])
# get recall & precision & f1
if not non_leaf_nums:
count_no_nonleaf += 1
non_leaf_coverage, non_leaf_precision = 1, 1
else:
non_leaf_overap = approx_overlap(
predicted_nonleaf_nums, non_leaf_nums)
non_leaf_coverage = non_leaf_overap/len(non_leaf_nums)
if not predicted_nonleaf_nums:
non_leaf_precision = 0
else:
non_leaf_precision = non_leaf_overap / \
len(predicted_nonleaf_nums)
# f1
if non_leaf_precision * non_leaf_coverage <= 0.01:
non_leaf_F1 = 0
else:
non_leaf_F1 = 2 * non_leaf_precision * non_leaf_coverage / \
(non_leaf_precision + non_leaf_coverage)
key = len(non_leaf_nums) # depth of problem
if key not in bins_non_leaf_coverage.keys():
bins_non_leaf_coverage[key] = []
bins_non_leaf_precision[key] = []
bins_non_leaf_F1[key] = []
bins_ans_accuracy[key] = []
bins_non_leaf_coverage[key].append(non_leaf_coverage)
bins_non_leaf_precision[key].append(non_leaf_precision)
bins_non_leaf_F1[key].append(non_leaf_F1)
# answer accuracy
nums = extract_nums(raw_ans)
if nums and approx_eq(eval(data_entry['answer']), nums[-1]):
ans_accuracy = 1
predicted_item["is_correct"] = True
predicted_ans = nums[-1]
else:
ans_accuracy = 0
predicted_item["is_correct"] = False
predicted_ans = ""
bins_ans_accuracy[key].append(ans_accuracy)
predicted_item['answer'] = predicted_ans
non_leaf_coverage_l.append(non_leaf_coverage)
non_leaf_precision_l.append(non_leaf_precision)
non_leaf_F1_l.append(non_leaf_F1)
accuracy_l.append(ans_accuracy)
predicted_item["rationale"] = predicted_rationale
predicted_item["rationale_tokens"] = predicted_rationale_tokens
predicted_item["nonleaf_nums"] = predicted_nonleaf_nums
predicted_item["steps"] = predicted_steps
question_id = question2id[predicted_item["question"]]
list_questionid.append(question_id)
data[question_id]["ground_truth"] = predicted_item["ground_truth"]
predicted_item.pop("ground_truth")
data[question_id]["prediction"] = predicted_item
data[question_id]["prediction"].pop("question")
accuracy = np.mean(accuracy_l) # round(np.mean(accuracy_l), 2)
recall = np.mean(non_leaf_coverage_l) # average non-leaf num recall
precision = np.mean(non_leaf_precision_l) # average non-leaf num precision
f1 = np.mean(non_leaf_F1_l) # average non-leaf num f1
dict_eval_others = dict()
dict_eval_others["accuracy"] = accuracy
dict_eval_others["recall"] = recall
dict_eval_others["precision"] = precision
dict_eval_others["f1"] = f1
accuracy_depth = []
coverage_depth = []
precision_depth = []
F1_depth = []
count_depth = dict()
for depth in range(1, 9):
if depth in bins_ans_accuracy.keys():
count_depth[depth] = len(bins_ans_accuracy[depth])
accuracy_depth.append(np.mean(bins_ans_accuracy[depth]))
coverage_depth.append(np.mean(bins_non_leaf_coverage[depth]))
precision_depth.append(np.mean(bins_non_leaf_precision[depth]))
F1_depth.append(np.mean(bins_non_leaf_F1[depth]))
dict_eval_others["accuracy_depth"] = accuracy_depth
dict_eval_others["coverage_depth"] = coverage_depth
dict_eval_others["precision_depth"] = precision_depth
dict_eval_others["F1_depth"] = F1_depth
dict_eval_others["count_depth"] = count_depth
for i in list_questionid:
data[i]["eval_others"] = dict_eval_others
return data, question2id, list_questionid
def util_svamp(file_path, num_test):
# file contains a list of dict with keys: question, answer, equation, type, id, query, ans_*
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
list_accuracy = []
for index in range(len(result)):
predicted_item = result[index]
result[index]["prediction"] = {}
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
result[index]["prediction"][key_[4:]] = predicted_item[key_]
predicted_rationale, raw_answer = get_rationale(
predicted_item[key_])
result[index]["prediction"]["rationale"] = predicted_rationale
# answer accuracy
predicted_ans = extract_answer(raw_answer, dataset="svamp")
if predicted_ans != None and approx_eq(eval(str(result[index]['answer'])), predicted_ans):
ans_accuracy = 1
result[index]["prediction"]["is_correct"] = True
else:
ans_accuracy = 0
result[index]["prediction"]["is_correct"] = False
result[index]["prediction"]['answer'] = predicted_ans
list_accuracy.append(ans_accuracy)
break
accuracy = np.mean(list_accuracy)
for index in range(len(result)):
result[index]["prediction"]["eval_others"] = {
"accuracy": accuracy} # round(accuracy, 2)
return result
def util_multiarith(file_path, num_test):
# file contains a list of dict with keys: question, answer, equation, equation_number, id, query, ans_*
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
list_accuracy = []
for index in range(len(result)):
predicted_item = result[index]
result[index]["prediction"] = {}
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
result[index]["prediction"][key_[4:]] = predicted_item[key_]
predicted_rationale, raw_answer = get_rationale(
predicted_item[key_])
# predicted_rationale = predicted_item[key_].split("Q:")[0].strip(" .")
result[index]["prediction"]["rationale"] = predicted_rationale
# answer accuracy
if "zero_shot" not in file_path:
predicted_ans = extract_answer(
raw_answer, dataset="multiarith")
else:
predicted_ans = extract_answer(
raw_answer, dataset="multiarith|zero_shot")
# nums = extract_nums(raw_answer)
if predicted_ans != None and approx_eq(eval(str(result[index]['answer'][0])), predicted_ans):
ans_accuracy = 1
result[index]["prediction"]["is_correct"] = True
else:
ans_accuracy = 0
result[index]["prediction"]["is_correct"] = False
result[index]["prediction"]['answer'] = predicted_ans
list_accuracy.append(ans_accuracy)
break
accuracy = np.mean(list_accuracy)
for index in range(len(result)):
result[index]["prediction"]["eval_others"] = {
"accuracy": accuracy} # round(accuracy, 2)
return result
def util_mathqa(file_path, num_test):
# file contains a list of dict with keys: question, answer, rationale, equation, equation_linear, type, query, ans_*
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
# dictionary maps question to id
dataset_name = "mathqa"
data = read_json(DEFAULT_INPUT_ANN_PATH + dataset_name + "/test.json")
question2id = dict()
for i in range(len(data)):
item = data[i]
question2id[item['Problem']] = i
dict_output_path = DEFAULT_INPUT_GEN_PATH + "mathqa_question2id.json"
with open(dict_output_path, 'w') as outfile:
json.dump(question2id, outfile)
outfile.write('\n')
print(f"Saved mathqa question2id dict in {dict_output_path}")
list_questionid = []
list_accuracy = []
for index in range(len(result)):
predicted_item = result[index]
data_entry = data[question2id[predicted_item['question']]]
assert data_entry['Problem'] == predicted_item['question']
question_id = question2id[predicted_item["question"]]
list_questionid.append(question_id)
result[index]["prediction"] = {}
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
result[index]["prediction"][key_[4:]] = predicted_item[key_]
predicted_rationale, predicted_ans = get_rationale(
predicted_item[key_])
result[index]["prediction"]["rationale"] = predicted_rationale
# result[index]["prediction"]['answer'] = extract_answer(predicted_ans, dataset_name)
result[index]["prediction"]['answer'] = extract_answer(
predicted_item[key_], dataset_name)
if result[index]["prediction"]['answer'].lower() == result[index]['answer'].lower():
result[index]["prediction"]["is_correct"] = True
ans_accuracy = 1
else:
result[index]["prediction"]["is_correct"] = False
ans_accuracy = 0
# if result[index]["options"][result[index]['answer']] in raw_ans:
# # directly give the choice content w/o choice
# result[index]["prediction"]["is_correct"] = True
# ans_accuracy = 1
# else:
# result[index]["prediction"]["is_correct"] = False
# ans_accuracy = 0
list_accuracy.append(ans_accuracy)
result[index]["id"] = question_id
break
accuracy = np.mean(list_accuracy)
for index in range(len(result)):
result[index]["prediction"]["eval_others"] = {
"accuracy": accuracy} # round(accuracy, 2)
return result
def util_csqa(file_path, num_test):
# file contains a list of dict with keys: question, answer, concept, id, query, ans_*
dataset_name = "csqa"
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
list_accuracy = []
for index in range(len(result)):
predicted_item = result[index]
result[index]["prediction"] = {}
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
result[index]["prediction"][key_[4:]] = predicted_item[key_]
predicted_rationale, predicted_ans = get_rationale(
predicted_item[key_])
# predicted_rationale = predicted_item[key_].lower().split("the answer ")[0].strip(" .")
result[index]["prediction"]["rationale"] = predicted_rationale
# result[index]["prediction"]['answer'] = extract_answer(predicted_ans, dataset_name)
result[index]["prediction"]['answer'] = extract_answer(
predicted_item[key_], dataset_name)
if result[index]["prediction"]['answer'].lower() == result[index]['answer'].lower():
result[index]["prediction"]["is_correct"] = True
ans_accuracy = 1
else:
result[index]["prediction"]["is_correct"] = False
ans_accuracy = 0
list_accuracy.append(ans_accuracy)
break
accuracy = np.mean(list_accuracy)
for index in range(len(result)):
result[index]["prediction"]["eval_others"] = {
"accuracy": accuracy} # round(accuracy, 2)
return result
def util_strategyqa(file_path, num_test):
if file_path.endswith(".jsonl"):
temp = read_jsonl(file_path)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
else:
with open(file_path, "r") as f:
temp = json.load(f)
# last element is the prompt
result, prompt = temp[:-1], temp[-1]
if num_test == -1:
num_test = len(result)
assert len(result) == num_test
for index in range(len(result)):
predicted_item = result[index]
for key_ in predicted_item.keys():
if key_.startswith('ans_'):
result[index]["prediction"][key_[4:]] = predicted_item[key_]
continue
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--prompt_name", default=None,
type=str, required=True, help="type for prompt")
parser.add_argument("--dataset", default=None, type=str,
required=True, help="dataset for experiments")
parser.add_argument("--engine", default="gpt-3.5-turbo",
type=str, required=True, help="engine")
parser.add_argument("--num_test", default=-1, type=int,
help="number of samples tested. -1 if on all test samples")
parser.add_argument("--seed", default=1357, type=int, help="random seed")
parser.add_argument("--temp", default=0.0, type=float,
help="temperature for generation")
parser.add_argument("--max_tokens", default=256, type=int,
help="max # of tokens for generation")
parser.add_argument("--test_ind", default=None, type=str,
help="dir to test indices. If not provided, randomly choose.")
parser.add_argument("--suffix", default="", type=str, help="")
parser.add_argument("--apikey_file", default="./api_key.txt",
type=str, help="file path for api key.")
parser.add_argument("--overwrite_cache", action="store_true",
help="Overwrite the cached generated rationale files in jsonl format")
parser.add_argument("--overwrite_prediction", action="store_true",
help="Overwrite the LLM-generated prediction result files in jsonl format")
parser.add_argument(
"--learning_type", default='few_shot', type=str, help='zero shot or few shot',
choices=['zero_shot', 'few_shot']
)
parser.add_argument(
"--reasoning_strategy", default='complex_cot', type=str, help='The reasoning strategy LLM applied to generate prediction',
choices=['complex_cot', 'plan_solve']
)
parser.add_argument("--self_consistency", '--sc', action="store_true",
help="Whether apply self consistency or not"
)
parser.add_argument("--self_verification", '--sv', action="store_true",
help="Whether apply self verification or not"
)
parser.add_argument("--dialog_icl", action="store_true",
help="Whether apply dialog in-context learning or not"
)
args = parser.parse_args()
print(args)
test_model = args.engine + "|" + args.prompt_name
# scale down. -1 if not.
NUM_TEST = args.num_test
file_name = DEFAULT_INPUT_GEN_PATH + "{}_{}|engine{}|samp{}|{}|{}|sc-{}|sv-{}|dial-{}|{}.jsonl".format(
args.dataset, args.prompt_name, args.engine, NUM_TEST, args.learning_type,
args.reasoning_strategy, args.self_consistency, args.self_verification, args.dialog_icl, args.suffix_ans)
if args.dataset == "gsm8k":
util_gsm8k(file_name, NUM_TEST)
elif args.dataset == "svamp":
util_svamp(file_name, NUM_TEST)
elif args.dataset == "multiarith":
util_multiarith(file_name, NUM_TEST)
elif args.dataset == "mathqa":
util_mathqa(file_name, NUM_TEST)
elif args.dataset == "csqa":
util_csqa(file_name, NUM_TEST)
elif args.dataset == "strategyqa":
util_strategyqa(file_name, NUM_TEST)