BERT代码阅读

BERT代码阅读

bert源码

bert源码

流程图

流程图

简要笔记
input_ids : 单词id

input_ids 是 输入单词的id

输入单词经历的转变:

  1. 单词文本 -----------------> 单词id
  2. 单词id -----embedding—> 单词向量
input_mask : 该单词是填充吗?
#max_seq_length:  ___________________________________________________________________________
#  tokens:        [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] 0 0 0 0 ... 0
#  input_mask:    1     1   1     1   1       1    1  1     1  1  1  1      1   0 0 0 0 ... 0
# input_mask:是否是 为了达到最大序列长度max_seq_length 而进行的填充(即padding)

input_mask 出现在 比如:

  1. run_classifier.py 的函数 convert_single_example()
  2. modeling.py 的函数 init()
token_type_ids : 是第几句话的单词?

token_type_ids : 是哪句话的单词?

token_type_ids 意思是 该单词 是第一句话的 还是 第二句话的

第一句话用0表示, 第二句话用1表示

代码中的:token_type_ids, segment_ids , type_ids 这些词都是一个意思


#以下这段来自 run_classifier.py 的函数 convert_single_example() :
# The convention in BERT is:
# (a) For sequence pairs:
#  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
#  type_ids: 0     0  0    0    0     0       0 0     1  1  1  1   1 1
# (b) For single sequences:
#  tokens:   [CLS] the dog is hairy . [SEP]
#  type_ids: 0     0   0   0  0     0 0

segment_ids 来自 run_classifier.py 的函数 convert_single_example()

token_type_ids 来自 modeling.py 的函数 init()

先持有引用, 再初始化实物.

正常逻辑应该是 : 先初始化实物, 再持引用.

但这里的情况是:

  • 先持有引用, 再初始化实物.
  • 或者 : 先创建实物, 中间使用引用, 最后初始化实物.
先拿到词向量的引用, 再初始化词向量.

embedding_lookup 拿到的 词向量 只是个引用, 对该词向量的初始化 是后续调用init_from_checkpoint 做的.

run_classifier.py 的函数 model_fn_builder:

#file run_classifier.py:
def model_fn_builder(...):
    def model_fn(...):
        #create_model 中调用 embedding_lookup 拿到的 词向量 只是个引用
        ...=create_model(...)
        ...
        
        # 对该词向量的初始化 是后续调用init_from_checkpoint 做的.
        if ...:
            ...init_from_checkpoint(...)
        else:
            ...init_from_checkpoint(...)
            
    return model_fn

def create_model(...):
    model = modeling.BertModel(...)
    ...

#file modeling.py:
def __init__(...):
    #embedding_lookup 拿到的 词向量 只是个引用
    ...=embedding_lookup(...)

debug

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值