-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiclass.py
478 lines (367 loc) · 17.2 KB
/
multiclass.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
'''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
import argparse
import datetime
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
parser.add_argument('--data', type=str) #, choices=['multi-label', 'wassem', 'AG10K', 'tweet50k']
# 1
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 = 100
if args.data == 'AG10K':
NUM_LABELS = 3
else:
NUM_LABELS = 4
batch_size = 16
epochs = args.epochs
train_path = str(args.data) + '_train.csv'
test_path = str(args.data) + '_test.csv'
validation_path = str(args.data) + '_validation.csv'
if args.testing:
train = pd.read_csv(train_path).sample(20)
test = pd.read_csv(test_path).sample(20).reset_index()
validation = pd.read_csv(validation_path).dropna()
elif args.running:
train = pd.read_csv(train_path)
test = pd.read_csv(test_path).reset_index()
validation = pd.read_csv(validation_path).dropna()
else:
print('need to define parameter, it is "--running" or "--testing"')
print('training dataset size: ', len(train))
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
# AG10K and tweet50k need to convert their labels to numbers
if args.data == 'AG10K':
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(["NAG", "CAG", "OAG"])
labels_train = le.transform(labels_train)
labels_test = le.transform(labels_test)
labels_validation = le.transform(labels_validation)
elif args.data == 'tweet50k':
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(['abusive', 'normal', 'hateful', 'spam'])
labels_train = le.transform(labels_train)
labels_test = le.transform(labels_test)
labels_validation = le.transform(labels_validation)
else:
pass
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.BertModel == 'RoBerta':
from transformers import RobertaTokenizer, RobertaForSequenceClassification, RobertaConfig
tokenizer = RobertaTokenizer.from_pretrained('roberta-base', do_lower_case=False)
model = RobertaForSequenceClassification.from_pretrained('roberta-base',
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=False)
print(' ')
print('using Roberta:')
elif args.BertModel == 'Bert':
from transformers import BertTokenizer, BertForSequenceClassification
tokenizer = BertTokenizer.from_pretrained('bert-base-cased', do_lower_case=False)
model = BertForSequenceClassification.from_pretrained('bert-base-cased',
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=False)
print(' ')
print('using Bert:')
elif args.BertModel == 'XLM':
from transformers import XLMTokenizer, XLMForSequenceClassification, XLMConfig
tokenizer = XLMTokenizer.from_pretrained('xlm-mlm-enfr-1024', do_lower_case=True)
model = XLMForSequenceClassification.from_pretrained('xlm-mlm-enfr-1024',
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=False,
)
print(' ')
print('using XLM:')
elif args.BertModel == 'gpt2':
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_multiclass_clf
model = GPT2_multiclass_clf.from_pretrained('gpt2',
num_labels=NUM_LABELS,
output_attentions=False,
output_hidden_states=False,
use_cache=False,
)
print(' ')
print('using GPT2:', model_name)
else:
print('defined multi-class classification but the model fails settingup')
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):
model.train()
total_loss = 0
for step, batch in enumerate(dataloader):
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()
#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):
pred_array = rounded_preds.cpu().detach().numpy()
label_array = label.cpu().detach().numpy()
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
def flat_accuracy(preds, labels):
pred_flat = np.argmax(preds, axis=1).flatten()
labels_flat = labels.flatten()
return np.sum(pred_flat == labels_flat) / len(labels_flat)
'''
================== 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)
resultname = str(args.BertModel) + '_' + str(args.data)
best_valid_loss = float('inf')
loss_values = []
# For each epoch...
import random
seed_val = 42
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
# Store the average loss after each epoch so we can plot them.
# loss_fn = nn.CrossEntropyLoss
loss_values = []
# For each epoch...
for epoch_i in range(0, epochs):
print("")
print('======== Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
t0 = time.time()
total_loss = 0 # 计算每epoch的总loss
model.train()
# For each batch of training data...
for step, batch in enumerate(train_dataloader):
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()
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask,
labels=b_labels)
# The call to `model` always returns a tuple, so we need to pull the
# loss value out of the tuple. loss only available when labels are given (e.g. in train mode)
loss = outputs[0]
total_loss += loss.item()
# Perform a backward pass to calculate the gradients.
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()
avg_train_loss = total_loss / len(train_dataloader)
loss_values.append(avg_train_loss)
print("")
print(" Average training loss: {0:.2f}".format(avg_train_loss))
print(" Training epcoh took: {:}".format(format_time(time.time() - t0)))
# ========================================
# Validation
# ========================================
# each training epoch, measure validation set.
print("")
print("Running Validation...")
t0 = time.time()
# Put the model in evaluation mode--the dropout layers behave differently
# during evaluation.
model.eval()
# Tracking variables
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0
# Evaluate data for one epoch
for batch in validation_dataloader:
# Add batch to GPU
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():
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask)
# Get the "logits" output by the model. The "logits" are the output values prior to applying an activation function like the softmax.
logits = outputs[0]
# Move logits and labels to CPU
logits = logits.detach().cpu().numpy()
label_ids = b_labels.to('cpu').numpy()
# Calculate the accuracy for this batch of test sentences.
tmp_eval_accuracy = flat_accuracy(logits, label_ids)
# Accumulate the total accuracy.
eval_accuracy += tmp_eval_accuracy
# Track the number of batches
nb_eval_steps += 1
# Report the final accuracy for this validation run.
print(" Accuracy: {0:.2f}".format(eval_accuracy / nb_eval_steps))
print(" Validation took: {:}".format(format_time(time.time() - t0)))
print("")
print("Training complete!")
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)
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():
output = model(b_input_ids.long(),
token_type_ids=None,
attention_mask=b_input_mask.long(),
output_attentions = False,
)
softmax = torch.nn.functional.softmax(output[0], 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(str(args.data))
print('f1_micro:', f1_micro)
print('f1_macro:', f1_macro)
print(classification_report(test['label_encoded'], test['prediction'], zero_division=1, digits=4))
test.to_csv(str(resultname) + '_result.csv')