权重初始化对于训练神经网络至关重要,好的初始化权重可以有效的避免梯度消失等问题的发生。
在pytorch的使用过程中有几种权重初始化的方法供大家参考。 注意:第一种方法不推荐。尽量使用后两种方法。
代码语言:javascript复制# not recommend
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
# recommend
def initialize_weights(m):
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0, 0.02)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.02)
m.bias.data.zero_()
# recommend
def weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_normal_(m.weight.data)
nn.init.xavier_normal_(m.bias.data)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight,1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight,1)
nn.init.constant_(m.bias, 0)
编写好weights_init
函数后,可以使用模型的apply
方法对模型进行权重初始化。
net = Residual() # generate an instance network from the Net class
net.apply(weights_init) # apply weight init