本文整理汇总了Python中torch.nn.init.kaiming_normal_方法的典型用法代码示例。如果您正苦于以下问题:Python init.kaiming_normal_方法的具体用法?Python init.kaiming_normal_怎么用?Python init.kaiming_normal_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.init
的用法示例。
在下文中一共展示了init.kaiming_normal_方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def initialize_weights(net_l, scale=1):
if not isinstance(net_l, list):
net_l = [net_l]
for net in net_l:
for m in net.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight, a=0, mode='fan_in')
m.weight.data *= scale # for residual block
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
init.kaiming_normal_(m.weight, a=0, mode='fan_in')
m.weight.data *= scale
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias.data, 0.0)
示例2: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type, init_gain):
def init_func(m):
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=init_gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, init_gain)
init.constant_(m.bias.data, 0.0)
net.apply(init_func)
示例3: weights_init_He_normal
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def weights_init_He_normal(m):
classname = m.__class__.__name__
# print classname
if classname.find('Transpose') != -1:
m.weight.data.normal_(0.0, 0.001)
if not m.bias is None:
m.bias.data.zero_()
elif classname.find('Conv') != -1:
# std = np.sqrt(2. / (m.kernel_size[0] * m.kernel_size[1] * m.out_channels))
# m.weight.data.normal_(0.0, std)
init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')
if not m.bias is None:
m.bias.data.zero_()
elif classname.find('BatchNorm') != -1:
m.weight.data.fill_(1.)
if not m.bias is None:
m.bias.data.zero_()
elif classname.find('Linear') != -1:
m.weight.data.normal_(0.0, 0.001)
if not m.bias is None:
m.bias.data.zero_()
示例4: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', gain=0.02):
def init_func(m):
# this will apply to each layer
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('conv')!=-1 or classname.find('Linear')!=-1):
if init_type=='normal':
init.normal_(m.weight.data, 0.0, gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')#good for relu
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, gain)
init.constant_(m.bias.data, 0.0)
#print('initialize network with %s' % init_type)
net.apply(init_func)
示例5: weights_init_kaiming
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def weights_init_kaiming(m, scale=1):
classname = m.__class__.__name__
if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
if classname != "MeanShift":
print('initializing [%s] ...' % classname)
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
m.weight.data *= scale
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, (nn.Linear)):
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
m.weight.data *= scale
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, (nn.BatchNorm2d)):
init.constant_(m.weight.data, 1.0)
m.weight.data *= scale
init.constant_(m.bias.data, 0.0)
示例6: init_model
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_model(self, scale=0.1):
# Common practise for initialization.
for layer in self.netG.modules():
if isinstance(layer, nn.Conv2d):
init.kaiming_normal_(layer.weight, a=0, mode='fan_in')
layer.weight.data *= scale # for residual block
if layer.bias is not None:
layer.bias.data.zero_()
elif isinstance(layer, nn.Linear):
init.kaiming_normal_(layer.weight, a=0, mode='fan_in')
layer.weight.data *= scale
if layer.bias is not None:
layer.bias.data.zero_()
elif isinstance(layer, nn.BatchNorm2d):
init.constant_(layer.weight, 1)
init.constant_(layer.bias.data, 0.0)
示例7: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', gain=0.02):
def init_func(m):
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, gain)
init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func)
示例8: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', gain=0.02):
def init_func(m):
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, gain)
init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func)
示例9: init_params
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_params(self):
'''
Function to initialze the parameters
'''
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal_(m.weight, std=0.001)
if m.bias is not None:
init.constant_(m.bias, 0)
示例10: get_weight_init_fn
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def get_weight_init_fn( activation_fn ):
"""get weight_initialization function according to activation_fn
Notes
-------------------------------------
if activation_fn requires arguments, use partial() to wrap activation_fn
"""
fn = activation_fn
if hasattr( activation_fn , 'func' ):
fn = activation_fn.func
if fn == nn.LeakyReLU:
negative_slope = 0
if hasattr( activation_fn , 'keywords'):
if activation_fn.keywords.get('negative_slope') is not None:
negative_slope = activation_fn.keywords['negative_slope']
if hasattr( activation_fn , 'args'):
if len( activation_fn.args) > 0 :
negative_slope = activation_fn.args[0]
return partial( kaiming_normal_ , a = negative_slope )
elif fn == nn.ReLU or fn == nn.PReLU :
return partial( kaiming_normal_ , a = 0 )
else:
return xavier_normal_
return
示例11: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', init_gain=0.02):
def init_func(m):
name = m.__class__.__name__
if hasattr(m, 'weight') and ('Conv' in name or 'Linear' in name):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=init_gain)
else:
raise NotImplementedError(
f'initialization method [{init_type}] is not implemented')
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif 'BatchNorm2d' in name:
init.normal_(m.weight.data, 1.0, init_gain)
init.constant_(m.bias.data, 0.0)
net.apply(init_func)
示例12: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', gain=0.02):
def init_func(m):
# this will apply to each layer
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('conv')!=-1 or classname.find('Linear')!=-1):
if init_type=='normal':
init.normal_(m.weight.data, 0.0, gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')#good for relu
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, gain)
init.constant_(m.bias.data, 0.0)
#print('initialize network with %s' % init_type)
net.apply(init_func)
############################################
# save checkpoint and resume
############################################
示例13: initi
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def initi(self):
#init.kaiming_normal_(self.con_layer.weight, a=0, mode='fan_in', nonlinearity='relu')
init.normal_(self.con_layer.weight, std=0.01)
if self.con_layer.bias is not None:
init.constant_(self.con_layer.bias, 0.0)
示例14: __init__
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def __init__(self, input_feature_size, embeding_fea_size=1024, dropout=0.5):
super(self.__class__, self).__init__()
# embeding
self.embeding_fea_size = embeding_fea_size
self.embeding = nn.Linear(input_feature_size, embeding_fea_size)
self.embeding_bn = nn.BatchNorm1d(embeding_fea_size)
init.kaiming_normal_(self.embeding.weight, mode='fan_out')
init.constant_(self.embeding.bias, 0)
init.constant_(self.embeding_bn.weight, 1)
init.constant_(self.embeding_bn.bias, 0)
self.drop = nn.Dropout(dropout)
示例15: init_weights
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import kaiming_normal_ [as 别名]
def init_weights(net, init_type='normal', init_gain=0.02):
"""Initialize network weights.
Parameters:
net (network) -- network to be initialized
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
init_gain (float) -- scaling factor for normal, xavier and orthogonal.
We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
work better for some applications. Feel free to try yourself.
"""
def init_func(m): # define the initialization function
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=init_gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
init.normal_(m.weight.data, 1.0, init_gain)
init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func) # apply the initialization function <init_func>