论文精读:Attention Is All You Need
论文原名:Attention Is All You Need 作者团队:Google Brain (Ashish Vaswani, Noam Shazeer 等) 发表年份:2017 (NIPS) 地位:深度学习历史上最重要的论文之一,提出的 Transformer 架构开启了如今的大模型时代(GPT, BERT, Claude 等均源于此)。
导读:阅读指南
在 Transformer 出现之前,处理语言任务主要靠 RNN(循环神经网络)或 CNN(卷积神经网络)。
- RNN 像接力赛跑者,必须串行处理,速度慢。
- Transformer 不需要递归和卷积,只需要注意力机制。这好比预制板技术,可以并行组装,速度快且结构稳固。
1. Abstract (摘要)
原文核心: "We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely."
解读:
- 抛弃旧皇:不再使用 RNN 和 CNN。
- 拥立新王:提出 Transformer,完全基于 Attention。
- 战绩:在 WMT 2014 英语 - 德语翻译任务上拿到 28.4 BLEU 分(当时历史最高),训练时间仅需 3.5 天。
划重点:
- 并行化:最大的卖点。Transformer 可一次性计算整个句子,无需串行。
- 质量更优:不仅快,翻译准确率也更高。
2. Introduction (引言)
原文核心: "Recurrent models typically factor computation along the symbol positions of the input and output sequences… This inherently precludes parallelization within training examples."
解读: 作者详细分析了 RNN(包括 LSTM 和 GRU)的痛点:
- 无法并行:第 N 个词的处理依赖前一个词的记忆,无法并行计算。
- 长距离遗忘:读到第 1000 个字时,可能已忘记第 1 个字的信息。
Transformer 的方案:
- 不再按顺序读,通过 Attention 直接查看任意位置信息。
- 一眼看全篇(Global Dependency)。无论两个词相隔多远,路径长度均为 O(1)。
3. Model Architecture (模型架构)
原文核心: "The Transformer follows this overall architecture using stacked self-attention and point-wise, fully connected layers for both the encoder and decoder."
解读: Transformer 沿用经典的 Encoder-Decoder 架构。 想象同声传译过程:
- Encoder(编码器):负责'听'和'理解',将输入转换为深层语义向量。
- Decoder(解码器):负责'说',根据语义生成输出序列。
结构大解剖
1. 堆叠(Stacked)
- Encoder 有 6 层。每层包含两个子层:
- Multi-Head Self-Attention(多头自注意力)
- Feed-Forward Network(前馈神经网络)
- Decoder 也有 6 层。每层包含三个子层:
- Masked Multi-Head Self-Attention(带掩码的自注意力)
- Multi-Head Attention(交叉注意力)
- Feed-Forward Network
2. 残差连接与归一化 (Add & Norm)
在每个子层后跟 LayerNorm(x + Sublayer(x))。
- Add(残差连接):防止深层网络梯度消失。
- Norm(层归一化):稳定训练数据分布。
4. Attention Mechanism (注意力机制)
这是论文的核心部分。
4.1 Scaled Dot-Product Attention (缩放点积注意力)
公式:
Attention(Q, K, V) = softmax(QK^T / sqrt(d_k))V
通俗版:图书检索系统
- Query (Q):读者的查询指令。
- Key (K):书籍的标签/索引。
- Value (V):书的具体内容。
过程:
- 匹配:拿 Q 与 K 对比,得到匹配度。
- 缩放:除以
sqrt(d_k),防止数值过大导致 Softmax 饱和。 - 归一化:Softmax 将匹配度转为概率。
- 加权求和:根据概率混合 V 的内容。
4.2 Multi-Head Attention (多头注意力)
原文核心: "Instead of performing a single attention function… we found it beneficial to linearly project the queries, keys and values h times…"
解读: 为什么要做 Multi-Head?单一 Attention 可能只关注一种关系(如语义)。多头允许模型同时关注不同子空间的关系(如语法、指代等)。
- 类比:不同颜色的滤镜观察同一幅画,最后拼合为完整画面。
参数:
- h = 8(8 个头)
- d_model = 512(总维度)
- d_k = d_v = 64(每个头的维度)
4.3 Attention 的三种用法
- Encoder-Decoder Attention:Q 来自 Decoder,K/V 来自 Encoder。用于翻译时查找原文线索。
- Encoder Self-Attention:Q/K/V 均来自 Encoder。用于理解原文内部逻辑(如词义消歧)。
- Masked Decoder Self-Attention:Q/K/V 均来自 Decoder。使用 Mask 遮住未来信息,防止作弊。
5. Details (细节)
5.1 Position-wise Feed-Forward Networks
公式:
FFN(x) = max(0, xW1 + b1)W2 + b2
即:线性变换 -> ReLU 激活 -> 线性变换。
- 维度变化:512 -> 2048 -> 512。
- 作用:Attention 收集信息,FFN 消化信息。
5.2 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…"
解读: Transformer 没有时间观念,需注入位置信息。论文使用正弦/余弦函数:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
为什么不用简单的 1, 2, 3?
- 避免数值过大干扰模型。
- Sin/Cos 有界且能表达相对位置关系。
6. Why Self-Attention (原理解析)
| 层类型 | 每层复杂度 | 顺序操作数 | 最大路径长度 |
|---|---|---|---|
| Self-Attention | O(n^2 · d) | O(1) | O(1) |
| Recurrent (RNN) | O(n · d^2) | O(n) | O(n) |
| Convolutional | O(k · n · d^2) | O(1) | O(log_k(n)) |
深度解读:
- 计算量:当句子短而维度大时,Transformer 比 RNN 省计算量。
- 并行度:Transformer 是 O(1),无需排队;RNN 是 O(n)。
- 长距离依赖:Transformer 任意两词距离恒为 1;RNN 最坏情况需走 n 步。
7. Training (训练细节)
- Optimizer:使用 Adam,配合 Warmup 学习率调度器。
- Regularization:
- Residual Dropout:P_drop = 0.1,防止过拟合。
- Label Smoothing:epsilon_ls = 0.1,强制模型输出留有余地,提升泛化能力。
8. Results (结果)
在 WMT 2014 翻译任务上:
- English-to-German:Transformer (Big) 达到 28.4 BLEU,比冠军高 2.0 分以上,训练成本仅为 1/4。
- English-to-French:达到 41.0 BLEU,刷新记录。
证明了 Transformer 既快(并行训练)又好(结构优越)。
9. 总结
- Attention Is ALL You Need:不需要 RNN 和 CNN,注意力机制足够强大。
- 并行是王道:使大规模预训练成为可能。
- 位置编码:通过数学手段找回了位置信息。
这篇论文提供了通用的序列处理框架。如今,文本(GPT)、图片(Vision Transformer)、音频及蛋白质结构预测(AlphaFold)均在使用 Transformer 架构。它是现代 AI 的基石。
建议参考 PyTorch 官方 nn.Transformer 实现,结合代码理解矩阵运算原理。


