地址总线是单向还是双向_基于Transformer的双向编码器表示:BERT

这是 Transformer系列 的第五篇。

GPT是单向语言模型,BERT(Bidirectional Encoder Representations from Transformers)[1]用Masked语言模型缓解了单向限制,基本思想是完形填空[2]。MLM随机遮住一些token,目标函数是根据上下文预测遮住的词。

模型结构

BERT是一个多层双向Transformer encoder。

是层数,
是hidden size,
是attention头数。双向LSTM是正向加反向的拼接,双向Transformer encoder也是正向和反向的拼接。

输入输出表示

输入可以用一个token序列无歧义地表示单个句子或句对。本文中,sentence指任意连续文本,sequence指输入序列,可以是单个句子或句对。

词表大小是30000,词嵌入是WordPiece[3]嵌入,意思是用subword作为词表里的“词”。句子的第一个token总是[CLS]。这个token的最终隐状态是整个句子的最终表示。对于句对,首先把中间加一个[SEP] token,然后为每个token分配一个嵌入来标记它属于句A还是句B。把输入嵌入记为

,[CLS]的最终隐状态记为
,第
个token的最终隐状态是
。一个token的表示是它的token嵌入、segment嵌入和位置嵌入的和[4],见图2。

8e01f712c9b55205de3becdb7bebbd81.png

e98395688f3c36c891accef7caab63e9.png

Masked LM

之前的语言模型都是单向的,而双向的会使得每个词间接地看到自己,这样就能轻而易举地预测[5]. 单向的意思是只考虑左边或右边的上下文,双向的意思是考虑的两边的上下文。为了训练一个双向表示,我们随机遮住一些词,然后预测遮住的词。遮住的词用特殊的token [MASK]表示,然后用transformer encoder进行attention操作,得到每个词的隐状态向量,也就是attention向量。然后用这个隐状态加上softmax去预测被遮住的词。如果预测正确率高,就说明模型完形填空能力强。实验中遮词比例是15%。

[MASK]这个特殊的词不出现在下游任务中,这是一个bug。为了缓解这个bug,我们不总是把被遮住的词替换成[MASK],而是80%的时间替换成[MASK],10%的时间随机替换成别的词,10%的时间替换为自身。然后,

就用来预测被遮住的词,损失函数是交叉熵。

模型结构如图1左侧。

Next Sentence Prediction (NSP)

许多重要的下游任务,比如问答(QA)和自然语言推断(NLI),是基于理解两个句子的关系,语言模型没有直接捕获这种信息。为了训练一个理解两个句子关系的模型,我们预训练一个二元NSP任务,仅仅通过任何单语语料。对于句A和句B,50%的时间B的确是A的下一句,标记为IsNext,50%的时间B是随机的其他句子,标记为NotNext。

如图1左侧,

用来预测NSP。

Fine-tuning BERT

对于每个任务,我们直接把任务相关的数据插入到BERT,然后端到端地训练模型。

对于输入,句A和句B可以是

  • sentence pairs in paraphrasing
  • hypothesis-premise pairs in entailment
  • question-passage pairs in question answering
  • a degenerate text-
    pair in text classification or sequence tagging

对于输出,每个token的表示用于词级别任务,比如序列标注和问答。[CLS]表示用于分类问题。


[1] BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding

[2] "Cloze Procedure": A New Tool For Measuring Readability。CLOZE PROCEDURE,完形填空之意。cloze一词源于closure, closure是一个心理学术语,指人类在看到残缺圆形的时候会把它看成完整的。人可以补全这个圆,是因为他对这个图案太熟悉了。这个原理也可以应用到语言上,对于"Chickens cackle and ___ quack",几乎所有人都会填ducks。A cloze unit may be defined as: Any single occurrence of a successful attempt to reproduce accurately a part deleted from a "message" (any language product) by deciding, from the context that remains, what the missing part should be. Cloze procedure may be defined as: A method of intercepting a message from a "transmitter" (writer or speaker), mutilating its language patterns by deleting parts, and so administering it to "receivers" (readers or listeners) that their attempts to make the patterns whole again potentially yield a considerable number of cloze units. 完形填空的主要贡献来自于total language context和dispositional mechanisms. The total context of any language behavior includes everything that tends to motivate, guide, assist or hinder that behavior. It includes verbal factorsgrammatical skills and multitudes of symbols-and non-verbal ones such as fears, desires, past experience and intelligence. Osgood relates the "redundancies" and "transitional probabilities" of language to the development of "dispositional mechanisms" that play a large part in both transmitting and receiving messages. Redundancy-"Man coming" means the same as "A man is coming this way now." Transitional Probabilities - Some words are more likely than others to appear in certain patterns or sequences. "Merry Christmas" is a more probable combination than "Merry birthday." Some transitions from one word to the next are more probable than others.

[3] 一文读懂BERT中的WordPiece 。wordpiece就是subword,主要实现方式是BPE(Byte-pair encoding)。

[4] Why BERT has 3 Embedding Layers and Their Implementation Details. (1)Token嵌入是token id经过词向量矩阵后的向量。(2)Segment嵌入用来标记token在句对中属于句A还是句B。句A的token(包括[CLS]和[SEP])的segment嵌入是全0向量,维度跟token嵌入相同。句B是全1向量。(3)位置嵌入是一个(max_len,emb_size)的look-up矩阵,不同句子相同位置的位置向量相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值