【Transformer 论文笔记】Attention is all you need

参考自:

Transformer 模型详解_空杯的境界的博客-CSDN博客_transformer

Self-Attention和Transformer - machine-learning-notes

目录

Abstract——对transformer的总体介绍

Introduction——引出Transformer

Background

Model Architecture——模型结构

“Encoder and Decoder Stacks”

“Scaled Dot-Product Attention”

Attention和self-attention的区别

“Multi-Head Attention”

“Position-wise Feed-Forward Networks”

“Embeddings and Softmax” 残差连接和归一化

“Positional Encoding” 位置编码

Decoder——解码器

Masked Multi-Head Attention

Conclusion——对未来的展望

Abstract——对transformer的总体介绍

“We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely.” (Vaswani 等。, 2017, p. 1) 我们提出了一种新的简单的网络架构Transformer,它完全基于注意力机制,完全不需要递归和卷积。

Introduction——引出Transformer

简述了循环神经网络递归迭代性能差的缺点,提出完全基于注意力机制的,允许更多并行化的Transformer模型

“Recurrent neural networks, long short-term memory [13] and gated recurrent [7] neural networks”:RNN,LSTM,GRU

复习:

  

Background

Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence.”


自注意力,有时称为帧内注意力,是一种将单个序列的不同位置联系起来的注意力机制,用于计算序列的表示。

“End-to-end memory networks are based on a recurrent attention mechanism instead of sequencealigned recurrence and have been shown to perform well on simple-language question answering and language modeling tasks


端到端记忆网络基于循环注意力机制,而不是顺序递归,并且已经被证明在简单的语言问答和语言建模任务中表现良好” [1]

Model Architecture——模型结构

“Encoder and Decoder Stacks”

编码器解码器结构:

简化后编码器的两个子层:

简化后解码器的三个子层:

“Scaled Dot-Product Attention”

Attention和self-attention的区别

以Encoder-Decoder框架为例,输入Source和输出Target内容是不一样的,比如对于英-中机器翻译来说,Source是英文句子,Target是对应的翻译出的中文句子,Attention发生在Target的元素Query和Source中的所有元素之间。

Self Attention,指的不是Target和Source之间的Attention机制,而是Source内部元素之间或者Target内部元素之间发生的Attention机制,也可以理解为Target=Source这种特殊情况下的Attention。

两者具体计算过程是一样的,只是计算对象发生了变化而已。

关于Q,K,V:

q 就是query,k就是key,v就是值,(k,v)就是键值对、也就是用query关键词去找到最相关的检索结果。上述过程中,不同的xi分享了同一个W,通过这个操作,x1x2已经发生了某种程度上的信息交换

关于zi的计算:

 query:查询

keys-values:键值对

score:注意力分数,q1与ki的点积,表示该单词对其他单词的重要程度

softmax/θ:归一化后的分数值

 

该式表示zi受v1,v2综合影响下的结果。

简化后式子为:

“Multi-Head Attention”

“Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.” [2] 多头注意力允许模型在不同位置共同关注来自不同表示子空间的信息。

设置8个attention表示不同方向上的注意力,得到8个z0,那就将多个版本的x拼接称为一个长向量,然后用一个全连接网络,即乘以一个矩阵,就能得到一个短的x向量

“Position-wise Feed-Forward Networks”

位置前馈网络,全连接前馈网络,两个线性变换组成,第一个全连接层的激活函数为 ReLU 激活函数。

“Embeddings and Softmax” 残差连接和归一化

add & normalize 具体操作:

“Positional Encoding” 位置编码

位置编码原因:Transformer Architecture: The Positional Encoding - Amirhossein Kazemnejad's Blog

一文教你彻底理解Transformer中Positional Encoding - 知乎 (zhihu.com)

表示序列中词顺序的方法。

从此开始是补充:

Decoder——解码器

复习一下解码器结构:

解码器工作流程:  

