Python torch.div() Examples
The following are 30
code examples of torch.div().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example #1
Source File: data_utils.py From LearnTrajDep with MIT License | 6 votes |
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
Example #2
Source File: resnet.py From cgnl-network.pytorch with MIT License | 6 votes |
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
Example #3
Source File: data_utils.py From LearnTrajDep with MIT License | 6 votes |
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
Example #4
Source File: recurrent.py From Tagger with BSD 3-Clause "New" or "Revised" License | 6 votes |
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])
Example #5
Source File: competing_completed.py From translate with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #6
Source File: loss.py From weakalign with MIT License | 6 votes |
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
Example #7
Source File: seq2seq_atten.py From video_captioning_rl with MIT License | 6 votes |
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
Example #8
Source File: transform_cnn.py From View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition with MIT License | 6 votes |
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
Example #9
Source File: char_encoder.py From translate with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #10
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_W_poisson_L2(H,W,lambda_,phi,V,eps_): # beta = 1 zeta(beta) = 1/2 V_ap = torch.matmul(W,H) + eps_ V_res = torch.div(V, V_ap) denom = torch.sum(H,1) + torch.div(phi*W,lambda_) + eps_ update = torch.pow(torch.div(torch.matmul(V_res,H.transpose(0,1)),denom),0.5) return W * update
Example #11
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_del(lambda_, lambda_last): del_ = torch.max(torch.div(torch.abs(lambda_ - lambda_last)), lambda_last) return del_
Example #12
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_H_gaussian_L2(H,W,lambda_,phi,V,eps_): #beta = 2 zeta(beta) = 1 denom = torch.matmul(W.transpose(0,1).type(V.dtype),torch.matmul(W, H).type(V.dtype) + eps_) + torch.div(phi * H, lambda_.reshape(-1,1)).type(V.dtype) + eps_ update = torch.div(torch.matmul(W.transpose(0,1).type(V.dtype),V),denom) return H * update.type(torch.float32)
Example #13
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_H_gaussian_L1(H,W,lambda_,phi,V,eps_): #beta = 2 gamma(beta) = 1 V_ap = torch.matmul(W, H) + eps_ denom = torch.matmul(W.transpose(0,1),V_ap) + torch.div(phi, lambda_ ).reshape(-1,1) + eps_ update = torch.div(torch.matmul(W.transpose(0,1),V),denom) return H * update
Example #14
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_W_gaussian_L1(H,W,lambda_,phi,V,eps_): #beta = 2 gamma(beta) = 1 V_ap = torch.matmul(W,H).type(V.dtype) + eps_ denom = torch.matmul(V_ap,H.transpose(0,1).type(V.dtype)) + torch.div(phi,lambda_).type(V.dtype) + eps_ update = torch.div(torch.matmul(V,H.transpose(0,1).type(V.dtype)),denom) return W * update.type(torch.float32)
Example #15
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_W_gaussian_L2(H,W,lambda_,phi,V,eps_): #beta = 2 zeta(beta) = 1 V_ap = torch.matmul(W,H) + eps_ denom = torch.matmul(V_ap,H.transpose(0,1)) + torch.div(phi*W,lambda_) + eps_ update = torch.div(torch.matmul(V,H.transpose(0,1)),denom) return W * update # update tolerance value for early stop criteria
Example #16
Source File: NMF_functions.py From SignatureAnalyzer-GPU with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_lambda_L1_L2(W,H,b0,C,eps_): return torch.div(torch.sum(W,0) + 0.5*torch.sum(H*H,1)+b0,C)
Example #17
Source File: model.py From VSE-C with MIT License | 5 votes |
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
Example #18
Source File: utils.py From context_encoder_pytorch with MIT License | 5 votes |
def normalize_batch(batch): # normalize using imagenet mean and std mean = batch.data.new(batch.data.size()) std = batch.data.new(batch.data.size()) mean[:, 0, :, :] = 0.485 mean[:, 1, :, :] = 0.456 mean[:, 2, :, :] = 0.406 std[:, 0, :, :] = 0.229 std[:, 1, :, :] = 0.224 std[:, 2, :, :] = 0.225 batch = torch.div(batch, 255.0) batch -= Variable(mean) # batch /= Variable(std) batch = torch.div(batch,Variable(std)) return batch
Example #19
Source File: model.py From SCAN with Apache License 2.0 | 5 votes |
def l2norm(X, dim, eps=1e-8): """L2-normalize columns of X """ norm = torch.pow(X, 2).sum(dim=dim, keepdim=True).sqrt() + eps X = torch.div(X, norm) return X
Example #20
Source File: model.py From SCAN with Apache License 2.0 | 5 votes |
def l1norm(X, dim, eps=1e-8): """L1-normalize columns of X """ norm = torch.abs(X).sum(dim=dim, keepdim=True) + eps X = torch.div(X, norm) return X
Example #21
Source File: model.py From dual_encoding with Apache License 2.0 | 5 votes |
def l2norm(X): """L2-normalize columns of X """ norm = torch.pow(X, 2).sum(dim=1, keepdim=True).sqrt() X = torch.div(X, norm) return X
Example #22
Source File: networks.py From viton-gan with MIT License | 5 votes |
def forward(self, feature): epsilon = 1e-6 norm = torch.pow(torch.sum(torch.pow(feature,2),1)+epsilon,0.5).unsqueeze(1).expand_as(feature) return torch.div(feature,norm)
Example #23
Source File: test_precision.py From PySyft with Apache License 2.0 | 5 votes |
def test_torch_div(workers): bob, alice, james = (workers["bob"], workers["alice"], workers["james"]) # With scalar x = torch.tensor([[9.0, 25.42], [3.3, 0.0]]).fix_prec() y = torch.tensor([[3.0, 6.2], [3.3, 4.7]]).fix_prec() z = torch.div(x, y).float_prec() assert (z == torch.tensor([[3.0, 4.1], [1.0, 0.0]])).all() # With negative numbers x = torch.tensor([[-9.0, 25.42], [-3.3, 0.0]]).fix_prec() y = torch.tensor([[3.0, -6.2], [-3.3, 4.7]]).fix_prec() z = torch.div(x, y).float_prec() assert (z == torch.tensor([[-3.0, -4.1], [1.0, 0.0]])).all() # AST divided by FPT x = torch.tensor([[9.0, 25.42], [3.3, 0.0]]).fix_prec().share(bob, alice, crypto_provider=james) y = torch.tensor([[3.0, 6.2], [3.3, 4.7]]).fix_prec() z = torch.div(x, y).get().float_prec() assert (z == torch.tensor([[3.0, 4.1], [1.0, 0.0]])).all() # With dtype int x = torch.tensor([[-9.0, 25.42], [-3.3, 0.0]]).fix_prec(dtype="int") y = torch.tensor([[3.0, -6.2], [-3.3, 4.7]]).fix_prec(dtype="int") z = torch.div(x, y) assert (z.float_prec() == torch.tensor([[-3.0, -4.1], [1.0, 0.0]])).all()
Example #24
Source File: final_classifier.py From CADA-VAE-PyTorch with MIT License | 5 votes |
def compute_per_class_acc(self, test_label, predicted_label, nclass): per_class_accuracies = torch.zeros(nclass).float().to(self.device).detach() target_classes = torch.arange(0, nclass, out=torch.LongTensor()).to(self.device) #changed from 200 to nclass on 24.06. predicted_label = predicted_label.to(self.device) test_label = test_label.to(self.device) for i in range(nclass): is_class = test_label==target_classes[i] per_class_accuracies[i] = torch.div((predicted_label[is_class]==test_label[is_class]).sum().float(),is_class.sum().float()) return per_class_accuracies.mean()
Example #25
Source File: final_classifier.py From CADA-VAE-PyTorch with MIT License | 5 votes |
def compute_per_class_acc_gzsl(self, test_label, predicted_label, target_classes): per_class_accuracies = Variable(torch.zeros(target_classes.size()[0]).float().to(self.device)).detach() predicted_label = predicted_label.to(self.device) for i in range(target_classes.size()[0]): is_class = test_label==target_classes[i] per_class_accuracies[i] = torch.div((predicted_label[is_class]==test_label[is_class]).sum().float(),is_class.sum().float()) return per_class_accuracies.mean()
Example #26
Source File: utils.py From visDial.pytorch with MIT License | 5 votes |
def l2_norm(input): """ input: feature that need to normalize. output: normalziaed feature. """ 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
Example #27
Source File: SpectralNormLayer.py From cortex with BSD 3-Clause "New" or "Revised" License | 5 votes |
def sn_weight(weight, u, height, n_power_iterations): weight.requires_grad_(False) for _ in range(n_power_iterations): v = l2normalize(torch.mv(weight.view(height, -1).t(), u)) u = l2normalize(torch.mv(weight.view(height, -1), v)) weight.requires_grad_(True) sigma = u.dot(weight.view(height, -1).mv(v)) return torch.div(weight, sigma), u
Example #28
Source File: utils.py From deep-head-pose with Apache License 2.0 | 5 votes |
def softmax_temperature(tensor, temperature): result = torch.exp(tensor / temperature) result = torch.div(result, torch.sum(result, 1).unsqueeze(1).expand_as(result)) return result
Example #29
Source File: resnet.py From cgnl-network.pytorch with MIT License | 5 votes |
def forward(self, x): residual = x t = self.t(x) p = self.p(x) g = self.g(x) b, c, h, w = t.size() t = t.view(b, c, -1).permute(0, 2, 1) p = p.view(b, c, -1) g = g.view(b, c, -1).permute(0, 2, 1) att = torch.bmm(t, p) if self.use_scale: att = att.div(c**0.5) att = self.softmax(att) x = torch.bmm(att, g) x = x.permute(0, 2, 1) x = x.contiguous() x = x.view(b, c, h, w) x = self.z(x) x = self.bn(x) + residual return x
Example #30
Source File: resnet.py From cgnl-network.pytorch with MIT License | 5 votes |
def forward(self, x): residual = x t = self.t(x) p = self.p(x) g = self.g(x) b, c, h, w = t.size() t = t.view(b, c, -1).permute(0, 2, 1) p = p.view(b, c, -1) g = g.view(b, c, -1).permute(0, 2, 1) att = torch.bmm(t, p) if self.use_scale: att = att.div(c**0.5) att = self.softmax(att) x = torch.bmm(att, g) x = x.permute(0, 2, 1) x = x.contiguous() x = x.view(b, c, h, w) x = self.z(x) x = self.bn(x) + residual return x