在PyTorch中自定义重量初始化

时间:2018-07-01 18:17:56

标签: python pytorch

custom weight initialization中实现PyTorch方法的正确方法是什么?

我相信我无法直接在'torch.nn.init`中添加任何方法,但希望使用自己的专有方法来初始化模型的权重。

2 个答案:

答案 0 :(得分:5)

您可以定义一种根据每个图层初始化权重的方法:

def weights_init(m):
    classname = m.__class__.__name__

    if classname.find('Conv2d') != -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)

然后将其应用于您的网络:

model = create_your_model()
model.apply(weights_init)

答案 1 :(得分:1)

请参阅https://discuss.pytorch.org/t/how-to-initialize-weights-bias-of-rnn-lstm-gru/2879/2以供参考。

您可以

weight_dict = net.state_dict()
new_weight_dict = {}
for param_key in state_dict:
     # custom initialization in new_weight_dict,
     # You can initialize partially i.e only some of the variables and let others stay as it is
weight_dict.update(new_weight_dict)
net.load_state_dict(new_weight_dict)