當前位置: 首頁>>代碼示例>>Python>>正文


Python init._calculate_fan_in_and_fan_out方法代碼示例

本文整理匯總了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 
開發者ID:joeynmt,項目名稱:joeynmt,代碼行數:22,代碼來源:initialization.py

示例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) 
開發者ID:bastings,項目名稱:interpretable_predictions,代碼行數:19,代碼來源:util.py

示例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) 
開發者ID:rtqichen,項目名稱:residual-flows,代碼行數:8,代碼來源:lipschitz.py

示例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) 
開發者ID:rtqichen,項目名稱:residual-flows,代碼行數:11,代碼來源:mixed_lipschitz.py

示例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) 
開發者ID:ruinmessi,項目名稱:ASFF,代碼行數:9,代碼來源:deform_conv2d.py

示例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)) 
開發者ID:emilemathieu,項目名稱:pvae,代碼行數:9,代碼來源:manifold_layers.py

示例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) 
開發者ID:ardigen,項目名稱:MAT,代碼行數:8,代碼來源:utils.py

示例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) 
開發者ID:ardigen,項目名稱:MAT,代碼行數:9,代碼來源:utils.py

示例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 
開發者ID:Philip-Bachman,項目名稱:amdim-public,代碼行數:59,代碼來源:utils.py


注:本文中的torch.nn.init._calculate_fan_in_and_fan_out方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。