How to initialize weights in PyTorch?

后端 未结 9 1930
暗喜
暗喜 2020-11-28 01:10

How to initialize the weights and biases (for example, with He or Xavier initialization) in a network in PyTorch?

9条回答
  •  隐瞒了意图╮
    2020-11-28 01:45

    If you see a deprecation warning (@Fábio Perez)...

    def init_weights(m):
        if type(m) == nn.Linear:
            torch.nn.init.xavier_uniform_(m.weight)
            m.bias.data.fill_(0.01)
    
    net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
    net.apply(init_weights)
    

提交回复
热议问题