How to initialize weights in PyTorch?

后端 未结 9 1923
暗喜
暗喜 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:32

    Sorry for being so late, I hope my answer will help.

    To initialise weights with a normal distribution use:

    torch.nn.init.normal_(tensor, mean=0, std=1)
    

    Or to use a constant distribution write:

    torch.nn.init.constant_(tensor, value)
    

    Or to use an uniform distribution:

    torch.nn.init.uniform_(tensor, a=0, b=1) # a: lower_bound, b: upper_bound
    

    You can check other methods to initialise tensors here

提交回复
热议问题