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


Python init.xavier_uniform方法代碼示例

本文整理匯總了Python中torch.nn.init.xavier_uniform方法的典型用法代碼示例。如果您正苦於以下問題:Python init.xavier_uniform方法的具體用法?Python init.xavier_uniform怎麽用?Python init.xavier_uniform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torch.nn.init的用法示例。


在下文中一共展示了init.xavier_uniform方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, in_size, out_size, kernel_size=3,stride=1, padding=1, activation=F.relu):
        super(residualUnit, self).__init__()
        self.conv1 = nn.Conv2d(in_size, out_size, kernel_size, stride=1, padding=1)
        init.xavier_uniform(self.conv1.weight, gain = np.sqrt(2.0)) #or gain=1
        init.constant(self.conv1.bias, 0)
        self.conv2 = nn.Conv2d(out_size, out_size, kernel_size, stride=1, padding=1)
        init.xavier_uniform(self.conv2.weight, gain = np.sqrt(2.0)) #or gain=1
        init.constant(self.conv2.bias, 0)
        self.activation = activation
        self.bn1 = nn.BatchNorm2d(out_size)
        self.bn2 = nn.BatchNorm2d(out_size)
        self.in_size = in_size
        self.out_size = out_size
        if in_size != out_size:
            self.convX = nn.Conv2d(in_size, out_size, kernel_size=1, stride=1, padding=0)
            self.bnX = nn.BatchNorm2d(out_size) 
開發者ID:ginobilinie,項目名稱:medSynthesisV1,代碼行數:18,代碼來源:Unet2d_pytorch.py

示例2: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, h_size, embedding_size, y_size):
        super(MLP_token_plain, self).__init__()
        self.deterministic_output = nn.Sequential(
            nn.Linear(h_size, embedding_size),
            nn.ReLU(),
            nn.Linear(embedding_size, y_size)
        )
        self.token_output = nn.Sequential(
            nn.Linear(h_size, embedding_size),
            nn.ReLU(),
            nn.Linear(embedding_size, 1)
        )

        for m in self.modules():
            if isinstance(m, nn.Linear):
                m.weight.data = init.xavier_uniform(m.weight.data, gain=nn.init.calculate_gain('relu')) 
開發者ID:JiaxuanYou,項目名稱:graph-generation,代碼行數:18,代碼來源:model.py

示例3: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, bias=True, dilation=1, nd=2):
        super(conv23D_bn_relu_Unit, self).__init__()
        
        assert nd==1 or nd==2 or nd==3, 'nd is not correctly specified!!!!, it should be {1,2,3}'
        if nd==2:
            self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation)
            self.bn = nn.BatchNorm2d(out_channels)
        elif nd==3:
            self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation)
            self.bn = nn.BatchNorm3d(out_channels)
        else:
            self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation)
            self.bn = nn.BatchNorm1d(out_channels)
       
        init.xavier_uniform(self.conv.weight, gain = np.sqrt(2.0))
        init.constant(self.conv.bias, 0)
        self.relu = nn.ReLU() 
開發者ID:ginobilinie,項目名稱:medSynthesisV1,代碼行數:19,代碼來源:nnBuildUnits.py

示例4: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, in_size, out_size, kernel_size=3,stride=1, padding=1, activation=F.relu):
        super(residualUnit, self).__init__()
        self.conv1 = nn.Conv3d(in_size, out_size, kernel_size, stride=1, padding=1)
        init.xavier_uniform(self.conv1.weight, gain = np.sqrt(2.0)) #or gain=1
        init.constant(self.conv1.bias, 0)
        self.conv2 = nn.Conv3d(out_size, out_size, kernel_size, stride=1, padding=1)
        init.xavier_uniform(self.conv2.weight, gain = np.sqrt(2.0)) #or gain=1
        init.constant(self.conv2.bias, 0)
        self.activation = activation
        self.bn1 = nn.BatchNorm3d(out_size)
        self.bn2 = nn.BatchNorm3d(out_size)
        self.in_size = in_size
        self.out_size = out_size
        if in_size != out_size:
            self.convX = nn.Conv3d(in_size, out_size, kernel_size=1, stride=1, padding=0)
            self.bnX = nn.BatchNorm3d(out_size) 
開發者ID:ginobilinie,項目名稱:medSynthesisV1,代碼行數:18,代碼來源:ResUnet3d_pytorch.py

示例5: conv_init

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def conv_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        init.xavier_uniform(m.weight, gain=np.sqrt(2))
        init.constant(m.bias, 0)
    elif classname.find('BatchNorm') != -1:
        init.constant(m.weight, 1)
        init.constant(m.bias, 0) 
開發者ID:wgrathwohl,項目名稱:JEM,代碼行數:10,代碼來源:wideresnet.py

