第9章 LayerNorm
本章目标:
理解为什么 Transformer 使用 LayerNorm 而不是 BatchNorm,以及 RMSNorm 的原理。
9.1 为什么需要归一化?
神经网络训练时,每一层的输入分布会随着参数更新而变化,这个现象叫做:
Internal Covariate Shift(内部协变量偏移)
这会导致:
- 训练不稳定
- 需要很小的学习率
- 收敛慢
归一化层的作用是:
让每一层的输入保持稳定的分布。
9.2 BatchNorm 是什么?
BatchNorm 对一个 batch 内的所有样本,在同一个特征维度上归一化:
#mermaid-svg-r7oL5BRU4p4Fe2y1{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .error-icon{fill:#552222;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .marker.cross{stroke:#333333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 p{margin:0;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster-label text{fill:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster-label span{color:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster-label span p{background-color:transparent;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .label text,#mermaid-svg-r7oL5BRU4p4Fe2y1 span{fill:#333;color:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .node rect,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node circle,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node ellipse,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node polygon,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .rough-node .label text,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node .label text,#mermaid-svg-r7oL5BRU4p4Fe2y1 .image-shape .label,#mermaid-svg-r7oL5BRU4p4Fe2y1 .icon-shape .label{text-anchor:middle;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .rough-node .label,#mermaid-svg-r7oL5BRU4p4Fe2y1 .node .label,#mermaid-svg-r7oL5BRU4p4Fe2y1 .image-shape .label,#mermaid-svg-r7oL5BRU4p4Fe2y1 .icon-shape .label{text-align:center;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .node.clickable{cursor:pointer;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .arrowheadPath{fill:#333333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-r7oL5BRU4p4Fe2y1 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r7oL5BRU4p4Fe2y1 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster text{fill:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .cluster span{color:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-r7oL5BRU4p4Fe2y1 rect.text{fill:none;stroke-width:0;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .icon-shape,#mermaid-svg-r7oL5BRU4p4Fe2y1 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .icon-shape p,#mermaid-svg-r7oL5BRU4p4Fe2y1 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .icon-shape .label rect,#mermaid-svg-r7oL5BRU4p4Fe2y1 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r7oL5BRU4p4Fe2y1 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-r7oL5BRU4p4Fe2y1 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-r7oL5BRU4p4Fe2y1 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Batch 维度归一化
样本1: x11, x12, x13
样本2: x21, x22, x23
样本3: x31, x32, x33
↕ 对每个特征维度
计算均值和方差
x^i=xi−μBσB2+ϵ\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}x^i=σB2+ϵ xi−μB
其中 μB\mu_BμB 和 σB2\sigma_B^2σB2 是 batch 内的均值和方差。
9.3 为什么 Transformer 不用 BatchNorm?
BatchNorm 有一个严重问题:
它依赖 batch 内的统计信息。
对于 NLP 任务:
- 序列长度不同:不同样本的 Token 数量不同,很难对齐
- batch 太小时不稳定:LLM 训练时 batch 很小(受显存限制)
- 推理时问题:推理时 batch=1,BatchNorm 的统计信息不准确
9.4 LayerNorm 的思想
LayerNorm 不在 batch 维度归一化,而是在每个样本的特征维度上归一化:
#mermaid-svg-4caQ5qqfFvkYiXhk{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4caQ5qqfFvkYiXhk .error-icon{fill:#552222;}#mermaid-svg-4caQ5qqfFvkYiXhk .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4caQ5qqfFvkYiXhk .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4caQ5qqfFvkYiXhk .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4caQ5qqfFvkYiXhk .marker.cross{stroke:#333333;}#mermaid-svg-4caQ5qqfFvkYiXhk svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4caQ5qqfFvkYiXhk p{margin:0;}#mermaid-svg-4caQ5qqfFvkYiXhk .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster-label text{fill:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster-label span{color:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster-label span p{background-color:transparent;}#mermaid-svg-4caQ5qqfFvkYiXhk .label text,#mermaid-svg-4caQ5qqfFvkYiXhk span{fill:#333;color:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk .node rect,#mermaid-svg-4caQ5qqfFvkYiXhk .node circle,#mermaid-svg-4caQ5qqfFvkYiXhk .node ellipse,#mermaid-svg-4caQ5qqfFvkYiXhk .node polygon,#mermaid-svg-4caQ5qqfFvkYiXhk .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4caQ5qqfFvkYiXhk .rough-node .label text,#mermaid-svg-4caQ5qqfFvkYiXhk .node .label text,#mermaid-svg-4caQ5qqfFvkYiXhk .image-shape .label,#mermaid-svg-4caQ5qqfFvkYiXhk .icon-shape .label{text-anchor:middle;}#mermaid-svg-4caQ5qqfFvkYiXhk .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-4caQ5qqfFvkYiXhk .rough-node .label,#mermaid-svg-4caQ5qqfFvkYiXhk .node .label,#mermaid-svg-4caQ5qqfFvkYiXhk .image-shape .label,#mermaid-svg-4caQ5qqfFvkYiXhk .icon-shape .label{text-align:center;}#mermaid-svg-4caQ5qqfFvkYiXhk .node.clickable{cursor:pointer;}#mermaid-svg-4caQ5qqfFvkYiXhk .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-4caQ5qqfFvkYiXhk .arrowheadPath{fill:#333333;}#mermaid-svg-4caQ5qqfFvkYiXhk .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-4caQ5qqfFvkYiXhk .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-4caQ5qqfFvkYiXhk .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4caQ5qqfFvkYiXhk .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-4caQ5qqfFvkYiXhk .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4caQ5qqfFvkYiXhk .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster text{fill:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk .cluster span{color:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-4caQ5qqfFvkYiXhk .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-4caQ5qqfFvkYiXhk rect.text{fill:none;stroke-width:0;}#mermaid-svg-4caQ5qqfFvkYiXhk .icon-shape,#mermaid-svg-4caQ5qqfFvkYiXhk .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4caQ5qqfFvkYiXhk .icon-shape p,#mermaid-svg-4caQ5qqfFvkYiXhk .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-4caQ5qqfFvkYiXhk .icon-shape .label rect,#mermaid-svg-4caQ5qqfFvkYiXhk .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4caQ5qqfFvkYiXhk .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-4caQ5qqfFvkYiXhk .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-4caQ5qqfFvkYiXhk :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} LayerNorm 特征维度归一化
样本1: x11, x12, x13 → 对这一行归一化
样本2: x21, x22, x23 → 对这一行归一化
样本3: x31, x32, x33 → 对这一行归一化
每个样本独立归一化,不依赖其他样本。
在 Transformer 中,这意味着:对每个 Token 自己的 512 个特征值做归一化。
text
512 个特征维度
┌──────────────────┐
The │ x x x x ... x │ ← 对这一行的512个数算均值和方差
bank │ x x x x ... x │ ← 对这一行的512个数算均值和方差
approved │ x x x x ... x │ ← 对这一行的512个数算均值和方差
loan │ x x x x ... x │ ← 对这一行的512个数算均值和方差
└──────────────────┘
bank 的归一化只看 bank 自己的 512 个数,不会把 The、approved、loan 混进来。
9.5 LayerNorm 的数学推导与手算
对于输入向量 x∈Rdx \in \mathbb{R}^{d}x∈Rd:
LayerNorm(x)=γ⋅x−μσ2+ϵ+β\text{LayerNorm}(x) = \gamma \cdot \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} + \betaLayerNorm(x)=γ⋅σ2+ϵ x−μ+β
我们用一个 4 维向量逐步手算,让每一步完全透明。
假设 bank 的向量是:
xbank=1,2,3,4x_{\text{bank}} = 1, 2, 3, 4xbank=1,2,3,4
Step 1:计算均值
μ=1+2+3+44=2.5\mu = \frac{1 + 2 + 3 + 4}{4} = 2.5μ=41+2+3+4=2.5
Step 2:计算方差
σ2=(1−2.5)2+(2−2.5)2+(3−2.5)2+(4−2.5)24=2.25+0.25+0.25+2.254=1.25\sigma^2 = \frac{(1-2.5)^2 + (2-2.5)^2 + (3-2.5)^2 + (4-2.5)^2}{4} = \frac{2.25 + 0.25 + 0.25 + 2.25}{4} = 1.25σ2=4(1−2.5)2+(2−2.5)2+(3−2.5)2+(4−2.5)2=42.25+0.25+0.25+2.25=1.25
Step 3:计算标准差
σ=1.25≈1.118\sigma = \sqrt{1.25} \approx 1.118σ=1.25 ≈1.118
Step 4:归一化(每个值减均值,除以标准差)
x^1=1−2.51.118≈−1.34\hat{x}_1 = \frac{1 - 2.5}{1.118} \approx -1.34x^1=1.1181−2.5≈−1.34
x^2=2−2.51.118≈−0.45\hat{x}_2 = \frac{2 - 2.5}{1.118} \approx -0.45x^2=1.1182−2.5≈−0.45
x^3=3−2.51.118≈0.45\hat{x}_3 = \frac{3 - 2.5}{1.118} \approx 0.45x^3=1.1183−2.5≈0.45
x^4=4−2.51.118≈1.34\hat{x}_4 = \frac{4 - 2.5}{1.118} \approx 1.34x^4=1.1184−2.5≈1.34
归一化后:
x^bank=−1.34,−0.45,0.45,1.34\hat{x}_{\text{bank}} = -1.34, -0.45, 0.45, 1.34x^bank=−1.34,−0.45,0.45,1.34
此时均值≈0,方差≈1。
Step 5:缩放和平移(可学习参数 γ\gammaγ, β\betaβ)
yi=γi⋅x^i+βiy_i = \gamma_i \cdot \hat{x}_i + \beta_iyi=γi⋅x^i+βi
γ\gammaγ(scale)和 β\betaβ(shift)是每个维度独立的可训练参数 ,初始化为 γ=1,β=0\gamma=1, \beta=0γ=1,β=0。
假设训练后模型学到 γ=2,β=1\gamma = 2, \beta = 1γ=2,β=1:
y=2×−1.34,−0.45,0.45,1.34+1=−1.68,0.10,1.90,3.68y = 2 \times -1.34, -0.45, 0.45, 1.34 + 1 = -1.68, 0.10, 1.90, 3.68y=2×−1.34,−0.45,0.45,1.34+1=−1.68,0.10,1.90,3.68
所以 LayerNorm 并不是强制让所有层永远保持"均值=0,方差=1"------归一化只是中间步骤,模型可以通过 γ\gammaγ 和 β\betaβ 学习适合当前任务的缩放和偏移。
9.6 BatchNorm vs LayerNorm 对比
#mermaid-svg-jMKdC0XNuMqhrNG0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jMKdC0XNuMqhrNG0 .error-icon{fill:#552222;}#mermaid-svg-jMKdC0XNuMqhrNG0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jMKdC0XNuMqhrNG0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .marker.cross{stroke:#333333;}#mermaid-svg-jMKdC0XNuMqhrNG0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jMKdC0XNuMqhrNG0 p{margin:0;}#mermaid-svg-jMKdC0XNuMqhrNG0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster-label text{fill:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster-label span{color:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster-label span p{background-color:transparent;}#mermaid-svg-jMKdC0XNuMqhrNG0 .label text,#mermaid-svg-jMKdC0XNuMqhrNG0 span{fill:#333;color:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .node rect,#mermaid-svg-jMKdC0XNuMqhrNG0 .node circle,#mermaid-svg-jMKdC0XNuMqhrNG0 .node ellipse,#mermaid-svg-jMKdC0XNuMqhrNG0 .node polygon,#mermaid-svg-jMKdC0XNuMqhrNG0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .rough-node .label text,#mermaid-svg-jMKdC0XNuMqhrNG0 .node .label text,#mermaid-svg-jMKdC0XNuMqhrNG0 .image-shape .label,#mermaid-svg-jMKdC0XNuMqhrNG0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-jMKdC0XNuMqhrNG0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .rough-node .label,#mermaid-svg-jMKdC0XNuMqhrNG0 .node .label,#mermaid-svg-jMKdC0XNuMqhrNG0 .image-shape .label,#mermaid-svg-jMKdC0XNuMqhrNG0 .icon-shape .label{text-align:center;}#mermaid-svg-jMKdC0XNuMqhrNG0 .node.clickable{cursor:pointer;}#mermaid-svg-jMKdC0XNuMqhrNG0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .arrowheadPath{fill:#333333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jMKdC0XNuMqhrNG0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jMKdC0XNuMqhrNG0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jMKdC0XNuMqhrNG0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster text{fill:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 .cluster span{color:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jMKdC0XNuMqhrNG0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jMKdC0XNuMqhrNG0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-jMKdC0XNuMqhrNG0 .icon-shape,#mermaid-svg-jMKdC0XNuMqhrNG0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jMKdC0XNuMqhrNG0 .icon-shape p,#mermaid-svg-jMKdC0XNuMqhrNG0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jMKdC0XNuMqhrNG0 .icon-shape .label rect,#mermaid-svg-jMKdC0XNuMqhrNG0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jMKdC0XNuMqhrNG0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jMKdC0XNuMqhrNG0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jMKdC0XNuMqhrNG0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} LayerNorm - 横向归一化
样本1: x11 x12 x13
样本2: x21 x22 x23
样本3: x31 x32 x33
BatchNorm - 竖向归一化
样本1: x11 x12 x13
样本2: x21 x22 x23
样本3: x31 x32 x33
- BatchNorm:对同一个特征维度,跨所有样本计算均值和方差(竖着看一列)
- LayerNorm:对同一个样本,跨所有特征维度计算均值和方差(横着看一行)
| 特性 | BatchNorm | LayerNorm |
|---|---|---|
| 归一化方向 | Batch 维度 | Feature 维度 |
| 依赖 batch | ✅ | ❌ |
| 适合 NLP | ❌ | ✅ |
| 适合 CV | ✅ | 可以 |
| 推理时稳定 | 需要运行均值 | ✅ |
| batch=1 时 | 不稳定 | ✅ |
9.7 在 Transformer 中的位置
LayerNorm 在 Transformer Block 中有两种放置方式:
Post-Norm(原始论文):
text
x → Attention → x + Attention(x) → LayerNorm → FFN → x + FFN(x) → LayerNorm
Pre-Norm(现代 LLM):
text
x → LayerNorm → Attention → x + Attention(LN(x)) → LayerNorm → FFN → x + FFN(LN(x))
现代 LLM 几乎都用 Pre-Norm,因为训练更稳定。
9.7.1 LayerNorm 会不会把不同 Token 的语义"洗掉"?
这是一个非常自然的疑问:如果每个 Token 都被归一化到均值≈0、方差≈1,不同 Token 会不会变得无法区分?
我们先构造一个"吓人"的例子。
假设:
text
bank = [1, 2, 3, 4]
loan = [10, 20, 30, 40]
对 bank 做 LayerNorm(前面已算过):
LN(bank)=−1.34,−0.45,0.45,1.34\text{LN}(bank) = -1.34, -0.45, 0.45, 1.34LN(bank)=−1.34,−0.45,0.45,1.34
对 loan 做 LayerNorm:
μloan=10+20+30+404=25\mu_{loan} = \frac{10+20+30+40}{4} = 25μloan=410+20+30+40=25
σloan=(−15)2+(−5)2+52+1524=125≈11.18\sigma_{loan} = \sqrt{\frac{(-15)^2+(-5)^2+5^2+15^2}{4}} = \sqrt{125} \approx 11.18σloan=4(−15)2+(−5)2+52+152 =125 ≈11.18
LN(loan)=10−2511.18,20−2511.18,30−2511.18,40−2511.18=−1.34,−0.45,0.45,1.34\text{LN}(loan) = \left\\frac{10-25}{11.18}, \\frac{20-25}{11.18}, \\frac{30-25}{11.18}, \\frac{40-25}{11.18}\\right = -1.34, -0.45, 0.45, 1.34LN(loan)=11.1810−25,11.1820−25,11.1830−25,11.1840−25=−1.34,−0.45,0.45,1.34
它们变成完全一样了! 😱
是不是说明 LayerNorm 把语义洗掉了?
别慌。 仔细看这个例子的特殊之处:
10,20,30,40\]=10×\[1,2,3,4\]\[10, 20, 30, 40\] = 10 \\times \[1, 2, 3, 4\]\[10,20,30,40\]=10×\[1,2,3,4
loan 的向量恰好是 bank 的 10 倍------它们内部各维度的比例关系完全一样。LayerNorm 消除的就是"整体偏移"和"整体缩放",所以简单成比例的两个向量确实会变一样。
但真实 Transformer 中,不同 Token 的向量几乎不可能是简单的比例关系。
换一个真实的例子:
text
bank = [1, 2, 3, 4]
loan = [10, 18, 35, 22]
loan 不是 bank 的等比放大------它们内部各维度的相对结构不同。
LN(bank)=−1.34,−0.45,0.45,1.34\text{LN}(bank) = -1.34, -0.45, 0.45, 1.34LN(bank)=−1.34,−0.45,0.45,1.34
μloan=10+18+35+224=21.25\mu_{loan} = \frac{10+18+35+22}{4} = 21.25μloan=410+18+35+22=21.25
LN(loan)≈−1.12,−0.32,1.37,0.07\text{LN}(loan) \approx -1.12, -0.32, 1.37, 0.07LN(loan)≈−1.12,−0.32,1.37,0.07
完全不同! 虽然都满足"均值≈0,方差≈1",但内部数值模式完全不一样。
山脉类比
把每个 Token 的向量想象成一条山脉的轮廓:
text
bank 的山脉: 低 → 中低 → 中高 → 高 (均匀上升)
loan 的山脉: 低 → 中低 → 很高 → 接近平均 (中间有尖峰)
LayerNorm 做的事情相当于:
把每条山脉都移动到海拔 0,并缩放到相同的整体高度范围。
移动和缩放之后,山脉的形状(哪里高、哪里低、起伏的模式)仍然不同------这就是 Token 之间的语义差异。
为什么数学上不会丢信息?
LayerNorm 对所有维度减去同一个 均值、除以同一个标准差,因此:
- 如果归一化前 x1>x2x_1 > x_2x1>x2,归一化后仍然 x1′>x2′x_1' > x_2'x1′>x2′(大小顺序不变)
- 维度之间的相对差异模式被完整保留
唯一丢失的是"整体偏移"和"整体缩放"------而这两者对于区分不同 Token 的语义几乎没有贡献。
💡 总结 :LayerNorm 只有在两个向量恰好成正比时才会"洗掉"区别。真实训练出的 Token 向量结构各不相同,LayerNorm 不会损害它们之间的语义差异。而且后面的 γ\gammaγ、β\betaβ 参数还允许模型学习恢复需要的缩放和偏移。
9.8 RMSNorm
RMSNorm(Root Mean Square Layer Normalization)是 LayerNorm 的简化版本。
LayerNorm 需要计算均值和方差:
x^i=xi−μσ2+ϵ\hat{x}_i = \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}}x^i=σ2+ϵ xi−μ
RMSNorm 只计算 RMS(均方根),去掉了均值:
RMS(x)=1d∑i=1dxi2\text{RMS}(x) = \sqrt{\frac{1}{d} \sum_{i=1}^{d} x_i^2}RMS(x)=d1i=1∑dxi2
x^i=xiRMS(x)⋅γi\hat{x}_i = \frac{x_i}{\text{RMS}(x)} \cdot \gamma_ix^i=RMS(x)xi⋅γi
9.8.1 为什么 Qwen 和 Llama 用 RMSNorm?
| 特性 | LayerNorm | RMSNorm |
|---|---|---|
| 计算量 | 需要均值+方差 | 只需均方根 |
| 速度 | 较慢 | 更快(约快 10-15%) |
| 效果 | 好 | 接近 LayerNorm |
| 参数 | γ, β | 只有 γ |
RMSNorm 的假设是:
归一化的主要作用来自缩放(scale),而不是平移(shift)。
去掉均值计算(去掉 β)后,效果几乎不变,但速度更快。
9.9 TensorFlow 实现
python
import tensorflow as tf
# LayerNorm(Keras 内置)
layer_norm = tf.keras.layers.LayerNormalization(epsilon=1e-6)
x = tf.random.normal([2, 5, 512])
output = layer_norm(x)
print(output.shape) # (2, 5, 512)
# RMSNorm 手动实现
class RMSNorm(tf.keras.layers.Layer):
def __init__(self, d_model, epsilon=1e-6):
super().__init__()
self.epsilon = epsilon
self.gamma = self.add_weight(
shape=(d_model,), initializer='ones', name='gamma'
)
def call(self, x):
# x: [batch, seq, d_model]
rms = tf.sqrt(tf.reduce_mean(tf.square(x), axis=-1, keepdims=True) + self.epsilon)
return x / rms * self.gamma
# 测试
rms_norm = RMSNorm(d_model=512)
x = tf.random.normal([2, 5, 512])
output = rms_norm(x)
print(output.shape) # (2, 5, 512)
9.10 各模型使用的归一化方案
| 模型 | 归一化方案 | 位置 |
|---|---|---|
| 原始 Transformer | LayerNorm | Post-Norm |
| BERT | LayerNorm | Post-Norm |
| GPT-2 | LayerNorm | Pre-Norm |
| GPT-3 | LayerNorm | Pre-Norm |
| Llama 1/2/3 | RMSNorm | Pre-Norm |
| Qwen 1/2 | RMSNorm | Pre-Norm |
| DeepSeek | RMSNorm | Pre-Norm |
本章总结
#mermaid-svg-T3g8sxe9aBIfME19{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-T3g8sxe9aBIfME19 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-T3g8sxe9aBIfME19 .error-icon{fill:#552222;}#mermaid-svg-T3g8sxe9aBIfME19 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-T3g8sxe9aBIfME19 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-T3g8sxe9aBIfME19 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-T3g8sxe9aBIfME19 .marker.cross{stroke:#333333;}#mermaid-svg-T3g8sxe9aBIfME19 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-T3g8sxe9aBIfME19 p{margin:0;}#mermaid-svg-T3g8sxe9aBIfME19 .edge{stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .section--1 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section--1 path,#mermaid-svg-T3g8sxe9aBIfME19 .section--1 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section--1 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section--1 text{fill:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth--1{stroke-width:17;}#mermaid-svg-T3g8sxe9aBIfME19 .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-0 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-0 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-0 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-0 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-0 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-0{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-0{stroke-width:14;}#mermaid-svg-T3g8sxe9aBIfME19 .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-1 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-1 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-1 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-1 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-1 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-1{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-1{stroke-width:11;}#mermaid-svg-T3g8sxe9aBIfME19 .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-2 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-2 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-2 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-2 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-2 text{fill:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-2{stroke-width:8;}#mermaid-svg-T3g8sxe9aBIfME19 .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-3 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-3 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-3 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-3 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-3 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-3{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-3{stroke-width:5;}#mermaid-svg-T3g8sxe9aBIfME19 .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-4 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-4 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-4 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-4 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-4 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-4{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-4{stroke-width:2;}#mermaid-svg-T3g8sxe9aBIfME19 .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-5 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-5 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-5 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-5 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-5 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-5{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-5{stroke-width:-1;}#mermaid-svg-T3g8sxe9aBIfME19 .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-6 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-6 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-6 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-6 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-6 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-6{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-6{stroke-width:-4;}#mermaid-svg-T3g8sxe9aBIfME19 .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-7 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-7 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-7 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-7 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-7 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-7{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-7{stroke-width:-7;}#mermaid-svg-T3g8sxe9aBIfME19 .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-8 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-8 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-8 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-8 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-8 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-8{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-8{stroke-width:-10;}#mermaid-svg-T3g8sxe9aBIfME19 .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-9 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-9 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-9 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-9 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-9 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-9{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-9{stroke-width:-13;}#mermaid-svg-T3g8sxe9aBIfME19 .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-10 rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-10 path,#mermaid-svg-T3g8sxe9aBIfME19 .section-10 circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-10 polygon,#mermaid-svg-T3g8sxe9aBIfME19 .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-10 text{fill:black;}#mermaid-svg-T3g8sxe9aBIfME19 .node-icon-10{font-size:40px;color:black;}#mermaid-svg-T3g8sxe9aBIfME19 .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .edge-depth-10{stroke-width:-16;}#mermaid-svg-T3g8sxe9aBIfME19 .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled,#mermaid-svg-T3g8sxe9aBIfME19 .disabled circle,#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:lightgray;}#mermaid-svg-T3g8sxe9aBIfME19 .disabled text{fill:#efefef;}#mermaid-svg-T3g8sxe9aBIfME19 .section-root rect,#mermaid-svg-T3g8sxe9aBIfME19 .section-root path,#mermaid-svg-T3g8sxe9aBIfME19 .section-root circle,#mermaid-svg-T3g8sxe9aBIfME19 .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-T3g8sxe9aBIfME19 .section-root text{fill:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .section-root span{color:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .section-2 span{color:#ffffff;}#mermaid-svg-T3g8sxe9aBIfME19 .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-T3g8sxe9aBIfME19 .edge{fill:none;}#mermaid-svg-T3g8sxe9aBIfME19 .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-T3g8sxe9aBIfME19 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 归一化
BatchNorm
沿Batch维度
适合CV
不适合NLP
LayerNorm
沿Feature维度
适合NLP
BERT/GPT使用
RMSNorm
去掉均值
更快
Llama/Qwen使用
本章思考题
- 为什么 BatchNorm 在 batch=1 时不稳定?
- LayerNorm 的 γ 和 β 参数的作用是什么?如果去掉它们会怎样?
- RMSNorm 去掉了均值计算,这意味着什么假设?
- Pre-Norm 为什么比 Post-Norm 训练更稳定?
下一章预告
下一章我们讲 Residual(残差连接)。
为什么残差连接能防止梯度消失?它和 ResNet 有什么关系?