本文整理汇总了Python中torch.nn.init._calculate_fan_in_and_fan_out方法的典型用法代码示例。如果您正苦于以下问题:Python init._calculate_fan_in_and_fan_out方法的具体用法?Python init._calculate_fan_in_and_fan_out怎么用?Python init._calculate_fan_in_and_fan_out使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.init
的用法示例。
在下文中一共展示了init._calculate_fan_in_and_fan_out方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xavier_uniform_n_
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def xavier_uniform_n_(w: Tensor, gain: float = 1., n: int = 4) -> None:
"""
Xavier initializer for parameters that combine multiple matrices in one
parameter for efficiency. This is e.g. used for GRU and LSTM parameters,
where e.g. all gates are computed at the same time by 1 big matrix.
:param w: parameter
:param gain: default 1
:param n: default 4
"""
with torch.no_grad():
fan_in, fan_out = _calculate_fan_in_and_fan_out(w)
assert fan_out % n == 0, "fan_out should be divisible by n"
fan_out //= n
std = gain * math.sqrt(2.0 / (fan_in + fan_out))
a = math.sqrt(3.0) * std
nn.init.uniform_(w, -a, a)
# pylint: disable=too-many-branches
示例2: xavier_uniform_n_
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def xavier_uniform_n_(w, gain=1., n=4):
"""
Xavier initializer for parameters that combine multiple matrices in one
parameter for efficiency. This is e.g. used for GRU and LSTM parameters,
where e.g. all gates are computed at the same time by 1 big matrix.
:param w:
:param gain:
:param n:
:return:
"""
with torch.no_grad():
fan_in, fan_out = _calculate_fan_in_and_fan_out(w)
assert fan_out % n == 0, "fan_out should be divisible by n"
fan_out = fan_out // n
std = gain * math.sqrt(2.0 / (fan_in + fan_out))
a = math.sqrt(3.0) * std
nn.init.uniform_(w, -a, a)
示例3: reset_parameters
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [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)
示例4: reset_parameters
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [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)
示例5: reset_parameters
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [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)
示例6: reset_parameters
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def reset_parameters(self):
init.kaiming_normal_(self._weight, a=math.sqrt(5))
fan_in, _ = init._calculate_fan_in_and_fan_out(self._weight)
bound = 4 / math.sqrt(fan_in)
init.uniform_(self._bias, -bound, bound)
if self.over_param:
with torch.no_grad(): self._bias.set_(self.manifold.expmap0(self._bias))
示例7: xavier_normal_small_init_
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def xavier_normal_small_init_(tensor, gain=1.):
# type: (Tensor, float) -> Tensor
fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
std = gain * math.sqrt(2.0 / float(fan_in + 4*fan_out))
return _no_grad_normal_(tensor, 0., std)
示例8: xavier_uniform_small_init_
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def xavier_uniform_small_init_(tensor, gain=1.):
# type: (Tensor, float) -> Tensor
fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
std = gain * math.sqrt(2.0 / float(fan_in + 4*fan_out))
a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation
return _no_grad_uniform_(tensor, -a, a)
示例9: init_pytorch_defaults
# 需要导入模块: from torch.nn import init [as 别名]
# 或者: from torch.nn.init import _calculate_fan_in_and_fan_out [as 别名]
def init_pytorch_defaults(m, version='041'):
'''
Apply default inits from pytorch version 0.4.1 or 1.0.0.
pytorch 1.0 default inits are wonky :-(
'''
if version == '041':
# print('init.pt041: {0:s}'.format(str(m.weight.data.size())))
if isinstance(m, nn.Linear):
stdv = 1. / math.sqrt(m.weight.size(1))
m.weight.data.uniform_(-stdv, stdv)
if m.bias is not None:
m.bias.data.uniform_(-stdv, stdv)
elif isinstance(m, nn.Conv2d):
n = m.in_channels
for k in m.kernel_size:
n *= k
stdv = 1. / math.sqrt(n)
m.weight.data.uniform_(-stdv, stdv)
if m.bias is not None:
m.bias.data.uniform_(-stdv, stdv)
elif isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
if m.affine:
m.weight.data.uniform_()
m.bias.data.zero_()
else:
assert False
elif version == '100':
# print('init.pt100: {0:s}'.format(str(m.weight.data.size())))
if isinstance(m, nn.Linear):
init.kaiming_uniform_(m.weight, a=math.sqrt(5))
if m.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(m.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(m.bias, -bound, bound)
elif isinstance(m, nn.Conv2d):
n = m.in_channels
init.kaiming_uniform_(m.weight, a=math.sqrt(5))
if m.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(m.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(m.bias, -bound, bound)
elif isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
if m.affine:
m.weight.data.uniform_()
m.bias.data.zero_()
else:
assert False
elif version == 'custom':
# print('init.custom: {0:s}'.format(str(m.weight.data.size())))
if isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
else:
assert False
else:
assert False