示例6: xavier

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def xavier(param):
    init.xavier_uniform(param) 
開發者ID:soo89,項目名稱:CSD-SSD,代碼行數:4,代碼來源:train_csd.py

示例7: reset_parameters

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def reset_parameters(self,
                         init_fc_w=xavier_uniform,
                         init_fc_b=lambda x: constant(x, 0),
                         init_embed=lambda x: normal(x, std=0.05),
                         **kwargs):
        """Resets the trainable weights."""
        def set_constant_row(parameters, iRow=0, value=0):
            """Return `parameters` with row `iRow` as s constant `value`."""
            data = parameters.data
            data[iRow, :] = value
            return torch.nn.Parameter(data, requires_grad=parameters.requires_grad)

        np.random.seed(self.seed)
        if self.seed is not None:
            torch.manual_seed(self.seed)

        if not self.isHash:
            self.embedding.weight = init_embed(self.embedding.weight)

            if self.paddingIdx is not None:
                # Unfortunately has to set weight to 0 even when paddingIdx =0
                self.embedding.weight = set_constant_row(self.embedding.weight)
        else:
            self.embedding.reset_parameters(**kwargs)

        self.fc1.weight = init_fc_w(self.fc1.weight)
        self.fc1.biais = init_fc_b(self.fc1.weight) 
開發者ID:YannDubs,項目名稱:Hash-Embeddings,代碼行數:29,代碼來源:model.py

示例8: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, input_size, width=3, dropout=0.2, nopad=False):
        super(GatedConv, self).__init__()
        self.conv = WeightNormConv2d(input_size, 2 * input_size,
                                     kernel_size=(width, 1), stride=(1, 1),
                                     padding=(width // 2 * (1 - nopad), 0))
        init.xavier_uniform(self.conv.weight, gain=(4 * (1 - dropout))**0.5)
        self.dropout = nn.Dropout(dropout) 
開發者ID:xiadingZ,項目名稱:video-caption-openNMT.pytorch,代碼行數:9,代碼來源:Conv2Conv.py

示例9: xavier_uniform_relu

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def xavier_uniform_relu(modules):
    for m in modules:
        if isinstance(m, nn.Conv2d):
            init.xavier_uniform(m.weight.data, gain=init.calculate_gain('relu'))
            if m.bias is not None:
                m.bias.data.zero_()
        elif isinstance(m, nn.BatchNorm2d):
            m.weight.data.fill_(1)
            m.bias.data.zero_() 
開發者ID:xdspacelab,項目名稱:sscdnet,代碼行數:11,代碼來源:init.py

示例10: xavier_uniform_sigmoid

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def xavier_uniform_sigmoid(modules):
    for m in modules:
        if isinstance(m, nn.Conv2d):
            init.xavier_uniform(m.weight.data, gain=init.calculate_gain('sigmoid'))
            if m.bias is not None:
                m.bias.data.zero_()
        elif isinstance(m, nn.BatchNorm2d):
            m.weight.data.fill_(1)
            m.bias.data.zero_() 
開發者ID:xdspacelab,項目名稱:sscdnet,代碼行數:11,代碼來源:init.py

示例11: __init__

# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def __init__(self, input_dim, hidden_dim, latent_dim, max_num_nodes, pool='sum'):
        '''
        Args:
            input_dim: input feature dimension for node.
            hidden_dim: hidden dim for 2-layer gcn.
            latent_dim: dimension of the latent representation of graph.
        '''
        super(GraphVAE, self).__init__()
        self.conv1 = model.GraphConv(input_dim=input_dim, output_dim=hidden_dim)
        self.bn1 = nn.BatchNorm1d(input_dim)
        self.conv2 = model.GraphConv(input_dim=hidden_dim, output_dim=hidden_dim)
        self.bn2 = nn.BatchNorm1d(input_dim)
        self.act = nn.ReLU()

        output_dim = max_num_nodes * (max_num_nodes + 1) // 2
        #self.vae = model.MLP_VAE_plain(hidden_dim, latent_dim, output_dim)
        self.vae = model.MLP_VAE_plain(input_dim * input_dim, latent_dim, output_dim)
        #self.feature_mlp = model.MLP_plain(latent_dim, latent_dim, output_dim)

        self.max_num_nodes = max_num_nodes
        for m in self.modules():
            if isinstance(m, model.GraphConv):
                m.weight.data = init.xavier_uniform(m.weight.data, gain=nn.init.calculate_gain('relu'))
            elif isinstance(m, nn.BatchNorm1d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

        self.pool = pool 
開發者ID:JiaxuanYou,項目名稱:graph-generation,代碼行數:30,代碼來源:model.py


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