赞
踩
BERT开源代码:GitHub - google-research/bert: TensorFlow code and pre-trained models for BERT。
该链接中给出了训练示例sample_text.txt,文本如下:
上述是每行一个句子,文档之间有空行。
①执行命令得到预训练数据:
- python create_pretraining_data.py \
- --input_file=sample_text.txt \
- --output_file=tf_examples.tfrecord \
- --vocab_file=BERT_BASE_UNCASED_DIR/vocab.txt \
- --do_lower_case=True \
- --max_seq_length=128 \
- --max_predictions_per_seq=20 \
- --masked_lm_prob=0.15 \
- --random_seed=12345 \
- --dupe_factor=5
-
-
-
-
- vocab_file:词表的位置,这里使用的是BERT_BASE_UNCASED(安装参考https://blog.csdn.net/zcs2632008/article/details/125672908?spm=1001.2014.3001.5502)
-
- max_seq_length:序列最大长度
- max_predictions_per_seq:进行掩码的最大数量(序列最大长度*掩码率)
- masked_lm_prob:掩码率

②训练BERT
执行命令:
- python run_pretraining.py \
- --input_file=tf_examples.tfrecord \
- --output_dir=pretraining_output \
- --do_train=True \
- --do_eval=True \
- --bert_config_file=BERT_BASE_UNCASED_DIR/bert_config.json \
- --init_checkpoint=BERT_BASE_UNCASED_DIR/bert_model.ckpt \
- --train_batch_size=32 \
- --max_seq_length=128 \
- --max_predictions_per_seq=20 \
- --num_train_steps=20 \
- --num_warmup_steps=10 \
- --learning_rate=2e-5
得到结果:
- global_step = 20
- loss = 0.35468453
- masked_lm_accuracy = 0.9327252
- masked_lm_loss = 0.35498306
- next_sentence_accuracy = 1.0
- next_sentence_loss = 0.00041163983
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。