Bert代码初识
简单读懂代码
modeling.py–定义一个bert模型的主体结构
BertConfig类
- BertModel类中所需要的超参数
- 定义从python dict中生成BertConfig的方法 from_dict(cls, json_object)
- 定义从json中生成BertConfig的方法 from_json_file(cls, json_file)
- 定义将BertConfig转换为python dict的方法 to_dict(self)
- 定义将BertConfig转换为json字符串的方法 to_json_string(self)
BertModel类
- 定义了激活函数,归一化函数以及过拟合函数等
- 定义了embedding_lookup函数 将输入input_ids[batch_size, seq_length]转换成[batch_size, seq_length, embedding_size]
- 定义了embedding_postprocessor函数 将token_type_embeddings和position_embedding加入到input_tensor中,对最后的结果进行归一化和dropout
- 定义了create_attention_mask_from_input_mask函数 构造attention mask ,将shape为[batch_size, to_seq_length]的mask转换为[batch_size, from_seq_length, to_seq_length] 的mask用于att