本文整理汇总了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)