深度学习中的参数初始化

一、参数初始化分类及原理

1、简介

  • 神经网络的训练过程中的参数学习是基于梯度下降法进行优化的。梯度下降法需要在开始训练时给每一个参数赋一个初始值。这个初始值的选取十分关键。一般我们希望数据和参数的均值都为 0,输入和输出数据的方差一致。在实际应用中,参数服从高斯分布或者均匀分布都是比较有效的初始化方式。

  • A well chosen initialization can:

    • Speed up the convergence of gradient descent
    • Increase the odds of gradient descent converging to a lower training (and generalization) error
  • Poor initialization can:
    • lead to vanishing/exploding gradients, which also slows down the optimization algorithm
  • Random initialization is used to break symmetry and make sure different hidden units can learn different things

2、分类

参数初始化分类

3、原理

  • 为了使得在经过多层网络后,信号不被过分放大或过分减弱,我们尽可能保持 每个神经元的输入和输出的方差一致
    参数初始化原理

  • 高斯分布

    • 均值为: m e a n = 0
    • Xavier 初始化: v a r = 1 n
    • He 初始化: v a r = 2 n
  • 均匀分布
    • 服从 [ r , r ] 间的均匀分布,均值为: m e a n = ( a + b ) 2 = 0 ,方差为: v a r = ( b a ) 2 12 = r 2 3
    • Xavier 初始化: v a r = 1 n ,可得, r = 3 n
    • He 初始化: v a r = 2 n ,可得, r = 6 n

4、总结

  • 使用 RELU(without BN) 激活函数时,最好选用 He 初始化方法,将参数初始化为服从高斯分布或者均匀分布的较小随机数
  • 使用 BN 时,减少了网络对参数初始值尺度的依赖,此时使用较小的标准差(eg:0.01)进行初始化即可
  • 借助预训练模型中参数作为新任务参数初始化的方式也是一种简便易行且十分有效的模型参数初始化方法

二、参数初始化代码实践

0、符合约定

n_in:为网络的输入大小
n_out:为网络的输出大小
n:为 n_in 或 (n_in + n_out) * 0.5--->(同时考虑信号在前向和反向传播中都不被放大或缩小)

1、Xavier 初始化

  • 高斯分布初始化:
# 适用于普通激活函数(tanh,sigmoid):stdev 为高斯分布的标准差,均值设为 0
stdev = np.sqrt(1/n) 
W = tf.Variable(np.random.randn(n_in, n_out) * stdev)
  • 均匀分布初始化:
# 适用于普通激活函数(tanh, sigmoid)
scale = np.sqrt(3/n)
W = tf.Variable(np.random.uniform(low=-scale, high=scale, size=[n_in, n_out]))

2、He 初始化

  • 高斯分布初始化:
# 适用于 ReLU:stdev 为高斯分布的标准差,均值设为 0
stdev = np.sqrt(2/n)
W = tf.Variable(np.random.randn(n_in, n_out) * stdev) 
  • 均匀分布初始化:
# 适用于 ReLU
scale = np.sqrt(6/n)
W = tf.Variable(np.random.uniform(low=-scale, high=scale, size=[n_in, n_out]))

3、BN+高斯分布(小 σ )随机初始化

W = tf.Variable(np.random.randn(node_in, node_out) * 0.01)
======
W = tf.Variable(np.random.randn(shape) * 0.01)  # shape 可以不止二维! 

......
fc = tf.contrib.layers.batch_norm(fc, center=True, scale=True, is_training=True)
fc = tf.nn.relu(fc)

4、TensorFLow 中的实现


三、参考文献

1、聊一聊深度学习的weight initialization
2、深度学习网络训练技巧汇总
3、A tutorial on Deep Learning for Objects and Scenes at CVPR(Kaiming He)
4、tensorflow 1.0 学习:参数初始化
5、第七章 网络优化与正则化(复旦-邱锡鹏)

猜你喜欢

转载自blog.csdn.net/mzpmzk/article/details/79839047