NLP

Attention is all you need

来自Google的一篇神经翻译的文章,在这篇文章中作者们抛弃了传统Encoder-Decoder中经典的卷积和循环结构,仅保留了attention的结构,在减少了训练成本的同时在数个数据集上取得了最优的BLEU.
paper link

Background

What is attention?

An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.

attention

在计算 Attention 时主要分为三步,第一步是将 query 和每个 key 进行相似度计算得到权重,常用的相似度函数有点积,拼接,感知机等;然后第二步一般是使用一个 softmax 函数对这些权重进行归一化;最后将权重和相应的键值 value 进行加权求和得到最后的 Attention。

Transformer Model Architecture

The  Transformer  -  model  architecture

Encoder

Encoder部分由6个相同的layer堆叠而成,其中,每个layer由两个sub-layer构成(上图中的左半部分,N=6)。第一个sub-layer使用multi-head self-attention机制实现,第二个sub-layer则是一个简单的全连接前馈神经网络。在每个sub-layer上都使用了残差连接以及layer normalization,即每个sub-layer的输出为 $LayerNorm(x+Sublayer(x))$ 。

Decoder

Decoder同样由6个相同的layer堆叠而成。其中,每个layer由三个sub-layer构成:其中两个与encoder相同,另外一个则是masked multi-head self-attention sub-layer,用于在训练时过滤掉不合法的连接(当前的生成过程不应当依赖于将来生成的值)。类似于encoder,decoder的每个sub-layer上也使用了残差连接与layer normalization。

Attention

Scaled Dot-Product Attention

输入是$d_{k}$维的query和key,value的维度为$d_{v}$。其操作步骤为:首先计算query与所有key的内积,然后再除以$\sqrt{d_{k}}$,并使用softmax获取value的权值,最后加权求和得到相应的输出。在Scaled Dot-Product Attention上加上一个Mask(仅在decoder的Masked Multi-head Attention中使用)单元,可以用于处理训练时的一些不合法信息流动的问题。

计算公式:

在实际操作里,通常Q是一系列query vectors的组合,$Q \in R^{ m \times d_{k}}$中的每一行对应于一个query;Q与$K^{T}\in\mathbb{R}^{d_{v}\times n}$相乘并进行scale之后,即可得到$m \times n$ 的矩阵,其中每一行都对应着一个 query vector的attention score;之后进行softmax归一化;然后,再将这个attention matrix与$V\in{\mathbb{R}^{n\times d_{v}}}$相乘,即可得到一个 $m \times d_{v}$ 维的attention output,其中每一行对应一个query output。

其中$\sqrt{d_{k}}$因子起到调节作用,使得内积不至于太大。

Multi-Head Attention

Query,Key,Value 首先经过一个线性变换,然后输入到放缩点积 Attention,注意这里要做 h 次,其实也就是所谓的多头,每一次算一个头。而且每次 Q,K,V 进行线性变换的参数 W 是不一样的。然后将 h 次的放缩点积 Attention 结果进行拼接,再进行一次线性变换得到的值作为多头 Attention 的结果。

Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.

Applications of Attention in Transformer Model

1)在encoder-decoder attention层,query来自于先前的解码层,key与value来自于encoder的输出。

2)在encoder上单独使用了self-attention。这里的key,value,query都来自于encoder中上一层的输出。

3)在decoder上也单独使用了self-attention。这里的key,value,query来自于decoder中当前时间步及之前的输出。为了避免信息向左流动,在scaled dot-product attention中增加了一个屏蔽层(mask),用以屏蔽掉那些softmax中的不合法连接(仅在训练时发挥作用)。

Attention visualization

Position-wise Feed-Forward Networks

Position-wise Feed-Forward Networks使用两层线性变换与一个ReLU激活函数实现:

Positional Encoding

Since our model contains no recurrence and no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute position of the tokens in the sequence. To this end, we add “positional encodings” to the input embeddings at the bottoms of the encoder and decoder stacks. The positional encodings have the same dimension $d_{model}$ as the embeddings, so that the two can be summed.

其中,_pos_表示position,$2i$ 或 $2i+1$ 表示对应的维度。这种PE的特点在于,对于相对位置偏移 $k$ 来说,$PE_{pos+k}$ 可以表示为 $PE_{pos}$ 的线性函数,这提供了表达相对位置信息的可能性。

Google 在论文中说到他们比较过直接训练出来的位置向量和上述公式计算出来的位置向量,效果是接近的。

Experiment

Reference