
Attention is All You Need
Attention is All You Need
Features
- 完全抛弃了 RNN、LSTM、GRU 中的 recurrent layer,摆脱了序列化计算的限制(难以并行);
- 完全基于注意力机制;
- 训练速度很快,效果好;
Model Architecture

Attention
Scaled Dot-Product Attention

In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix
. The keys and values are also packed together into matrices and . We compute the matrix of outputs as:
Expand to see computation details
computation:single input
首先从单个 input 的计算过程看起,这里假设

随后,
得出
Note
query 的来源并不一定与 key-value pair 相同,其数目也不一定相同。如果是上述情况,则称为 self-attention。
computation:batch
接下来就是用矩阵运算来一次性处理整个 batch。
假设批次大小为
首先,所有 input 的
然后,使用

形式化表示如下:
Multi-Head Attention

Instead of performing a single attention function with
-dimensional keys, values and queries, we found it beneficial to linearly project the queries, keys and values h times with different, learned linear projections to , and dimensions, respectively.
直观上理解,就是将
其中,投影矩阵
整个模型的可学参数为
Note
要注意这里的
Applications in the Model
三个输入依次为
- Encoder 中的 self-attention:用于将抽取输入序列的信息,输出 n 个
的向量。 - Decoder 中的 masked attention:在训练过程中,对于单个 sentence,例如“Bob is a boy”,我们希望将每个子句都当作一个训练用例,即“Bob [is]”、“Bob is [a]”、“Bob is a [boy]”。在预测当前位置时,是无法获取后续 token 的信息的。但从原理上讲,attention 会使用到所有的 key、query 值以计算权重矩阵。所以,需要通过 mask,将权重矩阵中对应的项置为0。又因为 mask 在 softmax 之前,所以实际上是将对应的项置为
。而在推理过程中,并不需要 mask,因为此时待预测的 token 一定位于句末。 - cross-attention:key-value 来自 encoder,而 query 来自 decoder。
Position-wise Feed-Forward Networks
对于 attention 模块的每个输出,进行一个全连接层(共享权重)计算:
其中,
Position-wise 指的是输入仅为单个向量,不需要考虑前后的向量,这是由于 attention 模块已经抽取了各输入序列之间的关联信息,这里的任务仅仅是将 attention 的输出映射到目标语义空间。
Embedding and Softmax
用于将输入 token 映射为
权重需要乘上
Positional Encoding
Attention 的计算过程不考虑时序信息,例如将一句话的单词顺序打乱,其 attention 计算结果一致。本文通过为 token 的位置 pos 计算一个
Train and Inference
以语音转文字为例,解释 transformer 的训练与推理过程。
Inference

encoder 部分只执行一次:将输入的“机器学习“语音 token 编码为 4 个
decoder 部分执行若干次:
- 首先以 BOS 为输入,输出 1 个
的 embedding,经过 Linear 与 Softmax 之后,得到词汇表上的概率,根据概率,选择输出“机”; - 随后,以 BOS 与“机” 为输入,输出 2 个
的 embedding,其中第 1 个 embedding 与第 1 轮一致,第二个 embedding 输出 “器”; - 重复上述过程,直到在某一轮中,最后一个 token 的预测输出为 EOS。
Train

与 inference 的区别在于,输入 embedding 不是上一轮的输出,而是 ground truth(相当于避免上一轮的结果影响这一轮的预测)。可以一次性计算 loss,只需要在 masked attention 中对每个位置加上不同的 mask 即可。
Comprehension
以下内容来自对文章以及 :Attention in transformers, visually explained 的个人理解,如有错误,敬请指正。
- 在 encoder 中,输入 token 之间在做 self-attention,其本质是修改 token 的初始 embedding(n token 输入,n value 输出,刚好一一对应),使其具有上下文信息。例如,“Miniature Eiffel” 与 “Great Eiffel” 中的 “Eiffel”,其初始 embedding 完全一样,而每经过一轮 self-attention,这两个 “Eiffel” 的 embedding 会被更新到不同的方向上。那么,对于同一序列中的 token 呢?例如,对于“Happy is a Happy dog” 中的两个 “Happy”,他们进行 attention 操作的结果应该完全相同,更新的结果也应该相同。别忘了 Positinal Encoding,这两个 Happy 的初始 embedding 已经包含了位置信息,所以并不一致。
- multi-head attention 中投影矩阵的实际含义:例如 “Eiffel” 的 embedding,作为一个名词,其投影得到的 query 将会与形容词、量词投影得到的 key 产生更大的内积,从而在对 value 加权求和时,形容词、量词对应 value 的权重更高,对 “Eiffel” 的影响越大。而形容词、量词投影得到的 value 则表示了其对于改变其他 embedding 语义的能力。
- transformer decoder 的两个 attention 模块分别用于抽取在结果序列中的信息,以及融合输入序列的信息来指导 next token 的生成。