Caffe学习 二 xavier初始化

上一篇随笔中,将默认的参数初始化"xaview"改成了"gaussian",虽然能运行得到不错的结果。

但是在加上针对性的std=sqrt(1/n_out)前,是无法收敛的。

相比之下,采用sigmoid就能收敛。

ReLU不够好的地方:

在学习率过高时,很多单元流经的梯度为0且不再更新就此死掉。——可以通过合理设置lr以及lr的更新方式。

输出范围为[0, +∞],得到数据的幅度过大。——采用xavier初始化,使每层的方差尽量相等。

下面引用自shuzfan的博文深度学习——Xavier初始化方法

“Xavier”初始化方法是一种很有效的神经网络初始化方法,方法来源于2010年的一篇论文《Understanding the difficulty of training deep feedforward neural networks》,可惜直到近两年,这个方法才逐渐得到更多人的应用和认可。

为了使得网络中信息更好的流动,每一层输出的方差应该尽量相等。

基于这个目标,现在我们就去推导一下:每一层的权重应该满足哪种条件。

文章先假设的是线性激活函数,而且满足0点处导数为1,即 
这里写图片描述

现在我们先来分析一层卷积: 
这里写图片描述 
其中ni表示输入个数。

根据概率统计知识我们有下面的方差公式: 
这里写图片描述

特别的,当我们假设输入和权重都是0均值时(目前有了BN之后,这一点也较容易满足),上式可以简化为: 
这里写图片描述

进一步假设输入x和权重w独立同分布,则有: 
这里写图片描述

于是,为了保证输入与输出方差一致,则应该有: 
这里写图片描述

对于一个多层的网络,某一层的方差可以用累积的形式表达: 
这里写图片描述

特别的,反向传播计算梯度时同样具有类似的形式: 
这里写图片描述

综上,为了保证前向传播和反向传播时每一层的方差一致,应满足:

这里写图片描述

但是,实际当中输入与输出的个数往往不相等,于是为了均衡考量,最终我们的权重方差应满足

——————————————————————————————————————— 
这里写图片描述 
———————————————————————————————————————

学过概率统计的都知道 [a,b] 间的均匀分布的方差为: 
这里写图片描述

因此,Xavier初始化的实现就是下面的均匀分布:

—————————————————————————————————————————— 
这里写图片描述 
———————————————————————————————————————————

caffe中相应的源码(includecaffefiller.hpp)如下。

/**
 * @brief Fills a Blob with values @f$ x sim U(-a, +a) @f$ where @f$ a @f$ is
 *        set inversely proportional to number of incoming nodes, outgoing
 *        nodes, or their average.
 *
 * A Filler based on the paper [Bengio and Glorot 2010]: Understanding
 * the difficulty of training deep feedforward neuralnetworks.
 *
 * It fills the incoming matrix by randomly sampling uniform data from [-scale,
 * scale] where scale = sqrt(3 / n) where n is the fan_in, fan_out, or their
 * average, depending on the variance_norm option. You should make sure the
 * input blob has shape (num, a, b, c) where a * b * c = fan_in and num * b * c
 * = fan_out. Note that this is currently not the case for inner product layers.
 *
 * TODO(dox): make notation in above comment consistent with rest & use LaTeX.
 */
template <typename Dtype>
class XavierFiller : public Filler<Dtype> {
 public:
  explicit XavierFiller(const FillerParameter& param)
      : Filler<Dtype>(param) {}
  virtual void Fill(Blob<Dtype>* blob) {
    CHECK(blob->count());
    int fan_in = blob->count() / blob->num();
    int fan_out = blob->count() / blob->channels();
    Dtype n = fan_in;  // default to fan_in
    if (this->filler_param_.variance_norm() ==
        FillerParameter_VarianceNorm_AVERAGE) {
      n = (fan_in + fan_out) / Dtype(2);
    } else if (this->filler_param_.variance_norm() ==
        FillerParameter_VarianceNorm_FAN_OUT) {
      n = fan_out;
    }
    Dtype scale = sqrt(Dtype(3) / n);
    caffe_rng_uniform<Dtype>(blob->count(), -scale, scale,
        blob->mutable_cpu_data());
    CHECK_EQ(this->filler_param_.sparse(), -1)
         << "Sparsity not supported by this Filler.";
  }
};

如果参数里面定义了方差归一化为AVERAGE则 n=(fan_in+fan_out)/2;为FAN_OUT则 n=fan_out,否则默认n= fan_in。

然后按scale=sqrt(3/n)生成[-scale,scale]均匀分布。

原文地址:https://www.cnblogs.com/qw12/p/6254248.html