Bert源码阅读

前言

对Google开源出来的bert代码,来阅读下。不纠结于代码组织形式,而只是梳理下其训练集的生成,训练的self-attention和multi-head的具体实现。

训练集的生成

主要实现在create_pretraining_data.py和tokenization.py两个脚本里。
输入文本格式举例,下面是两篇文章外加一篇空文章。两篇文章之间用空格作间隔。

This is a blog about bert code reading.
It is writed using markdown, which is a markup language that can be written using a plain text editor.
Hopefuly it will give the reader a deep understanding of bert.

本文是篇关于bert源码阅读的博客。
它是用markdown写的,markdown是种可以使用普通文本编辑器编写的标记语言。
希望本文能够给读者以对bert更深层次的理解。


1
2
3
4
5
6
7
8
9
第一步,读取raw文本,按行分词处理后存储all_documents[doc_0, doc_1, …]里面,doc_i=[line_0, line_1, …], line_i = [token_0, token_1, …],然后shuffle文章。
第二步,重复dupe_factor=10次,每篇文章生成样本,[CLS +A+SEP +B+SEP]作一条样本。
注意:上述样本既用于MLM,又用于next-Sentence预测训练。

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))
1
2
3
4
5
6
create_instances_from_document函数对每篇文章都生成一个训练样本实例。
从第一条句子循环到最后一条句子i ii,收集segment到current_chunk列表中,当收集到的总句子长度>=单条样本最长值时,构造A+B。

if i == len(document) - 1 or current_length >= target_seq_length:
1
随机截取 current_chunk的某个位置a_end,[0, a_end]作为子句A=token_a。
B句随机概率选择是Next or Not next,如果是next,则current_chunk的剩余[a_end, :]作为子句B=token_b。如果Not next,则随机挑一篇文章,选择某个长度的子句作为B=token_b。注意,Not next时,循环经过的B句子对应的步幅,要回去(因为这部分句子并没有被真正使用,所以退回去以免浪费)。

num_unused_segments = len(current_chunk) - a_end
i -= num_unused_segments
1
2
两个句子加和长度超过最大长度怎么办?使用truncate_seq_pair在A和B中随机选择一个,随机丢掉首/尾的词,每次丢一个token,直到加和长度<=最大长度。

truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)
1
之后根据token_a和token_b生成tokens和segment_ids
tokens=[CLS,A0,A1,A2,SEP,B0,B1,B2,SEP] tokens = [CLS, A_0, A_1, A_2, SEP, B_0, B_1, B_2, SEP]tokens=[CLS,A
0

,A
1

,A
2

,SEP,B
0

,B
1

,B
2

,SEP]
segment_ids=[0a,0a,0a,0a,0a,1b,1b,1b,1b] segment\_ids =[0_a, 0_a, 0_a, 0_a, 0_a, 1_b, 1_b, 1_b, 1_b]segment_ids=[0
a

,0
a

,0
a

,0
a

,0
a

,1
b

,1
b

,1
b

,1
b

]
再之后,根据tokens生成遮挡替换之后的tokens、遮挡位置masked_lm_positions、遮挡位置的真实词masked_lm_labels。

(tokens, masked_lm_positions,
masked_lm_labels) = create_masked_lm_predictions(
tokens, masked_lm_prob, max_predictions_per_seq, vocab_words, rng)
1
2
3
15%采样遮挡,对遮挡的处理情况如下:
a) 80%的概率,遮挡词被替换为[mask]。⟶ \longrightarrow⟶别人看不到我。
b) 10%的概率,遮挡词被替换为随机词。⟶ \longrightarrow⟶别人看走眼我。
c) 10%的概率,遮挡词被替换为原来词。⟶ \longrightarrow⟶别人能看到我。

masked_token = None
# 80% of the time, replace with [MASK]
if rng.random() < 0.8:
masked_token = "[MASK]"
else:
# 10% of the time, keep original
if rng.random() < 0.5:
masked_token = tokens[index]
# 10% of the time, replace with random word
else:
masked_token = vocab_words[rng.randint(0, len(vocab_words) - 1)]
1
2
3
4
5
6
7
8
9
10
11
输入和返回结果举例:

input tokens ="The man went to the store . He bought a gallon of milk "
ouput tokens ="The man went to the [mask] . He [mask] a gallon of ice"
output masked_lm_positions = [5, 8, 10, 11]
output masked_lm_labels = [store, bought, gallon, milk]
1
2
3
4
位置#5,#8被遮挡,#10被替换为原token,#11被替换为随机词。注意CLS和SEP不会被遮挡。
然后保存成TrainingInstance类,同时保留了is_next标记.

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)
1
2
3
4
5
6
tokenization.FullTokenizer类用来处理分词,标点符号,unknown词,Unicode转换等操作。注意:中文只有单个字的切分,没有词。

