本文整理匯總了Python中torch.nn.init.uniform方法的典型用法代碼示例。如果您正苦於以下問題:Python init.uniform方法的具體用法?Python init.uniform怎麽用?Python init.uniform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn.init
的用法示例。
在下文中一共展示了init.uniform方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: assert_and_infer_cfg
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def assert_and_infer_cfg(make_immutable=True):
"""Call this function in your script after you have finished setting all cfg
values that are necessary (e.g., merging a config from a file, merging
command line config options, etc.). By default, this function will also
mark the global cfg as immutable to prevent changing the global cfg settings
during script execution (which can lead to hard to debug errors or code
that's harder to understand than is necessary).
"""
if __C.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS:
assert __C.VGG.IMAGENET_PRETRAINED_WEIGHTS, \
"Path to the weight file must not be empty to load imagenet pertrained resnets."
if version.parse(torch.__version__) < version.parse('0.4.0'):
__C.PYTORCH_VERSION_LESS_THAN_040 = True
# create alias for PyTorch version less than 0.4.0
init.uniform_ = init.uniform
init.normal_ = init.normal
init.constant_ = init.constant
nn.GroupNorm = mynn.GroupNorm
if make_immutable:
cfg.immutable(True)
示例2: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def __init__(self, input_size, activation=nn.Tanh(),
method="dot"):
super(AttnScore, self).__init__()
self.activation = activation
self.input_size = input_size
self.method = method
if method == "general":
self.linear = nn.Linear(input_size, input_size)
init.uniform(self.linear.weight.data, -0.005, 0.005)
elif method == "concat":
self.linear_1 = nn.Linear(input_size*2, input_size)
self.linear_2 = nn.Linear(input_size, 1)
init.uniform(self.linear_1.weight.data, -0.005, 0.005)
init.uniform(self.linear_2.weight.data, -0.005, 0.005)
elif method == "tri_concat":
self.linear = nn.Linear(input_size*3, 1)
init.uniform(self.linear.weight.data, -0.005, 0.005)
示例3: forward_test
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def forward_test(self, input_features, adj):
self.max_num_nodes = 4
adj_data = torch.zeros(self.max_num_nodes, self.max_num_nodes)
adj_data[:4, :4] = torch.FloatTensor([[1,1,0,0], [1,1,1,0], [0,1,1,1], [0,0,1,1]])
adj_features = torch.Tensor([2,3,3,2])
adj_data1 = torch.zeros(self.max_num_nodes, self.max_num_nodes)
adj_data1 = torch.FloatTensor([[1,1,1,0], [1,1,0,1], [1,0,1,0], [0,1,0,1]])
adj_features1 = torch.Tensor([3,3,2,2])
S = self.edge_similarity_matrix(adj_data, adj_data1, adj_features, adj_features1,
self.deg_feature_similarity)
# initialization strategies
init_corr = 1 / self.max_num_nodes
init_assignment = torch.ones(self.max_num_nodes, self.max_num_nodes) * init_corr
#init_assignment = torch.FloatTensor(4, 4)
#init.uniform(init_assignment)
assignment = self.mpm(init_assignment, S)
#print('Assignment: ', assignment)
# matching
row_ind, col_ind = scipy.optimize.linear_sum_assignment(-assignment.numpy())
print('row: ', row_ind)
print('col: ', col_ind)
permuted_adj = self.permute_adj(adj_data, row_ind, col_ind)
print('permuted: ', permuted_adj)
adj_recon_loss = self.adj_recon_loss(permuted_adj, adj_data1)
print(adj_data1)
print('diff: ', adj_recon_loss)
示例4: weights_init_normal
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_normal(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.uniform(m.weight.data, 0.0, 0.02)
elif classname.find('Linear') != -1:
init.uniform(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例5: weights_init_xavier
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_xavier(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.xavier_normal(m.weight.data, gain=1)
elif classname.find('Linear') != -1:
init.xavier_normal(m.weight.data, gain=1)
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例6: weights_init_kaiming
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_kaiming(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例7: weights_init_orthogonal
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_orthogonal(m):
classname = m.__class__.__name__
print(classname)
if classname.find('Conv') != -1:
init.orthogonal(m.weight.data, gain=1)
elif classname.find('Linear') != -1:
init.orthogonal(m.weight.data, gain=1)
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例8: assert_and_infer_cfg
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def assert_and_infer_cfg(make_immutable=True):
"""Call this function in your script after you have finished setting all cfg
values that are necessary (e.g., merging a config from a file, merging
command line config options, etc.). By default, this function will also
mark the global cfg as immutable to prevent changing the global cfg settings
during script execution (which can lead to hard to debug errors or code
that's harder to understand than is necessary).
"""
if __C.MODEL.RPN_ONLY or __C.MODEL.FASTER_RCNN:
__C.RPN.RPN_ON = True
if __C.RPN.RPN_ON or __C.RETINANET.RETINANET_ON:
__C.TEST.PRECOMPUTED_PROPOSALS = False
if __C.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS:
assert __C.RESNETS.IMAGENET_PRETRAINED_WEIGHTS, \
"Path to the weight file must not be empty to load imagenet pertrained resnets."
if set([__C.MRCNN.ROI_MASK_HEAD, __C.KRCNN.ROI_KEYPOINTS_HEAD]) & _SHARE_RES5_HEADS:
__C.MODEL.SHARE_RES5 = True
if version.parse(torch.__version__) < version.parse('0.4.0'):
__C.PYTORCH_VERSION_LESS_THAN_040 = True
# create alias for PyTorch version less than 0.4.0
init.uniform_ = init.uniform
init.normal_ = init.normal
init.constant_ = init.constant
nn.GroupNorm = mynn.GroupNorm
if make_immutable:
cfg.immutable(True)
示例9: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def __init__(self, d_in, d_out, bias=True, out_norm=False):
super().__init__(d_in, d_out, bias)
self.out_norm = out_norm
stdv = 1. / math.sqrt(self.weight.size(1))
init.uniform(self.weight, -stdv, stdv)
if bias:
self.bias.data.zero_()
示例10: init_weights
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
# init_deconv_bilinear(m.weight)
示例11: init_weights
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
init.uniform(m.bias)
init.xavier_uniform(m.weight)
示例12: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def __init__(self, with_bn=False, fp16=False, rgb_max=255., div_flow=20):
super(FlowNet2CS, self).__init__()
self.with_bn = with_bn
self.fp16 = fp16
self.rgb_max = rgb_max
self.div_flow = div_flow
self.channelnorm = ChannelNorm()
# First Block (FlowNetC)
self.flownetc = FlowNetC(with_bn=with_bn, fp16=fp16)
self.upsample1 = nn.Upsample(scale_factor=4, mode='bilinear')
self.resample1 = (nn.Sequential(tofp32(), Resample2d(), tofp16())
if fp16 else Resample2d())
# Block (FlowNetS1)
self.flownets_1 = FlowNetS(with_bn=with_bn)
self.upsample2 = nn.Upsample(scale_factor=4, mode='bilinear')
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
nn_init.uniform(m.bias)
nn_init.xavier_uniform(m.weight)
if isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
nn_init.uniform(m.bias)
nn_init.xavier_uniform(m.weight)
示例13: weights_init_normal
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_normal(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.uniform(m.weight.data, 0.0, 0.02)
elif classname.find('Linear') != -1:
init.uniform(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例14: weights_init_xavier
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_xavier(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.xavier_normal(m.weight.data, gain=1)
elif classname.find('Linear') != -1:
init.xavier_normal(m.weight.data, gain=1)
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)
示例15: weights_init_kaiming
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform [as 別名]
def weights_init_kaiming(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('BatchNorm2d') != -1:
init.uniform(m.weight.data, 1.0, 0.02)
init.constant(m.bias.data, 0.0)