Attention Is All You Need
简介
这篇paper算是Attention机制最好的paper之一。对于机器翻译问题,2017年之前,占主导地位的都是比较复杂的RNN和CNN模型,由encoder接decoder组成。这些模型的输出序列 h t h_t ht是由之前的隐藏状态 h t − 1 h_{t-1} ht−1和当前的输入 t t t决定,这就导致了只能一个一个计算 h t h_t ht,无法进行并行化处理。于是这篇paper提出了名为Transformer的模型架构,摒弃了RNN,完全依赖于attention机制,在输入输出之间做了一个全局的依赖。该模型允许极大的并行化处理,训练速度非常快,且达到了state-of-the-art的性能。
模型架构
如下图所示,整体上看,仍然是一个encoder接一个decoder的模式。输入方面,会用word embedding的方法把句子转为tokens向量作为输入。为了弥补没有RNN/CNN,无法利用句内顺序信息的缺点,加入了positional encoding的做法,即对于每一个输入的词向量,加入一定的抖动,抖动公式如下:
P
E
p
o
s
,
2
i
=
s
i
n
(
p
o
s
1000
0
2
i
d
m
o
d
e
l
)
PE_{pos,2i} = sin(\frac{pos}{10000^{\frac{2i}{d_{model}}}})
PEpos,2i=sin(10000dmodel2ipos)
P
E
p
o
s
,
2
i
+
1
=
c
o
s
(
p
o
s
1000
0
2
i
d
m
o
d
e
l
)
PE_{pos,2i+1} = cos(\frac{pos}{10000^{\frac{2i}{d_{model}}}})
PEpos,2i+1=cos(10000dmodel2ipos)
这里pos是第几位,i是维度。
![Snip20201013_1.png](https://img-blog.csdnimg.cn/img_convert/efa7f0792ee4f345a7be7d49d4b1e4d3.png#align=left&display=inline&height=484&margin=[object Object]&name=Snip20201013_1.png&originHeight=968&originWidth=772&size=178838&status=done&style=none&width=386)
什么是Attention:Attention其实就是将query,一个key-value pair集合进行映射成一个output,公式为:
o
u
t
p
u
t
=
s
i
m
i
l
a
r
i
t
y
(
Q
K
T
)
V
output = similarity(QK^T)V
output=similarity(QKT)V
**Scaled Dot-Product Attention:**本文用了一个特殊的attention,名为“Scaled Dot-Product Attention”,其公式是:
A
t
t
e
n
t
i
o
n
(
Q
,
K
,
V
)
=
s
o
f
t
m
a
x
(
Q
K
T
d
k
)
V
Attention(Q,K,V) = softmax(\frac{QK^T}{\sqrt{d_k}})V
Attention(Q,K,V)=softmax(dkQKT)V
这里用了dot-product attention而不是additive attention是因为乘法能够更好的并行,在内存上也更有优势。有个
1
d
k
\frac{1}{\sqrt{d_k}}
dk1是因为作者怀疑对于比较大的
d
k
d_k
dk,点乘的值会变得很大,会导致softmax的功能不好,于是做了一个sacle处理。
Multi-Head Attention: 如下图所示,Query,Key,Value首先进过一个线性变换,然后输入到放缩点积attention,注意这里要做h次,其实也就是所谓的多头,每一次算一个头。而且每次Q,K,V进行线性变换的参数W是不一样的。然后将h次的放缩点积attention结果进行拼接,再进行一次线性变换得到的值作为多头attention的结果。可以看到,google提出来的多头attention的不同之处在于进行了h次计算而不仅仅算一次,论文中说到这样的好处是可以允许模型在不同的表示子空间里学习到相关的信息。公式如下:
M
u
l
t
i
H
e
a
d
(
Q
,
K
,
V
)
=
C
o
n
c
a
t
(
h
e
a
d
1
,
.
.
.
,
h
e
a
d
h
)
W
O
MultiHead(Q,K,V) = Concat(head_1,...,head_h)W^O
MultiHead(Q,K,V)=Concat(head1,...,headh)WO
w
h
e
r
e
h
e
a
d
i
=
A
t
t
e
n
t
i
o
n
(
Q
W
i
Q
,
K
W
i
K
,
V
W
i
V
)
where head_i = Attention(QW_i^Q,KW_i^K,VW_i^V)
whereheadi=Attention(QWiQ,KWiK,VWiV)
![Snip20201013_2.png](https://img-blog.csdnimg.cn/img_convert/2ae13bbcb7bfbf4142760a3b34cd4994.png#align=left&display=inline&height=271&margin=[object Object]&name=Snip20201013_2.png&originHeight=542&originWidth=482&size=68185&status=done&style=none&width=241)
该模型中Attention机制的应用:
首先在编码器到解码器的地方使用了多头attention进行连接,K,V,Q分别是编码器的层输出(这里K=V)和解码器中都头attention的输入。其实就和主流的机器翻译模型中的attention一样,利用解码器和编码器attention来进行翻译对齐。然后在编码器和解码器中都使用了多头自注意力self-attention来学习文本的表示。Self-attention即K=V=Q,例如输入一个句子,那么里面的每个词都要和该句子中的所有词进行attention计算。目的是学习句子内部的词依赖关系,捕获句子的内部结构。
**
为什么要self-Attention:
主要从三个方面来进行考虑,每一层的复杂度,是否可以并行,长距离依赖学习,和RNN,CNN的比较结果见下图。可以看出,如果输入序列n小于表示维度d的话,每一层的时间复杂度self-attention是比较有优势的。当n比较大时,作者也给出了一种解决方案self-attention(restricted)即每个词不是和所有词计算attention,而是只与限制的r个词去计算attention。在并行方面,多头attention和CNN一样不依赖于前一时刻的计算,可以很好的并行,优于RNN。在长距离依赖上,由于self-attention是每个词和所有词都要计算attention,所以不管他们中间有多长距离,最大的路径长度也都只是1。可以捕获长距离依赖关系。
![Snip20201013_3.png](https://img-blog.csdnimg.cn/img_convert/a177aec26badb2cb32f560270d464ce2.png#align=left&display=inline&height=252&margin=[object Object]&name=Snip20201013_3.png&originHeight=252&originWidth=1106&size=49101&status=done&style=none&width=1106)
实验设置
训练数据集用了WMT 2014 English-German和English-French dataset,硬件方面用8个NVIDIA P100 GPU,优化器用了Adam optimizer,学习率用了一个很有意思的的调整方法,整体呈现一个先增后减的趋势,在中期的学习率最大,公式见下:
l
r
a
t
e
=
d
m
o
d
e
l
−
0.5
⋅
m
i
n
(
s
t
e
p
_
n
u
m
−
0.5
,
s
t
e
p
_
n
u
m
⋅
w
a
r
m
u
p
_
s
t
e
p
s
−
1.5
)
lrate = d_{model}^{-0.5}·min(step\_num^{-0.5},step\_num·warmup\_steps^{-1.5})
lrate=dmodel−0.5⋅min(step_num−0.5,step_num⋅warmup_steps−1.5)
这样保证了更新的效果。训练的效果和成本见下表,可以发现这个模型的BLEU提高了,且训练成本下降了。![Snip20201013_4.png](https://img-blog.csdnimg.cn/img_convert/832a33efe0c9ae42a4f1d180e576df48.png#align=left&display=inline&height=424&margin=[object Object]&name=Snip20201013_4.png&originHeight=424&originWidth=1072&size=102069&status=done&style=none&width=1072)
实验结果与结论
该模型在WMT2014 English-to-German和English-to-French上都取得了state-of-the-art的效果,同时作者还做了几个超参对模型的影响评估,结果见下表。可以发现,1)multi-head attention确实可以提高效果,但当head的数量过多时,效果反而会下降,经过实验,作者发现8 heads时效果比较好,2)减少attention key size KaTeX parse error: Expected group after '_' at position 2: d_̲会使得效果下降,这说明可能需要一个比点乘更复杂的函数来进行similarity的评估。
![Snip20201013_5.png](https://img-blog.csdnimg.cn/img_convert/ae6b80bf3bf4f5eaab3f62b5ee102c3c.png#align=left&display=inline&height=722&margin=[object Object]&name=Snip20201013_5.png&originHeight=722&originWidth=1126&size=122286&status=done&style=none&width=1126)
作者也将transformer运用到了英语句法分析的问题上,发现效果也蛮不错的,效果见下表:![Snip20201013_6.png](https://img-blog.csdnimg.cn/img_convert/6252d45ac90642d27afa4f31d3f470a1.png#align=left&display=inline&height=406&margin=[object Object]&name=Snip20201013_6.png&originHeight=406&originWidth=918&size=118078&status=done&style=none&width=918)
总结与感想
本篇paper作为第一篇提出self-attention和transformer的paper,是非常具有划时代意义的。这篇paper提出来的新架构使得NLP算法重新回到能够并行化计算的时代,使得机器翻译等算法能够在日常生活中被部署。