本文整理匯總了Python中torch.nn.init.kaiming_uniform_方法的典型用法代碼示例。如果您正苦於以下問題:Python init.kaiming_uniform_方法的具體用法?Python init.kaiming_uniform_怎麽用?Python init.kaiming_uniform_使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn.init
的用法示例。
在下文中一共展示了init.kaiming_uniform_方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: init_weights
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def init_weights(model):
if isinstance(model, nn.Linear):
if model.weight is not None:
init.kaiming_uniform_(model.weight.data)
if model.bias is not None:
init.normal_(model.bias.data)
elif isinstance(model, nn.BatchNorm1d):
if model.weight is not None:
init.normal_(model.weight.data, mean=1, std=0.02)
if model.bias is not None:
init.constant_(model.bias.data, 0)
elif isinstance(model, nn.BatchNorm2d):
if model.weight is not None:
init.normal_(model.weight.data, mean=1, std=0.02)
if model.bias is not None:
init.constant_(model.bias.data, 0)
elif isinstance(model, nn.BatchNorm3d):
if model.weight is not None:
init.normal_(model.weight.data, mean=1, std=0.02)
if model.bias is not None:
init.constant_(model.bias.data, 0)
else:
pass
示例2: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def reset_parameters(self):
init.kaiming_uniform_(self.weight, a=math.sqrt(5))
if self.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(self.bias, -bound, bound)
示例3: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def reset_parameters(self, zero_init=False):
init.kaiming_uniform_(self.weight, a=math.sqrt(5))
if zero_init:
# normalize cannot handle zero weight in some cases.
self.weight.data.div_(1000)
if self.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(self.bias, -bound, bound)
示例4: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def reset_parameters(self):
n = self.in_channels
init.kaiming_uniform_(self.weight, a=math.sqrt(5))
if self.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(self.bias, -bound, bound)
示例5: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def __init__(self, a=0, mode='fan_in', nonlinearity='leaky_relu', modules=None,
targets=['Conv', 'Linear', 'Bilinear']):
def initialiser(module):
init.kaiming_uniform_(module.weight.data, a=a, mode=mode, nonlinearity=nonlinearity)
super(KaimingUniform, self).__init__(initialiser, modules=modules, targets=targets)
示例6: spectral_init
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def spectral_init(module, gain=1):
init.kaiming_uniform_(module.weight, gain)
if module.bias is not None:
module.bias.data.zero_()
return spectral_norm(module)
示例7: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def __init__(self, input_dim, hidden_dim, num_layers):
super(NICEModel, self).__init__()
assert (input_dim % 2 == 0), "[NICEModel] only even input dimensions supported for now"
assert (num_layers > 2), "[NICEModel] num_layers must be at least 3"
self.input_dim = input_dim
half_dim = int(input_dim / 2)
self.layer1 = AdditiveCouplingLayer(input_dim, 'odd', _build_relu_network(half_dim, hidden_dim, num_layers))
self.layer2 = AdditiveCouplingLayer(input_dim, 'even', _build_relu_network(half_dim, hidden_dim, num_layers))
self.layer3 = AdditiveCouplingLayer(input_dim, 'odd', _build_relu_network(half_dim, hidden_dim, num_layers))
self.layer4 = AdditiveCouplingLayer(input_dim, 'even', _build_relu_network(half_dim, hidden_dim, num_layers))
self.scaling_diag = nn.Parameter(torch.ones(input_dim))
# randomly initialize weights:
for p in self.layer1.parameters():
if len(p.shape) > 1:
init.kaiming_uniform_(p, nonlinearity='relu')
else:
init.normal_(p, mean=0., std=0.001)
for p in self.layer2.parameters():
if len(p.shape) > 1:
init.kaiming_uniform_(p, nonlinearity='relu')
else:
init.normal_(p, mean=0., std=0.001)
for p in self.layer3.parameters():
if len(p.shape) > 1:
init.kaiming_uniform_(p, nonlinearity='relu')
else:
init.normal_(p, mean=0., std=0.001)
for p in self.layer4.parameters():
if len(p.shape) > 1:
init.kaiming_uniform_(p, nonlinearity='relu')
else:
init.normal_(p, mean=0., std=0.001)
示例8: _init_params
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def _init_params(self):
for name, module in self.named_modules():
if isinstance(module, nn.Conv2d):
init.kaiming_uniform_(module.weight)
if module.bias is not None:
init.constant_(module.bias, 0)
示例9: _init_params
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import kaiming_uniform_ [as 別名]
def _init_params(self):
for name, module in self.named_modules():
if isinstance(module, nn.Conv2d):
if 'final_conv' in name:
init.normal_(module.weight, mean=0.0, std=0.01)
else:
init.kaiming_uniform_(module.weight)
if module.bias is not None:
init.constant_(module.bias, 0)