谷歌BERT预训练源码解析(一):训练数据生成

news/2025/2/5 2:29:59/

目录
预训练源码结构简介
输入输出
源码解析
参数
主函数
创建训练实例
下一句预测&实例生成
随机遮蔽
输出
结果一览
预训练源码结构简介
关于BERT,简单来说,它是一个基于Transformer架构,结合遮蔽词预测和上下句识别的预训练NLP模型。至于效果:在11种不同NLP测试中创出最佳成绩
关于介绍BERT的文章我看了一些,个人感觉介绍的最全面的是机器之心
再放上谷歌官方源码链接:BERT官方源码
在看本博客之前,读者先要了解:
1.Transformer架构
2.BERT模型的创新之处
3.python语言及tensorflow框架
我会在代码中直接指出对应的原理,如果没有了解架构直接刚代码可能会有些吃力
BERT的预训练主要分为三个部分:
1.预训练数据的预处理(create_pretraining_data.py)
2.核心模型的构建(modeling.py)
3.训练过程(run_pretraining.py)
我将分三次分别介绍这三个部分的源码,这次先介绍训练数据的训练数据生成脚本即create_pretraining_data.py。

输入输出
关于输入和输出,我们可以直接从官方提供的训练命令行中窥之一二
 

python create_pretraining_data.py \--input_file=./sample_text.txt \--output_file=/tmp/tf_examples.tfrecord \--vocab_file=$BERT_BASE_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

可以看到 这里谷歌为我们提供了一个小的训练样本sample_text.txt(输入),将这个训练样本进行处理后输出到**tf_examples.tfrecord(输出)**这个文件。在sample_text.txt中,空行前后是不同的文章,每个文章中的每句话都占一行(也就是说每篇文章的上下两行是一篇文章的上下句)。vocab_file是官方模型中提供的词汇表。
sample_text.txt
 

sample_text.txt

源码解析
参数
input_file:指定输入文档路径
output_file:指定输出路径
vocab_file:指定词典路径(谷歌已在预训练模型中提供)
do_lower_case:为True则忽略大小写
max_seq_length:每一条训练数据(两句话)相加后的最大长度限制
max_predictions_per_seq:每一条训练数据mask的最大数量
random_seed:一个随机种子
dupe_factor:对文档多次重复随机产生训练集,随机的次数
masked_lm_prob:一条训练数据产生mask的概率,即每条训练数据随机产生max_predictions_per_seq×masked_lm_prob数量的mask
short_seq_prob:为了缩小预训练和微调过程的差距,以此概率产生小于max_seq_length的训练数据
 

from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport collections
import randomimport tokenization
import tensorflow as tfflags = tf.flagsFLAGS = flags.FLAGSflags.DEFINE_string("input_file", None,"Input raw text file (or comma-separated list of files).")flags.DEFINE_string("output_file", None,"Output TF example file (or comma-separated list of files).")flags.DEFINE_string("vocab_file", None,"The vocabulary file that the BERT model was trained on.")flags.DEFINE_bool("do_lower_case", True,"Whether to lower case the input text. Should be True for uncased ""models and False for cased models.")flags.DEFINE_integer("max_seq_length", 128, "Maximum sequence length.")flags.DEFINE_integer("max_predictions_per_seq", 20,"Maximum number of masked LM predictions per sequence.")flags.DEFINE_integer("random_seed", 12345, "Random seed for data generation.")flags.DEFINE_integer("dupe_factor", 10,"Number of times to duplicate the input data (with different masks).")flags.DEFINE_float("masked_lm_prob", 0.15, "Masked LM probability.")flags.DEFINE_float("short_seq_prob", 0.1,"Probability of creating sequences which are shorter than the ""maximum length.")

