PyTorch中的自定义重量初始化

Omr*_*dik 4 python pytorch

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

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

Man*_*nas 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)
Run Code Online (Sandbox Code Playgroud)

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

model = create_your_model()
model.apply(weights_init)
Run Code Online (Sandbox Code Playgroud)


Uma*_*pta 2

请参阅https://discuss.pytorch.org/t/how-to-initialize-weights-bias-of-rnn-lstm-gru/287 ​​9/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)
Run Code Online (Sandbox Code Playgroud)