-
Notifications
You must be signed in to change notification settings - Fork 0
/
protomaml.py
425 lines (354 loc) · 15.1 KB
/
protomaml.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
import torch
from torch import nn
import numpy as np
from tqdm import tqdm
import os
from maml import *
class ProtoMAMLFewShotClassifier(MAMLFewShotClassifier):
def __init__(self, device, args):
super(ProtoMAMLFewShotClassifier, self).__init__(device, args)
# self.prototype_sim_lambda = torch.FloatTensor([args.prototype_sim_lambda]).to(device) # factor to multiply the similarity loss with
self.do_centralize = args.protomaml_do_centralize
def protomaml_fc_weights(self, prototypes):
fc_weight = 2 * prototypes
return nn.Parameter(fc_weight)
def protomaml_fc_bias(self, prototypes):
fc_bias = -torch.norm(prototypes, dim=1)
return nn.Parameter(fc_bias)
def compute_prototypes(self, embeddings, labels):
"""
Computes the prototype per class based on embeddings and labels
:param embeddings:
:param ohe_labels:
:return:
"""
num_labels = labels.size(1)
_, labels = labels.max(dim=1)
ohe_labels = torch.zeros(labels.size(0), labels.max() + 1).to(
embeddings.device
) # batch size x nr labels
ohe_labels.scatter_(1, labels.unsqueeze(1), 1) # create one hot encoding
embeddings = embeddings.unsqueeze(1)
ohe_labels = ohe_labels.unsqueeze(2)
class_sums = (ohe_labels * embeddings).sum(0)
samples_per_class = ohe_labels.sum(0)
prototypes = class_sums / samples_per_class
# standardize prototypes to be unit vectors
if self.do_centralize:
prototypes = torch.nn.functional.normalize(prototypes)
assert num_labels == prototypes.size(
0
), "There is a mismatch between the number of generated prototypes ({}) and inferred nr of classes ({}): \n {}".format(
prototypes.size(0), num_labels, labels
)
return prototypes
def get_prototype_similarity(self, prototypes):
num_prototypes = prototypes.size(0)
normalized = torch.nn.functional.normalize(prototypes) # creates unit vectors
similarity = normalized @ normalized.t()
upper = torch.triu(similarity, diagonal=1)
sim = torch.max(
upper.sum() / num_prototypes, torch.FloatTensor([0]).to(self.device)
)
return sim * self.prototype_sim_lambda
def forward(
self,
data_batch,
epoch,
use_second_order,
num_steps,
training_phase,
):
"""
Runs a forward outer loop pass on the batch of tasks using the MAML/++ framework.
:param data_batch: A data batch containing the support and target sets.
:param epoch: Current epoch's index
:param use_second_order: A boolean saying whether to use second order derivatives.
:param num_steps: Number of inner loop steps.
:param training_phase: Whether this is a training phase (True) or an evaluation phase (False)
:return: A dictionary with the collected losses of the current outer forward propagation.
"""
(
x_support_set,
len_support_set,
x_target_set,
len_target_set,
y_support_set,
y_target_set,
teacher_names,
) = data_batch
meta_batch_size = self.args.batch_size
self.classifier.zero_grad()
# Unfreeze slow model weights
if epoch >= self.num_freeze_epochs:
self.classifier.unfreeze()
losses = {"loss": 0}
task_accuracies = []
task_lang_logs = []
for (
task_id,
(
x_support_set_task,
len_support_set_task,
y_support_set_task,
x_target_set_task,
len_target_set_task,
y_target_set_task,
teacher_name,
),
) in enumerate(
zip(
x_support_set,
len_support_set,
y_support_set,
x_target_set,
len_target_set,
y_target_set,
teacher_names,
)
):
task_lang_log = [teacher_name, epoch]
task_losses = []
if epoch < self.num_freeze_epochs:
self.classifier.unfreeze()
# Get inner-loop parameters
fast_weights = self.classifier.get_inner_loop_params()
if epoch < self.num_freeze_epochs:
self.classifier.freeze()
x_support_set_task = x_support_set_task.squeeze()
len_support_set_task = len_support_set_task.squeeze()
y_support_set_task = y_support_set_task.squeeze()
x_target_set_task = x_target_set_task.squeeze()
len_target_set_task = len_target_set_task.squeeze()
y_target_set_task = y_target_set_task.squeeze()
total_task_loss = 0
with torch.no_grad(): # don't backprop through prototype creation
support_embeddings = self.classifier(
input_ids=x_support_set_task,
attention_mask=len_support_set_task,
num_step=0,
return_pooled=True,
params=fast_weights,
)[0]
# compute prototypes
prototypes = self.compute_prototypes(support_embeddings, y_support_set_task)
# compute weights for classification layer
fc_weight = self.protomaml_fc_weights(prototypes)
fc_bias = self.protomaml_fc_bias(prototypes)
# set weights
(
fast_weights["classifier.out_proj.weight"],
fast_weights["classifier.out_proj.bias"],
) = (
fc_weight.to(self.device),
fc_bias.to(self.device),
)
for num_step in range(num_steps):
torch.cuda.empty_cache()
if torch.cuda.device_count() > 1:
torch.cuda.synchronize()
support_loss, is_correct = self.net_forward(
x=x_support_set_task,
mask=len_support_set_task,
num_step=num_step,
teacher_unary=y_support_set_task,
fast_model=fast_weights,
training=True,
return_nr_correct=True,
task_name=teacher_name,
)
fast_weights = self.apply_inner_loop_update(
loss=support_loss,
names_weights_copy=fast_weights,
use_second_order=use_second_order,
current_step_idx=num_step,
)
if num_step == (self.args.number_of_training_steps_per_iter - 1):
# store support set statistics
task_lang_log.append(support_loss.detach().item())
task_lang_log.append(np.mean(is_correct))
if torch.cuda.device_count() > 1:
torch.cuda.synchronize()
target_loss, is_correct = self.net_forward(
x=x_target_set_task,
mask=len_target_set_task,
teacher_unary=y_target_set_task,
num_step=num_step,
fast_model=fast_weights,
training=True,
return_nr_correct=True,
task_name=teacher_name,
)
task_losses.append(target_loss)
accuracy = np.mean(is_correct)
task_accuracies.append(accuracy)
task_lang_log.append(target_loss.detach().item())
task_lang_log.append(accuracy)
# Achieve gradient accumulation by already backpropping current loss
torch.cuda.empty_cache()
task_losses = torch.sum(torch.stack(task_losses)) / meta_batch_size
task_losses.backward()
total_task_loss += task_losses.detach().cpu().item()
losses["loss"] += total_task_loss
task_lang_logs.append(task_lang_log)
losses["accuracy"] = np.mean(task_accuracies)
if training_phase:
return losses, task_lang_logs
else:
return losses
def finetune_epoch(
self,
names_weights_copy,
model_config,
train_dataloader,
dev_dataloader,
best_loss,
eval_every,
model_save_dir,
task_name,
epoch,
train_on_cpu=False,
writer=None,
):
# if self.classifier is not None:
# self.classifier.unfreeze()
if train_on_cpu:
self.device = torch.device("cpu")
self.inner_loop_optimizer.requires_grad_(False)
self.inner_loop_optimizer.eval()
self.inner_loop_optimizer.to(self.device)
if names_weights_copy is None:
if epoch <= self.num_freeze_epochs:
self.classifier.unfreeze()
# Get fast weights
fast_weights = self.classifier.get_inner_loop_params()
if epoch < self.num_freeze_epochs:
self.classifier.freeze()
else:
fast_weights = names_weights_copy
batch = next(iter(deepcopy(train_dataloader)))
batch = tuple(t.to(self.device) for t in batch)
x_support_set_task, mask, y_support_set_task = batch
# Get prototypes and init class head weights
with torch.no_grad():
support_embeddings = self.classifier(
input_ids=x_support_set_task,
attention_mask=mask,
num_step=0,
return_pooled=True,
params=fast_weights,
)[0]
# compute prototypes
prototypes = self.compute_prototypes(support_embeddings, y_support_set_task)
# compute weights for classification layer
fc_weight = self.protomaml_fc_weights(prototypes)
fc_bias = self.protomaml_fc_bias(prototypes)
# set weights
(
fast_weights["classifier.out_proj.weight"],
fast_weights["classifier.out_proj.bias"],
) = (
fc_weight.to(self.device),
fc_bias.to(self.device),
)
del prototypes
eval_every = (
eval_every if eval_every < len(train_dataloader) else len(train_dataloader)
)
if writer is not None: # create histogram of weights
for param_name, param in fast_weights.items():
writer.add_histogram(task_name + "/" + param_name, param, 0)
writer.flush()
with tqdm(
initial=0, total=eval_every * self.args.number_of_training_steps_per_iter
) as pbar_train:
for batch_idx, batch in enumerate(train_dataloader):
batch = tuple(t.to(self.device) for t in batch)
x, mask, y_true = batch
#########################################################
# Start of actual finetuning
#########################################################
for train_step in range(self.args.number_of_training_steps_per_iter):
torch.cuda.empty_cache()
# fast_model.set_fast_weights(names_weights_copy)
support_loss = self.net_forward(
x,
mask=mask,
teacher_unary=y_true,
num_step=train_step,
fast_model=fast_weights,
training=True,
)
fast_weights = self.apply_inner_loop_update(
loss=support_loss,
names_weights_copy=fast_weights,
use_second_order=False,
current_step_idx=train_step,
)
if writer is not None: # create histogram of weights
for param_name, param in fast_weights.items():
writer.add_histogram(
task_name + "/" + param_name, param, train_step + 1
)
writer.flush()
pbar_train.update(1)
pbar_train.set_description(
"finetuning phase {} -> loss: {}".format(
batch_idx * self.args.number_of_training_steps_per_iter
+ train_step
+ 1,
support_loss.item(),
)
)
#########################################################
# Evaluate finetuned model
#########################################################
if (batch_idx + 1) % eval_every == 0:
print("Evaluating model...")
losses = []
is_correct_preds = []
if train_on_cpu:
self.device = torch.device("cuda")
self.classifier.to(self.device)
with torch.no_grad():
for batch in tqdm(
dev_dataloader,
desc="Evaluating",
leave=False,
total=len(dev_dataloader),
):
batch = tuple(t.to(self.device) for t in batch)
x, mask, y_true = batch
loss, is_correct = self.net_forward(
x,
mask=mask,
teacher_unary=y_true,
fast_model=fast_weights,
training=False,
return_nr_correct=True,
num_step=train_step,
)
losses.append(loss.item())
is_correct_preds.extend(is_correct.tolist())
avg_loss = np.mean(losses)
accuracy = np.mean(is_correct_preds)
print("Accuracy", accuracy)
if avg_loss < best_loss:
best_loss = avg_loss
print(
"New best finetuned model with loss {:.05f}".format(
best_loss
)
)
torch.save(
names_weights_copy,
os.path.join(
model_save_dir,
"model_finetuned_{}".format(
task_name.replace("train/", "", 1)
.replace("val/", "", 1)
.replace("test/", "", 1)
),
),
)
return names_weights_copy, best_loss, avg_loss, accuracy