-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_classifier.py
907 lines (746 loc) · 32.5 KB
/
run_classifier.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import csv
import os
import logging
import argparse
import random
from tqdm import tqdm, trange
os.environ["CUDA_VISIBLE_DEVICES"] = "5"
import numpy as np
import torch
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from torch.utils.data.distributed import DistributedSampler
import tokenization
from transformers import BertConfig
from model import BertForSequenceClassification
from pytorch_pretrained_bert.optimization import BertAdam
# from modeling import BertConfig, BertForSequenceClassification
# from optimization import BERTAdam as BertAdam
import json
import re
n_class = 1
reverse_order = False
sa_step = False
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 InputExample(object):
"""A single training/test example for simple sequence classification."""
def __init__(self, guid, text_a, text_b=None, label=None, text_c=None):
"""Constructs a InputExample.
Args:
guid: Unique id for the example.
text_a: string. The untokenized text of the first sequence. For single
sequence tasks, only this sequence must be specified.
text_b: (Optional) string. The untokenized text of the second sequence.
Only must be specified for sequence pair tasks.
label: (Optional) string. The label of the example. This should be
specified for train and dev examples, but not for test examples.
"""
self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.text_c = text_c
self.label = label
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, input_ids, input_mask, segment_ids, label_id):
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
self.label_id = label_id
class DataProcessor(object):
"""Base class for data converters for sequence classification data sets."""
def get_train_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the train set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
@classmethod
def _read_tsv(cls, input_file, quotechar=None):
"""Reads a tab separated value file."""
with open(input_file, "r") as f:
reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
lines = []
for line in reader:
lines.append(line)
return lines
class bertProcessor(DataProcessor): #bert
def __init__(self):
random.seed(42)
self.D = [[], [], []]
for sid in range(3):
with open("data/"+["train.json", "dev.json", "test.json"][sid], "r", encoding="utf8") as f:
data = json.load(f)
if sid == 0:
random.shuffle(data)
for i in range(len(data)):
for j in range(len(data[i][1])):
rid = []
for k in range(36):
if k+1 in data[i][1][j]["rid"]:
rid += [1]
else:
rid += [0]
d = ['\n'.join(data[i][0]).lower(),
data[i][1][j]["x"].lower(),
data[i][1][j]["y"].lower(),
rid]
self.D[sid] += [d]
logger.info(str(len(self.D[0])) + "," + str(len(self.D[1])) + "," + str(len(self.D[2])))
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[0], "train")
def get_test_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[2], "test")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[1], "dev")
def get_labels(self):
"""See base class."""
return [str(x) for x in range(2)]
def _create_examples(self, data, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, d) in enumerate(data):
guid = "%s-%s" % (set_type, i)
text_a = tokenization.convert_to_unicode(data[i][0])
text_b = tokenization.convert_to_unicode(data[i][1])
text_c = tokenization.convert_to_unicode(data[i][2])
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=data[i][3], text_c=text_c))
return examples
class bertf1cProcessor(DataProcessor): #bert (conversational f1)
def __init__(self):
random.seed(42)
self.D = [[], [], []]
for sid in range(1, 3):
with open("data/"+["dev.json", "test.json"][sid-1], "r", encoding="utf8") as f:
data = json.load(f)
for i in range(len(data)):
for j in range(len(data[i][1])):
rid = []
for k in range(36):
if k+1 in data[i][1][j]["rid"]:
rid += [1]
else:
rid += [0]
for l in range(1, len(data[i][0])+1):
d = ['\n'.join(data[i][0][:l]).lower(),
data[i][1][j]["x"].lower(),
data[i][1][j]["y"].lower(),
rid]
self.D[sid] += [d]
logger.info(str(len(self.D[0])) + "," + str(len(self.D[1])) + "," + str(len(self.D[2])))
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[0], "train")
def get_test_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[2], "test")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[1], "dev")
def get_labels(self):
"""See base class."""
return [str(x) for x in range(2)]
def _create_examples(self, data, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, d) in enumerate(data):
guid = "%s-%s" % (set_type, i)
text_a = tokenization.convert_to_unicode(data[i][0])
text_b = tokenization.convert_to_unicode(data[i][1])
text_c = tokenization.convert_to_unicode(data[i][2])
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=data[i][3], text_c=text_c))
return examples
class bertsProcessor(DataProcessor): #bert_s
def __init__(self):
def is_speaker(a):
a = a.split()
return len(a) == 2 and a[0] == "speaker" and a[1].isdigit()
def rename(d, x, y):
unused = ["[unused1]", "[unused2]"]
a = []
if is_speaker(x):
a += [x]
else:
a += [None]
if x != y and is_speaker(y):
a += [y]
else:
a += [None]
for i in range(len(a)):
if a[i] is None:
continue
d = d.replace(a[i] + ":", unused[i] + " :")
if x == a[i]:
x = unused[i]
if y == a[i]:
y = unused[i]
return d, x, y
random.seed(42)
self.D = [[], [], []]
for sid in range(3):
with open("data/"+["train_b.json", "dev_b.json", "test_b.json"][sid], "r", encoding="utf8") as f:
data = json.load(f)
if sid == 0:
random.shuffle(data)
for i in range(len(data)):
for j in range(len(data[i][1])):
rid = []
for k in range(36):
if k+1 in data[i][1][j]["rid"]:
rid += [1]
else:
rid += [0]
d, h, t = rename('\n'.join(data[i][0]).lower(), data[i][1][j]["x"].lower(), data[i][1][j]["y"].lower())
d = [d,
h,
t,
rid]
self.D[sid] += [d]
logger.info(str(len(self.D[0])) + "," + str(len(self.D[1])) + "," + str(len(self.D[2])))
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[0], "train")
def get_test_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[2], "test")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[1], "dev")
def get_labels(self):
"""See base class."""
return [str(x) for x in range(2)]
def _create_examples(self, data, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, d) in enumerate(data):
guid = "%s-%s" % (set_type, i)
text_a = tokenization.convert_to_unicode(data[i][0])
text_b = tokenization.convert_to_unicode(data[i][1])
text_c = tokenization.convert_to_unicode(data[i][2])
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=data[i][3], text_c=text_c))
return examples
class bertsf1cProcessor(DataProcessor): #bert_s (conversational f1)
def __init__(self):
def is_speaker(a):
a = a.split()
return (len(a) == 2 and a[0] == "speaker" and a[1].isdigit())
def rename(d, x, y):
unused = ["[unused1]", "[unused2]"]
a = []
if is_speaker(x):
a += [x]
else:
a += [None]
if x != y and is_speaker(y):
a += [y]
else:
a += [None]
for i in range(len(a)):
if a[i] is None:
continue
d = d.replace(a[i] + ":", unused[i] + " :")
if x == a[i]:
x = unused[i]
if y == a[i]:
y = unused[i]
return d, x, y
random.seed(42)
self.D = [[], [], []]
for sid in range(1, 3):
with open("data/"+["dev.json", "test.json"][sid-1], "r", encoding="utf8") as f:
data = json.load(f)
for i in range(len(data)):
for j in range(len(data[i][1])):
rid = []
for k in range(36):
if k+1 in data[i][1][j]["rid"]:
rid += [1]
else:
rid += [0]
for l in range(1, len(data[i][0])+1):
d, h, t = rename('\n'.join(data[i][0][:l]).lower(), data[i][1][j]["x"].lower(), data[i][1][j]["y"].lower())
d = [d,
h,
t,
rid]
self.D[sid] += [d]
logger.info(str(len(self.D[0])) + "," + str(len(self.D[1])) + "," + str(len(self.D[2])))
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[0], "train")
def get_test_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[2], "test")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self.D[1], "dev")
def get_labels(self):
"""See base class."""
return [str(x) for x in range(2)]
def _create_examples(self, data, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, d) in enumerate(data):
guid = "%s-%s" % (set_type, i)
text_a = tokenization.convert_to_unicode(data[i][0])
text_b = tokenization.convert_to_unicode(data[i][1])
text_c = tokenization.convert_to_unicode(data[i][2])
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=data[i][3], text_c=text_c))
return examples
def tokenize(text, tokenizer):
D = ['[unused1]', '[unused2]']
text_tokens = []
textraw = [text]
for delimiter in D:
ntextraw = []
for i in range(len(textraw)):
t = textraw[i].split(delimiter)
for j in range(len(t)):
ntextraw += [t[j]]
if j != len(t)-1:
ntextraw += [delimiter]
textraw = ntextraw
text = []
for t in textraw:
if t in ['[unused1]', '[unused2]']:
text += [t]
else:
tokens = tokenizer.tokenize(t)
for tok in tokens:
text += [tok]
return text
def convert_examples_to_features(examples, label_list, max_seq_length, tokenizer):
"""Loads a data file into a list of `InputBatch`s."""
print("#examples", len(examples))
features = [[]]
for (ex_index, example) in enumerate(examples):
tokens_a = tokenize(example.text_a, tokenizer)
tokens_b = tokenize(example.text_b, tokenizer)
tokens_c = tokenize(example.text_c, tokenizer)
_truncate_seq_tuple(tokens_a, tokens_b, tokens_c, max_seq_length - 4)
tokens_b = tokens_b + ["[SEP]"] + tokens_c
tokens = []
segment_ids = []
tokens.append("[CLS]")
segment_ids.append(0)
for token in tokens_a:
tokens.append(token)
segment_ids.append(0)
tokens.append("[SEP]")
segment_ids.append(0)
for token in tokens_b:
tokens.append(token)
segment_ids.append(1)
tokens.append("[SEP]")
segment_ids.append(1)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
assert len(input_ids) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
label_id = example.label
if ex_index < 1:
logger.info("*** Example ***")
logger.info("guid: %s" % (example.guid))
logger.info("tokens: %s" % " ".join(
[tokenization.printable_text(x) for x in tokens]))
logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
logger.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
logger.info(
"segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
features[-1].append(
InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
label_id=label_id))
if len(features[-1]) == n_class:
features.append([])
if len(features[-1]) == 0:
features = features[:-1]
print('#features', len(features))
return features
def _truncate_seq_tuple(tokens_a, tokens_b, tokens_c, max_length):
"""Truncates a sequence tuple in place to the maximum length."""
# This is a simple heuristic which will always truncate the longer sequence
# one token at a time. This makes more sense than truncating an equal percent
# of tokens from each, since if one sequence is very short then each token
# that's truncated likely contains more information than a longer sequence.
while True:
total_length = len(tokens_a) + len(tokens_b) + len(tokens_c)
if total_length <= max_length:
break
if len(tokens_a) >= len(tokens_b) and len(tokens_a) >= len(tokens_c):
tokens_a.pop()
elif len(tokens_b) >= len(tokens_a) and len(tokens_b) >= len(tokens_c):
tokens_b.pop()
else:
tokens_c.pop()
def accuracy(out, labels):
out = out.reshape(-1)
out = 1 / (1 + np.exp(-out))
return np.sum((out > 0.5) == (labels > 0.5)) / 36
def f1_eval(logits, features):
def getpred(result, T1 = 0.5, T2 = 0.4):
ret = []
for i in range(len(result)):
r = []
maxl, maxj = -1, -1
for j in range(len(result[i])):
if result[i][j] > T1:
r += [j]
if result[i][j] > maxl:
maxl = result[i][j]
maxj = j
if len(r) == 0:
if maxl <= T2:
r = [36]
else:
r += [maxj]
ret += [r]
return ret
def geteval(devp, data):
correct_sys, all_sys = 0, 0
correct_gt = 0
for i in range(len(data)):
for id in data[i]:
if id != 36:
correct_gt += 1
if id in devp[i]:
correct_sys += 1
for id in devp[i]:
if id != 36:
all_sys += 1
precision = 1 if all_sys == 0 else correct_sys/all_sys
recall = 0 if correct_gt == 0 else correct_sys/correct_gt
f_1 = 2*precision*recall/(precision+recall) if precision+recall != 0 else 0
return f_1, precision, recall
logits = np.asarray(logits)
logits = list(1 / (1 + np.exp(-logits)))
labels = []
for f in features:
label = []
assert(len(f[0].label_id) == 36)
for i in range(36):
if f[0].label_id[i] == 1:
label += [i]
if len(label) == 0:
label = [36]
labels += [label]
assert(len(labels) == len(logits))
bestT2 = bestf_1 = 0
for T2 in range(51):
devp = getpred(logits, T2=T2/100.)
f_1, p, r = geteval(devp, labels)
if f_1 > bestf_1:
bestf_1 = f_1
bestT2 = T2/100.
print(p, r, f_1)
return bestf_1, bestT2
def main():
import config as args
processors = {
"bert": bertProcessor,
"bertf1c": bertf1cProcessor,
"berts": bertsProcessor,
"bertsf1c": bertsf1cProcessor,
}
device = torch.device("cuda")
n_gpu = torch.cuda.device_count()
logger.info("device %s n_gpu %d", device, n_gpu)
# args.train_batch_size = int(args.train_batch_size / args.gradient_accumulation_steps)
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
bert_config = BertConfig.from_json_file(args.bert_config_file)
if args.max_seq_length > bert_config.max_position_embeddings:
raise ValueError(
"Cannot use sequence length {} because the BERT model was only trained up to sequence length {}".format(
args.max_seq_length, bert_config.max_position_embeddings))
if os.path.exists(args.output_dir) and 'model.pt' in os.listdir(args.output_dir):
if args.do_train and not args.resume:
raise ValueError("Output directory ({}) already exists and is not empty.".format(args.output_dir))
else:
os.makedirs(args.output_dir, exist_ok=True)
task_name = args.task_name.lower()
if task_name not in processors:
raise ValueError("Task not found: %s" % (task_name))
processor = processors[task_name]()
label_list = processor.get_labels()
print(label_list)
tokenizer = tokenization.FullTokenizer(
vocab_file=args.vocab_file, do_lower_case=args.do_lower_case)
train_examples = None
num_train_steps = None
if args.do_train:
train_examples = processor.get_train_examples(args.data_dir)
num_train_steps = int(
len(train_examples) / n_class / args.train_batch_size / args.gradient_accumulation_steps * args.num_train_epochs)
model = BertForSequenceClassification(args.bert_dir, 1)
model.to(device)
param_optimizer = list(model.named_parameters())
no_decay = ['bias', 'gamma', 'beta']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if n not in no_decay], 'weight_decay_rate': 0.01},
{'params': [p for n, p in param_optimizer if n in no_decay], 'weight_decay_rate': 0.0}
]
optimizer = BertAdam(optimizer_grouped_parameters,
lr=args.learning_rate,
warmup=args.warmup_proportion,
t_total=num_train_steps)
global_step = 0
if args.do_eval:
eval_examples = processor.get_test_examples(args.data_dir) #### for test datasets
eval_features = convert_examples_to_features(
eval_examples, label_list, args.max_seq_length, tokenizer)
input_ids = []
input_mask = []
segment_ids = []
label_id = []
for f in eval_features:
input_ids.append([])
input_mask.append([])
segment_ids.append([])
for i in range(n_class):
input_ids[-1].append(f[i].input_ids)
input_mask[-1].append(f[i].input_mask)
segment_ids[-1].append(f[i].segment_ids)
label_id.append([f[0].label_id])
all_input_ids = torch.tensor(input_ids, dtype=torch.long)
all_input_mask = torch.tensor(input_mask, dtype=torch.long)
all_segment_ids = torch.tensor(segment_ids, dtype=torch.long)
all_label_ids = torch.tensor(label_id, dtype=torch.float)
eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
eval_sampler = SequentialSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
if args.do_train:
best_metric = 0
train_features = convert_examples_to_features(
train_examples, label_list, args.max_seq_length, tokenizer)
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_examples))
logger.info(" Batch size = %d", args.train_batch_size)
logger.info(" Num steps = %d", num_train_steps)
input_ids = []
input_mask = []
segment_ids = []
label_id = []
for f in train_features:
input_ids.append([])
input_mask.append([])
segment_ids.append([])
for i in range(n_class):
input_ids[-1].append(f[i].input_ids)
input_mask[-1].append(f[i].input_mask)
segment_ids[-1].append(f[i].segment_ids)
label_id.append([f[0].label_id])
all_input_ids = torch.tensor(input_ids, dtype=torch.long)
all_input_mask = torch.tensor(input_mask, dtype=torch.long)
all_segment_ids = torch.tensor(segment_ids, dtype=torch.long)
all_label_ids = torch.tensor(label_id, dtype=torch.float)
train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
train_sampler = RandomSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)
for _ in trange(int(args.num_train_epochs), desc="Epoch"):
model.train()
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
for step, batch in enumerate(tqdm(train_dataloader, desc="Iteration")):
batch = tuple(t.to(device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
loss, _ = model(input_ids, segment_ids, input_mask, label_ids, 1)
loss = loss.mean()
# if args.gradient_accumulation_steps > 1:
# loss = loss / args.gradient_accumulation_steps
loss.backward()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
#if (step + 1) % args.gradient_accumulation_steps == 0:
optimizer.step()
model.zero_grad()
global_step += 1
model.eval()
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0
logits_all = []
for input_ids, input_mask, segment_ids, label_ids in eval_dataloader:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
segment_ids = segment_ids.to(device)
label_ids = label_ids.to(device)
with torch.no_grad():
tmp_eval_loss, logits = model(input_ids, segment_ids, input_mask, label_ids, 1)
logits = logits.detach().cpu().numpy()
label_ids = label_ids.to('cpu').numpy()
for i in range(len(logits)):
logits_all += [logits[i]]
tmp_eval_accuracy = accuracy(logits, label_ids.reshape(-1))
eval_loss += tmp_eval_loss.mean().item()
eval_accuracy += tmp_eval_accuracy
nb_eval_examples += input_ids.size(0)
nb_eval_steps += 1
eval_loss = eval_loss / nb_eval_steps
eval_accuracy = eval_accuracy / nb_eval_examples
if args.do_train:
result = {'eval_loss': eval_loss,
'global_step': global_step,
'loss': tr_loss/nb_tr_steps}
else:
result = {'eval_loss': eval_loss}
eval_f1, eval_T2 = f1_eval(logits_all, eval_features)
result["f1"] = eval_f1
result["T2"] = eval_T2
logger.info("***** Eval results *****")
for key in sorted(result.keys()):
logger.info(" %s = %s", key, str(result[key]))
if eval_f1 >= best_metric:
torch.save(model.state_dict(), os.path.join(args.output_dir, "model_best.pt"))
best_metric = eval_f1
model.load_state_dict(torch.load(os.path.join(args.output_dir, "model_best.pt")))
torch.save(model.state_dict(), os.path.join(args.output_dir, "model.pt"))
model.load_state_dict(torch.load(os.path.join(args.output_dir, "model.pt")))
if args.do_eval:
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_examples))
logger.info(" Batch size = %d", args.eval_batch_size)
model.eval()
eval_loss = 0
nb_eval_steps, nb_eval_examples = 0, 0
logits_all = []
for input_ids, input_mask, segment_ids, label_ids in eval_dataloader:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
segment_ids = segment_ids.to(device)
label_ids = label_ids.to(device)
with torch.no_grad():
tmp_eval_loss, logits = model(input_ids, segment_ids, input_mask, label_ids, 1)
logits = logits.detach().cpu().numpy()
label_ids = label_ids.to('cpu').numpy()
for i in range(len(logits)):
logits_all += [logits[i]]
eval_loss += tmp_eval_loss.mean().item()
nb_eval_examples += input_ids.size(0)
nb_eval_steps += 1
eval_loss = eval_loss / nb_eval_steps
if args.do_train:
result = {'eval_loss': eval_loss,
'global_step': global_step,
'loss': tr_loss/nb_tr_steps}
else:
result = {'eval_loss': eval_loss}
output_eval_file = os.path.join(args.output_dir, "eval_results_dev.txt")
with open(output_eval_file, "w") as writer:
logger.info("***** Eval results *****")
for key in sorted(result.keys()):
logger.info(" %s = %s", key, str(result[key]))
writer.write("%s = %s\n" % (key, str(result[key])))
output_eval_file = os.path.join(args.output_dir, "logits_dev.txt")
with open(output_eval_file, "w") as f:
for i in range(len(logits_all)):
for j in range(len(logits_all[i])):
f.write(str(logits_all[i][j]))
if j == len(logits_all[i])-1:
f.write("\n")
else:
f.write(" ")
eval_examples = processor.get_test_examples(args.data_dir)
eval_features = convert_examples_to_features(
eval_examples, label_list, args.max_seq_length, tokenizer)
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_examples))
logger.info(" Batch size = %d", args.eval_batch_size)
input_ids = []
input_mask = []
segment_ids = []
label_id = []
for f in eval_features:
input_ids.append([])
input_mask.append([])
segment_ids.append([])
for i in range(n_class):
input_ids[-1].append(f[i].input_ids)
input_mask[-1].append(f[i].input_mask)
segment_ids[-1].append(f[i].segment_ids)
label_id.append([f[0].label_id])
all_input_ids = torch.tensor(input_ids, dtype=torch.long)
all_input_mask = torch.tensor(input_mask, dtype=torch.long)
all_segment_ids = torch.tensor(segment_ids, dtype=torch.long)
all_label_ids = torch.tensor(label_id, dtype=torch.float)
eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
eval_sampler = SequentialSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
model.eval()
eval_loss = 0
nb_eval_steps, nb_eval_examples = 0, 0
logits_all = []
for input_ids, input_mask, segment_ids, label_ids in eval_dataloader:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
segment_ids = segment_ids.to(device)
label_ids = label_ids.to(device)
with torch.no_grad():
tmp_eval_loss, logits = model(input_ids, segment_ids, input_mask, label_ids, 1)
logits = logits.detach().cpu().numpy()
label_ids = label_ids.to('cpu').numpy()
for i in range(len(logits)):
logits_all += [logits[i]]
eval_loss += tmp_eval_loss.mean().item()
nb_eval_examples += input_ids.size(0)
nb_eval_steps += 1
eval_loss = eval_loss / nb_eval_steps
if args.do_train:
result = {'eval_loss': eval_loss,
'global_step': global_step,
'loss': tr_loss/nb_tr_steps}
else:
result = {'eval_loss': eval_loss}
output_eval_file = os.path.join(args.output_dir, "eval_results_test.txt")
with open(output_eval_file, "w") as writer:
logger.info("***** Eval results *****")
for key in sorted(result.keys()):
logger.info(" %s = %s", key, str(result[key]))
writer.write("%s = %s\n" % (key, str(result[key])))
output_eval_file = os.path.join(args.output_dir, "logits_test.txt")
with open(output_eval_file, "w") as f:
for i in range(len(logits_all)):
for j in range(len(logits_all[i])):
f.write(str(logits_all[i][j]))
if j == len(logits_all[i])-1:
f.write("\n")
else:
f.write(" ")
if __name__ == "__main__":
main()