这里总结几种权重矩阵的初始化方式:
1:截断的正态分布:
def init_matrix(shape):
return tf.truncated_normal(shape, mean=0, stddev=0.1)
2.xavier_initializer()法:这个初始化器是用来保持每一层的梯度大小都差不多相同
W = tf.get_variable("W", shape=[784, 100],
initializer=tf.contrib.layers.xavier_initializer())
3.cs231n推荐:
w=np.random.randn(in,out)/np.sqrt(2.0/in)#in,out为输入输出的神经元个数
使用:
W1_init = np.random.randn(784, 100).astype(np.float32) * np.sqrt(2.0/(784))
b1_init = np.zeros([100]).astype(np.float32)
W2_init = np.random.randn(100, 100).astype(np.float32) * np.sqrt(2.0/(100))
b2_init = np.zeros([100]).astype(np.float32)
W3_init = np.random.randn(100, 10).astype(np.float32) * np.sqrt(2.0/(100))
b3_init = np.zeros([10]).astype(np.float32)
W_inits = [W1_init, b1_init, W2_init, b2_init, W3_init, b3_init]