本文整理匯總了Python中torch.empty_like方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.empty_like方法的具體用法?Python torch.empty_like怎麽用?Python torch.empty_like使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.empty_like方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def __init__(self, pos_inds, neg_inds, bboxes, gt_bboxes, assign_result,
gt_flags):
self.pos_inds = pos_inds
self.neg_inds = neg_inds
self.pos_bboxes = bboxes[pos_inds]
self.neg_bboxes = bboxes[neg_inds]
self.pos_is_gt = gt_flags[pos_inds]
self.num_gts = gt_bboxes.shape[0]
self.pos_assigned_gt_inds = assign_result.gt_inds[pos_inds] - 1
if gt_bboxes.numel() == 0:
# hack for index error case
assert self.pos_assigned_gt_inds.numel() == 0
self.pos_gt_bboxes = torch.empty_like(gt_bboxes).view(-1, 4)
else:
if len(gt_bboxes.shape) < 2:
gt_bboxes = gt_bboxes.view(-1, 4)
self.pos_gt_bboxes = gt_bboxes[self.pos_assigned_gt_inds, :]
if assign_result.labels is not None:
self.pos_gt_labels = assign_result.labels[pos_inds]
else:
self.pos_gt_labels = None
示例2: tforward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def tforward(self, disp0, im, std=None):
self.pattern = self.pattern.to(disp0.device)
self.uv0 = self.uv0.to(disp0.device)
uv0 = self.uv0.expand(disp0.shape[0], *self.uv0.shape[1:])
uv1 = torch.empty_like(uv0)
uv1[...,0] = uv0[...,0] - disp0.contiguous().view(disp0.shape[0],-1)
uv1[...,1] = uv0[...,1]
uv1[..., 0] = 2 * (uv1[..., 0] / (self.im_width-1) - 0.5)
uv1[..., 1] = 2 * (uv1[..., 1] / (self.im_height-1) - 0.5)
uv1 = uv1.view(-1, self.im_height, self.im_width, 2).clone()
pattern = self.pattern.expand(disp0.shape[0], *self.pattern.shape[1:])
pattern_proj = torch.nn.functional.grid_sample(pattern, uv1, padding_mode='border')
mask = torch.ones_like(im)
if std is not None:
mask = mask*std
diff = torchext.photometric_loss(pattern_proj.contiguous(), im.contiguous(), 9, self.loss_type, self.loss_eps)
val = (mask*diff).sum() / mask.sum()
return val, pattern_proj
示例3: backward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def backward(self, grad_output):
norm, std, weight = self.saved_tensors
grad_weight = torch.empty_like(weight)
grad_bias = torch.empty_like(weight)
grad_input = torch.empty_like(grad_output)
grad_output3d = grad_output.view(
grad_output.size(0), grad_output.size(1), -1)
grad_input3d = grad_input.view_as(grad_output3d)
ext_module.sync_bn_backward_param(grad_output3d, norm, grad_weight,
grad_bias)
# all reduce
if self.group_size > 1:
dist.all_reduce(grad_weight, group=self.group)
dist.all_reduce(grad_bias, group=self.group)
grad_weight /= self.group_size
grad_bias /= self.group_size
ext_module.sync_bn_backward_data(grad_output3d, weight, grad_weight,
grad_bias, norm, std, grad_input3d)
return grad_input, None, None, grad_weight, grad_bias, \
None, None, None, None
示例4: swap_swa_param
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def swap_swa_param(self):
r"""Swaps the values of the optimized variables and swa buffers.
It's meant to be called in the end of training to use the collected
swa running averages. It can also be used to evaluate the running
averages during training; to continue training `swap_swa_sgd`
should be called again.
"""
for group in self.param_groups:
for p in group['params']:
param_state = self.state[p]
if 'swa_buffer' not in param_state:
# If swa wasn't applied we don't swap params
warnings.warn(
"SWA wasn't applied to param {}; skipping it".format(p))
continue
buf = param_state['swa_buffer']
tmp = torch.empty_like(p.data)
tmp.copy_(p.data)
p.data.copy_(buf)
buf.copy_(tmp)
示例5: hard_neg_mining_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def hard_neg_mining_loss(scores, labels, neg_ratio=5):
# Flatten tensors along the spatial dimensions
scores = scores.flatten(2, 3)
labels = labels.flatten(2, 3)
count = labels.size(-1)
# Rank negative locations by the predicted confidence
_, inds = (scores.sigmoid() * (~labels).float()).sort(-1, descending=True)
ordinals = torch.arange(count, out=inds.new_empty(count)).expand_as(inds)
rank = torch.empty_like(inds)
rank.scatter_(-1, inds, ordinals)
# Include only positive locations + N most confident negative locations
num_pos = labels.long().sum(dim=-1, keepdim=True)
num_neg = (num_pos + 1) * neg_ratio
mask = (labels | (rank < num_neg)).float()
# Apply cross entropy loss
return F.binary_cross_entropy_with_logits(
scores, labels.float(), mask, reduction='sum')
示例6: score_partial_
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def score_partial_(self, y, next_token, state, x):
"""Score interface for both full and partial scorer.
Args:
y: previous char
next_token: next token need to be score
state: previous state
x: encoded feature
Returns:
tuple[torch.Tensor, List[Any]]: Tuple of
batchfied scores for next token with shape of `(n_batch, n_vocab)`
and next state list for ys.
"""
out_state = kenlm.State()
ys = self.chardict[y[-1]] if y.shape[0] > 1 else "<s>"
self.lm.BaseScore(state, ys, out_state)
scores = torch.empty_like(next_token, dtype=x.dtype, device=y.device)
for i, j in enumerate(next_token):
scores[i] = self.lm.BaseScore(
out_state, self.chardict[j], self.tmpkenlmstate
)
return scores, out_state
示例7: preload
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def preload(self):
try:
self.next_input, self.next_target = next(self.loader)
except StopIteration:
self.next_input = None
self.next_target = None
return
# if record_stream() doesn't work, another option is to make sure device inputs are created
# on the main stream.
# self.next_input_gpu = torch.empty_like(self.next_input, device='cuda')
# self.next_target_gpu = torch.empty_like(self.next_target, device='cuda')
# Need to make sure the memory allocated for next_* is not still in use by the main stream
# at the time we start copying to next_*:
# self.stream.wait_stream(torch.cuda.current_stream())
with torch.cuda.stream(self.stream):
self.next_input = self.next_input.cuda(non_blocking=True)
self.next_target = self.next_target.cuda(non_blocking=True)
self.next_input = self.normalize(self.next_input)
if self.is_cutout:
self.next_input = self.cutout(self.next_input)
示例8: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def forward(self, inputs):
while True:
gumbels = -torch.empty_like(self.arch_parameters).exponential_().log()
logits = (self.arch_parameters.log_softmax(dim=1) + gumbels) / self.tau
probs = nn.functional.softmax(logits, dim=1)
index = probs.max(-1, keepdim=True)[1]
one_h = torch.zeros_like(logits).scatter_(-1, index, 1.0)
hardwts = one_h - probs.detach() + probs
if (torch.isinf(gumbels).any()) or (torch.isinf(probs).any()) or (torch.isnan(probs).any()):
continue
else: break
feature = self.stem(inputs)
for i, cell in enumerate(self.cells):
if isinstance(cell, SearchCell):
feature = cell.forward_gdas(feature, hardwts, index)
else:
feature = cell(feature)
out = self.lastact(feature)
out = self.global_pooling( out )
out = out.view(out.size(0), -1)
logits = self.classifier(out)
return out, logits
示例9: select2withP
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def select2withP(logits, tau, just_prob=False, num=2, eps=1e-7):
if tau <= 0:
new_logits = logits
probs = nn.functional.softmax(new_logits, dim=1)
else :
while True: # a trick to avoid the gumbels bug
gumbels = -torch.empty_like(logits).exponential_().log()
new_logits = (logits.log_softmax(dim=1) + gumbels) / tau
probs = nn.functional.softmax(new_logits, dim=1)
if (not torch.isinf(gumbels).any()) and (not torch.isinf(probs).any()) and (not torch.isnan(probs).any()): break
if just_prob: return probs
#with torch.no_grad(): # add eps for unexpected torch error
# probs = nn.functional.softmax(new_logits, dim=1)
# selected_index = torch.multinomial(probs + eps, 2, False)
with torch.no_grad(): # add eps for unexpected torch error
probs = probs.cpu()
selected_index = torch.multinomial(probs + eps, num, False).to(logits.device)
selected_logit = torch.gather(new_logits, 1, selected_index)
selcted_probs = nn.functional.softmax(selected_logit, dim=1)
return selected_index, selcted_probs
示例10: test_fft
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def test_fft(self, backend):
x = torch.randn(2, 2, 2)
y = torch.empty_like(x)
y[0, 0, :] = x[0, 0, :] + x[0, 1, :] + x[1, 0, :] + x[1, 1, :]
y[0, 1, :] = x[0, 0, :] - x[0, 1, :] + x[1, 0, :] - x[1, 1, :]
y[1, 0, :] = x[0, 0, :] + x[0, 1, :] - x[1, 0, :] - x[1, 1, :]
y[1, 1, :] = x[0, 0, :] - x[0, 1, :] - x[1, 0, :] + x[1, 1, :]
z = backend.fft(x, direction='C2C')
assert torch.allclose(y, z)
z = backend.fft(x, direction='C2C', inverse=True)
z = z * 4.0
assert torch.allclose(y, z)
z = backend.fft(x, direction='C2R', inverse=True)
z = z * 4.0
assert z.shape == x.shape[:-1]
assert torch.allclose(y[..., 0], z)
示例11: swap_swa_sgd
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def swap_swa_sgd(self):
r"""Swaps the values of the optimized variables and swa buffers.
It's meant to be called in the end of training to use the collected
swa running averages. It can also be used to evaluate the running
averages during training; to continue training `swap_swa_sgd`
should be called again.
"""
for group in self.param_groups:
for p in group['params']:
param_state = self.state[p]
if 'swa_buffer' not in param_state:
# If swa wasn't applied we don't swap params
warnings.warn(
"SWA wasn't applied to param {}; skipping it".format(p))
continue
buf = param_state['swa_buffer']
tmp = torch.empty_like(p.data)
tmp.copy_(p.data)
p.data.copy_(buf)
buf.copy_(tmp)
示例12: add_noise
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def add_noise(data: torch.Tensor, noise_type: str,
out: Optional[torch.Tensor] = None, **kwargs) -> torch.Tensor:
"""
Add noise to input
Args:
data: input data
noise_type: supports all inplace functions of a pytorch tensor
out: if provided, result is saved in here
kwargs: keyword arguments passed to generating function
Returns:
torch.Tensor: data with added noise
See Also:
:func:`torch.Tensor.normal_`, :func:`torch.Tensor.exponential_`
"""
if not noise_type.endswith('_'):
noise_type = noise_type + '_'
noise_tensor = torch.empty_like(data, requires_grad=False)
getattr(noise_tensor, noise_type)(**kwargs)
return torch.add(data, noise_tensor, out=out)
示例13: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def forward(self, **data) -> dict:
"""
Args:
data: dict with tensors
Returns:
dict: dict with augmented data
"""
kwargs = {}
for k in self.property_names:
kwargs[k] = getattr(self, k)
kwargs.update(self.kwargs)
for _key in self.keys:
out = torch.empty_like(data[_key])
for _i in range(data[_key].shape[0]):
out[_i] = self.augment_fn(data[_key][_i], out=out[_i], **kwargs)
data[_key] = out
return data
示例14: _forward_alpha
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def _forward_alpha(self, emissions, M):
Tt, B, Ts = emissions.size()
alpha = utils.fill_with_neg_inf(torch.empty_like(emissions)) # Tt, B, Ts
# initialization t=1
initial = torch.empty_like(alpha[0]).fill_(-math.log(Ts)) # log(1/Ts)
# initial = utils.fill_with_neg_inf(torch.empty_like(alpha[0]))
# initial[:, 0] = 0
alpha[0] = emissions[0] + initial
# print('Initialize alpha:', alpha[0])
# induction
for i in range(1, Tt):
alpha[i] = torch.logsumexp(alpha[i-1].unsqueeze(-1) + M[i-1], dim=1)
alpha[i] = alpha[i] + emissions[i]
# print('Emissions@', i, emissions[i])
# print('alpha@',i, alpha[i])
return alpha
示例15: fill_controls_emissions_grid
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import empty_like [as 別名]
def fill_controls_emissions_grid(self, controls, emissions, indices, src_length):
"""
Return controls (C) and emissions (E) covering all the grid
C : Tt, N, Ts, 2
E : Tt, N, Ts
"""
N = controls[0].size(0)
tgt_length = len(controls)
Cread = controls[0].new_zeros((tgt_length, src_length, N, 1))
Cwrite = utils.fill_with_neg_inf(torch.empty_like(Cread))
triu_mask = torch.triu(controls[0].new_ones(tgt_length, src_length), 1).byte()
triu_mask = triu_mask.unsqueeze(-1).unsqueeze(-1).expand(-1, -1, N, 1)
Cwrite.masked_fill_(triu_mask, 0)
C = torch.cat((Cread, Cwrite), dim=-1)
E = utils.fill_with_neg_inf(emissions[0].new(tgt_length, src_length, N))
for t, (subC, subE) in enumerate(zip(controls, emissions)):
select = [indices[t]]
C[t].index_put_(select, subC.transpose(0, 1))
E[t].index_put_(select, subE.transpose(0, 1))
return C.transpose(1, 2), E.transpose(1, 2)