-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_exp3b_sentence-comparison.py
128 lines (108 loc) · 4.36 KB
/
run_exp3b_sentence-comparison.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
# ~~~~~~~~~~~~~~~~~~~ EXPERIMENT 3B: SENTENCE COMPARISON
import numpy as np
import pandas as pd
import argparse
from tqdm import tqdm
from utils import io
if __name__ == "__main__":
TASK = "sentence_comparison"
# Parse command-line arguments.
args = io.parse_args()
# Set random seed.
np.random.seed(args.seed)
# Meta information.
meta_data = {
"model": args.model,
"seed": args.seed,
"task": TASK,
"eval_type": args.eval_type,
"option_order": args.option_order,
"data_file": args.data_file,
"timestamp": io.timestamp()
}
# Set up model and other model-related variables.
model = io.initialize_model(args)
kwargs = {}
# Read corpus data.
df = pd.read_csv(args.data_file)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN LOOP
# Initialize results and get model outputs on each item.
results = []
for _, row in tqdm(list(df.iterrows()), total=len(df.index)):
good_sentence = row.good_sentence
bad_sentence = row.bad_sentence
if args.eval_type == "direct":
# Get standard full-sentence probabilities.
logprob_of_good_sentence = model.get_full_sentence_logprob(
good_sentence
)
logprob_of_bad_sentence = model.get_full_sentence_logprob(
bad_sentence
)
# Store results in dictionary.
res = {
"item_id": row.item_id,
"good_sentence": good_sentence,
"bad_sentence": bad_sentence,
"logprob_of_good_sentence": logprob_of_good_sentence,
"logprob_of_bad_sentence": logprob_of_bad_sentence
}
else:
# Present a particular order of the answer options.
if args.option_order == "goodFirst":
options = [good_sentence, bad_sentence]
else:
options = [bad_sentence, good_sentence]
# Create "continuations". We're essentially asking the models
# a multiple choice question.
good_continuation = "1" if args.option_order == "goodFirst" else "2"
bad_continuation = "2" if args.option_order == "goodFirst" else "1"
# Create prompt and get outputs.
good_prompt, logprob_of_good_continuation, logprobs_good = \
model.get_logprob_of_continuation(
"", # no "prefix"
good_continuation,
task=TASK,
options=options,
return_dist=True,
**kwargs
)
bad_prompt, logprob_of_bad_continuation, logprobs_bad = \
model.get_logprob_of_continuation(
"", # no "prefix"
bad_continuation,
task=TASK,
options=options,
return_dist=True,
**kwargs
)
# Store results in dictionary.
res = {
"item_id": row.item_id,
"good_prompt": good_prompt,
"good_sentence": good_sentence,
"bad_sentence": bad_sentence,
"good_continuation": good_continuation,
"bad_continuation": bad_continuation,
"logprob_of_good_continuation": logprob_of_good_continuation,
"logprob_of_bad_continuation": logprob_of_bad_continuation
}
# Deal with logprobs: different cases for OpenAI and Huggingface.
if args.model_type == "openai":
res["top_logprobs"] = logprobs
elif args.dist_folder is not None:
# Save full distribution over vocab items
# (only corresponding to the first subword token).
model.save_dist_as_numpy(
logprobs,
f"{args.dist_folder}/{row.item_id}.npy"
)
# Record results for this item.
results.append(res)
# Combine meta information with model results into one dict.
output = {
"meta": meta_data,
"results": results
}
# Save outputs to specified JSON file.
io.dict2json(output, args.out_file)