主函数
首先获取输入文本列表,对输入文本创建训练实例,再进行输出
简要介绍一下FullTokenizer这个类,它以vocab_file为词典,将词转化为该词对应的id,对于某些特殊词,如johanson,会先将johanson按照最大长度拆分,再看拆分的部分是否在vocab_file里。vocab_file里有没有"johanson"这个词,但有"johan"和"##son"这两个词,所以将"johanson"这个词拆分成两个词(##表示非开头匹配)
 

def main(_):tf.logging.set_verbosity(tf.logging.INFO)tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)input_files = []for input_pattern in FLAGS.input_file.split(","):input_files.extend(tf.gfile.Glob(input_pattern))        #获得输入文件列表tf.logging.info("*** Reading from input files ***")for input_file in input_files:tf.logging.info("  %s", input_file)rng = random.Random(FLAGS.random_seed)instances = create_training_instances(      #创建训练实例input_files, tokenizer, FLAGS.max_seq_length, FLAGS.dupe_factor,FLAGS.short_seq_prob, FLAGS.masked_lm_prob, FLAGS.max_predictions_per_seq,rng)output_files = FLAGS.output_file.split(",")tf.logging.info("*** Writing to output files ***")for output_file in output_files:tf.logging.info("  %s", output_file)write_instance_to_example_files(instances, tokenizer, FLAGS.max_seq_length,    #输出FLAGS.max_predictions_per_seq, output_files)

创建训练实例

这部分先将文章和每篇文章的每个句子加到二维列表,再将列表传入create_instances_from_document生成训练实例.
返回值:instances 一个列表 里面包含每个样例的TrainingInstance类

 

def create_training_instances(input_files, tokenizer, max_seq_length,dupe_factor, short_seq_prob, masked_lm_prob,max_predictions_per_seq, rng):"""Create `TrainingInstance`s from raw text."""all_documents = [[]]# Input file format:# (1) One sentence per line. These should ideally be actual sentences, not# entire paragraphs or arbitrary spans of text. (Because we use the# sentence boundaries for the "next sentence prediction" task).# (2) Blank lines between documents. Document boundaries are needed so# that the "next sentence prediction" task doesn't span between documents.for input_file in input_files:with tf.gfile.GFile(input_file, "r") as reader:while True:line = tokenization.convert_to_unicode(reader.readline())if not line:breakline = line.strip()# Empty lines are used as document delimitersif not line:all_documents.append([])tokens = tokenizer.tokenize(line)if tokens:all_documents[-1].append(tokens)    #二维列表  [文章,句子]# Remove empty documentsall_documents = [x for x in all_documents if x]   #删除空列表rng.shuffle(all_documents)   #随机排序vocab_words = list(tokenizer.vocab.keys())instances = []for _ in range(dupe_factor):for document_index in range(len(all_documents)):instances.extend(create_instances_from_document(all_documents, document_index, max_seq_length, short_seq_prob,masked_lm_prob, max_predictions_per_seq, vocab_words, rng))rng.shuffle(instances)return instances

 

下一句预测&实例生成
这部分是生成训练数据的具体过程,对每条数据生成TrainingInstance。这里的每条数据其实包含两个句子的信息。TrainingInstance包括tokens,segement_ids,is_random_next,masked_lm_positions,masked_lm_labels。下面给出这些属性的含义
tokens:词
segement_id:句子编码 第一句为0 第二句为1
is_random_next:第二句是随机查找,还是为第一句的下文
masked_lm_positions:tokens中被mask的位置
masked_lm_labels:tokens中被mask的原来的词
本部分含有BERT的创新点之一:下一句预测 类标的生成
返回值:instances
以下在关键代码出进行注释
 

