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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
| # 导包
```python
import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import sys
import re
from collections import Counter
import random
import json
from tqdm import tqdm
import numpy as np
import tensorflow.keras as keras
import tensorflow as tf
tf.config.run_functions_eagerly(True)
tf.get_logger().setLevel(tf.compat.v1.logging.ERROR)
from keras.metrics import top_k_categorical_accuracy, binary_accuracy
from keras.layers import *
from keras.callbacks import *
from keras.models import Model, load_model
import keras.backend as K
from keras.optimizers import Adam
from keras.utils import to_categorical
from keras.losses import SparseCategoricalCrossentropy, binary_crossentropy
from transformers import (
BertTokenizer,
TFBertForPreTraining,
TFBertModel,
)
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score
```
```python
tf.__version__
```
'2.3.0'
```python
```
```python
data_path = "sohu2021_open_data_clean/"
text_max_length = 512
bert_path = r"../chinese_L-12_H-768_A-12"
```
```python
```
```python
```
```python
```
```python
```
# 构建标签表
```python
label_to_id = {'0':0, '1':1}
```
```python
labels = [0, 1]
```
```python
```
```python
```
```python
```
```python
```
# 构建原数据文本迭代器
```python
def _transform_text(text):
text = text.strip().replace('\n', '。').replace('\t', '').replace('\u3000', '')
return re.sub(r'。+', '。', text)
```
```python
def get_data_iterator(data_path, file_name):
# TODO: 随机取
for category in os.listdir(data_path):
category_path = os.path.join(data_path, category)
if not os.path.isdir(category_path):
continue
file_path = os.path.join(category_path, file_name)
if not os.path.isfile(file_path):
continue
# print(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
data['source'] = _transform_text(data['source'])
if len(data['source']) == 0:
print('source:', line, data)
break
# continue
data['target'] = _transform_text(data['target'])
if len(data['target']) == 0:
print('target:', line, data)
break
# continue
label_name_list = list(key for key in data.keys() if key[:5]=='label')
if len(label_name_list) != 1:
print('label_name_list:', line, data)
break
# continue
label_name = label_name_list[0]
if data[label_name] not in label_to_id.keys():
print('label_name:', line, data, label_name)
break
# continue
yield data['source'], data['target'], label_to_id[data[label_name]]
```
```python
it = get_data_iterator(data_path, "train.txt")
```
```python
next(it)
```
('谁能打破科比81分纪录?奥尼尔给出5个候选人,补充利拉德比尔!', 'NBA现役能入名人堂的球星很多,但是能被立铜像只有2人', 0)
```python
```
```python
```
```python
```
```python
```
# 获取数据集样本个数
```python
def get_sample_num(data_path, file_name):
count = 0
it = get_data_iterator(data_path, file_name)
for data in tqdm(it):
count += 1
return count
```
```python
train_sample_count = get_sample_num(data_path, "train.txt")
```
59638it [00:04, 13354.07it/s]
```python
dev_sample_count = get_sample_num(data_path, "valid.txt")
```
9940it [00:00, 13041.43it/s]
```python
train_sample_count, dev_sample_count
```
(59638, 9940)
```python
```
```python
```
```python
```
```python
```
# 构建数据迭代器
```python
tokenizer = BertTokenizer.from_pretrained(bert_path)
```
```python
def _get_indices(text, text_pair=None):
return tokenizer.encode(text=text,
text_pair=text_pair,
max_length=text_max_length,
add_special_tokens=True,
padding='max_length',
truncation_strategy='only_first',
# return_tensors='tf'
)
```
```python
def get_keras_bert_iterator(data_path, file_name, tokenizer):
while True:
data_it = get_data_iterator(data_path, file_name)
for source, target, label in data_it:
indices = _get_indices(text=source,
text_pair=target)
yield indices, label
```
```python
it = get_keras_bert_iterator(data_path, "train.txt", tokenizer)
```
```python
# next(it)
```
```python
```
```python
```
```python
```
```python
```
# 构建批次数据迭代器
```python
def batch_iter(data_path, file_name, tokenizer, batch_size=64, shuffle=True):
"""生成批次数据"""
keras_bert_iter = get_keras_bert_iterator(data_path, file_name, tokenizer)
while True:
data_list = []
for _ in range(batch_size):
data = next(keras_bert_iter)
data_list.append(data)
if shuffle:
random.shuffle(data_list)
indices_list = []
label_list = []
for data in data_list:
indices, label = data
indices_list.append(indices)
label_list.append(label)
yield np.array(indices_list), np.array(label_list)
```
```python
it = batch_iter(data_path, "train.txt", tokenizer, batch_size=1)
```
```python
# next(it)
```
```python
it = batch_iter(data_path, "train.txt", tokenizer, batch_size=2)
```
```python
next(it)
```
/home/zsd-server/miniconda3/envs/my/lib/python3.8/site-packages/transformers/tokenization_utils_base.py:2162: FutureWarning: The `truncation_strategy` argument is deprecated and will be removed in a future version, use `truncation=True` to truncate examples to a max length. You can give a specific length with `max_length` (e.g. `max_length=45`) or leave max_length to None to truncate to the maximal input size of the model (e.g. 512 for Bert). If you have pairs of inputs, you can give a specific truncation strategy selected among `truncation='only_first'` (will only truncate the first sentence in the pairs) `truncation='only_second'` (will only truncate the second sentence in the pairs) or `truncation='longest_first'` (will iteratively remove tokens from the longest sentence in the pairs).
warnings.warn(
(array([[ 101, 6435, 2810, ..., 0, 0, 0],
[ 101, 6443, 5543, ..., 0, 0, 0]]),
array([0, 0]))
```python
```
```python
```
```python
```
```python
```
# 定义base模型
```python
# !transformers-cli convert --model_type bert \
# --tf_checkpoint chinese_L-12_H-768_A-12/bert_model.ckpt \
# --config chinese_L-12_H-768_A-12/bert_config.json \
# --pytorch_dump_output chinese_L-12_H-768_A-12/pytorch_model.bin
```
```python
# bert_model = TFBertForPreTraining.from_pretrained("./chinese_L-12_H-768_A-12/", from_pt=True)
```
```python
# # it = get_keras_bert_iterator(r"data/keras_bert_train.txt", cat_to_id, tokenizer)
# it = batch_iter(r"data/keras_bert_train.txt", cat_to_id, tokenizer, batch_size=1)
# out = bert_model(next(it)[0])
# out[0]
```
```python
def get_model(label_list):
K.clear_session()
bert_model = TFBertForPreTraining.from_pretrained(bert_path, from_pt=True)
input_indices = Input(shape=(None,), dtype='int32')
bert_output = bert_model(input_indices)
projection_logits = bert_output[0]
bert_cls = Lambda(lambda x: x[:, 0])(projection_logits) # 取出[CLS]对应的向量用来做分类
dropout = Dropout(0.5)(bert_cls)
output = Dense(len(label_list), activation='softmax')(dropout)
model = Model(input_indices, output)
model.compile(loss='sparse_categorical_crossentropy',
optimizer=Adam(1e-5), #用足够小的学习率
metrics=['accuracy'])
print(model.summary())
return model
```
```python
early_stopping = EarlyStopping(monitor='val_loss', patience=3) #早停法,防止过拟合
plateau = ReduceLROnPlateau(monitor="val_accuracy", verbose=1, mode='max', factor=0.5, patience=2) #当评价指标不在提升时,减少学习率
checkpoint = ModelCheckpoint('trained_model/keras_bert_sohu.hdf5', monitor='val_loss',verbose=2, save_best_only=True, mode='max', save_weights_only=True) #保存最好的模型
```
## 模型训练
```python
def get_step(sample_count, batch_size):
step = sample_count // batch_size
if sample_count % batch_size != 0:
step += 1
return step
```
```python
# batch_size = 2
# train_step = get_step(train_sample_count, batch_size)
# dev_step = get_step(dev_sample_count, batch_size)
# train_dataset_iterator = batch_iter(data_path, "train.txt", tokenizer, batch_size)
# dev_dataset_iterator = batch_iter(data_path, "valid.txt", tokenizer, batch_size)
# model = get_model(labels)
# #模型训练
# model.fit(
# train_dataset_iterator,
# steps_per_epoch=10,
# # steps_per_epoch=train_step,
# epochs=5,
# validation_data=dev_dataset_iterator,
# validation_steps=2,
# # validation_steps=dev_step,
# callbacks=[early_stopping, plateau, checkpoint],
# verbose=1
# )
# model.save_weights("trained_model/keras_bert_sohu_final.weights")
# model.save("trained_model/keras_bert_sohu_final.model")
```
```python
```
```python
```
```python
```
```python
```
# 多任务分支模型
## 构建数据迭代器
```python
label_type_to_id = {'labelA':0, 'labelB':1}
```
```python
def get_text_iterator(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
yield line
```
```python
def get_data_iterator(data_path, file_name):
# TODO: 随机取
file_iters = []
for category in os.listdir(data_path):
category_path = os.path.join(data_path, category)
if not os.path.isdir(category_path):
continue
file_path = os.path.join(category_path, file_name)
if not os.path.isfile(file_path):
continue
file_iter = get_text_iterator(file_path)
file_iters.append(file_iter)
while len(file_iters) > 0:
i = random.randrange(len(file_iters))
line = next(file_iters[i], None)
if line is None:
del file_iters[i]
continue
data = json.loads(line)
data['source'] = _transform_text(data['source'])
if len(data['source']) == 0:
print('source:', line, data)
break
# continue
data['target'] = _transform_text(data['target'])
if len(data['target']) == 0:
print('target:', line, data)
break
# continue
label_name_list = list(key for key in data.keys() if key[:5]=='label')
if len(label_name_list) != 1:
print('label_name_list:', line, data)
break
# continue
label_name = label_name_list[0]
if data[label_name] not in label_to_id.keys():
print('label_name:', line, data, label_name)
break
# continue
label_dict = {key:-1 for key in label_type_to_id.keys()}
label_dict[label_name] = label_to_id[data[label_name]]
if label_dict['labelA'] == 0:
label_dict['labelB'] = 0
if label_dict['labelB'] == 1:
label_dict['labelA'] = 1
yield data['source'], data['target'], label_dict['labelA'], label_dict['labelB']
```
```python
it = get_data_iterator(data_path, "train.txt")
```
```python
next(it)
```
('谁能打破科比81分纪录?奥尼尔给出5个候选人,补充利拉德比尔!', 'NBA现役能入名人堂的球星很多,但是能被立铜像只有2人', 0, 0)
```python
get_sample_num(data_path, "train.txt")
```
59638it [00:04, 11996.58it/s]
59638
```python
def _get_indices(text, text_pair=None):
return tokenizer.encode_plus(text=text,
text_pair=text_pair,
max_length=text_max_length,
add_special_tokens=True,
padding='max_length',
truncation_strategy='longest_first',
# return_tensors='tf',
return_token_type_ids=True
)
```
```python
def get_keras_bert_iterator(data_path, file_name, tokenizer):
while True:
data_it = get_data_iterator(data_path, file_name)
for source, target, labelA, labelB in data_it:
data = _get_indices(text=source,
text_pair=target)
# print(indices, type(indices), len(indices))
yield data['input_ids'], data['token_type_ids'], data['attention_mask'], labelA, labelB
```
```python
it = get_keras_bert_iterator(data_path, "train.txt", tokenizer)
```
```python
# next(it)
```
```python
def batch_iter(data_path, file_name, tokenizer, batch_size=64, shuffle=True):
"""生成批次数据"""
keras_bert_iter = get_keras_bert_iterator(data_path, file_name, tokenizer)
while True:
data_list = []
for _ in range(batch_size):
data = next(keras_bert_iter)
data_list.append(data)
if shuffle:
random.shuffle(data_list)
input_ids_list = []
token_type_ids_list = []
attention_mask_list = []
labelA_list = []
labelB_list = []
for data in data_list:
input_ids, token_type_ids, attention_mask, labelA, labelB = data
# print(indices, type(indices))
input_ids_list.append(input_ids)
token_type_ids_list.append(token_type_ids)
attention_mask_list.append(attention_mask)
labelA_list.append(labelA)
labelB_list.append(labelB)
yield [np.array(input_ids_list), np.array(token_type_ids_list), np.array(attention_mask_list)], [np.array(labelA_list, dtype=np.int32), np.array(labelB_list, dtype=np.int32)]
```
```python
it = batch_iter(data_path, "train.txt", tokenizer, batch_size=2)
```
```python
next(it)
```
([array([[ 101, 5381, 5273, ..., 0, 0, 0],
[ 101, 3297, 6818, ..., 8024, 4125, 102]]),
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 1, 1, 1]]),
array([[1, 1, 1, ..., 0, 0, 0],
[1, 1, 1, ..., 1, 1, 1]])],
[array([-1, 1], dtype=int32), array([ 0, -1], dtype=int32)])
## 定义模型
```python
def transform_y(y_true, y_pred):
mask_value = tf.constant(-1)
mask_y_true = tf.not_equal(tf.cast(y_true, dtype=tf.int32), tf.cast(mask_value, dtype=tf.int32))
# print(f"mask_y_true:{mask_y_true}")
# y_true_ = tf.cond(tf.equal(y_true, mask_value), lambda: 0, lambda: y_true)
y_true_ = tf.cast(y_true, dtype=tf.int32) * tf.cast(mask_y_true, dtype=tf.int32)
y_pred_ = tf.cast(y_pred, dtype=tf.float32) * tf.cast(mask_y_true, dtype=tf.float32)
# print(f"y_true_:{y_true_}, y_pred_:{y_pred_}")
return y_true_, y_pred_
```
```python
def my_binary_crossentropy(y_true, y_pred):
# print(f"y_true:{y_true}, y_pred:{y_pred}")
y_true, y_pred = transform_y(y_true, y_pred)
# print(f"y_true_:{y_true}, y_pred_:{y_pred}")
loss = binary_crossentropy(y_true, y_pred)
# print(f"loss:{loss}")
return loss
```
```python
def tarnsform_metrics(y_true, y_pred):
y_true_, y_pred_ = y_true.numpy(), y_pred.numpy()
for i in range(y_true_.shape[0]):
for j in range(y_true_.shape[1]):
if y_true_[i][j] == -1:
y_true_[i][j] = 0
y_pred_[i][j] = 0
if y_pred_[i][j] > 0.5:
y_pred_[i][j] = 1
else:
y_pred_[i][j] = 0
return y_true_, y_pred_
```
```python
def my_binary_accuracy(y_true, y_pred):
# print("my_binary_accuracy")
# print(f"y_true:{y_true}, y_pred:{y_pred}")
y_true_, y_pred_ = tarnsform_metrics(y_true, y_pred)
# print(f"y_true_:{y_true_}, y_pred_:{y_pred_}")
accuracy = binary_accuracy(y_true_, y_pred_)
return accuracy
```
```python
def my_f1_score(y_true, y_pred):
# print("my_f1_score")
# print(f"y_true:{y_true}, y_pred:{y_pred}")
y_true_, y_pred_ = tarnsform_metrics(y_true, y_pred)
# print(f"y_true_:{y_true_}, y_pred_:{y_pred_}")
return f1_score(y_true_, y_pred_, average='macro')
```
```python
def get_model():
K.clear_session()
bert_model = TFBertForPreTraining.from_pretrained(bert_path, from_pt=True)
input_ids = Input(shape=(None,), dtype='int32')
input_token_type_ids = Input(shape=(None,), dtype='int32')
input_attention_mask = Input(shape=(None,), dtype='int32')
bert_output = bert_model({'input_ids':input_ids, 'token_type_ids':input_token_type_ids, 'attention_mask':input_attention_mask}, return_dict=False, training=True)
projection_logits = bert_output[0]
bert_cls = Lambda(lambda x: x[:, 0])(projection_logits) # 取出[CLS]对应的向量用来做分类
dropout_A = Dropout(0.5)(bert_cls)
output_A = Dense(1, activation='sigmoid')(dropout_A)
dropout_B = Dropout(0.5)(bert_cls)
output_B = Dense(1, activation='sigmoid')(dropout_B)
model = Model([input_ids, input_token_type_ids, input_attention_mask], [output_A, output_B])
model.compile(
loss=my_binary_crossentropy,
# loss='binary_crossentropy',
# loss=binary_crossentropy,
optimizer=Adam(1e-5), #用足够小的学习率
metrics=[my_binary_accuracy, my_f1_score]
# metrics='accuracy'
)
print(model.summary())
return model
```
```python
early_stopping = EarlyStopping(monitor='val_loss', patience=3) #早停法,防止过拟合
plateau = ReduceLROnPlateau(monitor="val_loss", verbose=1, mode='max', factor=0.5, patience=2) #当评价指标不在提升时,减少学习率
checkpoint = ModelCheckpoint('trained_model/multi_keras_bert_sohu.hdf5', monitor='val_loss',verbose=2, save_best_only=True, mode='max', save_weights_only=True) #保存最好的模型
```
## 模型训练
```python
batch_size = 2
train_step = get_step(train_sample_count, batch_size)
dev_step = get_step(dev_sample_count, batch_size)
train_dataset_iterator = batch_iter(data_path, "train.txt", tokenizer, batch_size)
dev_dataset_iterator = batch_iter(data_path, "valid.txt", tokenizer, batch_size)
model = get_model()
#模型训练
model.fit(
train_dataset_iterator,
steps_per_epoch=10,
# steps_per_epoch=train_step,
epochs=2,
validation_data=dev_dataset_iterator,
validation_steps=2,
# validation_steps=dev_step,
callbacks=[early_stopping, plateau, checkpoint],
verbose=1
)
model.save_weights("trained_model/multi_keras_bert_sohu_final.weights")
model.save("trained_model/multi_keras_bert_sohu_final.model")
```
Some weights of the PyTorch model were not used when initializing the TF 2.0 model TFBertForPreTraining: ['bert.embeddings.position_ids', 'cls.predictions.decoder.bias']
- This IS expected if you are initializing TFBertForPreTraining from a PyTorch model trained on another task or with another architecture (e.g. initializing a TFBertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing TFBertForPreTraining from a PyTorch model that you expect to be exactly identical (e.g. initializing a TFBertForSequenceClassification model from a BertForSequenceClassification model).
All the weights of TFBertForPreTraining were initialized from the PyTorch model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFBertForPreTraining for predictions without further training.
WARNING: AutoGraph could not transform <bound method Socket.send of <zmq.sugar.socket.Socket object at 0x7f74107b3460>> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module, class, method, function, traceback, frame, or code object was expected, got cython_function_or_method
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
Model: "functional_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_3 (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
input_1 (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
tf_bert_for_pre_training (TFBer TFBertForPreTraining 102882442 input_3[0][0]
input_1[0][0]
input_2[0][0]
__________________________________________________________________________________________________
lambda (Lambda) (None, 21128) 0 tf_bert_for_pre_training[0][0]
__________________________________________________________________________________________________
dropout_37 (Dropout) (None, 21128) 0 lambda[0][0]
__________________________________________________________________________________________________
dropout_38 (Dropout) (None, 21128) 0 lambda[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 1) 21129 dropout_37[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 21129 dropout_38[0][0]
==================================================================================================
Total params: 102,924,700
Trainable params: 102,924,700
Non-trainable params: 0
__________________________________________________________________________________________________
None
Epoch 1/2
/home/zsd-server/miniconda3/envs/my/lib/python3.8/site-packages/transformers/tokenization_utils_base.py:2162: FutureWarning: The `truncation_strategy` argument is deprecated and will be removed in a future version, use `truncation=True` to truncate examples to a max length. You can give a specific length with `max_length` (e.g. `max_length=45`) or leave max_length to None to truncate to the maximal input size of the model (e.g. 512 for Bert). If you have pairs of inputs, you can give a specific truncation strategy selected among `truncation='only_first'` (will only truncate the first sentence in the pairs) `truncation='only_second'` (will only truncate the second sentence in the pairs) or `truncation='longest_first'` (will iteratively remove tokens from the longest sentence in the pairs).
warnings.warn(
/home/zsd-server/miniconda3/envs/my/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py:3349: UserWarning: Even though the tf.config.experimental_run_functions_eagerly option is set, this option does not apply to tf.data functions. tf.data functions are still traced and executed as graphs.
warnings.warn(
10/10 [==============================] - ETA: 0s - loss: 2.9102 - dense_loss: 0.7665 - dense_1_loss: 2.1437 - dense_my_binary_accuracy: 0.8500 - dense_my_f1_score: 0.8000 - dense_1_my_binary_accuracy: 0.6500 - dense_1_my_f1_score: 0.5667
Epoch 00001: val_loss improved from -inf to 1.98809, saving model to trained_model/multi_keras_bert_sohu.hdf5
10/10 [==============================] - 111s 11s/step - loss: 2.9102 - dense_loss: 0.7665 - dense_1_loss: 2.1437 - dense_my_binary_accuracy: 0.8500 - dense_my_f1_score: 0.8000 - dense_1_my_binary_accuracy: 0.6500 - dense_1_my_f1_score: 0.5667 - val_loss: 1.9881 - val_dense_loss: 1.9870 - val_dense_1_loss: 0.0011 - val_dense_my_binary_accuracy: 0.7500 - val_dense_my_f1_score: 0.6667 - val_dense_1_my_binary_accuracy: 1.0000 - val_dense_1_my_f1_score: 1.0000
Epoch 2/2
10/10 [==============================] - ETA: 0s - loss: 3.0176 - dense_loss: 2.8052 - dense_1_loss: 0.2125 - dense_my_binary_accuracy: 0.7500 - dense_my_f1_score: 0.7000 - dense_1_my_binary_accuracy: 0.9000 - dense_1_my_f1_score: 0.8667
Epoch 00002: val_loss improved from 1.98809 to 2.56778, saving model to trained_model/multi_keras_bert_sohu.hdf5
10/10 [==============================] - 114s 11s/step - loss: 3.0176 - dense_loss: 2.8052 - dense_1_loss: 0.2125 - dense_my_binary_accuracy: 0.7500 - dense_my_f1_score: 0.7000 - dense_1_my_binary_accuracy: 0.9000 - dense_1_my_f1_score: 0.8667 - val_loss: 2.5678 - val_dense_loss: 2.5678 - val_dense_1_loss: 7.7785e-06 - val_dense_my_binary_accuracy: 0.5000 - val_dense_my_f1_score: 0.3333 - val_dense_1_my_binary_accuracy: 1.0000 - val_dense_1_my_f1_score: 1.0000
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`).
The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
```python
dev_dataset_iterator = batch_iter(data_path, "valid.txt", tokenizer, batch_size=1)
data = next(dev_dataset_iterator)
model.predict(data[0]), data[1]
```
([array([[0.00284165]], dtype=float32),
array([[3.6964306e-05]], dtype=float32)],
[array([1], dtype=int32), array([-1], dtype=int32)])
```python
```
```python
```
```python
```
```python
```
# 模型加载及测试
## load_weights
## load_model
```python
```
```python
```
|