数据存储及读取

存储为TF-Record
输入sentence变量的处理

input_ids = tokenizer.convert_tokens_to_ids(instance.tokens) ## ID化 ##
input_mask = [1] * len(input_ids)
segment_ids = segment_ids
padding 0 --> max_seq_length
1. 对iput_ids 补0到句子最大长度
2. 对input_mask 补0到句子最大长度
3. 对segment_ids 补0到句子最大长度
1
2
3
4
5
6
7
注意:input_mask是样本中有效词句的标识,后面需要用作作attention视野的约束。
遮挡变量的处理

masked_lm_positions = list(instance.masked_lm_positions)
masked_lm_ids = tokenizer.convert_tokens_to_ids(instance.masked_lm_labels)
masked_lm_weights = [1.0] * len(masked_lm_ids)
## padding 0 --> max_seq_length
1
2
3
4
注意:

masked_lm_ids是有mask的词对应的ID,比如[120, 911, 234, 0, 0, 0, 0];
masked_lm_positions是有mask的词对应的句子中位置,比如[15, 23, 11, 0, 0, 0, 0];
masked_lm_weights记录遮挡词的有效位置,计算masked-loss时使用,比如[1, 1, 1, 0, 0, 0, 0]。
next_sentense 的标记处理

next_sentence_label = 1 if instance.is_random_next else 0
1
save format 处理

features = 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))
1
2
3
4
5
6
7
8
9
10
读取使用dataset。

input_ids = features["input_ids"]
''' tf.data.TFRecordDataset '''
1
2
BertModel

模型实例化 ,注意这里的变量对应。

model = modeling.BertModel(
config=bert_config,
is_training=is_training,
input_ids=input_ids,
input_mask=input_mask,
token_type_ids=segment_ids, ## token_type是句子标记 ##
use_one_hot_embeddings=use_one_hot_embeddings)
1
2
3
4
5
6
7
输入token_ids–>向量化处理, embeding_lookup返回token_emb 和查询的table表。

(self.embedding_output, self.embedding_table) = embedding_lookup(
input_ids=input_ids,
vocab_size=config.vocab_size,
embedding_size=config.hidden_size,
word_embedding_name="word_embeddings", #and so on#)
1
2
3
4
5
加入pos_emb和type_emb处理, embedding_postprocessor
注意:pos_emb并不是用sin/cos函数生成的,而是随机生成的。

self.embedding_output = embedding_postprocessor(
input_tensor=self.embedding_output,
use_token_type=True, ## type_emb的处理设置 ##
token_type_ids=token_type_ids,
token_type_vocab_size=config.type_vocab_size,
use_position_embeddings=True, ## pos_emb的处理设置 ##
dropout_prob=config.hidden_dropout_prob, # and so on #)
1
2
3
4
5
6
7
重要:构造attention可视域的attention_mask,因为每个样本都经过padding了,视野必须要约束到有效范围词句内。

# This converts a 2D mask of shape [batch_size, seq_length] to a 3D
# mask of shape [batch_size, seq_length, seq_length] which is used for the attention scores.
attention_mask = create_attention_mask_from_input_mask(input_ids, input_mask)
## 注意:
## input_ids 是经过padding后的 [32,108, 99, 0, 0]; ##
## input_mask 是有效词标志 [1, 1, 1, 0, 0] ##
def create_attention_mask_from_input_mask(from_tensor, to_mask):
"""Create 3D attention mask from a 2D tensor mask.
from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...].
to_mask: int32 Tensor of shape [batch_size, to_seq_length].
returns: [batch_size, from_seq_length, to_seq_length].
"""
1
2
3
4
5
6
7
8
9
10
11
12
Bert.Transformer

# Run the stacked transformer.
# `sequence_output` shape = [batch_size, seq_length, hidden_size].
self.all_encoder_layers = transformer_model(
input_tensor=self.embedding_output, ## 输入token_ids经过 emb + pos_emb + seg_emb之后的结果 ##
attention_mask=attention_mask, ## 根据input_mask得到的可视域3D表示 ##
num_attention_heads=config.num_attention_heads, ## 多头数量 ##
do_return_all_layers=True, # and so on #)
1
2
3
4
5
6
7
对Transformer内部,逐层attention
1)先搞self-attention,注意有效位置的计算attention_mask。
2)再对每个位置做前向网络,加个drop层,加个layer-norm层,残差连接2)的输入。
3)再对每个位置做前向网络,加个drop层,加个layer-norm层,残差连接3)的输入。
4)输出作下层的输入,直到N层。
重要:这里根据输入query=[batch_size * seq_length, emb_size]来梳理下计算单层self-attenion过程中的维度变化。