def create_instances_from_document(all_documents, document_index, max_seq_length, short_seq_prob,masked_lm_prob, max_predictions_per_seq, vocab_words, rng):"""Creates `TrainingInstance`s for a single document."""document = all_documents[document_index]# Account for [CLS], [SEP], [SEP]max_num_tokens = max_seq_length - 3# We *usually* want to fill up the entire sequence since we are padding# to `max_seq_length` anyways, so short sequences are generally wasted# computation. However, we *sometimes*# (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter# sequences to minimize the mismatch between pre-training and fine-tuning.# The `target_seq_length` is just a rough target however, whereas# `max_seq_length` is a hard limit.target_seq_length = max_num_tokensif rng.random() < short_seq_prob:         #产生一个随机数如果小于short_seq_prob 则产生一个较短的训练序列target_seq_length = rng.randint(2, max_num_tokens)# We DON'T just concatenate all of the tokens from a document into a long# sequence and choose an arbitrary split point because this would make the# next sentence prediction task too easy. Instead, we split the input into# segments "A" and "B" based on the actual "sentences" provided by the user# input.instances = []current_chunk = []    #产生训练集的候选集current_length = 0i = 0while i < len(document):segment = document[i]current_chunk.append(segment)current_length += len(segment)if i == len(document) - 1 or current_length >= target_seq_length:if current_chunk:# `a_end` is how many segments from `current_chunk` go into the `A`# (first) sentence.a_end = 1if len(current_chunk) >= 2:a_end = rng.randint(1, len(current_chunk) - 1)           #从current_chunk中随机选出一个文档作为句子1的截止文档tokens_a = []for j in range(a_end):tokens_a.extend(current_chunk[j])   #将截止文档之前的文档都加入到tokens_atokens_b = []# Random nextis_random_next = Falseif len(current_chunk) == 1 or rng.random() < 0.5:    #候选集只有一句的情况则随机抽取句子作为句子2;或以0.5的概率随机抽取句子作为句子2is_random_next = Truetarget_b_length = target_seq_length - len(tokens_a)# This should rarely go for more than one iteration for large# corpora. However, just to be careful, we try to make sure that# the random document is not the same as the document# we're processing.for _ in range(10):                         random_document_index = rng.randint(0, len(all_documents) - 1)   if random_document_index != document_index:breakrandom_document = all_documents[random_document_index]        #随机找一个文档作为截止文档random_start = rng.randint(0, len(random_document) - 1)                #随机找一个初始文档for j in range(random_start, len(random_document)):                      tokens_b.extend(random_document[j])                                           #将随机文档加入到token_bif len(tokens_b) >= target_b_length:break# We didn't actually use these segments so we "put them back" so# they don't go to waste.num_unused_segments = len(current_chunk) - a_endi -= num_unused_segments# Actual nextelse:is_random_next = False                       以第1句的后续作为句子2for j in range(a_end, len(current_chunk)):tokens_b.extend(current_chunk[j])truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)      #对两个句子进行长度剪裁assert len(tokens_a) >= 1assert len(tokens_b) >= 1tokens = []segment_ids = []tokens.append("[CLS]")segment_ids.append(0)for token in tokens_a:tokens.append(token)segment_ids.append(0)tokens.append("[SEP]")segment_ids.append(0)for token in tokens_b:tokens.append(token)segment_ids.append(1)tokens.append("[SEP]")segment_ids.append(1)(tokens, masked_lm_positions,masked_lm_labels) = create_masked_lm_predictions(             #对token创建masktokens, masked_lm_prob, max_predictions_per_seq, vocab_words, rng)instance = TrainingInstance(tokens=tokens,segment_ids=segment_ids,is_random_next=is_random_next,masked_lm_positions=masked_lm_positions,masked_lm_labels=masked_lm_labels)instances.append(instance)current_chunk = []current_length = 0i += 1return instances

随机遮蔽
这部分对token进行随机mask。这部分是BERT的创新点之二,随机遮蔽。为了防止双向模型在多层之后“看到自己”。这里对一部分词进行随机遮蔽,并在预训练中进行预测。遮蔽方案:
1.以80%的概率直接变成[MASK]
2.以10%的概率保留原词
3.以10%的概率在词典中随机找一个词替代
返回值:经过随机遮蔽后的(词,遮蔽位置,遮蔽前原词)
 

