-
Notifications
You must be signed in to change notification settings - Fork 34
/
transformer.py
589 lines (476 loc) · 24.6 KB
/
transformer.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
from argparse import ArgumentParser
from json import decoder
from logging import debug
import pytorch_lightning as pl
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
# Hide lines below until Lab 5
import wandb
import numpy as np
# Hide lines above until Lab 5
from .base import BaseLitModel
from .util import f1_eval, compute_f1, acc, f1_score
from transformers.optimization import get_linear_schedule_with_warmup
from functools import partial
import random
def mask_hook(grad_input, st, ed):
mask = torch.zeros((grad_input.shape[0], 1)).type_as(grad_input)
mask[st: ed] += 1.0 # 只优化id为1~8的token
# for the speaker unused token12
mask[1:3] += 1.0
return grad_input * mask
def multilabel_categorical_crossentropy(y_pred, y_true):
y_pred = (1 - 2 * y_true) * y_pred
y_pred_neg = y_pred - y_true * 1e12
y_pred_pos = y_pred - (1 - y_true) * 1e12
zeros = torch.zeros_like(y_pred[..., :1])
y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1)
y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1)
neg_loss = torch.logsumexp(y_pred_neg, dim=-1)
pos_loss = torch.logsumexp(y_pred_pos, dim=-1)
return (neg_loss + pos_loss).mean()
# class AMSoftmax(nn.Module):
# def __init__(self,
# in_feats,
# n_classes=10,
# m=0.35,
# s=30):
# super(AMSoftmax, self).__init__()
# self.m = m
# self.s = s
# self.in_feats = in_feats
# self.W = torch.nn.Linear(in_feats, n_classes)
# # self.W = torch.nn.Parameter(torch.randn(in_feats, n_classes), requires_grad=True)
# self.ce = nn.CrossEntropyLoss()
# # nn.init.xavier_normal_(self.W, gain=1)
# def forward(self, x, lb):
# assert x.size()[0] == lb.size()[0]
# assert x.size()[1] == self.in_feats
# x_norm = torch.norm(x, p=2, dim=1, keepdim=True).clamp(min=1e-12)
# x_norm = torch.div(x, x_norm)
# w_norm = torch.norm(self.W, p=2, dim=0, keepdim=True).clamp(min=1e-12)
# w_norm = torch.div(self.W, w_norm)
# costh = torch.mm(x_norm, w_norm)
# # print(x_norm.shape, w_norm.shape, costh.shape)
# lb_view = lb.view(-1, 1)
# if lb_view.is_cuda: lb_view = lb_view.cpu()
# delt_costh = torch.zeros(costh.size()).scatter_(1, lb_view, self.m)
# if x.is_cuda: delt_costh = delt_costh.cuda()
# costh_m = costh - delt_costh
# costh_m_s = self.s * costh_m
# loss = self.ce(costh_m_s, lb)
# return loss, costh_m_s
# class AMSoftmax(nn.Module):
# def __init__(self, in_features, out_features, s=30.0, m=0.35):
# '''
# AM Softmax Loss
# '''
# super().__init__()
# self.s = s
# self.m = m
# self.in_features = in_features
# self.out_features = out_features
# self.fc = nn.Linear(in_features, out_features, bias=False)
# def forward(self, x, labels):
# '''
# input shape (N, in_features)
# '''
# assert len(x) == len(labels)
# assert torch.min(labels) >= 0
# assert torch.max(labels) < self.out_features
# for W in self.fc.parameters():
# W = F.normalize(W, dim=1)
# x = F.normalize(x, dim=1)
# wf = self.fc(x)
# numerator = self.s * (torch.diagonal(wf.transpose(0, 1)[labels]) - self.m)
# excl = torch.cat([torch.cat((wf[i, :y], wf[i, y+1:])).unsqueeze(0) for i, y in enumerate(labels)], dim=0)
# denominator = torch.exp(numerator) + torch.sum(torch.exp(self.s * excl), dim=1)
# L = numerator - torch.log(denominator)
# return -torch.mean(L)
class BertLitModel(BaseLitModel):
"""
use AutoModelForMaskedLM, and select the output by another layer in the lit model
"""
def __init__(self, model, args, tokenizer):
super().__init__(model, args)
self.tokenizer = tokenizer
with open(f"{args.data_dir}/rel2id.json","r") as file:
rel2id = json.load(file)
Na_num = 0
for k, v in rel2id.items():
if k == "NA" or k == "no_relation" or k == "Other":
Na_num = v
break
num_relation = len(rel2id)
# init loss function
self.loss_fn = multilabel_categorical_crossentropy if "dialogue" in args.data_dir else nn.CrossEntropyLoss()
# self.loss_fn = AMSoftmax(self.model.config.hidden_size, num_relation)
# ignore the no_relation class to compute the f1 score
self.eval_fn = f1_eval if "dialogue" in args.data_dir else partial(f1_score, rel_num=num_relation, na_num=Na_num)
self.best_f1 = 0
self.t_lambda = args.t_lambda
self.label_st_id = tokenizer("[class1]", add_special_tokens=False)['input_ids'][0]
self.tokenizer = tokenizer
self._init_label_word()
# with torch.no_grad():
# self.loss_fn.fc.weight = nn.Parameter(self.model.get_output_embeddings().weight[self.label_st_id:self.label_st_id+num_relation])
# self.loss_fn.fc.bias = nn.Parameter(self.model.get_output_embeddings().bias[self.label_st_id:self.label_st_id+num_relation])
def _init_label_word(self, ):
args = self.args
# ./dataset/dataset_name
dataset_name = args.data_dir.split("/")[1]
model_name_or_path = args.model_name_or_path.split("/")[-1]
label_path = f"./dataset/{model_name_or_path}_{dataset_name}.pt"
# [num_labels, num_tokens], ignore the unanswerable
if "dialogue" in args.data_dir:
label_word_idx = torch.load(label_path)[:-1]
else:
label_word_idx = torch.load(label_path)
num_labels = len(label_word_idx)
self.model.resize_token_embeddings(len(self.tokenizer))
with torch.no_grad():
word_embeddings = self.model.get_input_embeddings()
continous_label_word = [a[0] for a in self.tokenizer([f"[class{i}]" for i in range(1, num_labels+1)], add_special_tokens=False)['input_ids']]
# for abaltion study
if self.args.init_answer_words:
if self.args.init_answer_words_by_one_token:
for i, idx in enumerate(label_word_idx):
word_embeddings.weight[continous_label_word[i]] = word_embeddings.weight[idx][-1]
else:
for i, idx in enumerate(label_word_idx):
word_embeddings.weight[continous_label_word[i]] = torch.mean(word_embeddings.weight[idx], dim=0)
# word_embeddings.weight[continous_label_word[i]] = self.relation_embedding[i]
if self.args.init_type_words:
so_word = [a[0] for a in self.tokenizer(["[obj]","[sub]"], add_special_tokens=False)['input_ids']]
meaning_word = [a[0] for a in self.tokenizer(["person","organization", "location", "date", "country"], add_special_tokens=False)['input_ids']]
for i, idx in enumerate(so_word):
word_embeddings.weight[so_word[i]] = torch.mean(word_embeddings.weight[meaning_word], dim=0)
assert torch.equal(self.model.get_input_embeddings().weight, word_embeddings.weight)
assert torch.equal(self.model.get_input_embeddings().weight, self.model.get_output_embeddings().weight)
self.word2label = continous_label_word # a continous list
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, labels, so = batch
result = self.model(input_ids, attention_mask, return_dict=True, output_hidden_states=True)
logits = result.logits
output_embedding = result.hidden_states[-1]
logits = self.pvp(logits, input_ids)
# logits = self.model.roberta(input_ids, attention_mask).last_hidden_state
# loss = self.get_loss(logits, input_ids, labels)
ke_loss = self.ke_loss(output_embedding, labels, so, input_ids)
loss = self.loss_fn(logits, labels) + self.t_lambda * ke_loss
self.log("Train/loss", loss)
self.log("Train/ke_loss", loss)
return loss
def get_loss(self, logits, input_ids, labels):
_, mask_idx = (input_ids == self.tokenizer.mask_token_id).nonzero(as_tuple=True)
bs = input_ids.shape[0]
mask_output = logits[torch.arange(bs), mask_idx]
loss = self.loss_fn(mask_output, labels)
return loss
def validation_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, labels, _ = batch
logits = self.model(input_ids, attention_mask, return_dict=True).logits
# logits = self.model.roberta(input_ids, attention_mask).last_hidden_state
# loss = self.loss_fn(logits, labels)
logits = self.pvp(logits, input_ids)
loss = self.loss_fn(logits, labels)
self.log("Eval/loss", loss)
return {"eval_logits": logits.detach().cpu().numpy(), "eval_labels": labels.detach().cpu().numpy()}
def validation_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["eval_logits"] for o in outputs])
labels = np.concatenate([o["eval_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Eval/f1", f1)
if f1 > self.best_f1:
self.best_f1 = f1
self.log("Eval/best_f1", self.best_f1, prog_bar=True, on_epoch=True)
def test_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, labels, _ = batch
logits = self.model(input_ids, attention_mask, return_dict=True).logits
logits = self.pvp(logits, input_ids)
return {"test_logits": logits.detach().cpu().numpy(), "test_labels": labels.detach().cpu().numpy()}
def test_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["test_logits"] for o in outputs])
labels = np.concatenate([o["test_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Test/f1", f1)
@staticmethod
def add_to_argparse(parser):
BaseLitModel.add_to_argparse(parser)
parser.add_argument("--t_lambda", type=float, default=0.01, help="")
parser.add_argument("--t_gamma", type=float, default=0.3, help="")
return parser
def pvp(self, logits, input_ids):
# convert the [batch_size, seq_len, vocab_size] => [batch_size, num_labels]
#! hard coded
_, mask_idx = (input_ids == self.tokenizer.mask_token_id).nonzero(as_tuple=True)
bs = input_ids.shape[0]
mask_output = logits[torch.arange(bs), mask_idx]
assert mask_idx.shape[0] == bs, "only one mask in sequence!"
final_output = mask_output[:,self.word2label]
return final_output
def ke_loss(self, logits, labels, so, input_ids):
subject_embedding = []
object_embedding = []
neg_subject_embedding = []
neg_object_embedding = []
bsz = logits.shape[0]
for i in range(bsz):
subject_embedding.append(torch.mean(logits[i, so[i][0]:so[i][1]], dim=0))
object_embedding.append(torch.mean(logits[i, so[i][2]:so[i][3]], dim=0))
# random select the neg samples
st_sub = random.randint(1, logits[i].shape[0] - 6)
span_sub = random.randint(1, 5)
st_obj = random.randint(1, logits[i].shape[0] - 6)
span_obj = random.randint(1, 5)
neg_subject_embedding.append(torch.mean(logits[i, st_sub:st_sub+span_sub], dim=0))
neg_object_embedding.append(torch.mean(logits[i, st_obj:st_obj+span_obj], dim=0))
subject_embedding = torch.stack(subject_embedding)
object_embedding = torch.stack(object_embedding)
neg_subject_embedding = torch.stack(neg_subject_embedding)
neg_object_embedding = torch.stack(neg_object_embedding)
# trick , the relation ids is concated,
_, mask_idx = (input_ids == self.tokenizer.mask_token_id).nonzero(as_tuple=True)
mask_output = logits[torch.arange(bsz), mask_idx]
mask_relation_embedding = mask_output
real_relation_embedding = self.model.get_output_embeddings().weight[labels+self.label_st_id]
d_1 = torch.norm(subject_embedding + mask_relation_embedding - object_embedding, p=2) / bsz
d_2 = torch.norm(neg_subject_embedding + real_relation_embedding - neg_object_embedding, p=2) / bsz
f = torch.nn.LogSigmoid()
loss = -1.*f(self.args.t_gamma - d_1) - f(d_2 - self.args.t_gamma)
return loss
def configure_optimizers(self):
no_decay_param = ["bias", "LayerNorm.weight"]
if not self.args.two_steps:
parameters = self.model.named_parameters()
else:
# model.bert.embeddings.weight
parameters = [next(self.model.named_parameters())]
# only optimize the embedding parameters
optimizer_group_parameters = [
{"params": [p for n, p in parameters if not any(nd in n for nd in no_decay_param)], "weight_decay": self.args.weight_decay},
{"params": [p for n, p in parameters if any(nd in n for nd in no_decay_param)], "weight_decay": 0}
]
optimizer = self.optimizer_class(optimizer_group_parameters, lr=self.lr, eps=1e-8)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=self.num_training_steps * 0.1, num_training_steps=self.num_training_steps)
return {
"optimizer": optimizer,
"lr_scheduler":{
'scheduler': scheduler,
'interval': 'step', # or 'epoch'
'frequency': 1,
}
}
class TransformerLitModelTwoSteps(BertLitModel):
def configure_optimizers(self):
no_decay_param = ["bais", "LayerNorm.weight"]
optimizer_group_parameters = [
{"params": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay_param)], "weight_decay": self.args.weight_decay},
{"params": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay_param)], "weight_decay": 0}
]
optimizer = self.optimizer_class(optimizer_group_parameters, lr=self.args.lr_2, eps=1e-8)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=self.num_training_steps * 0.1, num_training_steps=self.num_training_steps)
return {
"optimizer": optimizer,
"lr_scheduler":{
'scheduler': scheduler,
'interval': 'step', # or 'epoch'
'frequency': 1,
}
}
class DialogueLitModel(BertLitModel):
def training_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, token_type_ids , labels = batch
result = self.model(input_ids, attention_mask, token_type_ids, return_dict=True, output_hidden_states=True)
logits = result.logits
logits = self.pvp(logits, input_ids)
loss = self.loss_fn(logits, labels)
self.log("Train/loss", loss)
return loss
def validation_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, token_type_ids , labels = batch
logits = self.model(input_ids, attention_mask, token_type_ids, return_dict=True).logits
logits = self.pvp(logits, input_ids)
loss = self.loss_fn(logits, labels)
self.log("Eval/loss", loss)
return {"eval_logits": logits.detach().cpu().numpy(), "eval_labels": labels.detach().cpu().numpy()}
def validation_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["eval_logits"] for o in outputs])
labels = np.concatenate([o["eval_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Eval/f1", f1)
if f1 > self.best_f1:
self.best_f1 = f1
self.log("Eval/best_f1", self.best_f1, prog_bar=True, on_epoch=True)
def test_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, token_type_ids , labels = batch
logits = self.model(input_ids, attention_mask, token_type_ids, return_dict=True).logits
logits = self.pvp(logits, input_ids)
return {"test_logits": logits.detach().cpu().numpy(), "test_labels": labels.detach().cpu().numpy()}
def test_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["test_logits"] for o in outputs])
labels = np.concatenate([o["test_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Test/f1", f1)
@staticmethod
def add_to_argparse(parser):
BaseLitModel.add_to_argparse(parser)
parser.add_argument("--t_lambda", type=float, default=0.01, help="")
return parser
def pvp(self, logits, input_ids):
# convert the [batch_size, seq_len, vocab_size] => [batch_size, num_labels]
#! hard coded
_, mask_idx = (input_ids == 103).nonzero(as_tuple=True)
bs = input_ids.shape[0]
mask_output = logits[torch.arange(bs), mask_idx]
assert mask_idx.shape[0] == bs, "only one mask in sequence!"
final_output = mask_output[:,self.word2label]
return final_output
def decode(tokenizer, output_ids):
return [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in output_ids]
class GPTLitModel(BaseLitModel):
def __init__(self, model, args , data_config):
super().__init__(model, args)
# self.num_training_steps = data_config["num_training_steps"]
self.loss_fn = nn.CrossEntropyLoss()
# self.loss_fn = multilabel_categorical_crossentropy
self.best_f1 = 0
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, cls_idx , labels = batch
logits = self.model(input_ids, attention_mask=attention_mask, mc_token_ids=cls_idx)
if not isinstance(logits, torch.Tensor):
logits = logits.mc_logits
loss = self.loss_fn(logits, labels)
self.log("Train/loss", loss)
return loss
def validation_step(self, batch, batch_idx): # pylint: disable=unused-argument
input_ids, attention_mask, cls_idx , labels = batch
logits = self.model(input_ids, attention_mask=attention_mask, mc_token_ids=cls_idx)
if not isinstance(logits, torch.Tensor):
logits = logits.mc_logits
loss = self.loss_fn(logits, labels)
self.log("Eval/loss", loss)
return {"eval_logits": logits.detach().cpu().numpy(), "eval_labels": labels.detach().cpu().numpy()}
def validation_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["eval_logits"] for o in outputs])
labels = np.concatenate([o["eval_labels"] for o in outputs])
# f1 = compute_f1(logits, labels)["f1"]
f1 = f1_score(logits, labels)
self.log("Eval/f1", f1)
if f1 > self.best_f1:
self.best_f1 = f1
self.log("Eval/best_f1", self.best_f1, prog_bar=True, on_epoch=True)
def test_step(self, batch, batch_idx): # pylint: disable=unused-argumenT
input_ids, attention_mask, cls_idx , labels = batch
logits = self.model(input_ids, attention_mask=attention_mask, mc_token_ids=cls_idx)
if not isinstance(logits, torch.Tensor):
logits = logits.mc_logits
return {"test_logits": logits.detach().cpu().numpy(), "test_labels": labels.detach().cpu().numpy()}
def test_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["test_logits"] for o in outputs])
labels = np.concatenate([o["test_labels"] for o in outputs])
f1 = f1_score(logits, labels)
# f1 = acc(logits, labels)
self.log("Test/f1", f1)
from models.trie import get_trie
class BartRELitModel(BaseLitModel):
def __init__(self, model, args, tokenizer=None):
super().__init__(model, args)
self.best_f1 = 0
self.first = True
with open(f"{args.data_dir}/rel2id.json","r") as file:
rel2id = json.load(file)
Na_num = 0
for k, v in rel2id.items():
if k == "NA" or k == "no_relation" or k == "Other":
Na_num = v
break
num_relation = len(rel2id)
# init loss function
self.loss_fn = multilabel_categorical_crossentropy if "dialogue" in args.data_dir else nn.CrossEntropyLoss()
# ignore the no_relation class to compute the f1 score
self.eval_fn = f1_eval if "dialogue" in args.data_dir else partial(f1_score, rel_num=num_relation, na_num=Na_num)
self.tokenizer = tokenizer
self.trie, self.rel2id = get_trie(args, tokenizer=tokenizer)
self.decode = partial(decode, tokenizer=self.tokenizer)
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx): # pylint: disable=unused-argument
real_label = batch.pop("label")
loss = self.model(**batch).loss
self.log("Train/loss", loss)
return loss
def validation_step(self, batch, batch_idx):
real_label = batch.pop("label")
labels = batch.pop("labels")
batch.pop("decoder_input_ids")
topk = 1
outputs = self.model.generate(**batch,
prefix_allowed_tokens_fn=lambda batch_id, sent: self.trie.get(sent.tolist()),
num_beams=topk, num_return_sequences=topk,
output_scores=True,
min_length=0,
max_length=32,
).cpu()
# calculate the rank in the decoder output
pad_id = self.tokenizer.pad_token_id
outputs = self.decode(output_ids=outputs)
labels = self.decode(output_ids=labels)
preds = torch.tensor([self.rel2id[o] for o in outputs])
true = real_label
return {"eval_logits": preds.detach().cpu().numpy(), "eval_labels": true.detach().cpu().numpy()}
def validation_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["eval_logits"] for o in outputs])
labels = np.concatenate([o["eval_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Eval/f1", f1)
if f1 > self.best_f1 and not self.first:
self.best_f1 = f1
self.first = False
self.log("Eval/best_f1", self.best_f1, prog_bar=True, on_epoch=True)
def test_step(self, batch, batch_idx): # pylint: disable=unused-argument
real_label = batch.pop("label")
labels = batch.pop("labels")
batch.pop("decoder_input_ids")
topk = 1
outputs = self.model.generate(**batch,
prefix_allowed_tokens_fn=lambda batch_id, sent: self.trie.get(sent.tolist()),
num_beams=topk, num_return_sequences=topk,
output_scores=True,
min_length=0,
max_length=32,
).cpu()
# calculate the rank in the decoder output
pad_id = self.tokenizer.pad_token_id
outputs = self.decode(output_ids=outputs)
labels = self.decode(output_ids=labels)
preds = torch.tensor([self.rel2id[o] for o in outputs])
true = real_label
return {"test_logits": preds.detach().cpu().numpy(), "test_labels": true.detach().cpu().numpy()}
def test_epoch_end(self, outputs) -> None:
logits = np.concatenate([o["test_logits"] for o in outputs])
labels = np.concatenate([o["test_labels"] for o in outputs])
f1 = self.eval_fn(logits, labels)['f1']
self.log("Test/f1", f1)
def configure_optimizers(self):
no_decay_param = ["bias", "LayerNorm.weight"]
optimizer_group_parameters = [
{"params": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay_param)], "weight_decay": self.args.weight_decay},
{"params": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay_param)], "weight_decay": 0}
]
optimizer = self.optimizer_class(optimizer_group_parameters, lr=self.lr, eps=1e-8)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=self.num_training_steps * 0.1, num_training_steps=self.num_training_steps)
return {
"optimizer": optimizer,
"lr_scheduler":{
'scheduler': scheduler,
'interval': 'step', # or 'epoch'
'frequency': 1,
}
}