Custom parameter initializer

weight_init DEF (m): 
    IF the isinstance (m, nn.Linear): 
        nn.init.xavier_normal_ (m.weight) 
        nn.init.constant_ (m.bias, 0) 
    # may be determined whether conv2d, using the appropriate initialization embodiment 
    elif the isinstance (m, nn.Conv2d): 
        nn.init.kaiming_normal_ (m.weight, mODE = 'fan_out',-iinearity = 'RELU') 
     # whether the normalized batch layer 
    elif isinstance (m, nn.BatchNorm2d) : 
        nn.init.constant_ (m.weight,. 1) 
        nn.init.constant_ (m.bias, 0) 
# initialize the network structure 2.         
Model = net (in_dim, n_hidden_1, n_hidden_2, out_dim) 
# 3. application of the weight_init sub-module 
model.apply (weight_init)

  Custom parameter initializer

Original blog: https://blog.csdn.net/dss_dssssd/article/details/83990511

def weight_init (m): if isinstance (m, nn.Linear): nn.init.xavier_normal_ (m.weight) nn.init.constant_ (m.bias, 0) # may be determined whether conv2d, using the appropriate initialization embodiment elif isinstance (m, nn.Conv2d): nn.init.kaiming_normal_ (m.weight, mode = 'fan_out', nonlinearity = 'relu') # whether the normalized batch layer elif isinstance (m, nn.BatchNorm2d) : nn.init.constant_ (m.weight, 1) nn.init.constant_ (m.bias, 0) # 2. initialize the network structure model = net (in_dim, n_hidden_1, n_hidden_2, out_dim) # 3. application of the weight_init the sub-module model.apply (weight_init) ---------------- Disclaimer: This article is CSDN blogger "ink Yun 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: https: //blog.csdn.net/dss_dssssd/article/details/83990511

Guess you like

Origin www.cnblogs.com/baitian963/p/12075271.html