本文整理匯總了Python中torch.nn.init.uniform_方法的典型用法代碼示例。如果您正苦於以下問題:Python init.uniform_方法的具體用法?Python init.uniform_怎麽用?Python init.uniform_使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn.init
的用法示例。
在下文中一共展示了init.uniform_方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def __init__(self, features, orthogonal_initialization=True, using_cache=False):
"""Constructor.
Args:
features: int, number of input features.
orthogonal_initialization: bool, if True initialize weights to be a random
orthogonal matrix.
Raises:
TypeError: if `features` is not a positive integer.
"""
super().__init__(features, using_cache)
if orthogonal_initialization:
self._weight = nn.Parameter(utils.random_orthogonal(features))
else:
self._weight = nn.Parameter(torch.empty(features, features))
stdv = 1.0 / np.sqrt(features)
init.uniform_(self._weight, -stdv, stdv)
示例2: 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)
示例3: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def __init__(self,
features,
context_features,
activation=F.relu,
dropout_probability=0.,
use_batch_norm=False,
zero_initialization=True):
super().__init__()
self.activation = activation
self.use_batch_norm = use_batch_norm
if use_batch_norm:
self.batch_norm_layers = nn.ModuleList([
nn.BatchNorm1d(features, eps=1e-3)
for _ in range(2)
])
if context_features is not None:
self.context_layer = nn.Linear(context_features, features)
self.linear_layers = nn.ModuleList([
nn.Linear(features, features)
for _ in range(2)
])
self.dropout = nn.Dropout(p=dropout_probability)
if zero_initialization:
init.uniform_(self.linear_layers[-1].weight, -1e-3, 1e-3)
init.uniform_(self.linear_layers[-1].bias, -1e-3, 1e-3)
示例4: _initialize
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def _initialize(self, identity_init):
init.zeros_(self.bias)
if identity_init:
init.zeros_(self.lower_entries)
init.zeros_(self.upper_entries)
constant = np.log(np.exp(1 - self.eps) - 1)
init.constant_(self.unconstrained_upper_diag, constant)
else:
stdv = 1.0 / np.sqrt(self.features)
init.uniform_(self.lower_entries, -stdv, stdv)
init.uniform_(self.upper_entries, -stdv, stdv)
init.uniform_(self.unconstrained_upper_diag, -stdv, stdv)
示例5: _initialize
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def _initialize(self):
stdv = 1.0 / np.sqrt(self.features)
init.uniform_(self.upper_entries, -stdv, stdv)
init.uniform_(self.log_upper_diag, -stdv, stdv)
init.constant_(self.bias, 0.0)
示例6: _initialize
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def _initialize(self):
stdv = 1.0 / np.sqrt(self.features)
init.uniform_(self.log_diagonal, -stdv, stdv)
init.constant_(self.bias, 0.0)
示例7: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def reset_parameters(self):
stdv = 1.0 / math.sqrt(self.hidden_size)
for weight in self.parameters():
init.uniform_(weight, -stdv, stdv)
示例8: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [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)
示例9: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [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)
示例10: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [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)
示例11: __init__
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def __init__(self, emb_size, emb_dimension):
super(SkipGramModel, self).__init__()
self.emb_size = emb_size
self.emb_dimension = emb_dimension
self.u_embeddings = nn.Embedding(emb_size, emb_dimension, sparse=True)
self.v_embeddings = nn.Embedding(emb_size, emb_dimension, sparse=True)
initrange = 1.0 / self.emb_dimension
init.uniform_(self.u_embeddings.weight.data, -initrange, initrange)
init.constant_(self.v_embeddings.weight.data, 0)
示例12: init
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def init(self, emb_init):
"""Initializing the embeddings.
Parameters
----------
emb_init : float
The intial embedding range should be [-emb_init, emb_init].
"""
INIT.uniform_(self.emb, -emb_init, emb_init)
INIT.zeros_(self.state_sum)
示例13: reset_parameters
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def reset_parameters(self):
"""
This method initializes or reset all the parameters of the cell.
The paramaters are initiated following a uniform distribution.
"""
std = 1.0 / np.sqrt(self.hidden_size)
for w in self.parameters():
init.uniform_(w, -std, std)
示例14: XavierFill
# 需要導入模塊: from torch.nn import init [as 別名]
# 或者: from torch.nn.init import uniform_ [as 別名]
def XavierFill(tensor):
"""Caffe2 XavierFill Implementation"""
size = reduce(operator.mul, tensor.shape, 1)
fan_in = size / tensor.shape[0]
scale = math.sqrt(3 / fan_in)
return init.uniform_(tensor, -scale, scale)
示例15: 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)