-
Notifications
You must be signed in to change notification settings - Fork 51
/
run_classification.py
284 lines (245 loc) · 12.8 KB
/
run_classification.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
import argparse
from data_utils import load_dataset
from utils import *
def main(models, datasets, all_shots, num_seeds, subsample_test_set, api_num_log_prob, approx, use_saved_results, bs):
"""
Run experiment or load past results, print accuracy
"""
default_params = {
'conditioned_on_correct_classes': True,
'subsample_test_set': subsample_test_set,
'api_num_log_prob': api_num_log_prob,
'approx': approx,
'bs': bs
}
# list of all experiment parameters to run
all_params = []
for model in models:
for dataset in datasets:
for num_shots in all_shots:
for seed in range(num_seeds):
p = deepcopy(default_params)
p['model'] = model
p['dataset'] = dataset
p['seed'] = seed
p['num_shots'] = num_shots
p['expr_name'] = f"{p['dataset']}_{p['model']}_{p['num_shots']}shot_{repr(p['subsample_test_set'])}_subsample_seed{p['seed']}"
all_params.append(p)
# query the model and save the responses
if use_saved_results:
load_results(all_params)
else:
save_results(all_params)
def save_results(params_list, freeze_test_set=True):
"""
Run the model and save its responses and the rest of configs into a pickle file
"""
result_tree = dict()
for param_index, params in enumerate(params_list):
print("\nExperiment name:", params['expr_name'])
### load data
all_train_sentences, all_train_labels, all_test_sentences, all_test_labels = load_dataset(params)
params_check(params)
### sample test set
if params['subsample_test_set'] is None:
test_sentences, test_labels = all_test_sentences, all_test_labels
print(f"selecting full test set ({len(all_test_labels)} examples)")
else:
if freeze_test_set:
np.random.seed(0) # always use seed 0 result if freeze
else:
np.random.seed(params['seed'])
test_sentences, test_labels = random_sampling(all_test_sentences, all_test_labels, params['subsample_test_set'])
print(f"selecting {len(test_labels)} subsample of test set")
### sample few-shot training examples
np.random.seed(params['seed'])
train_sentences, train_labels = random_sampling(all_train_sentences, all_train_labels, params['num_shots'])
### Evaluate the performance and save all results
# obtaining model's response on test examples
print(f"getting raw resp for {len(test_sentences)} test sentences")
raw_resp_test = get_model_response(params, train_sentences, train_labels, test_sentences)
# get prob for each label
all_label_probs = get_label_probs(params, raw_resp_test, train_sentences, train_labels, test_sentences)
# calculate P_cf
content_free_inputs = ["N/A", "", "[MASK]"]
p_cf = get_p_content_free(params, train_sentences, train_labels, content_free_inputs=content_free_inputs)
acc_original = eval_accuracy(all_label_probs, test_labels)
acc_calibrated = eval_accuracy(all_label_probs, test_labels, mode="diagonal_W", p_cf=p_cf)
accuracies = [acc_original, acc_calibrated]
print(f"Accuracies: {accuracies}")
print(f"p_cf : {p_cf}")
# add to result_tree
keys = [params['dataset'], params['model'], params['num_shots']]
node = result_tree # root
for k in keys:
if not (k in node.keys()):
node[k] = dict()
node = node[k]
node[params['seed']] = accuracies
# save to file
result_to_save = dict()
params_to_save = deepcopy(params)
result_to_save['params'] = params_to_save
result_to_save['train_sentences'] = train_sentences
result_to_save['train_labels'] = train_labels
result_to_save['test_sentences'] = test_sentences
result_to_save['test_labels'] = test_labels
result_to_save['raw_resp_test'] = raw_resp_test
result_to_save['all_label_probs'] = all_label_probs
result_to_save['p_cf'] = p_cf
result_to_save['accuracies'] = accuracies
if 'prompt_func' in result_to_save['params'].keys():
params_to_save['prompt_func'] = None
save_pickle(params, result_to_save)
print_results(result_tree)
def eval_accuracy(all_label_probs, test_labels, mode=None, p_cf=None):
# evaluate the accuracy with and without contextual calibration
num_classes = all_label_probs.shape[1]
if p_cf is None:
# do not calibrate
W = np.identity(num_classes)
b = np.zeros([num_classes, 1])
else:
# calibrate
if mode == "diagonal_W":
W = np.linalg.inv(np.identity(num_classes) * p_cf)
b = np.zeros([num_classes, 1])
elif mode == "identity_W":
W = np.identity(num_classes)
b = -1 * np.expand_dims(p_cf, axis=-1)
else:
assert False
correctness_list = []
assert len(all_label_probs) == len(test_labels)
for label_probs, true_label in zip(all_label_probs, test_labels):
label_probs = label_probs / np.sum(label_probs) # normalize to 1
calibrate_label_probs = np.matmul(W, np.expand_dims(label_probs, axis=-1)) + b
ans_label = np.argmax(calibrate_label_probs)
if ans_label == true_label:
correctness_list.append(1)
else:
correctness_list.append(0)
return np.mean(correctness_list)
def get_label_probs(params, raw_resp, train_sentences, train_labels, test_sentences):
"""Obtain model's label probability for each of the test examples. The returned prob is NOT normalized"""
num_classes = len(params['label_dict'])
approx = params['approx']
assert len(raw_resp) == len(test_sentences)
# Fill in the labels that is in the top k prob
all_label_probs = []
all_missing_positions = []
for i, ans in enumerate(raw_resp):
top_logprobs = ans['logprobs']['top_logprobs'][0] # [0] since we only ask for complete one more token
label_probs = [0] * len(params['label_dict'].keys())
for j, label_list in params['label_dict'].items():
all_found = True
for label in label_list: # each possible label correspond to the same class
label = " " + label # notice prompt does not have space after 'A:'
if label in top_logprobs:
label_probs[j] += np.exp(top_logprobs[label])
else:
all_found = False
if not all_found:
position = (i, j) # (which test example, which label)
all_missing_positions.append(position)
all_label_probs.append(label_probs)
all_label_probs = np.array(all_label_probs) # prob not normalized
# Fill in the label probs that are NOT in top k probs, by asking the model to rate perplexity
# This helps a lot in zero shot as most labels wil not be in Top 100 tokens returned by LM
if (not approx) and (len(all_missing_positions) > 0):
print(f"Missing probs: {len(all_missing_positions)}/{len(raw_resp) * num_classes}")
all_additional_prompts = []
num_prompts_each = []
for position in all_missing_positions:
which_sentence, which_label = position
test_sentence = test_sentences[which_sentence]
label_list = params['label_dict'][which_label]
for label in label_list:
prompt = construct_prompt(params, train_sentences, train_labels, test_sentence)
prompt += " " + label
all_additional_prompts.append(prompt)
num_prompts_each.append(len(label_list))
# chunk the prompts and feed into model
chunked_prompts = list(chunks(all_additional_prompts, chunk_size_helper(params)))
all_probs = []
for chunk_id, chunk in enumerate(chunked_prompts):
resp = complete(chunk, 0, params['model'], echo=True, num_log_probs=1)
for ans in resp['choices']:
prob = np.exp(ans['logprobs']['token_logprobs'][-1])
all_probs.append(prob)
assert sum(num_prompts_each) == len(all_probs)
assert len(num_prompts_each) == len(all_missing_positions)
# fill in corresponding entries in all_label_probs
for index, num in enumerate(num_prompts_each):
probs = []
while num > 0:
probs.append(all_probs.pop(0))
num -= 1
prob = np.sum(probs)
i, j = all_missing_positions[index]
all_label_probs[i][j] = prob
assert len(all_probs) == 0, "all should be popped"
assert (all_label_probs > 0).all(), "all should be populated with non-zero value"
return all_label_probs # NOT NORMALIZED
def get_p_content_free(params, train_sentences, train_labels, content_free_inputs=('N/A',)):
"""Query model with content free input, return its prediction probability for each label"""
label_dict = params['label_dict']
all_p_y = []
for content_free_input in content_free_inputs:
prompt = construct_prompt(params, train_sentences, train_labels, content_free_input)
p_y = [0] * len(label_dict)
for i, answers in label_dict.items():
prob = 0
for a in answers:
prob += np.exp(complete(prompt + " " + a, 0, params['model'], echo=True, num_log_probs=1)['choices'][0]['logprobs']['token_logprobs'][-1])
p_y[i] = prob
all_p_y.append(p_y)
p_y = np.mean(np.array(all_p_y), axis=0)
p_y = p_y / np.sum(p_y) # normalize
return p_y
def params_check(params):
"""sanity check the experiment params"""
assert params['num_tokens_to_predict'] == 1
# for classification, make sure that all of the class names are one word.
for key, label_names in params['label_dict'].items():
for label_id, label_name in enumerate(label_names):
first_token_of_label_name = complete(' ' + label_name, 1, params['model'], echo=True, num_log_probs=2)['choices'][0]['logprobs']['tokens'][0]
if first_token_of_label_name[1:] != label_name:
print('label name is more than 1 token', label_name)
assert False
if not (params['dataset'] in ['cb', 'rte']):
# formatting: there should be a space after question/answer prefix
assert params["q_prefix"][-1] == " "
assert params["a_prefix"][-1] == " "
assert len(params["prompt_prefix"]) == 0 or params["prompt_prefix"][-2:] == '\n\n'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# required arguments
parser.add_argument('--models', dest='models', action='store', required=True, help='name of model(s), e.g., GPT2-XL')
parser.add_argument('--datasets', dest='datasets', action='store', required=True, help='name of dataset(s), e.g., agnews')
parser.add_argument('--num_seeds', dest='num_seeds', action='store', required=True, help='num seeds for the training set', type=int)
parser.add_argument('--all_shots', dest='all_shots', action='store', required=True, help='num training examples to use')
# other arguments
parser.add_argument('--subsample_test_set', dest='subsample_test_set', action='store', required=False, type=int,
default=None, help='size of test set to use to speed up eval. None means using all test set')
parser.add_argument('--api_num_log_prob', dest='api_num_log_prob', action='store', required=False, type=int,
default=100, help='number of top tokens to ask for when querying the model. Capped at 100 for OpenAI GPT-3 API')
parser.add_argument('--bs', dest='bs', action='store', required=False, type=int, default=None,
help='batch size for model queries. For OpenAI API, capped at 20. For local running, set this to max out your GPU memory.')
# flags
parser.add_argument('--use_saved_results', dest='use_saved_results', action='store_const', const=True, default=False,
help='whether to load the results from pickle files and not run the model')
parser.add_argument('--approx', dest='approx', action='store_const', const=True, default=False,
help='whether to set token prob to zero if not in top 100')
args = parser.parse_args()
args = vars(args)
# simple processing
def convert_to_list(items, is_int=False):
if is_int:
return [int(s.strip()) for s in items.split(",")]
else:
return [s.strip() for s in items.split(",")]
args['models'] = convert_to_list(args['models'])
args['datasets'] = convert_to_list(args['datasets'])
args['all_shots'] = convert_to_list(args['all_shots'], is_int=True)
main(**args)