forked from HuiResearch/tfbert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pretraining.py
270 lines (230 loc) · 11.6 KB
/
run_pretraining.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
# -*- coding: UTF-8 -*-
# author : 'huanghui'
# date : '2021/5/21 9:14'
# project: 'tfbert'
"""
使用 gen dataset 实现动态mask,但是数据量大的话可能搞不定,需要使用tfrecord。
"""
import json
import os
import argparse
import random
import tensorflow.compat.v1 as tf
from tfbert import (
Trainer, MaskedLM,
CONFIGS, TOKENIZERS, devices, set_seed,
compute_types, compute_shapes, process_dataset)
from tfbert.data.pretrain import create_masked_lm_predictions, convert_to_unicode
from tfbert.tokenizer.tokenization_base import PTMTokenizer
from typing import Dict, List
from sklearn.metrics import accuracy_score
def create_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_type', default='bert', type=str, choices=CONFIGS.keys())
parser.add_argument('--optimizer_type', default='adamw', type=str, help="优化器类型")
parser.add_argument('--model_dir', default='model_path', type=str,
help="预训练模型存放文件夹,文件夹下ckpt文件名为model.ckpt,"
"config文件名为config.json,词典文件名为vocab.txt")
parser.add_argument('--config_path', default=None, type=str, help="若配置文件名不是默认的,可在这里输入")
parser.add_argument('--vocab_path', default=None, type=str, help="若词典文件名不是默认的,可在这里输入")
parser.add_argument('--pretrained_checkpoint_path', default=None, type=str, help="若模型文件名不是默认的,可在这里输入")
parser.add_argument('--output_dir', default='output/pretrain', type=str, help="")
parser.add_argument('--export_dir', default='output/pretrain/pb', type=str, help="")
parser.add_argument('--train_dir', default='data/pretrain/train', type=str, help="训练文件所在文件夹")
parser.add_argument('--dev_dir', default='data/pretrain/dev', type=str, help="验证文件所在文件夹")
parser.add_argument("--num_train_epochs", default=10, type=int, help="训练轮次")
parser.add_argument("--max_seq_length", default=128, type=int, help="最大句子长度")
parser.add_argument("--batch_size", default=64, type=int, help="训练批次")
parser.add_argument("--gradient_accumulation_steps", default=1, type=int, help="梯度累积")
parser.add_argument("--learning_rate", default=5e-5, type=float, help="学习率")
parser.add_argument("--warmup_proportion", default=0.1, type=float,
help="Proportion of training to perform linear learning rate warmup for.")
parser.add_argument("--weight_decay", default=0.01, type=float, help="Weight decay if we apply some.")
parser.add_argument("--masked_lm_prob", default=0.15, type=float, help="mask 概率.")
parser.add_argument("--max_predictions_per_seq", default=20, type=int, help="最大mask数量.")
parser.add_argument("--ngram", default=4, type=int, help="ngram mask 最大个数.")
parser.add_argument("--do_train", action="store_true", help="Whether to run training.")
parser.add_argument("--do_eval", action="store_true", help="Whether to run eval on the dev set.")
parser.add_argument("--evaluate_during_training", action="store_true", help="是否边训练边验证")
parser.add_argument("--do_export", action="store_true", help="将模型导出为pb格式.")
parser.add_argument("--do_whole_word_mask", action="store_true", help="全词mask.")
parser.add_argument("--logging_steps", default=1000, type=int, help="训练时每隔几步验证一次")
parser.add_argument("--saving_steps", default=1000, type=int, help="训练时每隔几步保存一次")
parser.add_argument("--random_seed", default=42, type=int, help="随机种子")
parser.add_argument("--max_checkpoints", default=1, type=int, help="模型保存最大数量,默认只保存一个")
parser.add_argument("--single_device", action="store_true", help="是否只使用一个device,默认使用所有的device训练")
parser.add_argument("--use_xla", action="store_true", help="是否使用XLA加速")
parser.add_argument(
"--mixed_precision", action="store_true",
help="混合精度训练,tf下测试需要同时使用xla才有加速效果,但是开始编译很慢")
args = parser.parse_args()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
if not args.single_device:
args.batch_size = args.batch_size * len(devices())
def find_files(dir_or_file):
if os.path.isdir(dir_or_file):
files = os.listdir(dir_or_file)
files = [os.path.join(dir_or_file, file_) for file_ in files]
elif isinstance(dir_or_file, str):
files = [dir_or_file]
else:
files = []
return files
args.train_files = find_files(args.train_dir)
args.dev_files = find_files(args.dev_dir)
if len(args.dev_files) == 0:
args.do_eval = False
args.evaluate_during_training = False
if len(args.train_files) == 0 and args.do_train:
args.do_train = False
tf.logging.warn("If you need to perform training, please ensure that the training file is not empty")
return args
def create_dataset(args, input_files, tokenizer: PTMTokenizer, set_type):
if not isinstance(input_files, List):
input_files = [input_files]
all_tokens = []
for input_file in input_files:
with open(input_file, 'r', encoding='utf-8') as reader:
for line in reader:
line = convert_to_unicode(line)
if not line:
break
line = line.strip()
if not line:
continue
tokens = tokenizer.tokenize(line)
tokens = tokens[:args.max_seq_length - 2]
tokens = [tokenizer.cls_token] + tokens + [tokenizer.sep_token]
all_tokens.append(tokens)
# 打乱
random.shuffle(all_tokens)
# 定义生成器,提供动态mask
def dynamic_mask_gen():
for tokens in all_tokens:
output_tokens, masked_lm_positions, masked_lm_labels = create_masked_lm_predictions(
tokens, args.masked_lm_prob,
args.max_predictions_per_seq,
list(tokenizer.vocab.keys()),
do_whole_word_mask=args.do_whole_word_mask,
favor_shorter_ngram=True,
ngram=args.ngram
)
encoded = tokenizer.encode_plus(
output_tokens, add_special_tokens=False, padding="max_length",
truncation=True, max_length=args.max_seq_length
)
masked_lm_positions = list(masked_lm_positions)
masked_lm_ids = tokenizer.convert_tokens_to_ids(masked_lm_labels)
masked_lm_weights = [1.0] * len(masked_lm_ids)
while len(masked_lm_positions) < args.max_predictions_per_seq:
masked_lm_positions.append(0)
masked_lm_ids.append(0)
masked_lm_weights.append(0.0)
encoded.update(
{'masked_lm_ids': masked_lm_ids,
'masked_lm_weights': masked_lm_weights,
'masked_lm_positions': masked_lm_positions}
)
yield encoded
sample_example = {
'input_ids': [0], 'token_type_ids': [0], 'attention_mask': [0],
'masked_lm_ids': [0], 'masked_lm_weights': [0.0], 'masked_lm_positions': [0]
}
types = compute_types(sample_example)
shapes = compute_shapes(sample_example)
dataset = tf.data.Dataset.from_generator(
dynamic_mask_gen, types, shapes
)
dataset, steps = process_dataset(
dataset, args.batch_size, len(all_tokens), set_type, buffer_size=100)
return dataset, steps
def get_model_fn(config, args):
def model_fn(inputs, is_training):
model = MaskedLM(
model_type=args.model_type,
config=config,
is_training=is_training,
**inputs)
masked_lm_ids = tf.reshape(inputs['masked_lm_ids'], [-1])
masked_lm_log_probs = tf.reshape(model.prediction_scores,
[-1, model.prediction_scores.shape[-1]])
masked_lm_predictions = tf.argmax(
masked_lm_log_probs, axis=-1, output_type=tf.int32)
masked_lm_weights = tf.reshape(inputs['masked_lm_weights'], [-1])
outputs = {'outputs': {
'masked_lm_predictions': masked_lm_predictions,
'masked_lm_ids': masked_lm_ids,
'masked_lm_weights': masked_lm_weights
}}
if model.loss is not None:
loss = model.loss / args.gradient_accumulation_steps
outputs['loss'] = loss
return outputs
return model_fn
def metric_fn(outputs: Dict) -> Dict:
"""
这里定义评估函数
:param outputs: trainer evaluate 返回的预测结果,model fn的outputs包含哪些字段就会有哪些字段
:return: 需要返回字典结果
"""
score = accuracy_score(outputs['masked_lm_ids'], outputs['masked_lm_predictions'],
sample_weight=outputs['masked_lm_weights'])
return {'accuracy': score}
def main():
args = create_args()
set_seed(args.random_seed)
config = CONFIGS[args.model_type].from_pretrained(
args.model_dir if args.config_path is None else args.config_path)
tokenizer = TOKENIZERS[args.model_type].from_pretrained(
args.model_dir if args.vocab_path is None else args.vocab_path, do_lower_case=True)
# tf 自带的dataset不知道怎么自动得到一轮需要步数
# 因此提前算出来传入trainer
train_dataset, train_steps, dev_dataset, dev_steps = None, 0, None, 0
if args.do_train:
train_dataset, train_steps = create_dataset(args, args.train_files, tokenizer, 'train')
if args.do_eval:
dev_dataset, dev_steps = create_dataset(args, args.dev_files, tokenizer, 'dev')
trainer = Trainer(
train_dataset=train_dataset,
eval_dataset=dev_dataset,
metric_fn=metric_fn,
use_xla=args.use_xla,
optimizer_type=args.optimizer_type,
learning_rate=args.learning_rate,
num_train_epochs=args.num_train_epochs,
gradient_accumulation_steps=args.gradient_accumulation_steps,
max_checkpoints=1,
max_grad=1.0,
warmup_proportion=args.warmup_proportion,
mixed_precision=args.mixed_precision,
single_device=args.single_device,
logging=True
)
trainer.build_model(model_fn=get_model_fn(config, args))
if args.do_train and train_dataset is not None:
# 训练阶段需要先compile优化器才能初始化权重
# 因为adam也是具备参数的
trainer.compile()
trainer.from_pretrained(
args.model_dir if args.pretrained_checkpoint_path is None else args.pretrained_checkpoint_path)
if args.do_train and train_dataset is not None:
trainer.train(
output_dir=args.output_dir,
train_steps=train_steps, # 这个是一轮的步数
eval_steps=dev_steps,
evaluate_during_training=args.evaluate_during_training,
logging_steps=args.logging_steps,
saving_steps=args.saving_steps,
greater_is_better=True,
load_best_model=True,
metric_for_best_model='accuracy')
config.save_pretrained(args.output_dir)
tokenizer.save_pretrained(args.output_dir)
if args.do_eval and dev_dataset is not None:
eval_outputs = trainer.evaluate(eval_steps=dev_steps)
print(json.dumps(
eval_outputs, ensure_ascii=False, indent=4
))
if __name__ == '__main__':
main()