def create_masked_lm_predictions(tokens, masked_lm_prob,max_predictions_per_seq, vocab_words, rng):
"""Creates the predictions for the masked LM objective."""cand_indexes = []
for (i, token) in enumerate(tokens):if token == "[CLS]" or token == "[SEP]":continuecand_indexes.append(i)rng.shuffle(cand_indexes)                 #打乱顺序output_tokens = list(tokens)masked_lm = collections.namedtuple("masked_lm", ["index", "label"])  # p定义一个名为masked_lm的元组,里面有两个属性num_to_predict = min(max_predictions_per_seq,max(1, int(round(len(tokens) * masked_lm_prob))))  #所有要mask的词的数量为定值,取两个定义好参数的最小值masked_lms = []
covered_indexes = set()
for index in cand_indexes:if len(masked_lms) >= num_to_predict:breakif index in covered_indexes:continuecovered_indexes.add(index)              #要被mask的词的indexmasked_token = None# 80% of the time, replace with [MASK]          if rng.random() < 0.8:masked_token = "[MASK]"else:# 10% of the time, keep originalif rng.random() < 0.5:masked_token = tokens[index]# 10% of the time, replace with random wordelse:masked_token = vocab_words[rng.randint(0, len(vocab_words) - 1)]output_tokens[index] = masked_token              #用masked_token替换原词masked_lms.append(masked_lm(index=index, label=tokens[index]))   masked_lms = sorted(masked_lms, key=lambda x: x.index)masked_lm_positions = []
masked_lm_labels = []
for p in masked_lms:masked_lm_positions.append(p.index)          #被mask的indexmasked_lm_labels.append(p.label)                 #被mask的label(即原词)return (output_tokens, masked_lm_positions, masked_lm_labels)

 

输出

最后是将处理好的数据保存为tfrecord文件。首先将token转为id,增加input_mask用于记录实句长度。最后将不到最大长度的部分用0补齐。

def write_instance_to_example_files(instances, tokenizer, max_seq_length,max_predictions_per_seq, output_files):"""Create TF example files from `TrainingInstance`s."""writers = []for output_file in output_files:writers.append(tf.python_io.TFRecordWriter(output_file))writer_index = 0total_written = 0for (inst_index, instance) in enumerate(instances):input_ids = tokenizer.convert_tokens_to_ids(instance.tokens)      #词转idinput_mask = [1] * len(input_ids)                                    segment_ids = list(instance.segment_ids)        assert len(input_ids) <= max_seq_lengthwhile len(input_ids) < max_seq_length:            #未到最大长度时后面补0input_ids.append(0)input_mask.append(0)segment_ids.append(0)assert len(input_ids) == max_seq_lengthassert len(input_mask) == max_seq_lengthassert len(segment_ids) == max_seq_lengthmasked_lm_positions = list(instance.masked_lm_positions)                      #mask位置记录masked_lm_ids = tokenizer.convert_tokens_to_ids(instance.masked_lm_labels)    #mask预测值转idmasked_lm_weights = [1.0] * len(masked_lm_ids)              #mask位置的权重都为1,用于排除后续的“0”以便loss计算while len(masked_lm_positions) < max_predictions_per_seq:      #补0masked_lm_positions.append(0)masked_lm_ids.append(0)masked_lm_weights.append(0.0)next_sentence_label = 1 if instance.is_random_next else 0features = collections.OrderedDict()            features["input_ids"] = create_int_feature(input_ids)features["input_mask"] = create_int_feature(input_mask)features["segment_ids"] = create_int_feature(segment_ids)features["masked_lm_positions"] = create_int_feature(masked_lm_positions)features["masked_lm_ids"] = create_int_feature(masked_lm_ids)features["masked_lm_weights"] = create_float_feature(masked_lm_weights)features["next_sentence_labels"] = create_int_feature([next_sentence_label])tf_example = tf.train.Example(features=tf.train.Features(feature=features))      #生成训练样例writers[writer_index].write(tf_example.SerializeToString())     #输出到文件writer_index = (writer_index + 1) % len(writers)   total_written += 1if inst_index < 20:               对前20个训练样例进行打印tf.logging.info("*** Example ***")tf.logging.info("tokens: %s" % " ".join([tokenization.printable_text(x) for x in instance.tokens]))for feature_name in features.keys():feature = features[feature_name]values = []if feature.int64_list.value:values = feature.int64_list.valueelif feature.float_list.value:values = feature.float_list.valuetf.logging.info("%s: %s" % (feature_name, " ".join([str(x) for x in values])))for writer in writers:writer.close()tf.logging.info("Wrote %d total instances", total_written)

 