注意:输入词的emb_size必须跟Transformer 的输出dim=-1的size一样么,必须的,因为有残差连接,必须保持维度一致。但是,head_nums_size × \times× size_per_head = emb_size=hidden_size不用必须成立【notice,bert代码实现上是将其设为相等的】,query_layer的最后维度只需是head_nums × \times× 任意数。靠近输出的dense包括了drop/layer-norm操作。
self-attention的矩阵计算示例


再说明下,query–>query_layer的变换,并不是echo token独享一个转换矩阵,也不是每个位置独享一个矩阵,而是query的emb_size空间–> query_layer的hidden_size空间上的维度变换。
重要:attention的计算示例
1)先看下单条样本时,self-attention的计算示例SelfAttentionSingle.py 链接
2)再看下batch样本时,self-attention的计算示例SelfAttentionBatch.py 链接
3)最后再看下batch+heads时,self-attention的计算示例SelfAttentionBatchMultiHeads.py 链接

attention_mask的作用在于,softmax时,对非视野内的做负向大加权,使得attention-score只计算注意在可视域范围内【非补0的地方】的数值。

if attention_mask is not None:
# `attention_mask` = [B, 1, F, T]
attention_mask = tf.expand_dims(attention_mask, axis=[1])
# Since attention_mask is 1.0 for positions we want to attend and 0.0 for
# masked positions, this operation will create a tensor which is 0.0 for
# positions we want to attend and -10000.0 for masked positions.
adder = (1.0 - tf.cast(attention_mask, tf.float32)) * -10000.0
# Since we are adding it to the raw scores before the softmax, this is
# effectively the same as removing these entirely.
attention_scores += adder
# Normalize the attention scores to probabilities.
# `attention_probs` = [B, N, F, T]
attention_probs = tf.nn.softmax(attention_scores)
1
2
3
4
5
6
7
8
9
10
11
12
13
注意,计算完上下文信息后,要转换成[batch, seq_length, num_heads, size_per_head],再作其他处理。

BertModel.sequence_output 是取最后attenion层的输出。
BertModel.pooled_output 取sequence_output的第一个token“CLS”的emb,然后加个连接层。

Loss Compute

Masked Language Model的loss计算

(masked_lm_loss,
masked_lm_example_loss, masked_lm_log_probs) = get_masked_lm_output(
bert_config, model.get_sequence_output(), model.get_embedding_table(),
masked_lm_positions, masked_lm_ids, masked_lm_weights)
def get_masked_lm_output(bert_config, input_tensor, output_weights, positions,
label_ids, label_weights):
## input_tensor = model.get_sequence_output(), model的最后层输出 ## [B, seq_len, emb_size]
## output_weights = model.get_embedding_table(), vocab_table ## [vocab_size, emb_size]
## positions = msked_lm_positions 遮挡词的在句子中的位置 ## [B, seq_len] ## 前几个是位置 ## 举例 [#pos1, #pos3, #pos10, 0, 0, 0]
## label_ids = masked_lm_ids ## 遮挡词的ID ## [B, seq_len] ## 前几个是ID ## 举例 [119, 301, 911, 0, 0, 0]
## label_weights = masked_lm_weights ## 遮挡词权重=1,非遮挡词权重=0 ## [B, seq_len] ## 举例 [1, 1, 1, 0, 0, 0]
## = pdding([1.0] * len(masked_lm_ids))
"""Get loss and log probs for the masked LM."""
input_tensor = gather_indexes(input_tensor, positions)
## gather_indexes也会将补0取出来,所以总tensor_size是不变的, [B*seq_len, emb_size]
## 注意后续的计算,都是在这个尺寸上进行的 ##
## 又单独加了层输出dense,并增加了vocab_emb_table的乘积 + bias ##

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
注意: label_weight在最后计算总loss时,乘上,只计算有遮挡的位置的loss。

Next Sentence Predict的loss计算
注意:此处使用模型最后输出层的第一个token-"CLS"的emb作为输入。

(next_sentence_loss, next_sentence_example_loss,
next_sentence_log_probs) = get_next_sentence_output(
bert_config, model.get_pooled_output(), next_sentence_labels)
def get_next_sentence_output(bert_config, input_tensor, labels):
## input_tensor = model.get_pooled_output() ## 模型最后输出层的第一个token-"CLS"的emb ##
## labels = next_sentence_labels ##
"""Get loss and log probs for the next sentence prediction."""
## 剩下的就是添加个dense层,二元分类 计算loss ##
1
2
3
4
5
6
7
8
两个loss加和作总的损失,联合训练。

total_loss = masked_lm_loss + next_sentence_loss
1
参考

https://github.com/google-research/bert
---------------------
作者:于建民
来源:CSDN
原文:https://blog.csdn.net/yujianmin1990/article/details/85175905
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/jfdwd/p/11250829.html
今日推荐