Attention Is All You Need

论文信息

项目 内容
标题 Attention Is All You Need
作者 Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin
机构 Google Brain, Google Research, University of Toronto
发表 NeurIPS 2017
引用 Vaswani et al. (2017). “Attention Is All You Need”. Advances in Neural Information Processing Systems.

📝 摘要 (Abstract)

本文提出 Transformer —— 一种全新的网络架构,完全基于注意力机制,摒弃了循环和卷积。

核心贡献:

  • 提出 Transformer 架构,仅使用注意力机制
  • 在机器翻译任务上达到 state-of-the-art
  • 训练效率大幅提升(12 小时,8 个 P100 GPU)
  • 解决了序列建模中的长距离依赖问题

1️⃣ 引言 (Introduction)

背景问题

传统的序列模型(RNN、LSTM、GRU)存在以下限制:

问题 描述
顺序计算 无法并行化处理序列
长距离依赖 距离越远,依赖关系越难学习
训练效率低 序列长度限制 batch 大小

Transformer 的优势

RNN/LSTM:     输入 → [RNN] → [RNN] → [RNN] → 输出  (顺序处理)
Transformer:  输入 → [Self-Attention] → 输出        (并行处理)

2️⃣ 背景 (Background)

2.1 减少顺序计算的努力

模型 方法 距离复杂度
Extended Neural GPU 卷积 O(1)
ByteNet 卷积 O(log n)
ConvS2S 卷积 O(n)
Transformer 自注意力 O(1)

2.2 注意力机制

自注意力 (Self-Attention):关联同一序列中不同位置,计算序列的表示。

已成功应用于:

  • 阅读理解
  • 抽象摘要
  • 文本蕴含
  • 句子表示学习

3️⃣ 模型架构 (Model Architecture)

3.1 整体结构

┌─────────────────────────────────────┐
│           Decoder Stack             │
│  ┌───────────┐  ┌───────────┐       │
│  │ Add&Norm  │  │ Add&Norm  │       │
│  │    ↑      │  │    ↑      │       │
│  │ Multi-Head│  │ Multi-Head│       │
│  │ Attention │  │ Attention │       │
│  │    ↑      │  │    ↑      │       │
│  └───────────┘  │  Encoder  │       │
│                 │  Output   │       │
└─────────────────┴───────────────────┘
           ↑
    ┌──────────────┐
    │Encoder Stack │
    │  ┌────────┐  │
    │  │Add&Norm│  │
    │  │   ↑    │  │
    │  │Multi-  │  │
    │  │Head    │  │
    │  │Attention│ │
    │  │   ↑    │  │
    │  │Position│  │
    │  │Feed    │  │
    │  │Forward │  │
    │  └────────┘  │
    └──────────────┘
           ↑
    Input Embeddings + Positional Encoding

3.2 编码器 - 解码器架构

组件 层数 功能
Encoder 6 层 处理输入序列
Decoder 6 层 生成输出序列
每层子层 2 个 Multi-Head Attention + Feed Forward

3.3 注意力函数

Scaled Dot-Product Attention:

Attention(Q, K, V) = softmax(QK^T / √d_k) V
符号 含义 维度
Q Query n × d_k
K Key m × d_k
V Value m × d_v
d_k Key 维度 64
d_v Value 维度 64

3.4 多头注意力 (Multi-Head Attention)

MultiHead(Q, K, V) = Concat(head_1, ..., head_h) W^O
where head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)

参数:

  • h = 8 (头数)
  • d_model = 512
  • d_k = d_v = d_model/h = 64

优势:

  • 并行关注不同位置的信息
  • 增强模型表示能力

3.5 位置编码 (Positional Encoding)

由于 Transformer 没有循环/卷积,需要注入位置信息:

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))

4️⃣ 为什么用自注意力 (Why Self-Attention)

计算复杂度对比

层类型 每层复杂度 顺序操作 最大路径长度
Self-Attention O(n²·d) O(1) O(1)
Recurrent O(n·d²) O(n) O(n)
Convolutional O(k·n·d²) O(1) O(log_k n)

结论: 自注意力在长距离依赖学习上更高效。


5️⃣ 训练细节 (Training)

数据集

数据集 语言对 训练集大小
WMT 2014 英语→德语 4.5M 句对
WMT 2014 英语→法语 36M 句对

硬件与时间

配置 训练时间
8 × P100 GPU 12 小时 (base)
8 × P100 GPU 3.5 天 (big)

优化器

Adam optimizer: β₁=0.9, β₂=0.98, ε=10⁻⁹
学习率调度:warmup + decay

正则化

  • Dropout (P_drop = 0.1)
  • Label Smoothing (ε_ls = 0.1)
  • Weight Decay

6️⃣ 实验结果 (Results)

机器翻译性能

模型 BLEU (EN-DE) BLEU (EN-FR) 训练成本
ByteNet 23.75 - -
ConvS2S 25.16 40.46 -
GNMT+RL 24.6 39.92 -
MoE 26.03 40.56 -
Transformer (base) 27.3 38.1 12 小时
Transformer (big) 28.4 41.8 3.5 天

消融实验

变体 BLEU (EN-DE)
Transformer (big) 28.4
No LayerNorm 24.5
Single Head Attention 26.9
Limited Positional Encoding 27.5

7️⃣ 注意力可视化 (Attention Visualization)

示例:自注意力模式

输入:The animal didn't cross the street because it was too tired.
                    ↓
注意力:"it" → "animal" (高权重)

发现:

  • 模型能学习长距离依赖
  • 不同头关注不同语法关系
  • 可以捕捉句法结构

8️⃣ 结论 (Conclusion)

主要贡献

  1. ✅ 提出 Transformer —— 首个完全基于注意力的 transduction 模型
  2. ✅ 在机器翻译任务上达到 SOTA
  3. ✅ 训练效率显著提升
  4. ✅ 解决了长距离依赖问题

未来方向

  • 应用到其他 NLP 任务
  • 探索更大的模型
  • 研究多模态应用

📚 参考文献 (References)

  1. [Bahdanau et al., 2014] Neural Machine Translation by Jointly Learning to Align and Translate
  2. [Vaswani et al., 2017] Attention Is All You Need (原始论文)
  3. [Devlin et al., 2018] BERT: Pre-training of Deep Bidirectional Transformers
  4. [Radford et al., 2018] Improving Language Understanding by Generative Pre-Training

🔑 关键公式汇总

Scaled Dot-Product Attention

Attention(Q, K, V) = softmax(QK^T / √d_k) V

Multi-Head Attention

MultiHead(Q, K, V) = Concat(head_1, ..., head_h) W^O
head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)

Positional Encoding

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))

Layer Normalization

LayerNorm(x) = γ ⊙ (x - μ) / σ + β

Markdown 转换完成 | 基于《Attention Is All You Need》PDF 内容提取与整理

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