本文整理汇总了Python中torch.typename方法的典型用法代码示例。如果您正苦于以下问题:Python torch.typename方法的具体用法?Python torch.typename怎么用?Python torch.typename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.typename方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __init__(self, params, defaults):
self.defaults = defaults
if isinstance(params, Variable) or torch.is_tensor(params):
raise TypeError("params argument given to the optimizer should be "
"an iterable of Variables or dicts, but got " +
torch.typename(params))
self.state = defaultdict(dict)
self.param_groups = []
param_groups = list(params)
if len(param_groups) == 0:
raise ValueError("optimizer got an empty parameter list")
if not isinstance(param_groups[0], dict):
param_groups = [{'params': param_groups}]
for param_group in param_groups:
self.add_param_group(param_group)
示例2: _short_str
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def _short_str(tensor):
# unwrap variable to tensor
if not torch.is_tensor(tensor):
# (1) unpack variable
if hasattr(tensor, 'data'):
tensor = getattr(tensor, 'data')
# (2) handle include_lengths
elif isinstance(tensor, tuple):
return str(tuple(_short_str(t) for t in tensor))
# (3) fallback to default str
else:
return str(tensor)
# copied from torch _tensor_str
size_str = 'x'.join(str(size) for size in tensor.size())
device_str = '' if not tensor.is_cuda else \
' (GPU {})'.format(tensor.get_device())
strt = '[{} of size {}{}]'.format(torch.typename(tensor),
size_str, device_str)
return strt
示例3: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __init__(self, named_params, defaults):
self.defaults = defaults
if isinstance(named_params, torch.Tensor):
raise TypeError("params argument given to the optimizer should be "
"an iterable of Tensors or dicts, but got " +
torch.typename(named_params))
self.state = defaultdict(dict)
self.param_groups = []
param_groups = list(named_params)
if len(param_groups) == 0:
raise ValueError("optimizer got an empty parameter list")
if not isinstance(param_groups[0], dict):
param_groups = [{'params': param_groups}]
for param_group in param_groups:
self.add_param_group(param_group)
示例4: print_size
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def print_size(self, input, output):
print(torch.typename(self).split('.')[-1], ' output size:',output.data.size())
示例5: __setattr__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __setattr__(self, name, value):
"""Redefine __setattr__ so that any submodules created
inside the Module object are registered with _modules
OrderedDict.
"""
def remove_from(*dicts):
for d in dicts:
if name in d:
del d[name]
modules = self.__dict__.get("_modules")
if isinstance(value, Module):
if modules is None:
raise AttributeError(
"cannot assign module before Module.__init__() call"
)
remove_from(self.__dict__, self._parameters, self._buffers)
modules[name] = value
elif modules is not None and name in modules:
if value is not None:
raise TypeError(
"cannot assign '{}' as child module '{}' "
"(torch.nn.Module or None expected)".format(
torch.typename(value), name
)
)
modules[name] = value
else:
object.__setattr__(self, name, value)
示例6: print_tensor_dict
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def print_tensor_dict(params):
kmax = max(len(key) for key in params.keys())
for i, (key, v) in enumerate(params.items()):
print(str(i).ljust(5), key.ljust(kmax + 3), str(tuple(v.shape)).ljust(23), torch.typename(v), v.requires_grad)
示例7: vector_to_parameters
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def vector_to_parameters(vec, parameters, grad=True):
"""Convert one vector to the parameters or gradients of the parameters
Arguments:
vec (torch.Tensor): a single vector represents the parameters of a model.
parameters (Iterable[Variable]): an iterator of Variables that are the
parameters of a model.
grad (bool): True for assigning de-vectorized `vec` to gradients
"""
# Ensure vec of type Variable
if not isinstance(vec, torch.cuda.FloatTensor):
raise TypeError('expected torch.Tensor, but got: {}'
.format(torch.typename(vec)))
# Flag for the device where the parameter is located
param_device = None
# Pointer for slicing the vector for each parameter
pointer = 0
if grad:
for param in parameters:
# Ensure the parameters are located in the same device
param_device = _check_param_device(param, param_device)
# The length of the parameter
num_param = torch.prod(torch.LongTensor(list(param.size())))
param.grad.data = vec[pointer:pointer + num_param].view(
param.size())
# Increment the pointer
pointer += num_param
else:
for param in parameters:
# Ensure the parameters are located in the same device
param_device = _check_param_device(param, param_device)
# The length of the parameter
num_param = torch.prod(torch.LongTensor(list(param.size())))
param.data = vec[pointer:pointer + num_param].view(
param.size())
# Increment the pointer
pointer += num_param
示例8: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __init__(self, model, n_samples=20):
super(BayesNN, self).__init__()
if not isinstance(model, nn.Module):
raise TypeError("model {} is not a Module subclass".format(
torch.typename(model)))
self.n_samples = n_samples
# w_i ~ StudentT(w_i | mu=0, lambda=shape/rate, nu=2*shape)
# for efficiency, represent StudentT params using Gamma params
self.w_prior_shape = 1.
self.w_prior_rate = 0.05
# noise variance 1e-6: beta ~ Gamma(beta | shape, rate)
self.beta_prior_shape = 2.
self.beta_prior_rate = 1.e-6
# replicate `n_samples` instances with the same network as `model`
instances = []
for i in range(n_samples):
new_instance = copy.deepcopy(model)
# initialize each model instance with their defualt initialization
# instead of the prior
new_instance.reset_parameters()
print('Reset parameters in model instance {}'.format(i))
instances.append(new_instance)
self.nnets = nn.ModuleList(instances)
del instances
# log precision (Gamma) of Gaussian noise
log_beta = Gamma(self.beta_prior_shape,
self.beta_prior_rate).sample((self.n_samples,)).log()
for i in range(n_samples):
self.nnets[i].log_beta = Parameter(log_beta[i])
print('Total number of parameters: {}'.format(self._num_parameters()))
示例9: __str__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __str__(self):
if not self.__dict__:
return 'Empty {} instance'.format(torch.typename(self))
fields_to_index = filter(lambda field: field is not None, self.fields)
var_strs = '\n'.join(['\t[.' + name + ']' + ":" + _short_str(getattr(self, name))
for name in fields_to_index if hasattr(self, name)])
data_str = (' from {}'.format(self.dataset.name.upper())
if hasattr(self.dataset, 'name')
and isinstance(self.dataset.name, str) else '')
strt = '[{} of size {}{}]\n{}'.format(torch.typename(self),
self.batch_size, data_str, var_strs)
return '\n' + strt
示例10: permute
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def permute(x, perm):
"""Permutes the last three dimensions of the input Tensor or Array.
Args:
x (Tensor or Array): Input to be permuted.
perm (tuple or list): Permutation.
Note:
If the input has less than three dimensions a copy is returned.
"""
if is_tensor(x):
if x.dim() < 3:
return x.data.clone()
else:
s = tuple(range(0, x.dim()))
permutation = s[:-3] + tuple(s[-3:][i] for i in perm)
return x.permute(*permutation).contiguous()
elif is_array(x):
if x.ndim < 3:
return x.copy()
else:
s = tuple(range(0,x.ndim))
permutation = s[:-3] + tuple(s[-3:][i] for i in perm)
# Copying to get rid of negative strides
return np.transpose(x, permutation).copy()
else:
raise TypeError('Uknown type {0} encountered.'.format(torch.typename(x)))
示例11: channel_flip
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def channel_flip(x, dim=-3):
"""Reverses the channel dimension.
Args:
x (Tensor or Array): Input to have its channels flipped.
dim (int, optional): Channels dimension (default -3).
Note:
If the input has less than three dimensions a copy is returned.
"""
if is_tensor(x):
dim = x.dim() + dim if dim < 0 else dim
if x.dim() < 3:
return x.data.clone()
return x[tuple(slice(None, None) if i != dim
else torch.arange(x.size(i)-1, -1, -1).long()
for i in range(x.dim()))]
elif is_array(x):
dim = x.ndim + dim if dim < 0 else dim
if x.ndim < 3:
return x.copy()
return np.ascontiguousarray(np.flip(x,dim))
else:
raise TypeError('Uknown type {0} encountered.'.format(torch.typename(x)))
# Default is dimension -3 (e.g. for bchw)
示例12: _get_tensor
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def _get_tensor(x):
x = x[0] if torch.typename(x) in ['tuple', 'list'] else x
return x
示例13: _register
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def _register(net, hook, modules=None, match_names=None, do_forward=True):
modules = process_none(modules)
match_names = process_none(match_names)
for mod_name, mod in net.named_modules():
name_match = any([torch.typename(modules).find(x) >= 0 for x in match_names])
instance_match = any([isinstance(mod, x) for x in modules])
if instance_match or name_match:
if do_forward:
mod.register_forward_hook(hook(mod_name))
else:
mod.register_backward_hook(hook(mod_name))
return net
示例14: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def __init__(self, params, lr=required, gravity=required, truncate_freq=1, weight_decay=0):
defaults = dict(lr=lr, gravity=gravity, truncate_freq=truncate_freq, weight_decay=weight_decay)
super(TruncateSGD, self).__init__(params, defaults)
if not isinstance(truncate_freq, int) or truncate_freq <= 0:
raise ValueError('truncate_freq should be integer and greater than 0',
'while type(truncate_freq) =', torch.typename(truncate_freq),
'truncate_freq =', truncate_freq)
示例15: updatePadding
# 需要导入模块: import torch [as 别名]
# 或者: from torch import typename [as 别名]
def updatePadding(net, nn_padding):
typename = torch.typename(net)
# print(typename)
if typename.find('Sequential') >= 0 or typename.find('Bottleneck') >= 0:
modules_keys = list(net._modules.keys())
for i in reversed(range(len(modules_keys))):
subnet = net._modules[modules_keys[i]]
out = updatePadding(subnet, nn_padding)
if out != -1:
p = out
in_c, out_c, k, s, _, d, g, b = \
subnet.in_channels, subnet.out_channels, \
subnet.kernel_size[0], subnet.stride[0], \
subnet.padding[0], subnet.dilation[0], \
subnet.groups, subnet.bias,
conv_temple = nn.Conv2d(in_c, out_c, k, stride=s, padding=0,
dilation=d, groups=g, bias=b)
conv_temple.weight = subnet.weight
conv_temple.bias = subnet.bias
if p > 1:
net._modules[modules_keys[i]] = nn.Sequential(SymmetricPad2d(p), conv_temple)
else:
net._modules[modules_keys[i]] = nn.Sequential(nn_padding(p), conv_temple)
else:
if typename.find('torch.nn.modules.conv.Conv2d') >= 0:
k_sz, p_sz = net.kernel_size[0], net.padding[0]
if ((k_sz == 3) or (k_sz == 7)) and p_sz != 0:
return p_sz
return -1