-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_irony_epida_eda.py
439 lines (401 loc) · 15.9 KB
/
train_irony_epida_eda.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
# EPiDA Easy Plug-in Data Augumentation
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
import torch.nn as nn
import numpy as np
import re
import random
import transformers
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertModel, XLNetTokenizer
from transformers import AdamW, BertForSequenceClassification,XLNetForSequenceClassification
from eda import eda,epda,epda_bert
from nlp_aug import eda_4
from sklearn.utils import shuffle
from sklearn.metrics import accuracy_score,f1_score,classification_report
from utils import SoftCrossEntropy,FocalLoss
import nlpaug.augmenter.char as nac
import nlpaug.augmenter.word as naw
import nlpaug.augmenter.sentence as nas
train_dataset = 'data/irony'
data_split = '40'
BATCH_SIZE = 32
MODEL_NAME = 'Bert'
ONLINE = True
NEED_AUG = True
NUM_AUG = 3
MIX_UP = False
AUG_METHOD = 'EPDA'
EPDA_ENGINE = 'EDA'
ALPHA = 0.05
train_file_name = 'train_'+str(data_split)+'.txt'
train_dir = train_dataset+'/'+train_file_name
test_dir = train_dataset+'/'+'test.txt'
num_classes = 0
LR = 5e-5
if 'irony' in train_dir:
num_classes = 2
BASIC_EPOCH = 20
LR = 2e-5
elif 'sentiment' in train_dir:
BASIC_EPOCH = 20
LR = 5e-5
BATCH_SIZE = 32
num_classes = 3
elif 'offense' in train_dir:
num_classes = 4
BASIC_EPOCH = 5
# LR = 5e-6
elif 'trec' in train_dir:
num_classes = 6
BASIC_EPOCH = 60
LR = 2e-5
elif 'agnews' in train_dir:
num_classes = 4
BASIC_EPOCH = 12
LR = 2e-5
print("LR=",LR)
device = torch.device('cuda')
import torch.distributed as dist
# Define Tokenizer
if MODEL_NAME=='Bert':
TOKENIZER = BertTokenizer.from_pretrained('bert-base-uncased',local_files_only=True)
elif MODEL_NAME=='XLNet':
TOKENIZER = XLNetTokenizer.from_pretrained('xlnet-base-cased',local_files_only=True)
def get_world_size():
if not dist.is_available():
return 1
if not dist.is_initialized():
return 1
return dist.get_world_size()
def move_to_device(batch, rank = None):
ans = {}
if (rank is None):
device = 'cuda'
else:
device = 'cuda:{}'.format(rank)
for key in batch:
try:
ans[key] = batch[key].to(device = device)
except Exception as e:
# print(str(e))
ans[key] = batch[key]
return ans
def reduce_loss_dict(loss_dict):
world_size = get_world_size()
if world_size < 2:
return loss_dict
with torch.no_grad():
loss_names = []
all_losses = []
for k in sorted(loss_dict.keys()):
loss_names.append(k)
all_losses.append(loss_dict[k])
all_losses = torch.stack(all_losses, dim = 0)
dist.reduce(all_losses, dst = 0)
if dist.get_rank() == 0:
all_losses /= world_size
reduced_losses = {k: v for k, v in zip(loss_names, all_losses)}
return reduced_losses
class Collect_FN():
def __init__(self, with_label_hard, with_GT_labels = False):
super(Collect_FN, self).__init__()
if MODEL_NAME=='Bert':
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased',local_files_only=True)
elif MODEL_NAME=='XLNet':
self.tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased',local_files_only=True)
self.with_label = with_label_hard
self.with_GT_labels = with_GT_labels
def __call__(self, batchs):
# print(batchs)
if (self.with_label and self.with_GT_labels == False):
sentences, labels = map(list, zip(*batchs))
elif (self.with_label == True and self.with_GT_labels == True):
sentences, labels, GT_labels = map(list, zip(*batchs))
else:
sentences = batchs
encoding = self.tokenizer(sentences, return_tensors = 'pt', padding = True, truncation = True)
# input_ids = encoding['input_ids']
# attention_mask = encoding['attention_mask']
# ans = {'input_ids': input_ids, 'attention_mask': attention_mask}
if (self.with_label):
labels = torch.tensor(labels).long()
encoding['labels'] = labels
if (self.with_GT_labels):
GT_labels = torch.tensor(GT_labels).long()
encoding['GT_labels'] = GT_labels
encoding['sentences'] = sentences
return encoding
class EPDADataSet(Dataset):
def __init__(self,input_dir,max_len=30,num_classes=2):
self.max_len = max_len
self.num_classes = num_classes
self.dir = input_dir
print("Start to read: ",input_dir, flush = True)
#先预读一下
lines = open(input_dir,'r').readlines()
Xs,Ys=[],[]
count = [0] * num_classes
for line in lines:
y,x = line.split('\t')
y = int(y)
# 最后后一个\n的
x = x[:-1]
# if 'train' in input_dir:
# if y==0 or y==2:
# continue
# if count[y] >= int(434*int(data_split)/10*2) and 'train' in input_dir:
# continue
count[y] += 1
if len(x)<=2:
continue
x = self.get_only_chars(x)
Xs.append(x)
Ys.append(y)
# weight_per_class = [0.] * num_classes
# N = float(sum(count))
# for i in range(num_classes):
# weight_per_class[i] = N/float(count[i])
# weight = [0] * len(Ys)
# for idx, val in enumerate(Ys):
# weight[idx] = weight_per_class[val]
# self.weights = weight_per_class
# print(weight_per_class,count)
# os._exit(233)
# if not 'test' in input_dir:
# Xs,Ys = self.upsample_balance(Xs,Ys)
# print("Balance dataset Over.")
self.Xs = Xs
self.Ys = Ys
self.O_Xs = self.Xs
self.O_Ys = self.Ys
print("Load Over, Find: ",len(self.Xs)," datas.", flush = True)
def get_only_chars(self,line):
clean_line = ""
line = line.lower()
line = line.replace(" 's", " is")
line = line.replace("-", " ") #replace hyphens with spaces
line = line.replace("\t", " ")
line = line.replace("\n", " ")
line = line.replace("'", "")
for char in line:
if char in 'qwertyuiopasdfghjklzxcvbnm ':
clean_line += char
else:
clean_line += ' '
clean_line = re.sub(' +',' ',clean_line) #delete extra spaces
if clean_line[0] == ' ':
clean_line = clean_line[1:]
return clean_line
def __getitem__(self, idx):
assert idx < len(self.Xs)
return self.Xs[idx],self.Ys[idx]
def __len__(self):
return len(self.Xs)
def update(self,Xs,Ys):
print("Start Update Dataset, Find ",len(self.Xs),'datas.', flush = True)
# if not 'test' in self.dir:
# Xs,Ys = self.upsample_balance(Xs,Ys)
# print("Balance dataset Over.")
self.Xs = Xs
self.Ys = Ys
print("Update Dataset Finish, Find ",len(self.Xs),'datas.', flush = True)
return
def reset(self):
if self.O_Xs is not None:
self.Xs = self.O_Xs
self.Ys = self.O_Ys
def upsample_balance(self, sentences, labels):
sample_number_per_class = [0]*self.num_classes
for y in labels:
sample_number_per_class[y] +=1
sample_number_per_class = np.array(sample_number_per_class)
max_number = np.max(sample_number_per_class)
fill_number_each_class = max_number - sample_number_per_class
# print("??",sample_number_per_class,fill_number_each_class)
sentence_each_class = [[] for i in range(self.num_classes)]
for s, l in zip(sentences, labels):
sentence_each_class[l].append(s)
for class_index, (sentences_cur_class, fill_num_cur_class) in enumerate(
zip(sentence_each_class, fill_number_each_class)):
append_cur_class = []
for i in range(fill_num_cur_class):
append_cur_class.append(sentences_cur_class[i % len(sentences_cur_class)])
sentence_each_class[class_index] = sentences_cur_class + append_cur_class
ans_sentences = []
ans_labels = []
for class_index in range(self.num_classes):
for s in sentence_each_class[class_index]:
ans_sentences.append(s)
ans_labels.append(class_index)
return ans_sentences, ans_labels
def do_aug(inputs,labels,aug_method,get_embed_fn,model=None,num_aug=1):
if aug_method == 'EDA':
aug_fn = eda_4
elif aug_method == 'EEDA':
aug_fn = eda
elif aug_method == 'EPDA':
aug_fn = epda_bert
# print(len(inputs),'vs',len(labels))
Xs,Ys= [],[]
for i in range(len(inputs)):
if aug_method == 'EPDA':
translator = None
if EPDA_ENGINE == 'CWE':
nlp_auger = naw.ContextualWordEmbsAug(action='insert',device='cuda')
elif EPDA_ENGINE == 'BT':
nlp_auger = naw.BackTranslationAug (device='cuda')
else:
nlp_auger = None
# if labels[i]==0 or labels[i]==2:
# # print("Dont aug")
# augedtxts = [inputs[i]]
# else:
augedtxts,_ = aug_fn(txt=inputs[i],label=labels[i],num_aug=NUM_AUG,model=model,translator=translator,
engine=EPDA_ENGINE,alpha=ALPHA,mix_up=MIX_UP,get_embed_fn=get_embed_fn,loss_fn=nn.CrossEntropyLoss(),nlp_auger = nlp_auger)
Xs+=augedtxts
# 再填一堆同样的
for j in range(len(augedtxts)):
Ys.append(labels[i])
else:
txts = aug_fn(inputs[i],num_aug=NUM_AUG)
for txt in txts:
embed = get_embed_fn(txt)
# print("Size",embed.size())
Xs.append(embed)
label_tensor = torch.zeros(1)
label_tensor[0] = labels[i]
label_tensor = label_tensor.long()
Ys.append(label_tensor)
# 一个就好了
return Xs,Ys
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def get_model():
if MODEL_NAME == 'Bert':
model = BertForSequenceClassification.from_pretrained('bert-base-uncased',
num_labels = num_classes,
gradient_checkpointing = True,
).cuda()
elif MODEL_NAME == 'XLNet':
model = XLNetForSequenceClassification.from_pretrained('xlnet-base-cased',
num_labels = num_classes,
).cuda()
return model
def train(train_data_loader,test_data_loader,model):
EPOCHES = BASIC_EPOCH
if NEED_AUG:
# EPOCHES += int(BASIC_EPOCH*NUM_AUG//(NUM_AUG+1))
EPOCHES*=2
print("Update EPOCHES to",EPOCHES)
max_f1_score = 0.0
trained_iter = 0
UPDATED = False
T_max = EPOCHES * (len(train_dataset)//BATCH_SIZE+1)
optimizer = AdamW(model.parameters(), lr=LR, eps=1e-8, weight_decay=1e-3)
# scheduler = transformers.get_linear_schedule_with_warmup(optimizer,EPOCHES,T_max)
# scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, [int(EPOCHES)*0.6,int(EPOCHES)*0.9], gamma=0.5, last_epoch=-1)
# reset the dataset.
train_data_loader.dataset.reset()
loss_fn = nn.CrossEntropyLoss()
for epoch in range(EPOCHES):
model.train()
if (NEED_AUG and ONLINE == False and epoch == BASIC_EPOCH) or (NEED_AUG and ONLINE and epoch % 5==0 and epoch>=BASIC_EPOCH):
print("Start to update Dataset")
input_dir = train_data_loader.dataset.dir
lines = open(input_dir,'r').readlines()
Xs,Ys=[],[]
count = [0]*num_classes
for line in lines:
y,x = line.split('\t')
y = int(y)
# if count[y] >= int(434*int(data_split)*2):
# continue
count[y] += 1
x = x[:-1]
x = train_data_loader.dataset.get_only_chars(x)
Xs.append(x)
Ys.append(y)
inputs,label = do_aug(Xs,Ys,AUG_METHOD,get_embed_fn=TOKENIZER,model=model,num_aug=NUM_AUG)
print('Before',len(train_data_loader))
train_data_loader.dataset.update(inputs,label)
print("< Update Done.")
print('After',len(train_data_loader))
UPDATED = True
# 完成剩下的训练
if not ONLINE:
max_f1_score = 0.0
# model = get_model()
# optimizer = AdamW(model.parameters(), lr=LR, eps=1e-8, weight_decay=1e-3)
for i,batch in enumerate(train_data_loader):
# print(batch)
batch = move_to_device(batch)
optimizer.zero_grad()
# print(batch)
# input_ids: [16,128] label_id:[16]
# print(batch['sentences'])
# os._exit(233)
del batch['sentences']
# print(batch)
# os._exit(233)
output = model(**batch)
# loss = output.loss
loss = loss_fn(output.logits,batch['labels'])
# loss_dict_reduced = reduce_loss_dict({'loss_all': loss})
# losses_reduced = sum(loss for loss in loss_dict_reduced.values())
# meters.update(loss = losses_reduced, **loss_dict_reduced)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(),1.0)
optimizer.step()
trained_iter += 1
if (trained_iter % 1 == 0 and 'offense' not in train_dataset) or trained_iter%5==0:
model.eval()
pred_y,gt_y=[],[]
for i,batch in enumerate(test_data_loader):
label = batch['labels']
del batch['sentences']
batch = move_to_device(batch)
outputs = model(**batch).logits
b,_ = outputs.size()
outputs = torch.softmax(outputs,1)
# confidence_mat = torch.ones(outputs.size())
outputs = torch.argmax(outputs,1).detach().cpu()
for j in range(b):
pred_y.append(outputs[j])
gt_y.append(label[j])
# print(outputs[j],'vs',label[j])
# print(gt_y[:10],'vs',pred_y[:10])
# if num_classes==2:
# score = f1_score(gt_y, pred_y, average='binary')
# else:
score = f1_score(gt_y, pred_y, average='macro')
print("--F1 Score",score, flush = True)
print("Report",classification_report(gt_y,pred_y))
if UPDATED or (not NEED_AUG):
max_f1_score = max(max_f1_score,score)
# os._exit(233)
return max_f1_score
def compute_model(train_dir,test_dir):
f1_scores = []
train_dataset = EPDADataSet(train_dir,num_classes=num_classes)
test_dataset = EPDADataSet(test_dir,num_classes=num_classes)
collate_fn = Collect_FN(True)
# train_sampler = torch.utils.data.sampler.WeightedRandomSampler(train_dataset.weights, BATCH_SIZE)
# print(max(train_dataset.weights),min(train_dataset.weights))
train_data_loader = DataLoader(dataset=train_dataset,batch_size=BATCH_SIZE,collate_fn=collate_fn,shuffle=True)
test_data_loader = DataLoader(dataset=test_dataset,batch_size=64,shuffle=False,collate_fn=collate_fn)
# Test them for 5 times
for i in range(5):
setup_seed(i+1)
model = get_model()
f1 = train(train_data_loader,test_data_loader,model)
f1_scores.append(f1)
print("[IMPORTANT] i=",i,"Current F1 Score",f1,"Average F1 Score: ",sum(f1_scores)/len(f1_scores), flush = True)
print("> Done.", flush = True)
if __name__ == "__main__":
compute_model(train_dir,test_dir)