Xavier Initialization/zh

    From Marovi AI
    This page is a translated version of the page Xavier Initialization and the translation is 81% complete.
    Other languages:
    Article
    Topic area Deep Learning
    Prerequisites Backpropagation, Activation Function


    概述

    Xavier 初始化(又称 Glorot 初始化)是一种为深度神经网络设置初始权重的方案,使得激活值与梯度的方差在训练开始时大致在各层之间保持恒定。该方法由 Xavier Glorot 与 Yoshua Bengio 于 2010 年提出,至今仍是使用最广泛的默认初始化器之一,尤其适用于具有对称、零中心激活函数(例如双曲正切)的网络。[1]

    其核心思想是一个简单的方差平衡论证。如果每一层的权重都从一个其方差与该层 fan-in 和 fan-out 相校准的分布中抽取,那么前向激活的幅度与反向梯度的幅度在信号沿深度传播时既不会收缩到零,也不会无限增大。这就避免了梯度消失与梯度爆炸的病态现象,正是它们使得在人们尚未理解谨慎初始化之前,训练深度网络变得极为困难。

    动机

    在 Glorot 与 Bengio 的分析问世之前,深度网络通常以从一个较小的高斯分布或均匀分布中抽取的权重进行初始化,该分布通常是手工选择或通过试错确定的。实践者观察到,使用sigmoid或双曲正切激活的非常深的网络几乎无法训练:由反向传播计算得到的梯度,要么在浅层衰减到零(梯度消失问题),要么无限增长(梯度爆炸问题),这取决于权重的尺度与激活函数的选择。诸如 tanh 之类的对称饱和激活会进一步加剧这一问题,因为它们的导数在零附近一个狭窄区域之外迅速衰减。

    Glorot 与 Bengio 分析了前向传播中激活值方差的演化以及反向传播中梯度方差的演化,所依据的假设是:激活函数在零附近近似线性,且权重独立同分布且均值为零。他们证明,要在层级链中同时保持前向与反向方差的稳定,每个权重的方差应与该层的 fan-in(输入单元数)与 fan-out(输出单元数)挂钩。由此得到的规定即今天所称的Xavier 初始化

    公式表述

    考虑一个全连接层,它通过权重矩阵 $ W $ 将一个维度为 $ n_{\text{in}} $ 的输入向量映射到一个维度为 $ n_{\text{out}} $ 的输出向量。假设输入相互独立、均值为零且共享方差 $ \sigma_x^2 $,权重相互独立、均值为零且方差为 $ \sigma_w^2 $,并假设激活函数在零附近近似线性且斜率为一。那么每个输出的方差为

    $ {\displaystyle \operatorname{Var}(y) = n_{\text{in}} \, \sigma_w^2 \, \sigma_x^2.} $

    为了使方差在前向传播中得以保持,需要 $ \sigma_w^2 = 1 / n_{\text{in}} $。将对称的论证应用于反向传播,其中梯度被乘以权重矩阵的转置,则得到 $ \sigma_w^2 = 1 / n_{\text{out}} $。由于这两条要求通常彼此冲突,Glorot 与 Bengio 提出了如下调和折中

    $ {\displaystyle \sigma_w^2 = \frac{2}{n_{\text{in}} + n_{\text{out}}}.} $

    这就是定义 Xavier 初始化的方差。有两种常见的参数化方式可以实现它:

    • Xavier 正态:高斯分布 $ \mathcal{N}\!\left(0, \frac{2}{n_{\text{in}} + n_{\text{out}}}\right) $ 中独立地抽取每个权重。
    • Xavier 均匀:$ \mathcal{U}(-a, a) $ 中独立地抽取每个权重,其中 $ a = \sqrt{6 / (n_{\text{in}} + n_{\text{out}})} $,以使该均匀分布具有所规定的方差。

    偏置通常初始化为零。对于卷积层,fan-in 与 fan-out 的计算方式为:输入通道数或输出通道数乘以感受野的大小(例如,卷积核的高度乘以其宽度)。

    推导概要

    该推导依赖三个近似:权重与输入相互独立且均值为零,激活函数在原点附近局部线性且导数为一,并且偏置为零。在这些假设下,前向方差的递推关系为

    $ {\displaystyle \operatorname{Var}(y^{(\ell)}) = n^{(\ell)}_{\text{in}} \, \sigma^{(\ell)\,2}_w \, \operatorname{Var}(y^{(\ell-1)}),} $

    于是当 $ n^{(\ell)}_{\text{in}} \sigma^{(\ell)\,2}_w = 1 $ 成立时,方差在层之间得到精确保持。反向递推具有相同的形式,但 $ n^{(\ell)}_{\text{in}} $$ n^{(\ell)}_{\text{out}} $ 替换,因为梯度通过转置进行传播。对这两个条件取平均即得 Xavier 规则。Glorot 与 Bengio 通过实验证实,按此方式初始化的网络在深度方向上呈现出远为稳定的激活与梯度直方图,并且其训练速度远高于使用旧式启发式 $ \mathcal{U}(-1/\sqrt{n_{\text{in}}}, 1/\sqrt{n_{\text{in}}}) $ 初始化的网络。

    与 He 初始化的比较

    Xavier 初始化所依赖的线性激活假设对于原点附近的 tanh 与逻辑sigmoid是合理的,但对于修正线性单元便不再成立。由于 ReLU 会将负输入置零,仅约一半的预激活值对输出方差有贡献,因此 Xavier 初始化倾向于低估 ReLU 网络合适的权重尺度,导致激活值随深度衰减。He 初始化由 Kaiming He 等人于 2015 年提出,它通过使用

    $ {\displaystyle \sigma_w^2 = \frac{2}{n_{\text{in}}}} $

    来代替,将方差加倍以补偿整流器。[2] 在现代实践中,Xavier 初始化是基于 tanh、sigmoid 与 softmax 的体系结构的默认选择,而 He 初始化则是 ReLU 及其变体的默认选择。两者都是更一般的"缩放"初始化原则的特例,有时被统称为方差保持型初始化。

    实用考量

    对于使用批归一化层归一化的深度网络而言,初始化器的选择已不像过去那样关键,因为归一化层会在每次线性变换之后重新缩放激活值,从而部分吸收初始尺度的失校。尽管如此,一个合理的初始化器仍能加速训练的最初若干 epoch,并可能成为网络发散与收敛之间的分水岭,尤其是在没有归一化的非常深的网络中,或在非常深的残差网络的残差分支中——在那里较小的初始尺度有所裨益。

    大多数深度学习框架同时内置了 Xavier 均匀与 Xavier 正态两种初始化器,通常以 "glorot_uniform" 与 "glorot_normal" 命名(例如在 TensorFlow 与 Keras 中),或以 "xavier_uniform_" 与 "xavier_normal_" 命名(在 PyTorch 中)。PyTorch 线性层的默认初始化采用了一种与之密切相关的 He-均匀方案。在混合多种激活或与归一化组合使用时,实践者通常遵循的约定是:将 tanh 与 sigmoid 层与 Xavier 配对,将 ReLU 层与 He 配对,同时让框架对归一化层与嵌入层的默认初始化保持不变。

    For recurrent neural networks, orthogonal initialization of the recurrent weight matrix is a stronger choice than Xavier, because it preserves the norm of the hidden state across time steps exactly rather than only on average. Xavier remains a reasonable choice for the input-to-hidden weights of a recurrent layer.

    Limitations

    Xavier initialization makes assumptions that are routinely violated in practice. It assumes the activation is linear near zero with unit slope, which excludes ReLU and its variants; it assumes inputs and weights are independent with zero mean, which is approximately but not exactly true at initialization; and it ignores the effect of biases, normalization layers, and skip connections. For networks with strong nonlinearities, it underestimates or overestimates the appropriate scale, and dedicated derivations such as He initialization or LSUV initialization are preferred.

    The scheme also says nothing about the geometric structure of the weight matrix. Two networks with identical Xavier-distributed weights can still have very different conditioning of their Jacobians, and pathological eigenvalue structures can persist even when variances are well calibrated. For very deep networks, more careful initializations such as orthogonal matrices, dynamical isometry, or Fixup initialization for residual networks address the conditioning question directly.[3] Finally, the scheme is purely about the start of training; with sufficient training, modern optimizers can usually overcome moderately mis-scaled initial weights, so Xavier initialization is best understood as a robust default rather than as an optimal choice in any strict sense.

    References