-
Notifications
You must be signed in to change notification settings - Fork 21
/
fewshot_softpilot.py
394 lines (307 loc) · 15.9 KB
/
fewshot_softpilot.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
from tqdm import tqdm
from openprompt.data_utils.text_classification_dataset import AgnewsProcessor, DBpediaProcessor, ImdbProcessor, AmazonProcessor
from openprompt.data_utils.huggingface_dataset import YahooAnswersTopicsProcessor
import torch
from openprompt.data_utils.utils import InputExample
import argparse
import numpy as np
from openprompt import PromptDataLoader
from openprompt.prompts import ManualVerbalizer, KnowledgeableVerbalizer, SoftVerbalizer, AutomaticVerbalizer
from openprompt.prompts import ManualTemplate
parser = argparse.ArgumentParser("")
parser.add_argument("--shot", type=int, default=5)
parser.add_argument("--seed", type=int, default=144)
parser.add_argument("--plm_eval_mode", action="store_true")
parser.add_argument("--model", type=str, default='roberta') # tested model are gpt2/t5
parser.add_argument("--model_name_or_path", default='../../plm_cache/roberta-large')
parser.add_argument("--openprompt_path", type=str, default="OpenPrompt")
parser.add_argument("--verbalizer", type=str)
parser.add_argument("--calibration", action="store_true")
parser.add_argument("--not_manual", action="store_true")
parser.add_argument("--filter", default="none", type=str)
parser.add_argument("--template_id", type=int)
parser.add_argument("--dataset",type=str)
parser.add_argument("--result_file", type=str, default="../sfs_scripts/results_fewshot_manual_kpt.txt")
parser.add_argument("--gradient_accumulation_steps", type=int, default=1)
parser.add_argument("--max_epochs", type=int, default=5)
parser.add_argument("--kptw_lr", default=0.06, type=float)
parser.add_argument("--pred_temp", default=1.0, type=float)
parser.add_argument("--max_token_split", default=-1, type=int)
args = parser.parse_args()
import random
this_run_unicode = str(random.randint(0, 1e10))
from openprompt.utils.reproduciblity import set_seed
set_seed(args.seed)
from openprompt.plms import load_plm
plm, tokenizer, model_config, WrapperClass = load_plm(args.model, args.model_name_or_path)
dataset = {}
if args.dataset == "agnews":
dataset['train'] = AgnewsProcessor().get_train_examples(f"{args.openprompt_path}/datasets/TextClassification/agnews/")
dataset['test'] = AgnewsProcessor().get_test_examples(f"{args.openprompt_path}/datasets/TextClassification/agnews/")
class_labels =AgnewsProcessor().get_labels()
scriptsbase = "TextClassification/agnews"
scriptformat = "txt"
cutoff=0.5
max_seq_l = 128
batch_s = 30
elif args.dataset == "dbpedia":
dataset['train'] = DBpediaProcessor().get_train_examples(f"{args.openprompt_path}/datasets/TextClassification/dbpedia/")
dataset['test'] = DBpediaProcessor().get_test_examples(f"{args.openprompt_path}/datasets/TextClassification/dbpedia/")
class_labels =DBpediaProcessor().get_labels()
scriptsbase = "TextClassification/dbpedia"
scriptformat = "txt"
cutoff=0.5
max_seq_l = 128
batch_s = 30
elif args.dataset == "yahoo":
dataset['train'] = YahooAnswersTopicsProcessor().get_train_examples()
dataset['test'] = YahooAnswersTopicsProcessor().get_test_examples()
class_labels =YahooAnswersTopicsProcessor().get_labels()
scriptsbase = "TextClassification/yahoo_answers_topics"
scriptformat = "json"
cutoff=0.5
max_seq_l = 128
batch_s = 30
elif args.dataset == "imdb":
dataset['train'] = ImdbProcessor().get_train_examples(f"{args.openprompt_path}/datasets/TextClassification/imdb/")
dataset['test'] = ImdbProcessor().get_test_examples(f"{args.openprompt_path}/datasets/TextClassification/imdb/")
class_labels = ImdbProcessor().get_labels()
scriptsbase = "TextClassification/imdb"
scriptformat = "txt"
cutoff=0
max_seq_l = 512
batch_s = 5
elif args.dataset == "amazon":
dataset['train'] = AmazonProcessor().get_train_examples(f"{args.openprompt_path}/datasets/TextClassification/amazon/")
dataset['test'] = AmazonProcessor().get_test_examples(f"{args.openprompt_path}/datasets/TextClassification/amazon/")
class_labels = AmazonProcessor().get_labels()
scriptsbase = "TextClassification/amazon"
scriptformat = "txt"
cutoff=0
max_seq_l = 512
batch_s = 5
else:
raise NotImplementedError
mytemplate = ManualTemplate(tokenizer=tokenizer).from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/manual_template.txt", choice=args.template_id)
if args.verbalizer == "kpt":
myverbalizer = KnowledgeableVerbalizer(tokenizer, classes=class_labels, candidate_frac=cutoff, pred_temp=args.pred_temp, max_token_split=args.max_token_split).from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/knowledgeable_verbalizer.{scriptformat}")
elif args.verbalizer == "manual":
myverbalizer = ManualVerbalizer(tokenizer, classes=class_labels).from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/manual_verbalizer.{scriptformat}")
elif args.verbalizer == "soft":
if args.not_manual:
myverbalizer = SoftVerbalizer(tokenizer, model=plm, classes=class_labels)#.from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/manual_verbalizer.{scriptformat}")
else:
myverbalizer = SoftVerbalizer(tokenizer, model=plm, classes=class_labels).from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/manual_verbalizer.{scriptformat}")
elif args.verbalizer == "auto":
myverbalizer = AutomaticVerbalizer(tokenizer, classes=class_labels)
# (contextual) calibration
if args.verbalizer in ["kpt","manual"]:
if args.calibration or args.filter != "none":
from openprompt.data_utils.data_sampler import FewShotSampler
support_sampler = FewShotSampler(num_examples_total=200, also_sample_dev=False)
dataset['support'] = support_sampler(dataset['train'], seed=args.seed)
# for example in dataset['support']:
# example.label = -1 # remove the labels of support set for clarification
support_dataloader = PromptDataLoader(dataset=dataset["support"], template=mytemplate, tokenizer=tokenizer,
tokenizer_wrapper_class=WrapperClass, max_seq_length=max_seq_l, decoder_max_length=3,
batch_size=batch_s,shuffle=False, teacher_forcing=False, predict_eos_token=False,
truncate_method="tail")
from openprompt import PromptForClassification
use_cuda = True
prompt_model = PromptForClassification(plm=plm,template=mytemplate, verbalizer=myverbalizer, freeze_plm=False, plm_eval_mode=args.plm_eval_mode)
if use_cuda:
prompt_model= prompt_model.cuda()
# HP
# if args.calibration:
if args.verbalizer in ["kpt","manual"]:
if args.calibration or args.filter != "none":
org_label_words_num = [len(prompt_model.verbalizer.label_words[i]) for i in range(len(class_labels))]
from contextualize_calibration import calibrate
# calculate the calibration logits
cc_logits = calibrate(prompt_model, support_dataloader)
print("the calibration logits is", cc_logits)
print("origial label words num {}".format(org_label_words_num))
if args.calibration:
myverbalizer.register_calibrate_logits(cc_logits.mean(dim=0))
new_label_words_num = [len(myverbalizer.label_words[i]) for i in range(len(class_labels))]
print("After filtering, number of label words per class: {}".format(new_label_words_num))
from filter_method import *
if args.filter == "tfidf_filter":
tfidf_filter(myverbalizer, cc_logits, class_labels)
elif args.filter == "none":
pass
else:
raise NotImplementedError
# register the logits to the verbalizer so that the verbalizer will divide the calibration probability in producing label logits
# currently, only ManualVerbalizer and KnowledgeableVerbalizer support calibration.
from openprompt.data_utils.data_sampler import FewShotSampler
sampler = FewShotSampler(num_examples_per_label=args.shot, also_sample_dev=True, num_examples_per_label_dev=args.shot)
dataset['train'], dataset['validation'] = sampler(dataset['train'], seed=args.seed)
train_dataloader = PromptDataLoader(dataset=dataset["train"], template=mytemplate, tokenizer=tokenizer,
tokenizer_wrapper_class=WrapperClass, max_seq_length=max_seq_l, decoder_max_length=3,
batch_size=batch_s,shuffle=True, teacher_forcing=False, predict_eos_token=False,
truncate_method="tail")
validation_dataloader = PromptDataLoader(dataset=dataset["validation"], template=mytemplate, tokenizer=tokenizer,
tokenizer_wrapper_class=WrapperClass, max_seq_length=max_seq_l, decoder_max_length=3,
batch_size=batch_s,shuffle=False, teacher_forcing=False, predict_eos_token=False,
truncate_method="tail")
# zero-shot test
test_dataloader = PromptDataLoader(dataset=dataset["test"], template=mytemplate, tokenizer=tokenizer,
tokenizer_wrapper_class=WrapperClass, max_seq_length=max_seq_l, decoder_max_length=3,
batch_size=batch_s,shuffle=False, teacher_forcing=False, predict_eos_token=False,
truncate_method="tail")
def evaluate(prompt_model, dataloader, desc):
prompt_model.eval()
allpreds = []
alllabels = []
pbar = tqdm(dataloader, desc=desc)
for step, inputs in enumerate(pbar):
if use_cuda:
inputs = inputs.cuda()
logits = prompt_model(inputs)
labels = inputs['label']
alllabels.extend(labels.cpu().tolist())
allpreds.extend(torch.argmax(logits, dim=-1).cpu().tolist())
acc = sum([int(i==j) for i,j in zip(allpreds, alllabels)])/len(allpreds)
return acc
############
#############
###############
from transformers import AdamW, get_linear_schedule_with_warmup
loss_func = torch.nn.CrossEntropyLoss()
def prompt_initialize(verbalizer, prompt_model, init_dataloader):
dataloader = init_dataloader
with torch.no_grad():
for batch in tqdm(dataloader, desc="Init_using_{}".format("train")):
batch = batch.cuda()
logits = prompt_model(batch)
verbalizer.optimize_to_initialize()
if args.verbalizer == "soft":
no_decay = ['bias', 'LayerNorm.weight']
# it's always good practice to set no decay to biase and LayerNorm parameters
optimizer_grouped_parameters1 = [
{'params': [p for n, p in prompt_model.plm.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in prompt_model.plm.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
# Using different optimizer for prompt parameters and model parameters
optimizer_grouped_parameters2 = [
{'params': prompt_model.verbalizer.group_parameters_1, "lr":3e-5},
{'params': prompt_model.verbalizer.group_parameters_2, "lr":3e-4},
]
optimizer1 = AdamW(optimizer_grouped_parameters1, lr=3e-5)
optimizer2 = AdamW(optimizer_grouped_parameters2)
tot_step = len(train_dataloader) // args.gradient_accumulation_steps * args.max_epochs
scheduler1 = get_linear_schedule_with_warmup(
optimizer1,
num_warmup_steps=0, num_training_steps=tot_step)
scheduler2 = get_linear_schedule_with_warmup(
optimizer2,
num_warmup_steps=0, num_training_steps=tot_step)
elif args.verbalizer == "auto":
prompt_initialize(myverbalizer, prompt_model, train_dataloader)
no_decay = ['bias', 'LayerNorm.weight']
# it's always good practice to set no decay to biase and LayerNorm parameters
optimizer_grouped_parameters1 = [
{'params': [p for n, p in prompt_model.plm.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in prompt_model.plm.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
# Using different optimizer for prompt parameters and model parameters
optimizer1 = AdamW(optimizer_grouped_parameters1, lr=3e-5)
tot_step = len(train_dataloader) // args.gradient_accumulation_steps * args.max_epochs
scheduler1 = get_linear_schedule_with_warmup(
optimizer1,
num_warmup_steps=0, num_training_steps=tot_step)
optimizer2 = None
scheduler2 = None
elif args.verbalizer == "kpt":
no_decay = ['bias', 'LayerNorm.weight']
# it's always good practice to set no decay to biase and LayerNorm parameters
optimizer_grouped_parameters1 = [
{'params': [p for n, p in prompt_model.plm.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in prompt_model.plm.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
# Using different optimizer for prompt parameters and model parameters
# optimizer_grouped_parameters2 = [
# {'params': , "lr":1e-1},
# ]
optimizer1 = AdamW(optimizer_grouped_parameters1, lr=3e-5)
optimizer2 = AdamW(prompt_model.verbalizer.parameters(), lr=args.kptw_lr)
# print(optimizer_grouped_parameters2)
tot_step = len(train_dataloader) // args.gradient_accumulation_steps * args.max_epochs
scheduler1 = get_linear_schedule_with_warmup(
optimizer1,
num_warmup_steps=0, num_training_steps=tot_step)
# scheduler2 = get_linear_schedule_with_warmup(
# optimizer2,
# num_warmup_steps=0, num_training_steps=tot_step)
scheduler2 = None
elif args.verbalizer == "manual":
no_decay = ['bias', 'LayerNorm.weight']
# it's always good practice to set no decay to biase and LayerNorm parameters
optimizer_grouped_parameters1 = [
{'params': [p for n, p in prompt_model.plm.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in prompt_model.plm.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
# Using different optimizer for prompt parameters and model parameters
optimizer1 = AdamW(optimizer_grouped_parameters1, lr=3e-5)
tot_step = len(train_dataloader) // args.gradient_accumulation_steps * args.max_epochs
scheduler1 = get_linear_schedule_with_warmup(
optimizer1,
num_warmup_steps=0, num_training_steps=tot_step)
optimizer2 = None
scheduler2 = None
tot_loss = 0
log_loss = 0
best_val_acc = 0
for epoch in range(args.max_epochs):
tot_loss = 0
prompt_model.train()
for step, inputs in enumerate(train_dataloader):
if use_cuda:
inputs = inputs.cuda()
logits = prompt_model(inputs)
labels = inputs['label']
loss = loss_func(logits, labels)
loss.backward()
torch.nn.utils.clip_grad_norm_(prompt_model.parameters(), 1.0)
tot_loss += loss.item()
optimizer1.step()
scheduler1.step()
optimizer1.zero_grad()
if optimizer2 is not None:
optimizer2.step()
optimizer2.zero_grad()
if scheduler2 is not None:
scheduler2.step()
val_acc = evaluate(prompt_model, validation_dataloader, desc="Valid")
if val_acc>=best_val_acc:
torch.save(prompt_model.state_dict(),f"ckpts/{this_run_unicode}.ckpt")
best_val_acc = val_acc
print("Epoch {}, val_acc {}".format(epoch, val_acc), flush=True)
# print("verbalizer weights", myverbalizer.label_words_weights, flush=True)
prompt_model.load_state_dict(torch.load(f"ckpts/{this_run_unicode}.ckpt"))
prompt_model = prompt_model.cuda()
test_acc = evaluate(prompt_model, test_dataloader, desc="Test")
############
#############
###############
# roughly ~0.853 when using template 0
content_write = "="*20+"\n"
content_write += f"dataset {args.dataset}\t"
content_write += f"temp {args.template_id}\t"
content_write += f"seed {args.seed}\t"
content_write += f"shot {args.shot}\t"
content_write += f"verb {args.verbalizer}\t"
content_write += f"cali {args.calibration}\t"
content_write += f"filt {args.filter}\t"
content_write += f"maxsplit {args.max_token_split}\t"
content_write += f"kptw_lr {args.kptw_lr}\t"
content_write += f"not_manual {args.not_manual}\t"
content_write += "\n"
content_write += f"Acc: {test_acc}"
content_write += "\n\n"
print(content_write)
with open(f"{args.result_file}", "a") as fout:
fout.write(content_write)
import os
os.remove(f"ckpts/{this_run_unicode}.ckpt")