MENU

BART详解

November 1, 2020 • Read: 17946 • Deep Learning阅读设置

一切都得从Transformer说起。Transformer左半边为Encoder,右半边为Decoder。我们将Encoder输入的句子称为source,Decoder输入的句子称为target

Encoder负责将source进行self-attention并获得句子中每个词的representation,最经典的Encoder架构就是BERT,通过Masked Language Model来学习词之间的关系,另外还有XLNet, RoBERTa, ALBERT, DistilBERT等等。但是单独Encoder结构不适用于生成任务

Decoder如下图所示,输入与输出之间差一个位置,主要是模拟在Inference时,不能让模型看到未来的词,这种方式称为AutoRegressive,常见的基于Decoder的模型通常是用来做序列生成的,例如GPT, CTRL等等。但是单独Decoder结构仅基于左侧上下文预测单词,无法学习双向交互

而两者合在一起后,就能当成一种Seq2Seq模型,进行翻译任务。下图是BART的主要结构,看上去似乎和Transformer没什么不同,主要区别在于source和target

训练阶段,Encoder端使用双向模型编码被破坏的文本,然后Decoder采用自回归的方式计算出原始输入;测试阶段或者是微调阶段,Encoder和Decoder的输入都是未被破坏的文本

BART vs Transformer

BART使用标准的Transformer模型,不过做了一些改变:

  1. 同GPT一样,将ReLU激活函数改为GeLU,并且参数初始化服从正态分布$N(0, 0.02)$
  2. BART base模型的Encoder和Decoder各有6层,large模型增加到了12层
  3. BART解码器的各层对编码器最终隐藏层额外执行cross-attention
  4. BERT在词预测之前使用了额外的Feed Forward Layer,而BART没有

Pre-training BART

BART作者尝试了不同的方式来破坏输入:

  • Token Masking:Following BERT (Devlin et al., 2019), random tokens are sampled and replaced with [MASK] elements.
  • Sentence Permutation:A document is divided into sentences based on full stops, and these sentences are shuffled in a random order.
  • Document Rotation:A token is chosen uniformly at random, and the document is rotated so that it begins with that token. This task trains the model to identify the start of the document.
  • Token Deletion:Random tokens are deleted from the input. In contrast to token masking, the model must decide which positions are missing inputs.
  • Text Infilling:A number of text spans are sampled, with span lengths drawn from a Poisson distribution ($\lambda=3$). Each span is replaced with a single [MASK] token. 0-length spans correspond to the insertion of [MASK] tokens. Text infilling teaches the model to predict how many tokens are missing from a span.

Fine-tuning BART

Sequence Classification Tasks

序列分类任务中,编码器和解码器的输入相同,解码器token的最终隐藏状态被输入到多类别线性分类器中。BART在解码器最后额外添加了一个token,如下图所示,该token位置的输出可以被认为是该句子的representation

Sequence Generation Tasks

由于BART具备自回归解码器,因此它可以针对序列生成任务进行直接微调,如问答或者文本摘要

Machine Translation

作者采用新的随机初始化Encoder替换BART编码器的Embedding层。该模型以端到端的方式进行训练,即训练一个新的编码器将外来词映射到输入。新的编码器可以使用不同于原始 BART 模型的词汇。其中随机初始化Encoder的训练分两步,均需要将来自 BART 模型输出的交叉熵损失进行反向传播。第一步,作者冻结 BART 的大部分参数,仅更新随机初始化的Encoder、BART 位置嵌入和 BART 编码器第一层的自注意力输入投影矩阵。第二步,作者将所有模型参数进行少量迭代训练

Results

从上表可以看出,貌似带上Document Rotation或Sentence Shuffling效果都不是太好,可以这么理解,假如模型在训练的时候看到的句子顺序都是乱的,它可能就认为这个世界的句子顺序都是乱的,当你做测试的时候,输入的句子是正序的,可能模型就不知所措了。实际上Text Infilling可以看作是Token Masking+Token Deletion,所以Text Infilling效果这么好也可以理解

Reference

Last Modified: April 25, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

6 Comments
  1. 神奇的老宝贝 神奇的老宝贝

    图表显示不出来

    1. mathor mathor

      @神奇的老宝贝已替换图片

  2. Ccc Ccc

    标题应该是bert呀

    1. mathor mathor

      @Ccc这是另一个模型,这个模型名字就叫bart

  3. Wonderfulrush Wonderfulrush

    Transformer和bart的区别那里没有看懂,能再讲解一下吗

    1. mathor mathor

      @Wonderfulrush其实区别不大,BART也是Transformer的结构,不同点在于,BART设计了更多的无监督预训练任务