-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_classifier.py
791 lines (642 loc) · 32.1 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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import csv
import json
import os
import time
import numpy as np
import tensorflow as tf
from bert import modeling
from bert import optimization
from bert import tokenization
MIN_FLOAT = -1e30
flags = tf.flags
FLAGS = flags.FLAGS
flags.DEFINE_string("bert_config_file", None, "The config json file corresponding to the pre-trained BERT model.")
flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.")
flags.DEFINE_string("data_dir", None, "The input data dir. Should contain the .json files for the task.")
flags.DEFINE_string("task_name", None, "The name of the task to train.")
flags.DEFINE_string("output_dir", None, "The output directory where the model checkpoints will be written.")
flags.DEFINE_string("export_dir", None, "The export directory where the saved model will be written.")
flags.DEFINE_string("init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).")
flags.DEFINE_bool("do_lower_case", True, "Whether to lower case the input text. True for uncased models and False for cased models.")
flags.DEFINE_integer("random_seed", 100, "Random seed for weight initialzation.")
flags.DEFINE_string("predict_tag", None, "Predict tag for predict result tracking.")
flags.DEFINE_integer(
"max_seq_length", 128,
"The maximum total input sequence length after WordPiece tokenization. "
"Sequences longer than this will be truncated, and sequences shorter than this will be padded.")
flags.DEFINE_bool("do_train", False, "Whether to run training.")
flags.DEFINE_bool("do_eval", False, "Whether to run evaluation.")
flags.DEFINE_bool("do_predict", False, "Whether to run prediction.")
flags.DEFINE_bool("do_export", False, "Whether to run exporting.")
flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.")
flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.")
flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predict.")
flags.DEFINE_float("learning_rate", 5e-5, "The initial learning rate for Adam.")
flags.DEFINE_float("num_train_epochs", 3.0, "Total number of training epochs to perform.")
flags.DEFINE_float("warmup_proportion", 0.1, "Proportion of training to perform linear learning rate warmup for.")
flags.DEFINE_integer("save_checkpoints_steps", 1000, "How often to save the model checkpoint.")
flags.DEFINE_integer("iterations_per_loop", 1000, "How many steps to make in each estimator call.")
flags.DEFINE_bool("use_tpu", False, "Whether to use TPU or GPU/CPU.")
flags.DEFINE_integer("num_tpu_cores", 8,"Only used if `use_tpu` is True. Total number of TPU cores to use.")
flags.DEFINE_string("master", None, "[Optional] TensorFlow master URL.")
flags.DEFINE_string(
"tpu_name", None,
"The Cloud TPU to use for training. This should be either the name "
"used when creating the Cloud TPU, or a grpc://ip.address.of.tpu:8470 url.")
flags.DEFINE_string(
"tpu_zone", None,
"[Optional] GCE zone where the Cloud TPU is located in. If not "
"specified, we will attempt to automatically detect the GCE project from metadata.")
flags.DEFINE_string(
"gcp_project", None,
"[Optional] Project name for the Cloud TPU-enabled project. If not "
"specified, we will attempt to automatically detect the GCE project from metadata.")
class InputExample(object):
"""A single training/test example for simple sequence classification."""
def __init__(self,
guid,
text,
sent_label=None):
"""Constructs a InputExample.
Args:
guid: Unique id for the example.
text: string. The untokenized text of the first sequence. For single
sequence tasks, only this sequence must be specified.
sent_label: (Optional) string. The sentence label of the example. This should be
specified for train and dev examples, but not for test examples.
"""
self.guid = guid
self.text = text
self.sent_label = sent_label
class PaddingInputExample(object):
"""Fake example so the num input examples is a multiple of the batch size.
When running eval/predict on the TPU, we need to pad the number of examples
to be a multiple of the batch size, because the TPU requires a fixed batch
size. The alternative is to drop the last batch, which is bad because it means
the entire output data won't be generated.
We use this class instead of `None` because treating `None` as padding
battches could cause silent errors.
"""
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self,
input_ids,
input_masks,
segment_ids,
sent_label_id):
self.input_ids = input_ids
self.input_masks = input_masks
self.segment_ids = segment_ids
self.sent_label_id = sent_label_id
class ClassificationProcessor(object):
"""Processor for the classification data set."""
def __init__(self,
data_dir,
task_name):
self.data_dir = data_dir
self.task_name = task_name
def get_train_examples(self):
"""Gets a collection of `InputExample`s for the train set."""
data_path = os.path.join(self.data_dir, "train-{0}".format(self.task_name), "train-{0}.json".format(self.task_name))
data_list = self._read_json(data_path)
example_list = self._get_example(data_list)
return example_list
def get_dev_examples(self):
"""Gets a collection of `InputExample`s for the dev set."""
data_path = os.path.join(self.data_dir, "dev-{0}".format(self.task_name), "dev-{0}.json".format(self.task_name))
data_list = self._read_json(data_path)
example_list = self._get_example(data_list)
return example_list
def get_test_examples(self):
"""Gets a collection of `InputExample`s for the test set."""
data_path = os.path.join(self.data_dir, "test-{0}".format(self.task_name), "test-{0}.json".format(self.task_name))
data_list = self._read_json(data_path)
example_list = self._get_example(data_list)
return example_list
def get_sent_labels(self):
"""Gets the list of sentence labels for this data set."""
data_path = os.path.join(self.data_dir, "resource", "sent_label.vocab")
sent_labels = self._read_text(data_path)
return sent_labels
def _read_text(self,
data_path):
if os.path.exists(data_path):
with open(data_path, "rb") as file:
data_list = []
for line in file:
data_list.append(line.decode("utf-8").strip())
return data_list
else:
raise FileNotFoundError("data path not found")
def _read_json(self,
data_path):
if os.path.exists(data_path):
with open(data_path, "r") as file:
data_list = json.load(file)
return data_list
else:
raise FileNotFoundError("data path not found")
def _get_example(self,
data_list):
example_list = []
for data in data_list:
guid = data["id"]
text = tokenization.convert_to_unicode(data["text"])
sent_label = tokenization.convert_to_unicode(data["sent_label"])
example = InputExample(guid=guid, text=text, sent_label=sent_label)
example_list.append(example)
return example_list
def convert_single_example(ex_index,
example,
sent_label_list,
max_seq_length,
tokenizer):
"""Converts a single `InputExample` into a single `InputFeatures`."""
if isinstance(example, PaddingInputExample):
return InputFeatures(
input_ids=[0] * max_seq_length,
input_masks=[0] * max_seq_length,
segment_ids=[0] * max_seq_length,
sent_label_id=0)
sent_label_map = {}
for (i, sent_label) in enumerate(sent_label_list):
sent_label_map[sent_label] = i
tokens = tokenizer.tokenize(example.text)
if len(tokens) > max_seq_length - 2:
tokens = tokens[0:(max_seq_length - 2)]
# The convention in BERT is:
# (a) For sequence pairs:
# tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
# type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1
# (b) For single sequences:
# tokens: [CLS] the dog is hairy . [SEP]
# type_ids: 0 0 0 0 0 0 0
#
# Where "type_ids" are used to indicate whether this is the first
# sequence or the second sequence. The embedding vectors for `type=0` and
# `type=1` were learned during pre-training and are added to the wordpiece
# embedding vector (and position vector). This is not *strictly* necessary
# since the [SEP] token unambiguously separates the sequences, but it makes
# it easier for the model to learn the concept of sequences.
#
# For classification tasks, the first vector (corresponding to [CLS]) is
# used as the "sentence vector". Note that this only makes sense because
# the entire model is fine-tuned.
input_tokens = []
segment_ids = []
sent_label_id = sent_label_map[example.sent_label]
input_tokens.append("[CLS]")
segment_ids.append(0)
for token in tokens:
input_tokens.append(token)
segment_ids.append(0)
input_tokens.append("[SEP]")
segment_ids.append(0)
input_ids = tokenizer.convert_tokens_to_ids(input_tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real tokens are attended to.
input_masks = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_masks.append(0)
segment_ids.append(0)
assert len(input_ids) == max_seq_length
assert len(input_masks) == max_seq_length
assert len(segment_ids) == max_seq_length
if ex_index < 5:
tf.logging.info("*** Example ***")
tf.logging.info("guid: %s" % (example.guid))
tf.logging.info("tokens: %s" % " ".join([tokenization.printable_text(x) for x in tokens]))
tf.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
tf.logging.info("input_masks: %s" % " ".join([str(x) for x in input_masks]))
tf.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
tf.logging.info("sent_label_id: %s" % str(sent_label_id))
feature = InputFeatures(
input_ids=input_ids,
input_masks=input_masks,
segment_ids=segment_ids,
sent_label_id=sent_label_id)
return feature
def convert_examples_to_features(examples,
sent_label_list,
max_seq_length,
tokenizer):
"""Convert a set of `InputExample`s to a list of `InputFeatures`."""
features = []
for (ex_index, example) in enumerate(examples):
if ex_index % 10000 == 0:
tf.logging.info("Writing example %d of %d" % (ex_index, len(examples)))
feature = convert_single_example(ex_index, example, sent_label_list, max_seq_length, tokenizer)
features.append(feature)
return features
def input_fn_builder(features,
seq_length,
is_training,
drop_remainder):
"""Creates an `input_fn` closure to be passed to TPUEstimator."""
all_input_ids = []
all_input_masks = []
all_segment_ids = []
all_sent_label_ids = []
for feature in features:
all_input_ids.append(feature.input_ids)
all_input_masks.append(feature.input_masks)
all_segment_ids.append(feature.segment_ids)
all_sent_label_ids.append(feature.sent_label_id)
def input_fn(params):
batch_size = params["batch_size"]
num_examples = len(features)
# This is for demo purposes and does NOT scale to large data sets. We do
# not use Dataset.from_generator() because that uses tf.py_func which is
# not TPU compatible. The right way to load data is with TFRecordReader.
d = tf.data.Dataset.from_tensor_slices({
"input_ids": tf.constant(all_input_ids, shape=[num_examples, seq_length], dtype=tf.int32),
"input_masks": tf.constant(all_input_masks, shape=[num_examples, seq_length], dtype=tf.int32),
"segment_ids": tf.constant(all_segment_ids, shape=[num_examples, seq_length], dtype=tf.int32),
"sent_label_ids": tf.constant(all_sent_label_ids, shape=[num_examples], dtype=tf.int32),
})
if is_training:
d = d.repeat()
d = d.shuffle(buffer_size=100, seed=np.random.randint(10000))
d = d.batch(batch_size=batch_size, drop_remainder=drop_remainder)
return d
return input_fn
def file_based_convert_examples_to_features(examples,
sent_label_list,
max_seq_length,
tokenizer,
output_file):
"""Convert a set of `InputExample`s to a TFRecord file."""
def create_int_feature(values):
return tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))
writer = tf.python_io.TFRecordWriter(output_file)
for (ex_index, example) in enumerate(examples):
if ex_index % 10000 == 0:
tf.logging.info("Writing example %d of %d" % (ex_index, len(examples)))
feature = convert_single_example(ex_index, example, sent_label_list, max_seq_length, tokenizer)
features = collections.OrderedDict()
features["input_ids"] = create_int_feature(feature.input_ids)
features["input_masks"] = create_int_feature(feature.input_masks)
features["segment_ids"] = create_int_feature(feature.segment_ids)
features["sent_label_ids"] = create_int_feature([feature.sent_label_id])
tf_example = tf.train.Example(features=tf.train.Features(feature=features))
writer.write(tf_example.SerializeToString())
writer.close()
def file_based_input_fn_builder(input_file,
seq_length,
is_training,
drop_remainder):
"""Creates an `input_fn` closure to be passed to TPUEstimator."""
name_to_features = {
"input_ids": tf.FixedLenFeature([seq_length], tf.int64),
"input_masks": tf.FixedLenFeature([seq_length], tf.int64),
"segment_ids": tf.FixedLenFeature([seq_length], tf.int64),
"sent_label_ids": tf.FixedLenFeature([], tf.int64),
}
def _decode_record(record,
name_to_features):
"""Decodes a record to a TensorFlow example."""
example = tf.parse_single_example(record, name_to_features)
# tf.Example only supports tf.int64, but the TPU only supports tf.int32. So cast all int64 to int32.
for name in list(example.keys()):
t = example[name]
if t.dtype == tf.int64:
t = tf.to_int32(t)
example[name] = t
return example
def input_fn(params):
"""The actual input function."""
batch_size = params["batch_size"]
# For training, we want a lot of parallel reading and shuffling.
# For eval, we want no shuffling and parallel reading doesn't matter.
d = tf.data.TFRecordDataset(input_file)
if is_training:
d = d.repeat()
d = d.shuffle(buffer_size=100, seed=np.random.randint(10000))
d = d.apply(tf.contrib.data.map_and_batch(
lambda record: _decode_record(record, name_to_features),
batch_size=batch_size,
drop_remainder=drop_remainder))
return d
return input_fn
def create_model(bert_config,
input_ids,
input_masks,
segment_ids,
sent_label_ids,
sent_label_list,
mode,
use_tpu):
"""Creates a Classifier model."""
is_training = (mode == tf.estimator.ModeKeys.TRAIN)
model = modeling.BertModel(
config=bert_config,
is_training=is_training,
input_ids=input_ids,
input_mask=input_masks,
token_type_ids=segment_ids,
use_one_hot_embeddings=use_tpu)
# If you want to use sentence-level output, use model.get_pooled_output()
# If you want to use token-level output, use model.get_sequence_output()
with tf.variable_scope("sent", reuse=tf.AUTO_REUSE):
sent_result = model.get_pooled_output()
sent_result_mask = tf.cast(tf.reduce_max(input_masks, axis=-1, keepdims=True), dtype=tf.float32)
sent_kernel_initializer = tf.glorot_uniform_initializer(seed=np.random.randint(10000), dtype=tf.float32)
sent_bias_initializer = tf.zeros_initializer
sent_dense_layer = tf.keras.layers.Dense(units=len(sent_label_list), activation=None, use_bias=True,
kernel_initializer=sent_kernel_initializer, bias_initializer=sent_bias_initializer,
kernel_regularizer=None, bias_regularizer=None, trainable=True)
sent_dropout_layer = tf.keras.layers.Dropout(rate=0.1, seed=np.random.randint(10000))
sent_result = sent_dense_layer(sent_result)
if mode == tf.estimator.ModeKeys.TRAIN:
sent_result = sent_dropout_layer(sent_result)
masked_sent_predict = sent_result * sent_result_mask + MIN_FLOAT * (1 - sent_result_mask)
sent_predict_probs = tf.nn.softmax(masked_sent_predict, axis=-1)
sent_predict_ids = tf.cast(tf.argmax(sent_predict_probs, axis=-1), dtype=tf.int32)
sent_predict_scores = tf.reduce_max(sent_predict_probs, axis=-1)
loss = tf.constant(0.0, dtype=tf.float32)
if mode not in [tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL]:
return loss, sent_predict_ids, sent_predict_scores, sent_predict_probs
if sent_label_ids is not None:
with tf.variable_scope("sent_loss", reuse=tf.AUTO_REUSE):
sent_label = tf.cast(sent_label_ids, dtype=tf.float32)
sent_label_mask = tf.cast(tf.reduce_max(input_masks, axis=-1), dtype=tf.float32)
masked_sent_label = tf.cast(sent_label * sent_label_mask, dtype=tf.int32)
sent_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=masked_sent_label, logits=masked_sent_predict)
sent_loss = tf.reduce_sum(sent_cross_entropy * sent_label_mask) / tf.reduce_sum(tf.reduce_max(sent_label_mask, axis=-1))
loss = loss + sent_loss
return loss, sent_predict_ids, sent_predict_scores, sent_predict_probs
def model_fn_builder(bert_config,
sent_label_list,
init_checkpoint,
learning_rate,
num_train_steps,
num_warmup_steps,
use_tpu):
"""Returns `model_fn` closure for TPUEstimator."""
def model_fn(features,
labels,
mode,
params): # pylint: disable=unused-argument
"""The `model_fn` for TPUEstimator."""
tf.logging.info("*** Features ***")
for name in sorted(features.keys()):
tf.logging.info(" name = %s, shape = %s" % (name, features[name].shape))
input_ids = features["input_ids"]
input_masks = features["input_masks"]
segment_ids = features["segment_ids"]
sent_label_ids = features["sent_label_ids"] if mode in [tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL] else None
loss, sent_predict_ids, sent_predict_scores, sent_predict_probs = create_model(bert_config,
input_ids, input_masks, segment_ids, sent_label_ids, sent_label_list, mode, use_tpu)
tvars = tf.trainable_variables()
initialized_variable_names = {}
scaffold_fn = None
if init_checkpoint:
assignment_map, initialized_variable_names = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
if use_tpu:
def tpu_scaffold():
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
return tf.train.Scaffold()
scaffold_fn = tpu_scaffold
else:
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
tf.logging.info("**** Trainable Variables ****")
for var in tvars:
init_string = ""
if var.name in initialized_variable_names:
init_string = ", *INIT_FROM_CKPT*"
tf.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string)
output_spec = None
if mode == tf.estimator.ModeKeys.TRAIN:
train_op = optimization.create_optimizer(loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu)
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
scaffold_fn=scaffold_fn)
elif mode == tf.estimator.ModeKeys.EVAL:
def metric_fn(sent_label_ids,
sent_predict_ids):
sent_accuracy = tf.metrics.accuracy(labels=sent_label_ids, predictions=sent_predict_ids)
metric = {
"sent_accuracy": sent_accuracy,
}
return metric
eval_metrics = (metric_fn, [sent_label_ids, sent_predict_ids])
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
mode=mode,
loss=loss,
eval_metrics=eval_metrics,
scaffold_fn=scaffold_fn)
else:
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
mode=mode,
predictions={
"sent_predict_id": sent_predict_ids,
"sent_predict_score": sent_predict_scores,
"sent_predict_prob": sent_predict_probs
},
scaffold_fn=scaffold_fn)
return output_spec
return model_fn
def get_masked_data(data_ids,
label_list):
label_map = {}
for (i, label) in enumerate(label_list):
label_map[label] = i
pad_id = tf.constant(label_map["[PAD]"], shape=[], dtype=tf.int32)
out_id = tf.constant(label_map["O"], shape=[], dtype=tf.int32)
x_id = tf.constant(label_map["X"], shape=[], dtype=tf.int32)
cls_id = tf.constant(label_map["[CLS]"], shape=[], dtype=tf.int32)
sep_id = tf.constant(label_map["[SEP]"], shape=[], dtype=tf.int32)
masked_data_ids = (tf.cast(tf.not_equal(data_ids, pad_id), dtype=tf.int32) *
tf.cast(tf.not_equal(data_ids, out_id), dtype=tf.int32) *
tf.cast(tf.not_equal(data_ids, x_id), dtype=tf.int32) *
tf.cast(tf.not_equal(data_ids, cls_id), dtype=tf.int32) *
tf.cast(tf.not_equal(data_ids, sep_id), dtype=tf.int32))
return masked_data_ids
def serving_input_fn():
with tf.variable_scope("export"):
features = {
'input_ids': tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='input_ids'),
'input_masks': tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='input_masks'),
'segment_ids': tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='segment_ids')
}
return tf.estimator.export.build_raw_serving_input_receiver_fn(features)()
def decode_predicts(predicts,
sent_label_list,
max_seq_length,
tokenizer):
decoded_predicts = []
for predict in predicts:
input_tokens = tokenizer.convert_ids_to_tokens(predict["input_ids"])
input_masks = predict["input_masks"]
decoded_tokens = []
for input_token, input_mask in zip(input_tokens, input_masks):
if input_mask == 0:
break
if input_token in ["[CLS]", "[SEP]"]:
continue
if input_token[:2] == "##":
decoded_tokens[-1] = decoded_tokens[-1] + input_token[2:]
continue
decoded_tokens.append(input_token)
decoded_predict = {
"text": " ".join(decoded_tokens),
"sent_label": sent_label_list[predict["sent_label_id"]],
"sent_predict": sent_label_list[predict["sent_predict_id"]],
"sent_score": float(predict["sent_predict_score"]),
"sent_probs": [float(prob) for prob in predict["sent_predict_prob"]]
}
decoded_predicts.append(decoded_predict)
return decoded_predicts
def write_to_json(data_list,
data_path):
data_folder = os.path.dirname(data_path)
if not os.path.exists(data_folder):
os.mkdir(data_folder)
with open(data_path, "w") as file:
json.dump(data_list, file, indent=4)
def write_to_text(data_list,
data_path):
data_folder = os.path.dirname(data_path)
if not os.path.exists(data_folder):
os.mkdir(data_folder)
with open(data_path, "w") as file:
for data in data_list:
file.write("{0}\n".format(data))
def main(_):
tf.logging.set_verbosity(tf.logging.INFO)
np.random.seed(FLAGS.random_seed)
bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
if FLAGS.max_seq_length > bert_config.max_position_embeddings:
raise ValueError("Cannot use sequence length %d because the BERT model was only trained up to sequence length %d" %
(FLAGS.max_seq_length, bert_config.max_position_embeddings))
tf.gfile.MakeDirs(FLAGS.output_dir)
tokenization.validate_case_matches_checkpoint(FLAGS.do_lower_case, FLAGS.init_checkpoint)
tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
data_dir = FLAGS.data_dir
task_name = FLAGS.task_name.lower()
processor = ClassificationProcessor(data_dir, task_name)
sent_label_list = processor.get_sent_labels()
train_examples = None
num_train_steps = None
num_warmup_steps = None
if FLAGS.do_train:
train_examples = processor.get_train_examples()
num_train_steps = int(len(train_examples) / FLAGS.train_batch_size * FLAGS.num_train_epochs)
num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion)
tpu_cluster_resolver = None
if FLAGS.use_tpu and FLAGS.tpu_name:
tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)
is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
run_config = tf.contrib.tpu.RunConfig(
cluster=tpu_cluster_resolver,
master=FLAGS.master,
model_dir=FLAGS.output_dir,
save_checkpoints_steps=FLAGS.save_checkpoints_steps,
tpu_config=tf.contrib.tpu.TPUConfig(
iterations_per_loop=FLAGS.iterations_per_loop,
num_shards=FLAGS.num_tpu_cores,
per_host_input_for_training=is_per_host))
model_fn = model_fn_builder(
bert_config=bert_config,
sent_label_list=sent_label_list,
init_checkpoint=FLAGS.init_checkpoint,
learning_rate=FLAGS.learning_rate,
num_train_steps=num_train_steps,
num_warmup_steps=num_warmup_steps,
use_tpu=FLAGS.use_tpu)
# If TPU is not available, this will fall back to normal Estimator on CPU or GPU.
estimator = tf.contrib.tpu.TPUEstimator(
use_tpu=FLAGS.use_tpu,
model_fn=model_fn,
config=run_config,
export_to_tpu=FLAGS.use_tpu,
train_batch_size=FLAGS.train_batch_size,
eval_batch_size=FLAGS.eval_batch_size,
predict_batch_size=FLAGS.predict_batch_size)
if FLAGS.do_train:
tf.logging.info("***** Run training *****")
tf.logging.info(" Num examples = %d", len(train_examples))
tf.logging.info(" Batch size = %d", FLAGS.train_batch_size)
tf.logging.info(" Num steps = %d", num_train_steps)
train_features = convert_examples_to_features(
examples=train_examples,
sent_label_list=sent_label_list,
max_seq_length=FLAGS.max_seq_length,
tokenizer=tokenizer)
train_input_fn = input_fn_builder(
features=train_features,
seq_length=FLAGS.max_seq_length,
is_training=True,
drop_remainder=True)
estimator.train(input_fn=train_input_fn, max_steps=num_train_steps)
if FLAGS.do_eval:
eval_examples = processor.get_dev_examples()
tf.logging.info("***** Run evaluation *****")
tf.logging.info(" Num examples = %d", len(eval_examples))
tf.logging.info(" Batch size = %d", FLAGS.eval_batch_size)
eval_features = convert_examples_to_features(
examples=eval_examples,
sent_label_list=sent_label_list,
max_seq_length=FLAGS.max_seq_length,
tokenizer=tokenizer)
eval_input_fn = input_fn_builder(
features=eval_features,
seq_length=FLAGS.max_seq_length,
is_training=False,
drop_remainder=False)
result = estimator.evaluate(input_fn=eval_input_fn)
sent_accuracy = result["sent_accuracy"]
tf.logging.info("***** Evaluation result *****")
tf.logging.info(" Accuracy (sent-level) = %s", str(sent_accuracy))
if FLAGS.do_predict:
predict_examples = processor.get_test_examples()
tf.logging.info("***** Run prediction *****")
tf.logging.info(" Num examples = %d", len(predict_examples))
tf.logging.info(" Batch size = %d", FLAGS.predict_batch_size)
predict_features = convert_examples_to_features(
examples=predict_examples,
sent_label_list=sent_label_list,
max_seq_length=FLAGS.max_seq_length,
tokenizer=tokenizer)
predict_input_fn = input_fn_builder(
features=predict_features,
seq_length=FLAGS.max_seq_length,
is_training=False,
drop_remainder=False)
result = estimator.predict(input_fn=predict_input_fn)
predicts = [{
"input_ids": feature.input_ids,
"input_masks": feature.input_masks,
"sent_label_id": feature.sent_label_id,
"sent_predict_id": predict["sent_predict_id"],
"sent_predict_score": predict["sent_predict_score"],
"sent_predict_prob": predict["sent_predict_prob"].tolist()
} for feature, predict in zip(predict_features, result)]
decoded_predicts = decode_predicts(
predicts=predicts,
sent_label_list=sent_label_list,
max_seq_length=FLAGS.max_seq_length,
tokenizer=tokenizer)
predict_tag = FLAGS.predict_tag if FLAGS.predict_tag else str(time.time())
output_path = os.path.join(FLAGS.output_dir, "predict.{0}.json".format(predict_tag))
write_to_json(decoded_predicts, output_path)
if FLAGS.do_export:
tf.logging.info("***** Running exporting *****")
tf.gfile.MakeDirs(FLAGS.export_dir)
estimator.export_savedmodel(FLAGS.export_dir, serving_input_fn, as_text=False)
if __name__ == "__main__":
flags.mark_flag_as_required("bert_config_file")
flags.mark_flag_as_required("vocab_file")
flags.mark_flag_as_required("init_checkpoint")
flags.mark_flag_as_required("data_dir")
flags.mark_flag_as_required("output_dir")
flags.mark_flag_as_required("export_dir")
tf.app.run()