Lecture 5 Transformer

Lecture 5: Sequence to sequence

文章目录

  • Transformer
    • Sequence-to-sequence (Seq2seq)
      • Seq2seq for Syntactic Parsing
      • Seq2seq for Multi-label Classification
      • Seq2seq for Object Detection
    • Overview of Seq2seq Model
    • Encoder
      • To learn more
    • Decoder
      • Autoregressive (AT) (Speech Recognition as example)
      • Non-autoregressive (NAT)
        • AT v.s. NAT
      • Masked Multi-Head Attention
      • Cross Attention
    • Training
      • Tips
        • Copy Mechanism
        • Guided Attention
        • Beam Search
      • Optimization Evaluation Metrics
      • Exposure Bias
        • Scheduled Sampling

Transformer

Sequence-to-sequence (Seq2seq)

Lecture 5 Transformer_第1张图片

Seq2seq for Syntactic Parsing

Lecture 5 Transformer_第2张图片 Lecture 5 Transformer_第3张图片

1412.7449.pdf (arxiv.org)

Seq2seq for Multi-label Classification

Lecture 5 Transformer_第4张图片

Seq2seq for Object Detection

End-to-End Object Detection with Transformers (arxiv.org)

Overview of Seq2seq Model

Lecture 5 Transformer_第5张图片

Sequence to Sequence Learning with Neural Networks (arxiv.org)

Lecture 5 Transformer_第6张图片

Attention Is All You Need (arxiv.org)

Lecture 5 Transformer_第7张图片

Encoder

Encoder 所要完成的任务是“给一排向量,输出一排向量”。许多模型都可以做到这一点,如 self-attention、CNN 和 RNN。

Lecture 5 Transformer_第8张图片
  • Transformer’s Encoder 中的 block \text{block} block 并不是简单的 self-attention \text{self-attention} self-attention。输入向量经过 self-attention \text{self-attention} self-attention 得到 a \bf a a,再加上原输入 b \bf b b (这种操作叫做 residual \text{residual} residual),最后对这个 residual \text{residual} residual 结果进行 layer normalization \text{layer normalization} layer normalization 作为下一层 FC, Fully-Connected Network \text{FC, Fully-Connected Network} FC, Fully-Connected Network的输入( Add & Norm \text{Add \& Norm} Add & Norm)。
  • 而在 FC \text{FC} FC 中也有 residual \text{residual} residual 的设计,输入向量经过 FC \text{FC} FC 得到输出向量,将输出向量和原输入向量相加,再进行 layer normalization \text{layer normalization} layer normalization 就得到了 Encoder 中一个 Block \text{Block} Block 的输出。
  • layer normalization \text{layer normalization} layer normalization batch normalization \text{batch normalization} batch normalization 而言更为简单,不需要考虑 batch \text{batch} batch 的限制。注意区别, batch normalization \text{batch normalization} batch normalization 是对同一维度的不同特征输入做 norm \text{norm} norm,而 layer normalization \text{layer normalization} layer normalization 是对同一特征中不同维度的值做 norm \text{norm} norm
Lecture 5 Transformer_第9张图片

To learn more

On Layer Normalization in the Transformer Architecture (mlr.press)

PowerNorm: Rethinking Batch Normalization in Transformers (mlr.press)

Lecture 5 Transformer_第10张图片

Decoder

Autoregressive (AT) (Speech Recognition as example)

Lecture 5 Transformer_第11张图片
  • Decoder 的输入是 Encoder 的输出。在语音识别这个例子中,需要有一个特别的 token:  {\text{token: }} token:  来告诉 Decoder 识别开始。
  • 输入向量通过 Decoder 经过 s o f t m a x softmax softmax 得到一个输出向量,其大小为 vocabulary \text{vocabulary} vocabulary 的长度,对每个字给一个分数(概率),取最大值对应的字为最终结果。这个过程会持续下去。
Lecture 5 Transformer_第12张图片
  • 可以看到,Decoder 中上一个时间点的输出是下一个时间点的输入。那么,如果上一个时间点的输出出现错误(如 “器 → 气”),后续的输出会出错吗?—— 暂时放下这个问题。
  • 还需要另一个特别的 token:  {\text{token: }} token:  来告诉 Decoder 识别结束。

Non-autoregressive (NAT)

AT v.s. NAT

Lecture 5 Transformer_第13张图片

Masked Multi-Head Attention

Lecture 5 Transformer_第14张图片

Self-attention 的计算过程见:Lecture 4 Sequence as input