结果一览

最后打印的结果是这酱的

 

å¨è¿éæå¥å¾çæè¿°

 

谷歌对训练数据的处理就介绍这么多,如果有错误欢迎大家批评指正,如果有问题也欢迎大家提问互相探讨。关于模型篇的代码解析我会在下一篇博客中给出。

 

 

 

 

 

 

 

 

 

 

 


http://www.ppmy.cn/news/606259.html

相关文章

LeetCode刷题:栈和队列的相关题目

前言1.用栈来实现队列1.1思路1.2图示1.3代码2.用队列来实现栈2.2思路2.2代码3.最小栈3.1思路3.2图示3.3代码4.删除字符串中所有相邻重复项4.1思路4.2代码前言 大家如果对于队列的性质等不太了解的话&#xff0c;我推荐一篇博客&#xff0c;写得很细节&#xff0c;大家可以去看看…

LeetCode简单题之机器人能否返回原点

题目 在二维平面上&#xff0c;有一个机器人从原点 (0, 0) 开始。给出它的移动顺序&#xff0c;判断这个机器人在完成移动后是否在 (0, 0) 处结束。 移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R&#xff08;右&#xff09;&#xff0c;L&…

谷歌BERT预训练源码解析(三):训练过程

目录 前言 源码解析 主函数 自定义模型 遮蔽词预测 下一句预测 规范化数据集 前言 本部分介绍BERT训练过程&#xff0c;BERT模型训练过程是在自己的TPU上进行的&#xff0c;这部分我没做过研究所以不做深入探讨。BERT针对两个任务同时训练。1.下一句预测。2.遮蔽词识别 下面介绍…

【解决方法】Python嵌套list如何换行

需求 输出一个excel&#xff0c;其中有一个单元格的数据要换行。 问题 list中间加‘\n’不会换行&#xff0c;会直接显示‘\n’字符。 解决方法 记住一个原则&#xff0c;str中的’\n’是能换行的。 想办法把list转为str即可。 示例 原代码 import pandas as pdout_lis…

《attention is all you need》解读

Motivation: 靠attention机制&#xff0c;不使用rnn和cnn&#xff0c;并行度高通过attention&#xff0c;抓长距离依赖关系比rnn强创新点&#xff1a; 通过self-attention&#xff0c;自己和自己做attention&#xff0c;使得每个词都有全局的语义信息&#xff08;长依赖由于 Se…

LeetCode简单题之句子中的有效单词数

题目 句子仅由小写字母&#xff08;‘a’ 到 ‘z’&#xff09;、数字&#xff08;‘0’ 到 ‘9’&#xff09;、连字符&#xff08;’-’&#xff09;、标点符号&#xff08;’!’、’.’ 和 ‘,’&#xff09;以及空格&#xff08;’ &#xff09;组成。每个句子可以根据空格…

LeetCode简单题之比特位计数

题目 给你一个整数 n &#xff0c;对于 0 < i < n 中的每个 i &#xff0c;计算其二进制表示中 1 的个数 &#xff0c;返回一个长度为 n 1 的数组 ans 作为答案。 示例 1&#xff1a; 输入&#xff1a;n 2 输出&#xff1a;[0,1,1] 解释&#xff1a; 0 --> 0 1 --&…

AMD与Intel,挑战英伟达GPU

AMD与Intel&#xff0c;挑战英伟达GPU 作为CPU界的霸主&#xff0c;英特尔对高性能GPU市场一直没有死心。从1998年和Real3D合作推出的i740独显&#xff0c;到2009年无故流产的Larrabee独显&#xff0c;再到去年公布的Xe GPU架构。任谁来都能看出&#xff0c;英特尔进军独立显卡…