神经网络中的权重衰减 (Weight Decay)
权重衰减(Weight Decay)是深度学习中最广泛使用的 正则化(Regularization) 技术之一。它通过在训练过程中抑制参数(权重)的幅度,限制模型的复杂度,从而提高模型的泛化能力。
1. 数学原理与推导
权重衰减的核心思想是在损失函数中增加一个惩罚项,使得模型在最小化原始误差的同时,也试图最小化权重的幅度。
1.1 目标函数的修正
假设原始的损失函数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> L ( w ) L(\mathbf{w}) </math>L(w)(如均方误差 MSE 或交叉熵 Cross Entropy),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w 为网络权重向量。 引入权重衰减后,我们定义一个新的目标函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> J ( w ) J(\mathbf{w}) </math>J(w):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> J ( w ) = L ( w ) + λ 2 ∥ w ∥ 2 2 J(\mathbf{w}) = L(\mathbf{w}) + \frac{\lambda}{2} \|\mathbf{w}\|_2^2 </math>J(w)=L(w)+2λ∥w∥22
- <math xmlns="http://www.w3.org/1998/Math/MathML"> L ( w ) L(\mathbf{w}) </math>L(w): 原始的数据损失(Data Loss)。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> λ \lambda </math>λ: 超参数(正则化系数),控制惩罚的强度。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ w ∥ 2 2 \|\mathbf{w}\|_2^2 </math>∥w∥22: 权重的 L2 范数平方,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∑ w i 2 \sum w_i^2 </math>∑wi2。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 2 \frac{1}{2} </math>21: 常数系数,用于在求导时抵消平方项产生的 <math xmlns="http://www.w3.org/1998/Math/MathML"> 2 2 </math>2,简化公式。
1.2 梯度更新公式的推导
在使用随机梯度下降(SGD)优化时,我们需要对 <math xmlns="http://www.w3.org/1998/Math/MathML"> J ( w ) J(\mathbf{w}) </math>J(w) 求关于 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w 的偏导数(梯度):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∇ w J ( w ) = ∇ w L ( w ) + ∇ w ( λ 2 w T w ) = ∇ w L ( w ) + λ w \begin{aligned} \nabla_{\mathbf{w}} J(\mathbf{w}) &= \nabla_{\mathbf{w}} L(\mathbf{w}) + \nabla_{\mathbf{w}} \left( \frac{\lambda}{2} \mathbf{w}^T \mathbf{w} \right) \\ &= \nabla_{\mathbf{w}} L(\mathbf{w}) + \lambda \mathbf{w} \end{aligned} </math>∇wJ(w)=∇wL(w)+∇w(2λwTw)=∇wL(w)+λw
将该梯度代入参数更新公式(设学习率为 <math xmlns="http://www.w3.org/1998/Math/MathML"> η \eta </math>η):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> w t + 1 = w t − η ⋅ ∇ w J ( w t ) = w t − η ( ∇ w L ( w t ) + λ w t ) = w t − η ∇ w L ( w t ) − η λ w t \begin{aligned} \mathbf{w}_{t+1} &= \mathbf{w}t - \eta \cdot \nabla{\mathbf{w}} J(\mathbf{w}_t) \\ &= \mathbf{w}t - \eta \left( \nabla{\mathbf{w}} L(\mathbf{w}_t) + \lambda \mathbf{w}_t \right) \\ &= \mathbf{w}t - \eta \nabla{\mathbf{w}} L(\mathbf{w}_t) - \eta \lambda \mathbf{w}_t \end{aligned} </math>wt+1=wt−η⋅∇wJ(wt)=wt−η(∇wL(wt)+λwt)=wt−η∇wL(wt)−ηλwt
1.3 "衰减"的体现
整理上述公式,提取公共项 <math xmlns="http://www.w3.org/1998/Math/MathML"> w t \mathbf{w}t </math>wt:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> w t + 1 = ( 1 − η λ ) ⏟ 衰减系数 < 1 w t − η ∇ w L ( w t ) \mathbf{w}{t+1} = \underbrace{(1 - \eta \lambda)}_{\text{衰减系数} < 1} \mathbf{w}t - \eta \nabla{\mathbf{w}} L(\mathbf{w}_t) </math>wt+1=衰减系数<1 (1−ηλ)wt−η∇wL(wt)
结论 : 在每一步更新中,权重向量首先会乘以一个小于 1 的系数 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 1 − η λ ) (1 - \eta \lambda) </math>(1−ηλ) 进行收缩(即衰减),然后再沿着梯度的反方向移动。这使得如果不持续通过数据梯度获得更新,权重将自然趋向于 0。
2. 与范数 (Norm) 的关系
权重衰减在数学上等价于 L2 范数正则化 。这一关系可以从约束优化的角度来理解。
2.1 等价性
我们将优化问题看作是在限制权重大小的前提下最小化损失。 即:
- Minimize : <math xmlns="http://www.w3.org/1998/Math/MathML"> L ( w ) L(\mathbf{w}) </math>L(w)
- Subject to : <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ w ∥ 2 2 ≤ C \|\mathbf{w}\|_2^2 \le C </math>∥w∥22≤C (权重必须在一个固定半径的高维球体内)
根据拉格朗日乘数法 (Lagrange Multipliers),上述带约束的优化问题可以转化为无约束的优化问题:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> L ( w , λ ) = L ( w ) + λ ( ∥ w ∥ 2 2 − C ) \mathcal{L}(\mathbf{w}, \lambda) = L(\mathbf{w}) + \lambda (\|\mathbf{w}\|_2^2 - C) </math>L(w,λ)=L(w)+λ(∥w∥22−C)
忽略常数 <math xmlns="http://www.w3.org/1998/Math/MathML"> C C </math>C,这正是我们定义的加入权重衰减后的目标函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> J ( w ) J(\mathbf{w}) </math>J(w)。
2.2 几何意义
- L2 范数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ w ∥ 2 \|\mathbf{w}\|_2 </math>∥w∥2) 的约束区域是一个球体(Ball)。
- 在优化过程中,数据损失项 <math xmlns="http://www.w3.org/1998/Math/MathML"> L ( w ) L(\mathbf{w}) </math>L(w) 的等高线试图找到中心最小值,而正则化项试图将解拉向原点。
- 最终的最优解通常位于数据损失等高线与 L2 范数球体的切点处。
3. 抑制过拟合的机理
为什么限制权重的大小(让 <math xmlns="http://www.w3.org/1998/Math/MathML"> w w </math>w 变小)可以减少过拟合?
3.1 限制函数复杂度 (奥卡姆剃刀)
过拟合通常表现为模型为了迎合训练数据中的噪声,学习到了极其复杂的边界。
- 大权重:允许神经元在很小的输入范围内发生剧烈的激活变化,导致模型曲线"陡峭"且复杂。
- 小权重 :迫使模型忽略微小的特征干扰,倾向于学习更简单、更平滑的规律。这符合奥卡姆剃刀原则(如无必要,勿增实体)。
3.2 提高函数平滑度 (Lipschitz 连续性)
从数学分析的角度看,神经网络是一个函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(\mathbf{x}) </math>f(x)。对于线性层 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) = w T x f(\mathbf{x}) = \mathbf{w}^T\mathbf{x} </math>f(x)=wTx,考虑输入的一个微小扰动(噪声) <math xmlns="http://www.w3.org/1998/Math/MathML"> Δ x \Delta \mathbf{x} </math>Δx:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∣ f ( x + Δ x ) − f ( x ) ∣ = ∣ w T Δ x ∣ ≤ ∥ w ∥ ⋅ ∥ Δ x ∥ |f(\mathbf{x} + \Delta \mathbf{x}) - f(\mathbf{x})| = |\mathbf{w}^T \Delta \mathbf{x}| \le \|\mathbf{w}\| \cdot \|\Delta \mathbf{x}\| </math>∣f(x+Δx)−f(x)∣=∣wTΔx∣≤∥w∥⋅∥Δx∥
- 如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ w ∥ \|\mathbf{w}\| </math>∥w∥ 很大 :微小的输入噪声 <math xmlns="http://www.w3.org/1998/Math/MathML"> Δ x \Delta \mathbf{x} </math>Δx 会被放大,导致输出发生巨大变化。这使得模型对噪声敏感,容易过拟合。
- 如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ w ∥ \|\mathbf{w}\| </math>∥w∥ 被限制(衰减) :输出的变化幅度被限制,函数变得更加平滑 (Smooth) 和稳定,对未知数据的泛化能力更强。