Lecture 5 Transformer_第15张图片
  • 如上图所示, Masked Self-attention \text{Masked Self-attention} Masked Self-attention 相较于 Self-attention 在计算上有区别。在计算输出 b 1 {\bf{b}}^1 b1 时,仅考虑输入 a 1 {\bf a}^1 a1;计算 b 2 {\bf b}^2 b2 时,仅考虑 a 1 ,   a 2 {\bf a}^1,\ {\bf a}^2 a1, a2;计算 b 3 {\bf b}^3 b3 时,仅考虑 a 1 ,   a 2 ,   a 3 {\bf a}^1,\ {\bf a}^2,\ {\bf a}^3 a1, a2, a3;计算 b 4 {\bf b}^4 b4 时,考虑 a 1 ,   a 2 ,   a 3 ,   a 3 {\bf a}^1,\ {\bf a}^2,\ {\bf a}^3,\ {\bf a}^3 a1, a2, a3, a3

  • 更具体地看,以计算 b 2 {\bf b}^2 b2 为例,我们只会用由 a 2 {\bf a}^2 a2 计算得到的 query  q 2 {\text {query}}\ {\bf q}^2 query q2 key  k 2 {\text{key}}\ {\bf k}^2 key k2 及由 a 1 {\bf a}^1 a1 计算得到的 key  k 1 {\text {key}}\ {\bf k}^1 key k1 计算 attention score  α 2 , 1 ′ ,   α 2 , 2 ′ \text{attention score}\ \alpha_{2,1}',\ \alpha_{2,2}' attention score α2,1, α2,2。—— 为什么需要 Masked \text{Masked} Masked?很直观,Decoder 的输入来自于上一时间点的输出,也就是无法同时得到所有的输入向量。

Lecture 5 Transformer_第16张图片

Cross Attention

Lecture 5 Transformer_第17张图片

Cross attention 是连接 Encoder 和 Decoder 的桥梁。Cross attention 的输入共有三个,其中 两个 \text{\color{blue}两个} 两个来自 Encoder, 一个 \text{\color{green}一个} 一个来自 Decoder。

Lecture 5 Transformer_第18张图片
  • 由 Encoder 的输出 a ( 1 , 2 , 3 ) {\bf a}^{(1,2,3)} a(1,2,3) 计算( W k W^k Wk)得到 key  k ( 1 , 2 , 3 ) \text{key}\ {\bf k}^{(1,2,3)} key k(1,2,3),Decoder 中输入经过 masked multi-head attention \text{masked multi-head attention} masked multi-head attention 得到一个输出再乘上 W q W^q Wq 得到 query  q \text{query}\ {\bf q} query q,由 q {\bf q} q k ( 1 , 2 , 3 ) {\bf k}^{(1,2,3)} k(1,2,3) 计算出 attention score \text{attention score} attention score
  • a ( 1 , 2 , 3 ) {\bf a}^{(1,2,3)} a(1,2,3) 计算( W v W^v Wv)得到 value  v ( 1 , 2 , 3 ) \text{value}\ {\bf v}^{(1,2,3)} value v(1,2,3) v = weightedsum ( v 1 , 2 , 3 ) {\bf v}=\text{weightedsum}({\bf v^{1,2,3}}) v=weightedsum(v1,2,3),将 v {\bf v} v 输入到后续的 FC \text{FC} FC 中。
  • Cross \text{Cross} Cross 体现在 key  k ,  value  v \text{key}\ {\bf k},\ \text{value}\ {\bf v} key k, value v 来自于 Encoder, query  q \text{query}\ {\bf q} query q 来自于 Decoder。

Training

Lecture 5 Transformer_第19张图片

在训练的时候,我们将 ground truth \text{ground truth} ground truth 作为 Decoder 的输入 —— Teacher Forcing \text{Teacher Forcing} Teacher Forcing。而在测试阶段,显然没有 ground truth \text{ground truth} ground truth 作为 Decoder 的输入;同样,在实际应用中,在 inference \text{inference} inference 阶段,也不可能有 ground truth \text{ground truth} ground truth 作为 Decoder 的输入,Decoder 仅能看到自己的输入 —— 会出现 mismatch \text{mismatch} mismatch

Lecture 5 Transformer_第20张图片

Tips

怎么解决上面提到的 mismatch \text{mismatch} mismatch 问题呢?

Copy Mechanism

Lecture 5 Transformer_第21张图片 Lecture 5 Transformer_第22张图片

Get To The Point: Summarization with Pointer-Generator Networks (arxiv.org)

Guided Attention

Lecture 5 Transformer_第23张图片

Beam Search

Lecture 5 Transformer_第24张图片

Optimization Evaluation Metrics

在测试的时候,我们通常采用 BLEU \text{BLEU} BLEU 来评估模型效果,而在训练阶段我们去最小化输出与 ground truth \text{ground truth} ground truth 间的交叉熵损失 —— 这有助于在 BLEU \text{BLEU} BLEU 上的表现吗?

Lecture 5 Transformer_第25张图片

Sequence Level Training with Recurrent Neural Networks (arxiv.org)

Exposure Bias

现在来看[前面提到的问题](#Autoregressive (AT) (Speech Recognition as example))

Lecture 5 Transformer_第26张图片

一个可以尝试的思路是,在用 ground truth \text{ground truth} ground truth 作为输入是,可以加入一些错误的信息,反而训练效果会更好 —— Scheduled Sampling。

Scheduled Sampling

Lecture 5 Transformer_第27张图片

你可能感兴趣的:(2022,Spring,李宏毅ML,transformer,深度学习,自然语言处理)