-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·358 lines (283 loc) · 10.9 KB
/
main.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
import argparse, time, torch, os, gc, random, pickle, json, copy, logging, warnings, sys
import numpy as np
from torch.utils.data import DataLoader, RandomSampler
from data import CorpusQA, CorpusSC, CorpusTC, CorpusPO, CorpusPA
from utils import evaluateQA, evaluateNLI, evaluateNER, evaluatePA, evaluatePOS
from model import BertMetaLearning
from itertools import cycle
import matplotlib.pyplot as plt
from datapath import loc
from transformers import (
WEIGHTS_NAME,
AdamW,
get_linear_schedule_with_warmup,
)
from transformers.data.metrics.squad_metrics import (
compute_predictions_log_probs,
compute_predictions_logits,
squad_evaluate,
)
logging.getLogger("transformers.tokenization_utils").setLevel(logging.ERROR)
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('--meta_lr', type=float, default=5e-5, help='meta learning rate')
parser.add_argument('--dropout', type=float, default=0.1, help='')
parser.add_argument('--hidden_dims', type=int, default=768, help='')
parser.add_argument('--sc_labels', type=int, default=3, help='')
parser.add_argument('--qa_labels', type=int, default=2, help='')
parser.add_argument('--tc_labels', type=int, default=10, help='')
parser.add_argument('--po_labels', type=int, default=18, help='')
parser.add_argument('--pa_labels', type=int, default=2, help='')
parser.add_argument('--qa_batch_size', type=int, default=8, help='batch size')
parser.add_argument('--sc_batch_size', type=int, default=32, help='batch size')
parser.add_argument('--tc_batch_size', type=int, default=32, help='batch size')
parser.add_argument('--po_batch_size', type=int, default=32, help='batch_size')
parser.add_argument('--pa_batch_size', type=int, default=8, help='batch size')
parser.add_argument('--task_per_queue', type=int, default=8, help='')
parser.add_argument('--update_step', type=int, default=3, help='')
parser.add_argument('--beta', type=float, default=1.0, help='')
parser.add_argument('--meta_epochs', type=int, default=3, help='iterations')
parser.add_argument('--seed', type=int, default=42, help='seed for numpy and pytorch')
parser.add_argument('--log_interval', type=int, default=200, help='Print after every log_interval batches')
parser.add_argument('--cuda', action='store_true',help='use CUDA')
parser.add_argument('--save', type=str, default='saved/', help='')
parser.add_argument('--load', type=str, default='', help='')
parser.add_argument('--grad_clip', type=float, default=5.0)
parser.add_argument('--meta_tasks', type=str, default='sc,pa,qa,tc,po')
parser.add_argument('--sampler',type=str,default='uniform_batch',choices=['uniform_batch'])
parser.add_argument('--temp', type=float, default=1.0)
parser.add_argument("--n_best_size", default=20, type=int)
parser.add_argument("--max_answer_length", default=30, type=int)
parser.add_argument("--weight_decay", default=0.0, type=float, help="Weight decay if we apply some.")
parser.add_argument("--warmup", default=0, type=int)
parser.add_argument("--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer.")
args = parser.parse_args()
torch.manual_seed(args.seed)
np.random.seed(args.seed)
if not os.path.exists(args.save):
os.makedirs(args.save)
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open(os.path.join(args.save,"output.txt"), "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.log.flush()
sys.stdout = Logger()
print (args)
if torch.cuda.is_available():
if not args.cuda:
args.cuda = True
torch.cuda.manual_seed_all(args.seed)
DEVICE = torch.device("cuda" if args.cuda else "cpu")
task_types = args.meta_tasks.split(',')
list_of_tasks = []
for tt in loc['train'].keys():
if tt[:2] in task_types:
list_of_tasks.append(tt)
for tt in task_types:
if '_' in tt:
list_of_tasks.append(tt)
list_of_tasks = list(set(list_of_tasks))
num_tasks = len(list_of_tasks)
print (list_of_tasks)
train_corpus = {}
dev_corpus = {}
for k in list_of_tasks:
if 'qa' in k:
train_corpus[k] = CorpusQA(loc['train'][k][0], loc['train'][k][1])
dev_corpus[k] = CorpusQA(loc['dev'][k][0], loc['dev'][k][1])
elif 'sc' in k:
train_corpus[k] = CorpusSC(loc['train'][k][0], loc['train'][k][1])
dev_corpus[k] = CorpusSC(loc['dev'][k][0], loc['dev'][k][1])
elif 'tc' in k:
train_corpus[k] = CorpusTC(loc['train'][k][0])
dev_corpus[k] = CorpusTC(loc['dev'][k][0])
elif 'po' in k:
train_corpus[k] = CorpusPO(loc['train'][k][0])
dev_corpus[k] = CorpusPO(loc['dev'][k][0])
elif 'pa' in k:
train_corpus[k] = CorpusPA(loc['train'][k][0])
dev_corpus[k] = CorpusPA(loc['dev'][k][0])
train_dataloaders = {}
dev_dataloaders = {}
for k in list_of_tasks:
batch_size = args.qa_batch_size if 'qa' in k else \
args.sc_batch_size if 'sc' in k else \
args.tc_batch_size if 'tc' in k else \
args.po_batch_size if 'po' in k else \
args.pa_batch_size
train_dataloaders[k] = DataLoader(train_corpus[k], batch_size = batch_size, shuffle = True, pin_memory = True)
dev_dataloaders[k] = DataLoader(dev_corpus[k], batch_size = batch_size, pin_memory = True)
model = BertMetaLearning(args).to(DEVICE)
if args.load != '':
model = torch.load(args.load)
params = list(model.parameters())
steps = args.meta_epochs * sum([len(train_dataloaders[x]) for x in list_of_tasks]) // (args.task_per_queue * args.update_step)
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": args.weight_decay,
"lr": args.meta_lr
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
"lr": args.meta_lr,
}
]
optim = AdamW(optimizer_grouped_parameters, lr = args.meta_lr, eps=args.adam_epsilon)
scheduler = get_linear_schedule_with_warmup(
optim, num_warmup_steps=args.warmup, num_training_steps=steps
)
logger = {}
logger['total_val_loss'] = []
logger['val_loss'] = {k:[] for k in list_of_tasks}
logger['train_loss'] = []
logger['args'] = args
def get_batch(dataloader_iter, dataloader):
try:
batch = next(dataloader_iter)
except StopIteration:
dataloader_iter = iter(dataloader)
batch = next(dataloader_iter)
return batch
class Sampler:
def __init__(self, p, steps, func):
# Sampling Weights
self.init_p = p
self.total_steps = steps
self.func = func
self.curr_step = 0
self.update_step = args.update_step
self.task_per_queue = args.task_per_queue
self.list_of_tasks = list_of_tasks
self.list_of_iters = {k:iter(train_dataloaders[k]) for k in self.list_of_tasks}
def __iter__(self):
return self
def __next__(self):
curr_p = self.func(self.init_p, self.list_of_tasks, self.curr_step, self.total_steps)
self.curr_step += 1
tasks = np.random.choice(self.list_of_tasks,self.task_per_queue,p=curr_p)
queue = []
for i in range(self.task_per_queue):
l = {'task':tasks[i],'data':[]}
for _ in range(self.update_step):
l['data'].append(get_batch(self.list_of_iters[tasks[i]], train_dataloaders[tasks[i]]))
queue.append(l)
return queue
def identity(x,y,z,w): return x
def UniformBatchSampler():
p = np.array([len(train_dataloaders[y])*1.0/sum([len(train_dataloaders[x]) for x in list_of_tasks]) for y in list_of_tasks])
p_temp = np.power(p, 1.0/args.temp)
p_temp = p_temp / np.sum(p_temp)
sampler = iter(Sampler(p_temp, steps, identity))
return sampler
sampler = UniformBatchSampler()
print (sampler.init_p)
def metastep(model, queue):
t1 = time.time()
n = len(queue)
old_vars = []
running_vars = []
for param in model.parameters():
old_vars.append(param.data.clone())
losses = [[0 for _ in range(args.update_step)] for _ in range(n)]
for i in range(n):
for k in range(args.update_step):
t = time.time()
optim.zero_grad()
output = model.forward(queue[i]['task'],queue[i]['data'][k])
loss = output[0].mean()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
optim.step()
losses[i][k] += loss.item()
if running_vars == []:
for _,param in enumerate(model.parameters()):
running_vars.append(param.data.clone())
else:
for idx,param in enumerate(model.parameters()):
running_vars[idx].data += param.data.clone()
for idx,param in enumerate(model.parameters()):
param.data = old_vars[idx].data.clone()
for param in running_vars:
param /= n
for idx,param in enumerate(model.parameters()):
param.data = old_vars[idx].data + args.beta * (running_vars[idx].data - old_vars[idx].data)
return [(queue[i]['task'],sum(l)/len(l)) for i,l in enumerate(losses)], time.time() - t1
def evaluate(model, task, data):
with torch.no_grad():
total_loss = 0.0
correct = 0.0
total = 0.0
for j,batch in enumerate(data):
output = model.forward(task,batch)
loss = output[0].mean()
total_loss += loss.item()
total_loss /= len(data)
return total_loss
def evaluateMeta(model):
loss_dict = {}
total_loss = 0
model.eval()
for task in list_of_tasks:
loss = evaluate(model, task, dev_dataloaders[task])
loss_dict[task] = loss
total_loss += loss
return loss_dict, total_loss
def main():
# Meta learning stage
print ("*" * 50)
print ("Meta Learning Stage")
print ("*" * 50)
print ('Training for %d metasteps'%steps)
total_loss = 0
min_task_losses = {
"qa" : float('inf'),
"sc" : float('inf'),
"po" : float('inf'),
"tc" : float('inf'),
"pa" : float('inf'),
}
global_time = time.time()
try:
for j,metabatch in enumerate(sampler):
if j > steps: break
loss, _ = metastep(model, metabatch)
total_loss += sum([y for (x,y) in loss])/len(loss)
logger['train_loss'].append(sum([y for (x,y) in loss])/len(loss))
if (j + 1) % args.log_interval == 0:
val_loss_dict, val_loss_total = evaluateMeta(model)
logger['total_val_loss'].append(val_loss_total)
for task in val_loss_dict.keys():
logger['val_loss'][task].append(val_loss_dict[task])
total_loss /= args.log_interval
print('Val Loss Dict : ',val_loss_dict)
loss_per_task = {}
for task in val_loss_dict.keys():
if task[:2] in loss_per_task.keys():
loss_per_task[task[:2]] = loss_per_task[task[:2]] + val_loss_dict[task]
else:
loss_per_task[task[:2]] = val_loss_dict[task]
print('Time : %f , Step : %d , Train Loss : %f, Val Loss : %f' % (time.time() - global_time,j+1,total_loss,val_loss_total))
print('===============================================')
global_time = time.time()
for task in loss_per_task.keys():
if loss_per_task[task] < min_task_losses[task]:
torch.save(model,os.path.join(args.save,"model_"+task+".pt"))
min_task_losses[task] = loss_per_task[task]
print("Saving "+task+" Model")
total_loss = 0
with open(os.path.join(args.save,'logger.pickle'),'wb') as f:
pickle.dump(logger,f)
scheduler.step()
except KeyboardInterrupt:
print ('skipping meta learning')
with open(os.path.join(args.save,'logger.pickle'),'wb') as f:
pickle.dump(logger,f)
if __name__ == '__main__':
main()