本文整理汇总了Python中torch.div方法的典型用法代码示例。如果您正苦于以下问题:Python torch.div方法的具体用法?Python torch.div怎么用?Python torch.div使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.div方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _transform
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def _transform(x, mat, maxmin):
rot = mat[:,0:3]
trans = mat[:,3:6]
x = x.contiguous().view(-1, x.size()[1] , x.size()[2] * x.size()[3])
max_val, min_val = maxmin[:,0], maxmin[:,1]
max_val, min_val = max_val.contiguous().view(-1,1), min_val.contiguous().view(-1,1)
max_val, min_val = max_val.repeat(1,3), min_val.repeat(1,3)
trans, rot = _trans_rot(trans, rot)
x1 = torch.matmul(rot,x)
min_val1 = torch.cat((min_val, Variable(min_val.data.new(min_val.size()[0], 1).fill_(1))), dim=-1)
min_val1 = min_val1.unsqueeze(-1)
min_val1 = torch.matmul(trans, min_val1)
min_val = torch.div( torch.add(torch.matmul(rot, min_val1).squeeze(-1), - min_val), torch.add(max_val, - min_val))
min_val = min_val.mul_(255)
x = torch.add(x1, min_val.unsqueeze(-1))
x = x.contiguous().view(-1,3, 224,224)
return x
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:26,代码来源:transform_cnn.py
示例2: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def forward(self, dec_state, enc_states, mask, dag=None):
"""
:param dec_state:
decoder hidden state of size batch_size x dec_dim
:param enc_states:
all encoder hidden states of size batch_size x max_enc_steps x enc_dim
:param flengths:
encoder video frame lengths of size batch_size
"""
dec_contrib = self.decoder_in(dec_state)
batch_size, max_enc_steps, _ = enc_states.size()
enc_contrib = self.encoder_in(enc_states.contiguous().view(-1, self.enc_dim)).contiguous().view(batch_size, max_enc_steps, self.attn_dim)
pre_attn = F.tanh(enc_contrib + dec_contrib.unsqueeze(1).expand_as(enc_contrib))
energy = self.attn_linear(pre_attn.view(-1, self.attn_dim)).view(batch_size, max_enc_steps)
alpha = F.softmax(energy, 1)
# mask alpha and renormalize it
alpha = alpha* mask
alpha = torch.div(alpha, alpha.sum(1).unsqueeze(1).expand_as(alpha))
context_vector = torch.bmm(alpha.unsqueeze(1), enc_states).squeeze(1) # (batch_size, enc_dim)
return context_vector, alpha
示例3: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def forward(self, theta_aff, theta_aff_tps, matches,return_outliers=False):
batch_size=theta_aff.size()[0]
mask = self.compGeometricTnf(image_batch=expand_dim(self.mask_id,0,batch_size),
theta_aff=theta_aff,
theta_aff_tps=theta_aff_tps)
if return_outliers:
mask_outliers = self.compGeometricTnf(image_batch=expand_dim(1.0-self.mask_id,0,batch_size),
theta_aff=theta_aff,
theta_aff_tps=theta_aff_tps)
if self.normalize:
epsilon=1e-5
mask = torch.div(mask,
torch.sum(torch.sum(torch.sum(mask+epsilon,3),2),1).unsqueeze(1).unsqueeze(2).unsqueeze(3).expand_as(mask))
if return_outliers:
mask_outliers = torch.div(mask,
torch.sum(torch.sum(torch.sum(mask_outliers+epsilon,3),2),1).unsqueeze(1).unsqueeze(2).unsqueeze(3).expand_as(mask_outliers))
score = torch.sum(torch.sum(torch.sum(torch.mul(mask,matches),3),2),1)
if return_outliers:
score_outliers = torch.sum(torch.sum(torch.sum(torch.mul(mask_outliers,matches),3),2),1)
return (score,score_outliers)
return score
示例4: kernel
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def kernel(self, t, p, g, b, c, h, w):
"""The linear kernel (dot production).
Args:
t: output of conv theata
p: output of conv phi
g: output of conv g
b: batch size
c: channels number
h: height of featuremaps
w: width of featuremaps
"""
t = t.view(b, 1, c * h * w)
p = p.view(b, 1, c * h * w)
g = g.view(b, c * h * w, 1)
att = torch.bmm(p, g)
if self.use_scale:
att = att.div((c*h*w)**0.5)
x = torch.bmm(att, t)
x = x.view(b, c, h, w)
return x
示例5: select_next_words
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def select_next_words(
self, word_scores, bsz, beam_size, possible_translation_tokens
):
cand_scores, cand_indices = torch.topk(word_scores.view(bsz, -1), k=beam_size)
possible_tokens_size = self.vocab_size
if possible_translation_tokens is not None:
possible_tokens_size = possible_translation_tokens.size(0)
cand_beams = torch.div(cand_indices, possible_tokens_size)
cand_indices.fmod_(possible_tokens_size)
# Handle vocab reduction
if possible_translation_tokens is not None:
possible_translation_tokens = possible_translation_tokens.view(
1, possible_tokens_size
).expand(cand_indices.size(0), possible_tokens_size)
cand_indices = torch.gather(
possible_translation_tokens, dim=1, index=cand_indices, out=cand_indices
)
return cand_scores, cand_indices, cand_beams
示例6: _load_projection
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def _load_projection(self):
"""
Function to load the weights associated with the pretrained projection
layer. In order to ensure the norm of the weights match up with the
rest of the model, we need to normalize the pretrained weights.
Here we divide by a fixed constant.
"""
input_dim = self.filter_dims
self.projection = nn.Linear(input_dim, self.char_cnn_output_dim, bias=True)
weight = self.npz_weights["W_proj"]
bias = self.npz_weights["b_proj"]
self.projection.weight.data.copy_(
torch.div(torch.FloatTensor(np.transpose(weight)), 10.0)
)
self.projection.bias.data.copy_(
torch.div(torch.FloatTensor(np.transpose(bias)), 10.0)
)
self.projection.weight.requires_grad = self._finetune_pretrained_weights
self.projection.bias.requires_grad = self._finetune_pretrained_weights
示例7: rotmat2quat_torch
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def rotmat2quat_torch(R):
"""
Converts a rotation matrix to quaternion
batch pytorch version ported from the corresponding numpy method above
:param R: N * 3 * 3
:return: N * 4
"""
rotdiff = R - R.transpose(1, 2)
r = torch.zeros_like(rotdiff[:, 0])
r[:, 0] = -rotdiff[:, 1, 2]
r[:, 1] = rotdiff[:, 0, 2]
r[:, 2] = -rotdiff[:, 0, 1]
r_norm = torch.norm(r, dim=1)
sintheta = r_norm / 2
r0 = torch.div(r, r_norm.unsqueeze(1).repeat(1, 3) + 0.00000001)
t1 = R[:, 0, 0]
t2 = R[:, 1, 1]
t3 = R[:, 2, 2]
costheta = (t1 + t2 + t3 - 1) / 2
theta = torch.atan2(sintheta, costheta)
q = Variable(torch.zeros(R.shape[0], 4)).float().cuda()
q[:, 0] = torch.cos(theta / 2)
q[:, 1:] = torch.mul(r0, torch.sin(theta / 2).unsqueeze(1).repeat(1, 3))
return q
示例8: expmap2rotmat_torch
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def expmap2rotmat_torch(r):
"""
Converts expmap matrix to rotation
batch pytorch version ported from the corresponding method above
:param r: N*3
:return: N*3*3
"""
theta = torch.norm(r, 2, 1)
r0 = torch.div(r, theta.unsqueeze(1).repeat(1, 3) + 0.0000001)
r1 = torch.zeros_like(r0).repeat(1, 3)
r1[:, 1] = -r0[:, 2]
r1[:, 2] = r0[:, 1]
r1[:, 5] = -r0[:, 0]
r1 = r1.view(-1, 3, 3)
r1 = r1 - r1.transpose(1, 2)
n = r1.data.shape[0]
R = Variable(torch.eye(3, 3).repeat(n, 1, 1)).float().cuda() + torch.mul(
torch.sin(theta).unsqueeze(1).repeat(1, 9).view(-1, 3, 3), r1) + torch.mul(
(1 - torch.cos(theta).unsqueeze(1).repeat(1, 9).view(-1, 3, 3)), torch.matmul(r1, r1))
return R
示例9: top_k_softmax
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def top_k_softmax(logits, k, n):
top_logits, top_indices = torch.topk(logits, k=min(k + 1, n))
top_k_logits = top_logits[:, :k]
top_k_indices = top_indices[:, :k]
probs = torch.softmax(top_k_logits, dim=-1)
batch = top_k_logits.shape[0]
k = top_k_logits.shape[1]
# Flat to 1D
indices_flat = torch.reshape(top_k_indices, [-1])
indices_flat = indices_flat + torch.div(
torch.arange(batch * k, device=logits.device), k) * n
tensor = torch.zeros([batch * n], dtype=logits.dtype,
device=logits.device)
tensor = tensor.scatter_add(0, indices_flat.long(),
torch.reshape(probs, [-1]))
return torch.reshape(tensor, [batch, n])
示例10: l2norm
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def l2norm(X):
"""L2-normalize columns of X
"""
norm = torch.pow(X, 2).sum(dim=1).sqrt().view(X.size(0), -1)
X = torch.div(X, norm.expand_as(X))
return X
示例11: decode_ord
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def decode_ord(self, y):
batch_size, prob, height, width = y.shape
y = torch.reshape(y, (batch_size, prob//2, 2, height, width))
denominator = torch.sum(torch.exp(y), 2)
pred_score = torch.div(torch.exp(y[:, :, 1, :, :]), denominator)
return pred_score
示例12: weight_norm
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def weight_norm(self):
w = self.fc.weight.data
norm = w.norm(p=2, dim=1, keepdim=True)
self.fc.weight.data = w.div(norm.expand_as(w))
示例13: l2_norm
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def l2_norm(self,input):
input_size = input.size()
buffer = torch.pow(input, 2)
norm = torch.sum(buffer, 1).add_(1e-10)
norm = torch.sqrt(norm)
_output = torch.div(input, norm.view(-1, 1).expand_as(input))
output = _output.view(input_size)
return output
示例14: l2_norm
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def l2_norm(self,input):
input_size = input.size()
buffer = torch.pow(input, 2)
normp = torch.sum(buffer, 1).add_(1e-10)
norm = torch.sqrt(normp)
_output = torch.div(input, norm.view(-1, 1).expand_as(input))
output = _output.view(input_size)
return output
示例15: weight_norm
# 需要导入模块: import torch [as 别名]
# 或者: from torch import div [as 别名]
def weight_norm(self):
w = self.classifier.fc.weight.data
norm = w.norm(p=2, dim=1, keepdim=True)
self.classifier.fc.weight.data = w.div(norm.expand_as(w))