GPT-2 中的残差权重初始化

GPT-2 中的残差权重初始化

1. 概述

在深度残差网络中,每一层的输出都会被加到其输入上。如果不对这些层的权重进行特殊处理,随着网络深度的增加,残差路径上累加的信号方差可能会不断增大,导致数值不稳定和训练发散。

为了解决这个问题,GPT-2 的作者在论文中提出了一种针对残差连接路径上的层进行特殊缩放的初始化方法

2. 哪些是"残差权重"?

在 GPT-2 的一个 Decoder Block 中,有两个子层的输出会直接被加到残差流(Residual Stream)上。因此,这两层的权重就是我们所说的"残差权重":

  1. 多头自注意力层的输出投影层 : 在 Hugging Face 的实现中,这层通常被称为 c_proj
  2. 前馈网络 (FFN) 的第二个线性层 : 这也是一个投影层,同样被称为 c_proj

3. GPT-2 的初始化策略

GPT-2 的权重初始化分为两个步骤:一个通用的标准初始化,和一个针对上述"残差权重"的特殊缩放。

步骤一:通用的标准初始化

模型中的所有权重(包括嵌入层、QKV 投影层、FFN第一层以及残差层)首先都会从一个均值为 0、标准差为 0.02 的正态分布中进行初始化。

  • 权重 (Weights) : <math xmlns="http://www.w3.org/1998/Math/MathML"> W ∼ N ( 0 , 0.0 2 2 ) W \sim \mathcal{N}(0, 0.02^2) </math>W∼N(0,0.022)
  • 偏置 (Biases) : 所有偏置都初始化为 0。

这是模型参数的基础初始化值。

步骤二:针对残差权重的特殊缩放

在完成通用初始化之后,GPT-2 会专门对"残差权重"进行一次额外的缩放操作。

缩放公式:

根据 GPT-2 论文的描述,这些残差层的权重会被乘以一个缩放因子:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> 1 N \frac{1}{\sqrt{N}} </math>N 1

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N 是模型中残差层(或说残差连接)的总数量

  • 在一个标准的 GPT-2 模型中,每个 Transformer Block(n_layer)都包含 2 个残差连接(一个在自注意力后,一个在 FFN 后)。
  • 因此,总的残差层数量 <math xmlns="http://www.w3.org/1998/Math/MathML"> N = 2 × n layer N = 2 \times n_{\text{layer}} </math>N=2×nlayer。

例如:

  • 对于 gpt2-base,它有 12 个 Block (n_layer=12),所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> N = 2 × 12 = 24 N = 2 \times 12 = 24 </math>N=2×12=24。缩放因子就是 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 24 \frac{1}{\sqrt{24}} </math>24 1。
  • 对于 gpt2-large,它有 36 个 Block (n_layer=36),所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> N = 2 × 36 = 72 N = 2 \times 36 = 72 </math>N=2×36=72。缩放因子就是 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 72 \frac{1}{\sqrt{72}} </math>72 1。

这个操作通常是在代码层面,将这些特定层的权重张量乘以该缩放因子来完成。

4. 为什么要进行特殊缩放?

核心目的:控制残差流中的方差累积。

  • 问题 : 在一个深度网络中,残差流 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x 会经过多个 Block 的累加: <math xmlns="http://www.w3.org/1998/Math/MathML"> x final = x initial + output 1 + output 2 + ⋯ + output N x_{\text{final}} = x_{\text{initial}} + \text{output}_1 + \text{output}_2 + \dots + \text{output}_N </math>xfinal=xinitial+output1+output2+⋯+outputN。如果每个 <math xmlns="http://www.w3.org/1998/Math/MathML"> output i \text{output}_i </math>outputi 的方差是 <math xmlns="http://www.w3.org/1998/Math/MathML"> σ 2 \sigma^2 </math>σ2,那么在理想情况下,最终输出的方差会累积到 <math xmlns="http://www.w3.org/1998/Math/MathML"> N × σ 2 N \times \sigma^2 </math>N×σ2。当 <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N 很大时,方差会爆炸,导致训练不稳定。
  • 解决方案 : 通过将每个残差层的权重乘以 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 N \frac{1}{\sqrt{N}} </math>N 1,其输出的方差大约会被缩放到原来的 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 N \frac{1}{N} </math>N1(因为方差与权重的平方成正比)。
  • 效果 : 这样,当 <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N 个残差输出累加时,总的方差大约保持在 <math xmlns="http://www.w3.org/1998/Math/MathML"> σ 2 \sigma^2 </math>σ2 的水平( <math xmlns="http://www.w3.org/1998/Math/MathML"> N × σ 2 N = σ 2 N \times \frac{\sigma^2}{N} = \sigma^2 </math>N×Nσ2=σ2),从而保证了无论网络有多深,流经主干道的信息信号强度都能保持稳定。

5. 初始化总结

下表总结了 GPT-2 中不同层的初始化方式:

层 / 参数 标准初始化 特殊缩放 (仅限残差层)
嵌入层 (wte, wpe) <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( 0 , 0.0 2 2 ) \mathcal{N}(0, 0.02^2) </math>N(0,0.022) 不适用
注意力 QKV 投影 (c_attn) <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( 0 , 0.0 2 2 ) \mathcal{N}(0, 0.02^2) </math>N(0,0.022) 不适用
注意力输出投影 (c_proj) <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( 0 , 0.0 2 2 ) \mathcal{N}(0, 0.02^2) </math>N(0,0.022) ,权重乘以 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 2 × n layer \frac{1}{\sqrt{2 \times n_{\text{layer}}}} </math>2×nlayer 1
FFN 第一个线性层 (c_fc) <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( 0 , 0.0 2 2 ) \mathcal{N}(0, 0.02^2) </math>N(0,0.022) 不适用
FFN 第二个线性层 (c_proj) <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( 0 , 0.0 2 2 ) \mathcal{N}(0, 0.02^2) </math>N(0,0.022) ,权重乘以 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 2 × n layer \frac{1}{\sqrt{2 \times n_{\text{layer}}}} </math>2×nlayer 1
所有偏置 (bias) 初始化为 0 不适用

结论: GPT-2 的残差权重初始化是一种精巧的设计,它通过在标准正态初始化之后,对特定的残差层权重应用一个与网络深度相关的缩放因子,成功地稳定了深度 Transformer 模型的训练过程,是其能够有效扩展到更多层数的关键技术之一。

相关推荐
过期的秋刀鱼!几秒前
机器学习-逻辑回归的成本函数的补充-推导
人工智能·机器学习·逻辑回归
shangjian0078 分钟前
AI大模型-核心概念-机器学习
人工智能·机器学习
kaizq24 分钟前
大语言模型典型本地搭建及其应用
人工智能·ollama·cherry studio·文本对话聊天·知识库/代码库·mcp服务编制·大语言模型llm本地应用
wenzhangli727 分钟前
2025软件行业寒冬突围:破解AI编程冲击与项目制困局,一拖三闭环方案成破局关键
人工智能·ai编程
汽车仪器仪表相关领域29 分钟前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试
夜雨深秋来30 分钟前
都2026年了你还不知道AI工程化!
人工智能·代码规范
●VON33 分钟前
AI 伦理治理实操指南:从原则到生产线
人工智能
星浩AI40 分钟前
Google 官方发布:让你的 AI 编程助手"边写、边看、边调",像人类开发者一样工作
人工智能·后端·开源
Codebee1 小时前
SkillFlow:回归本质的AI能力流程管控
人工智能
巫山老妖1 小时前
2026 年 AI 趋势深度研究报告
人工智能