-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_roberta.py
407 lines (314 loc) · 15.5 KB
/
binary_roberta.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
'''apply fine-tuned bert based modle on four datasets'''
from transformers import BertPreTrainedModel, BertModel
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
from transformers import AdamW
import numpy as np
import pandas as pd
from sklearn.metrics import f1_score, classification_report
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from sklearn.model_selection import train_test_split
import argparse
import datetime
from sklearn import preprocessing
if torch.cuda.is_available():
device = torch.device("cuda")
print('There are %d GPU(s) available.' % torch.cuda.device_count())
print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
print('No GPU available, using the CPU instead.')
device = torch.device("cpu")
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def format_time(elapsed):
elapsed_rounded = int(round((elapsed)))
return str(datetime.timedelta(seconds=elapsed_rounded))
parser = argparse.ArgumentParser(description='run fine-tuned model on multi-label dataset')
# 0
# 1
parser.add_argument('--FTModel', type=str, help= 'where is the saved trained language model, including path and name')
parser.add_argument('--BertModel', type=str, action='store', choices = ['Bert','RoBerta','XLM', 'XLNet', 'ELECTRA', 'gpt2', 'bert_xlm'])
# 2
parser.add_argument('-e', '--epochs', type=int, default=3, metavar='', help='how many epochs')
# 3
group = parser.add_mutually_exclusive_group()
group.add_argument('--running', action='store_true', help='running using the original big dataset')
group.add_argument('--testing', action='store_true', help='testing')
# 3
# 4
parser.add_argument('--resultpath', type=str, help='where to save the result csv')
args = parser.parse_args()
MAX_LEN = 500
NUM_LABELS = 2
batch_size = 16
epochs = args.epochs
if args.testing:
data = pd.read_csv('./data/IMDB.csv', header=0, names=['comment', 'label']).sample(500)
elif args.running:
data = pd.read_csv('./data/IMDB.csv', header=0, names=['comment', 'label'])
else:
print('need to define parameter, it is "--running" or "--testing"')
print('data # ', len(data))
train, test = train_test_split(data, train_size=0.4, stratify=data['label'])
test, validation = train_test_split(test, test_size=5000, stratify=test['label'])
print('train # ', len(train))
print('test # ', len(test))
print('validation # ', len(validation))
sentences_train = train.comment.values
labels_train = train.label.values
sentences_test = test.comment.values
labels_test = test.label.values
sentences_validation = validation.comment.values
labels_validation = validation.label.values
le = preprocessing.LabelEncoder()
le.fit(['negative', 'positive'])
labels_train = le.transform(labels_train)
labels_test = le.transform(labels_test)
labels_validation = le.transform(labels_validation)
train_labels = torch.tensor(labels_train).to(device)
test_labels = torch.tensor(labels_test).to(device)
validation_labels = torch.tensor(labels_validation).to(device)
if args.FTModel != None:
model_name = str(args.FTModel)
elif args.BertModel != None:
if args.BertModel == 'Bert':
model_name = 'bert-base-cased'
elif args.BertModel == 'RoBerta':
model_name = 'roberta-base'
elif args.BertModel == 'XLM':
model_name = 'xlm-mlm-enfr-1024'
elif args.BertModel == 'gpt2':
model_name = 'gpt2'
else:
print('the model name is not set up, it should be from a pretrained model file(as args.FTModel) or '
'bert-base-cased or roberta-base or xlm-mlm-enfr-1024')
print('model_name: ', model_name)
from multi_label_fns import validate_multilable, train_multilabel
if (('RoBerta' in model_name) or ('roberta' in model_name)):
from transformers import RobertaTokenizer, RobertaModel
tokenizer = RobertaTokenizer.from_pretrained('roberta-base', do_lower_case=False)
from multi_label_fns import RoBerta_clf
model = RoBerta_clf.from_pretrained(model_name,
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=True)
print('using RoBerta:', model_name)
elif (('Bert' in model_name) or ('bert' in model_name)):
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-cased', do_lower_case=False)
from multi_label_fns import Bert_clf
model = Bert_clf.from_pretrained(model_name,
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=True)
print('using Bert:', model_name)
elif (('XLM' in model_name) or ('xlm' in model_name)):
from transformers import XLMTokenizer
tokenizer = XLMTokenizer.from_pretrained('xlm-mlm-enfr-1024', do_lower_case=False)
from multi_label_fns import XLM_clf
model = XLM_clf.from_pretrained(model_name,
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=True)
print('using XLM:', model_name)
elif 'gpt2' in model_name:
from transformers import GPT2Tokenizer, GPT2PreTrainedModel, GPT2DoubleHeadsModel
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', do_lower_case=True)
tokenizer.cls_token = tokenizer.cls_token_id
tokenizer.pad_token = tokenizer.eos_token
from gpt2 import GPT2_multilabel_clf
model = GPT2_multilabel_clf.from_pretrained(model_name,
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=False,
use_cache=False,
)
print(' ')
print('using GPT2:', model_name)
############################ Model and Tokenizer all set up
print('===========================')
print(f'The model has {count_parameters(model):,} trainable parameters')
print('===========================')
model.to(device)
train_inputs = torch.Tensor()
train_masks = torch.Tensor()
for sent in sentences_train:
encoded_sent = tokenizer.encode_plus(sent, # Sentence to encode.
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
max_length=MAX_LEN, # Truncate all sentences.
pad_to_max_length=True,
return_attention_mask=True,
return_token_type_ids=False,
truncation=True,
return_tensors='pt') # return pytorch not tensorflow tensor
train_inputs = torch.cat((train_inputs, encoded_sent['input_ids'].float()), dim=0)
train_masks = torch.cat((train_masks, encoded_sent['attention_mask'].float()), dim=0)
train_inputs.to(device)
train_masks.to(device)
validation_inputs = torch.Tensor()
validation_masks = torch.Tensor()
for sent in sentences_validation:
encoded_sent = tokenizer.encode_plus(sent, # Sentence to encode.
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
max_length=MAX_LEN, # Truncate all sentences.
pad_to_max_length=True,
return_attention_mask=True,
return_token_type_ids=False,
truncation=True,
return_tensors='pt') # return pytorch not tensorflow tensor
validation_inputs = torch.cat((validation_inputs, encoded_sent['input_ids'].float()), dim=0)
validation_masks = torch.cat((validation_masks, encoded_sent['attention_mask'].float()), dim=0)
validation_inputs.to(device)
validation_masks.to(device)
test_inputs = torch.Tensor()
test_masks = torch.Tensor()
for sent in sentences_test:
encoded_sent = tokenizer.encode_plus(sent, # Sentence to encode.
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
max_length=MAX_LEN, # Truncate all sentences.
pad_to_max_length=True,
return_attention_mask=True,
return_token_type_ids=False,
truncation=True,
return_tensors='pt') # return pytorch not tensorflow tensor
test_inputs = torch.cat((test_inputs, encoded_sent['input_ids'].float()), dim=0)
test_masks = torch.cat((test_masks, encoded_sent['attention_mask'].float()), dim=0)
test_inputs.to(device)
test_masks.to(device)
# for training data
train_data = TensorDataset(train_inputs, train_masks, train_labels)
train_sampler = RandomSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)
# for validation set.
validation_data = TensorDataset(validation_inputs, validation_masks, validation_labels)
validation_sampler = SequentialSampler(validation_data)
validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size)
def train(model, dataloader):
from transformers import get_linear_schedule_with_warmup
optimizer = AdamW(model.parameters(), lr=5e-5, eps=1e-8)
total_steps = len(dataloader) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=0, # Default value in run_glue.py
num_training_steps=total_steps)
model.train()
total_loss = 0
for step, batch in enumerate(dataloader):
if step % 2000 == 0 and not step == 0:
# Calculate elapsed time in minutes.
elapsed = format_time(time.time() - t0)
# Report progress.
print(' Batch {:>5,} of {:>5,}. Elapsed: {:}.'.format(step, len(train_dataloader), elapsed))
b_input_ids = batch[0].long().to(device)
b_input_mask = batch[1].long().to(device)
b_labels = batch[2].long().to(device)
optimizer.zero_grad()
loss, logit = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask,
labels=b_labels,
output_attentions = False,
#output_hidden_states=False,
)
total_loss += loss.item()
loss.backward()
# Clip the norm of the gradients to 1.0.
# This is to help prevent the "exploding gradients" problem.
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
train_loss_this_epoch = total_loss / len(dataloader)
print("")
print(" Average training loss: {0:.2f}".format(train_loss_this_epoch))
return train_loss_this_epoch
def validate(model, dataloader):
print(" === Validate function for multi-class ===")
model.eval()
valid_loss, f1_micro_total = 0, 0
for step, batch in enumerate(dataloader):
batch = tuple(t.to(device) for t in batch)
# Unpack the inputs from our dataloader
b_input_ids = batch[0].long()
b_input_mask = batch[1].long()
b_labels = batch[2].long()
with torch.no_grad():
loss, logits = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask, labels=b_labels)
softmax = torch.nn.functional.softmax(logits, dim=1)
prediction = softmax.argmax(dim=1).detach().cpu().numpy()
labels = b_labels.to('cpu').numpy()
f1_micro = f1_score(labels, prediction, average='micro', zero_division=1)
f1_micro_total += f1_micro
valid_loss += loss
return valid_loss / len(dataloader), f1_micro_total / len(dataloader)
# Report the final accuracy for this validation run.
def metrics(rounded_preds, label):
"""
preds (batch size, 6) before sigmoid
label (batch size, 6)
"""
#rounded_preds = torch.round(torch.sigmoid(preds)) # (batch size, 6)
pred_array = rounded_preds.cpu().detach().numpy()
label_array = label.cpu().detach().numpy()
#correct = (pred_array == label).float() # convert into float for division
#acc = correct.sum() / len(correct)
micro_f1 = f1_score(label_array, pred_array, average='micro', zero_division=1)
macro_f1 = f1_score(label_array, pred_array, average='macro', zero_division=1)
return micro_f1, macro_f1
'''
================== Training Loop =======================
'''
optimizer = AdamW(model.parameters(), lr=0.0005, weight_decay = 0.01, eps = 1e-6)
from transformers import get_linear_schedule_with_warmup
total_steps = len(train_dataloader) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps= int(total_steps*0.06), # Default value in run_glue.py
num_training_steps=total_steps)
best_valid_loss = float('inf')
loss_values = []
# For each epoch...
for epoch_i in range(0, epochs):
print("")
print('========== Epoch {:} / {:} =========='.format(epoch_i + 1, epochs))
t0 = time.time()
train_loss = train(model, train_dataloader)
print(" Training epcoh took: {:}".format(format_time(time.time() - t0)))
print("")
print("Running Validation...")
t0 = time.time()
valid_loss = validate(model, validation_dataloader)
print(" Validation took: {:}".format(format_time(time.time() - t0)))
#torch.save(model.state_dict(), str(args.resultpath) + resultname + '_model.pt')
print("")
print("Training complete!")
prediction_data = TensorDataset(test_inputs, test_masks, test_labels)
prediction_sampler = SequentialSampler(prediction_data)
prediction_dataloader = DataLoader(prediction_data, sampler=prediction_sampler, batch_size=batch_size, shuffle = False)
model.eval()
predictions = torch.Tensor().to(device)
def clean_dataset(df):
assert isinstance(df, pd.DataFrame), "df needs to be a pd.DataFrame"
df.dropna(inplace=True)
indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(1)
return df[indices_to_keep].astype(np.float64)
for batch in prediction_dataloader:
batch = tuple(t.to(device) for t in batch)
b_input_ids, b_input_mask, b_labels = batch
with torch.no_grad():
# Forward pass, calculate logit predictions, 没有给label, 所以不outputloss
outputs = model(b_input_ids.long(), token_type_ids=None,
attention_mask=b_input_mask) # return: loss(only if label is given), logi
softmax = torch.nn.functional.softmax(outputs, dim=1)
prediction = softmax.argmax(dim=1)
predictions = torch.cat((predictions, prediction.float()))
# true_labels = torch.cat((true_labels, b_labels.float()))
print(' DONE.')
predictions_np = predictions.cpu().tolist()
test['prediction'] = predictions_np
test['label_encoded'] = labels_test
f1_micro = f1_score(test['label_encoded'], test['prediction'], average='micro')
f1_macro = f1_score(test['label_encoded'], test['prediction'], average='macro')
print('RESULTS -----------')
print('f1_micro:', f1_micro, 'f1_macro:', f1_macro)
print(classification_report(test['label_encoded'], test['prediction'], zero_division=1, digits=4))
test.to_csv('IMBD_Roberta.csv')