-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtrain.py
194 lines (165 loc) · 5.9 KB
/
train.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
#! -*- coding: utf-8 -*-
# 词级别的中文BERT预训练
# MLM任务
import os, json
import numpy as np
from bert4keras.backend import keras, K
from bert4keras.layers import Loss
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer, load_vocab
from bert4keras.optimizers import Adam
from bert4keras.optimizers import extend_with_weight_decay
from bert4keras.optimizers import extend_with_gradient_accumulation
from bert4keras.snippets import sequence_padding, open
from bert4keras.snippets import DataGenerator
from bert4keras.snippets import text_segmentate
import jieba
jieba.initialize()
# 基本参数
maxlen = 512
batch_size = 16
epochs = 100000
num_words = 20000
# bert配置
config_path = '/root/kg/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12/bert_config.json'
checkpoint_path = '/root/kg/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12/bert_model.ckpt'
dict_path = '/root/kg/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12/vocab.txt'
def corpus():
"""语料生成器
"""
while True:
f = '/root/data_pretrain/data_shuf.json'
with open(f) as f:
for l in f:
l = json.loads(l)
for text in text_process(l['text']):
yield text
def text_process(text):
"""分割文本
"""
texts = text_segmentate(text, 32, u'\n。')
result, length = '', 0
for text in texts:
if result and len(result) + len(text) > maxlen * 1.3:
yield result
result, length = '', 0
result += text
if result:
yield result
if os.path.exists('tokenizer_config.json'):
token_dict, keep_tokens, compound_tokens = json.load(
open('tokenizer_config.json')
)
else:
# 加载并精简词表
token_dict, keep_tokens = load_vocab(
dict_path=dict_path,
simplified=True,
startswith=['[PAD]', '[UNK]', '[CLS]', '[SEP]', '[MASK]'],
)
pure_tokenizer = Tokenizer(token_dict.copy(), do_lower_case=True)
user_dict = []
for w, _ in sorted(jieba.dt.FREQ.items(), key=lambda s: -s[1]):
if w not in token_dict:
token_dict[w] = len(token_dict)
user_dict.append(w)
if len(user_dict) == num_words:
break
compound_tokens = [pure_tokenizer.encode(w)[0][1:-1] for w in user_dict]
json.dump([token_dict, keep_tokens, compound_tokens],
open('tokenizer_config.json', 'w'))
tokenizer = Tokenizer(
token_dict,
do_lower_case=True,
pre_tokenize=lambda s: jieba.cut(s, HMM=False)
)
def random_masking(token_ids):
"""对输入进行随机mask
"""
rands = np.random.random(len(token_ids))
source, target = [], []
for r, t in zip(rands, token_ids):
if r < 0.15 * 0.8:
source.append(tokenizer._token_mask_id)
target.append(t)
elif r < 0.15 * 0.9:
source.append(t)
target.append(t)
elif r < 0.15:
source.append(np.random.choice(tokenizer._vocab_size - 1) + 1)
target.append(t)
else:
source.append(t)
target.append(0)
return source, target
class data_generator(DataGenerator):
"""数据生成器
"""
def __iter__(self, random=False):
batch_token_ids, batch_segment_ids, batch_output_ids = [], [], []
for is_end, text in self.sample(random):
token_ids, segment_ids = tokenizer.encode(text, maxlen=maxlen)
source, target = random_masking(token_ids)
batch_token_ids.append(source)
batch_segment_ids.append(segment_ids)
batch_output_ids.append(target)
if len(batch_token_ids) == self.batch_size or is_end:
batch_token_ids = sequence_padding(batch_token_ids)
batch_segment_ids = sequence_padding(batch_segment_ids)
batch_output_ids = sequence_padding(batch_output_ids)
yield [
batch_token_ids, batch_segment_ids, batch_output_ids
], None
batch_token_ids, batch_segment_ids, batch_output_ids = [], [], []
class CrossEntropy(Loss):
"""交叉熵作为loss,并mask掉输入部分
"""
def compute_loss(self, inputs, mask=None):
y_true, y_pred = inputs
y_mask = K.cast(K.not_equal(y_true, 0), K.floatx())
accuracy = keras.metrics.sparse_categorical_accuracy(y_true, y_pred)
accuracy = K.sum(accuracy * y_mask) / K.sum(y_mask)
self.add_metric(accuracy, name='accuracy')
loss = K.sparse_categorical_crossentropy(
y_true, y_pred, from_logits=True
)
loss = K.sum(loss * y_mask) / K.sum(y_mask)
return loss
model = build_transformer_model(
config_path,
checkpoint_path,
with_mlm='linear',
keep_tokens=keep_tokens, # 只保留keep_tokens中的字,精简原字表
compound_tokens=compound_tokens, # 增加词,用字平均来初始化
)
# 训练用模型
y_in = keras.layers.Input(shape=(None,))
outputs = CrossEntropy(1)([y_in, model.output])
train_model = keras.models.Model(model.inputs + [y_in], outputs)
AdamW = extend_with_weight_decay(Adam, name='AdamW')
AdamWG = extend_with_gradient_accumulation(AdamW, name='AdamWG')
optimizer = AdamWG(
learning_rate=5e-6,
weight_decay_rate=0.01,
exclude_from_weight_decay=['Norm', 'bias'],
grad_accum_steps=16,
)
train_model.compile(optimizer=optimizer)
train_model.summary()
class Evaluator(keras.callbacks.Callback):
"""训练回调
"""
def on_epoch_end(self, epoch, logs=None):
model.save_weights('bert_model.weights') # 保存模型
if __name__ == '__main__':
# 启动训练
evaluator = Evaluator()
train_generator = data_generator(corpus(), batch_size, 10**5)
train_model.fit_generator(
train_generator.forfit(),
steps_per_epoch=1000,
epochs=epochs,
callbacks=[evaluator]
)
else:
model.load_weights('bert_model.weights')