本文整理匯總了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)
示例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'))
示例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()
示例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)
示例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)
示例6: xavier
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import xavier_uniform [as 別名]
def xavier(param):
init.xavier_uniform(param)
示例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)
示例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)
示例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_()
示例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_()
示例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