哪位大神讲解一下Transformer的Decoder的输入输出都是什么?能解释一下每个部分都是什么? - 知乎

Transformer Decoder的输入:

  • 初始输入:前一时刻Decoder输入+前一时刻Decoder的预测结果 + Positional Encoding
  • 中间输入:最后一个编码器的输出是一组注意力向量 Key 和 Value(Decoder输出的第一个单词由此决定)。这些向量将在每个解码器的 Encoder-Decoder Attention 层被使用,这有助于解码器把注意力集中在输入序列的合适位置。

Masked Multi-Head Attention

“This masking, combined with fact that the output embeddings are offset by one position, ensures that the predictions for position i can depend only on the known outputs at positions less than i.” [1] 这种掩蔽,结合输出嵌入被一个位置抵消的事实,保证了对位置i的预测只能依赖于小于i位置的已知输出。

“We implement this inside of scaled dot-product attention by masking out (setting to −∞) all values in the input of the softmax which correspond to illegal connections.” [2] 我们通过屏蔽softmax输入中与非法连接相对应的所有(设置为-∞)值来实现缩放点积注意力的内部。

什么是Mask

mask表示掩码,它对某些值进行掩盖,使其在参数更新时不产生效果。Transformer模型里面涉及两种mask,分别是 padding mask和sequence mask。 其中,padding mask在所有的scaled dot-product attention 里面都需要用到,而sequence mask只有在Decoder的Self-Attention里面用到。

Padding Mask

什么是padding mask呢?因为每个批次输入序列长度是不一样的也就是说,我们要对输入序列进行对齐。具体来说,就是给在较短的序列后面填充0。但是如果输入的序列太长,则是截取左边的内容,把多余的直接舍弃。因为这些填充的位置,其实是没什么意义的,所以我们的Attention机制不应该把注意力放在这些位置上,所以我们需要进行一些处理。

具体的做法是,把这些位置的值加上一个非常大的负数(负无穷),这样的话,经过softmax,这些位置的概率就会接近0! 而我们的padding mask 实际上是一个张量,每个值都是一个Boolean,值为false的地方就是我们要进行处理的地方。

Sequence mask

文章前面也提到,sequence mask是为了使得Decoder不能看见未来的信息。也就是对于一个序列,在time_step为t的时刻,我们的解码输出应该只能依赖于t时刻之前的输出,而不能依赖t之后的输出。因此我们需要想一个办法,把t之后的信息给隐藏起来。 那么具体怎么做呢?也很简单:产生一个上三角矩阵,上三角的值全为0。把这个矩阵作用在每一个序列上,就可以达到我们的目的。

对于Decoder的Self-Attention,里面使用到的scaled dot-product attention,同时需要padding mask和sequence mask作为attn_mask,具体实现就是两个mask相加作为attn_mask。

其他情况,attn_mask一律等于padding mask。

举个例子:

假设最大允许的序列长度为10,先令padding mask为

[0 0 0 0 0 0 0 0 0 0]

然后假设当前句子一共有5个单词(加一个起始标识),在输入第三个单词的时候,前面有一个开始标识和两个单词,则此刻的sequence mask为

[1 1 1 0 0 0]

然后padding mask和sequence mask相加,得

[1 1 1 0 0 0 0 0 0 0]

Conclusion——对未来的展望

“We plan to extend the Transformer to problems involving input and output modalities other than text and to investigate local, restricted attention mechanisms to efficiently handle large inputs and outputs such as images, audio and video.” (Vaswani 等。, 2017, p. 10) 我们计划将Transformer扩展到涉及文本以外的输入和输出模式的问题,并研究本地的、受限制的注意力机制,以有效地处理图像、音频和视频等大型输入和输出。
对未来的展望。

读论文及组会感想:

1. 不要过于拘泥于细节实现,而要多思考为什么要这么做。

2. 多搜相关论文博客笔记看,不要浅尝辄止。

3. 读论文不要太心急,读得多不比读得多重要。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值