-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_disc_span_electra.py
403 lines (353 loc) · 13.4 KB
/
train_disc_span_electra.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
import os
import torch
from tqdm import tqdm
import pandas as pd
import torch.nn as nn
import numpy as np
import time, random
import logging
from processing import SpanELectraDataset, InputExample
from configuration_span_electra import SpanElectraConfig
from modelling_span_electra import SpanElectraForPretraining
from argument import SE_trainDataArgs, SE_validDataArgs, SE_trainingConfig
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader
import tokenizers
from transformers import AdamW
from transformers import get_linear_schedule_with_warmup
from utilis import plot2, save_stats, get_f1
from sklearn.metrics import f1_score
from matplotlib import pyplot as plt
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
# self.record= []
def update(self, val, n=1):
self.val = val
# self.record.append(val)
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def get_flat_acc_t(orig, pred, ignore_label=2):
tl = 0
totalScore = 0
for x, y in zip(orig, pred):
if x != ignore_label:
tl += 1
if x == y:
totalScore += 1
if tl == 0:
return 0, tl
return totalScore / tl, tl
def predictSE(evalData, batch_size, device, model, ignore_label, worker=0):
model.eval()
evalDataLoader = DataLoader(
evalData,
batch_size=batch_size,
num_workers=worker,
collate_fn=evalData.collate_fun,
)
tdl = tqdm(evalDataLoader, total=len(evalDataLoader))
sboLoss = AverageMeter()
sboF1 = AverageMeter()
atLoss = AverageMeter()
atF1 = AverageMeter()
predictions = []
t0 = time.time()
for idx, batch in enumerate(tdl):
# ids= batch['input_id'].to(device, dtype= torch.long)
mask_ids = batch["input_mask"].to(device, dtype=torch.long)
seg_ids = batch["segment_id"].to(device, dtype=torch.long)
lm_sentence = batch["clf_sentence"].to(device, dtype=torch.long)
pairs = batch["pairs"].to(device, dtype=torch.long)
span_labels = batch["span_labels"].to(device, dtype=torch.long)
all_token_labels = batch["all_token_labels"].to(device, dtype=torch.long)
with torch.no_grad():
at_loss, at_logits, sbo_loss, sbo_logits = model(
input_ids=lm_sentence,
attention_mask=mask_ids,
token_type_ids=seg_ids,
pairs=pairs,
span_labels=span_labels,
all_tok_labels=all_token_labels,
)
if at_loss is not None:
at_logits = at_logits.view(-1, at_logits.size(-1))
at_pred = at_logits.argmax(dim=1)
all_token_labels = all_token_labels.view(-1)
at_f1 = get_f1(all_token_labels, at_pred, ignore_label=ignore_label)
atLoss.update(at_loss.item(), batch_size)
atF1.update(at_f1, batch_size)
if sbo_loss is not None:
clf_logits = sbo_logits.view(-1, sbo_logits.size(-1))
sbo_pred = clf_logits.argmax(dim=1)
span_labels = span_labels.view(-1)
sbo_f1 = get_f1(span_labels, sbo_pred, ignore_label=ignore_label)
sboLoss.update(sbo_loss.item(), batch_size)
sboF1.update(sbo_f1, batch_size)
tdl.set_postfix(
sbo_loss=sboLoss.avg, sbo_f1=sboF1.avg, at_loss=atLoss.avg, at_f1=atF1.avg
)
logger.info(
"Validation: sbo_loss {:.4f}, sbo_f1 {:.4f}, all_token_loss: {:.4f}, all_token_f1 {:.4f} ".format(
sboLoss.avg, sboF1.avg, atLoss.avg, atF1.avg
)
)
logger.info("validation took {:.2f} sec".format(time.time() - t0))
return sboLoss.avg, sboF1.avg, atLoss.avg, atF1.avg
def trainSE(trainData, validData, device, train_config):
seed_val = 42
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
logger.info("seed value: {} ".format(seed_val))
model = SpanElectraForPretraining(train_config.modelConfig)
logger.info(
"config of model for train {}".format(train_config.modelConfig.__dict__)
)
logger.info("preaparing train data in batches")
start_time = time.time()
batch_size = train_config.train_batch_size
trainDataloader = DataLoader(
trainData,
batch_size=train_config.train_batch_size,
num_workers=train_config.num_workers,
collate_fn=trainData.collate_fun,
)
logger.info("batching took {:.3f} sec".format(time.time() - start_time))
param_optimizer = list(model.named_parameters()) # get parameter of models
no_decay = [
"bias",
"LayerNorm.bias",
"LayerNorm.weight",
] ##doubt layers to be not decayed #issue
optimizer_parameters = [
{
"params": [
p for n, p in param_optimizer if not any(nd in n for nd in no_decay)
],
"weight_decay": 0.001,
},
{
"params": [
p for n, p in param_optimizer if any(nd in n for nd in no_decay)
],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_parameters, lr=train_config.learningRate)
total_len = trainData.__len__()
logger.info("optimizer: {}".format(optimizer))
num_steps = total_len / train_config.train_batch_size * train_config.epochs
logger.info("total steps: {}".format(num_steps))
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=0, num_training_steps=num_steps
)
model.to(device)
logger.info("using device: {}".format(device))
logger.info("################# training started ##################")
logger.info("initializing weight of disc with gen")
model.load_state_dict(torch.load(train_config.generator_weight_path), strict=False)
logger.info("weight loaded")
start_time = time.time()
epoch_sbo_loss = []
epoch_sbo_accc = []
batch_sbo_loss = []
batch_sbo_accc = []
valid_sbo_loss = []
valid_sbo_accc = []
epoch_at_loss = []
epoch_at_accc = []
batch_at_loss = []
batch_at_accc = []
valid_at_loss = []
valid_at_accc = []
for epoch_i in range(0, train_config.epochs):
print("")
print(
"======== Epoch {:} / {:} ========".format(epoch_i + 1, train_config.epochs)
)
print("Training...")
t0 = time.time()
sboLoss = AverageMeter()
sboF1 = AverageMeter()
atLoss = AverageMeter()
atF1 = AverageMeter()
logger.info(
"============= Epoch {:} / {:} ===========".format(
epoch_i + 1, train_config.epochs
)
)
tdl = tqdm(trainDataloader, total=len(trainDataloader))
model.train()
for idx, batch in enumerate(tdl):
tb = time.time()
# ids= batch['input_id'].to(device, dtype= torch.long)
mask_ids = batch["input_mask"].to(device, dtype=torch.long)
seg_ids = batch["segment_id"].to(device, dtype=torch.long)
clf_sentence = batch["clf_sentence"].to(device, dtype=torch.long)
pairs = batch["pairs"].to(device, dtype=torch.long)
span_labels = batch["span_labels"].to(device, dtype=torch.long)
all_token_labels = batch["all_token_labels"].to(device, dtype=torch.long)
model.zero_grad()
at_loss, at_logits, sbo_loss, sbo_logits = model(
input_ids=clf_sentence,
attention_mask=mask_ids,
token_type_ids=seg_ids,
pairs=pairs,
span_labels=span_labels,
all_tok_labels=all_token_labels,
)
loss = None
if at_loss is not None:
loss = at_loss
at_logits = at_logits.view(-1, at_logits.size(-1))
at_pred = at_logits.argmax(dim=1)
all_token_labels = all_token_labels.view(-1)
at_f1 = get_f1(
all_token_labels, at_pred, ignore_label=train_config.ignore_label
)
atLoss.update(at_loss.item(), batch_size)
atF1.update(at_f1, batch_size)
batch_at_loss.append(at_loss.item())
batch_at_accc.append(at_f1)
if sbo_loss is not None:
loss += sbo_loss
clf_logits = sbo_logits.view(-1, sbo_logits.size(-1))
sbo_pred = clf_logits.argmax(dim=1)
span_labels = span_labels.view(-1)
sbo_f1 = get_f1(
span_labels, sbo_pred, ignore_label=train_config.ignore_label
)
sboLoss.update(sbo_loss.item(), batch_size)
sboF1.update(sbo_f1, batch_size)
batch_sbo_loss.append(sbo_loss.item())
batch_sbo_accc.append(sbo_f1)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
if idx % 50 == 0:
logger.info(
"\nepoch {}, batch no {}: sbo_loss {:.4f}, sbo_f1 {:.4f}, all_token_loss: {:.4f}, all_token_f1 {:.4f} ".format(
epoch_i, idx, sboLoss.avg, sboF1.avg, atLoss.avg, atF1.avg
)
)
tdl.set_postfix(
sbo_loss=sboLoss.avg,
sbo_f1=sboF1.avg,
at_loss=atLoss.avg,
at_f1=atF1.avg,
)
logger.info("epoch {} took {:.2f} seconds".format(epoch_i, time.time() - t0))
if validData:
logger.info("##########validating after epoch end#############")
vsl, vsa, vatl, vata = predictSE(
validData,
train_config.valid_batch_size,
device,
model,
ignore_label=train_config.ignore_label,
worker=train_config.worker,
)
valid_at_loss.append(vatl)
valid_at_accc.append(vata)
valid_sbo_loss.append(vsl)
valid_sbo_accc.append(vsa)
epoch_at_loss.append(atLoss.avg)
epoch_at_accc.append(atF1.avg)
epoch_sbo_loss.append(sboLoss.avg)
epoch_sbo_accc.append(sboF1.avg)
logger.info(
"weight stats, and model are saved in dir {}".format(train_config.save_dir)
)
torch.save(
model, train_config.save_dir + "SEmodel_{}".format(epoch_i)
) # save whole model after epoch
torch.save(
model.state_dict(),
train_config.save_dir + "SEmodel_wight{}".format(epoch_i) + ".pt",
) # save weight
logger.info("total train time {:.2f}".format(time.time() - start_time))
# save loss and accu, per step and epoch, may be needed in future
save_stats(
save_dir=train_config.save_dir,
name="twoStageDisc",
epoch_train_sbo_loss=epoch_sbo_loss,
epoch_train_sbo_acc=epoch_sbo_accc,
epoch_train_at_loss=epoch_at_loss,
epoch_train_at_acc=epoch_at_accc,
batch_train_sbo_loss=batch_sbo_loss,
batch_train_sbo_acc=batch_sbo_accc,
batch_train_at_loss=batch_at_loss,
batch_train_at_acc=batch_at_accc,
epoch_valid_sbo_loss=valid_sbo_loss,
epoch_valid_sbo_acc=valid_sbo_accc,
epoch_valid_at_loss=valid_at_loss,
epoch_valid_at_acc=valid_at_accc,
)
# loss and accu per epoch plot for valid and train
plot2(
ts=num_steps,
loss={"train": batch_at_loss},
acc={"train": batch_at_accc},
x1="batch num",
y1="all token loss",
x2="batch num",
y2="all tok f1",
figName="disc_batch_all_tok",
dire=train_config.save_dir,
)
# plot sbo loss , acc per batch
plot2(
ts=num_steps,
loss={"train": batch_sbo_loss},
acc={"train": batch_sbo_accc},
x1="batch num",
y1="sbo loss",
x2="batch num",
y2="sbo f1 score",
figName="disc_batch_sbo",
dire=train_config.save_dir,
)
def main():
device_id = SE_trainingConfig.device_id
if torch.cuda.is_available():
device = torch.device("cuda:" + str(device_id))
print("There are %d GPU(s) available." % torch.cuda.device_count())
print("We will use the GPU:", torch.cuda.get_device_name(device_id))
else:
print("No GPU available, using the CPU instead.")
device = torch.device("cpu")
targ = SE_trainDataArgs()
varg = SE_validDataArgs()
logger.addHandler(
logging.FileHandler(os.path.join(targ.out_dir, "disc_logFile.log"), "w")
) # initalize logger
logger.info("training data args")
logger.info(targ.__dict__) # log train data arg
logger.info("validation data args")
logger.info(varg.__dict__)
trainData = SpanELectraDataset(targ)
validData = SpanELectraDataset(varg)
trainSE(
trainData=trainData,
validData=validData,
device=device,
train_config=SE_trainingConfig,
)
if __name__ == "__main__":
main()