Python torch.ne() Examples
The following are 30
code examples of torch.ne().
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: eval.py From pytorch-priv with MIT License | 6 votes |
def intersectionAndUnion(batch_data, pred, numClass): (imgs, segs, infos) = batch_data _, preds = torch.max(pred.data.cpu(), dim=1) # compute area intersection intersect = preds.clone() intersect[torch.ne(preds, segs)] = -1 area_intersect = torch.histc(intersect.float(), bins=numClass, min=0, max=numClass - 1) # compute area union: preds[torch.lt(segs, 0)] = -1 area_pred = torch.histc(preds.float(), bins=numClass, min=0, max=numClass - 1) area_lab = torch.histc(segs.float(), bins=numClass, min=0, max=numClass - 1) area_union = area_pred + area_lab - area_intersect return area_intersect, area_union
Example #2
Source File: io.py From keyphrase-generation-rl with MIT License | 5 votes |
def _pad(self, input_list): input_list_lens = [len(l) for l in input_list] max_seq_len = max(input_list_lens) padded_batch = self.pad_idx * np.ones((len(input_list), max_seq_len)) for j in range(len(input_list)): current_len = input_list_lens[j] padded_batch[j][:current_len] = input_list[j] padded_batch = torch.LongTensor(padded_batch) input_mask = torch.ne(padded_batch, self.pad_idx) input_mask = input_mask.type(torch.FloatTensor) return padded_batch, input_list_lens, input_mask
Example #3
Source File: cat_lazy_tensor.py From gpytorch with MIT License | 5 votes |
def _get_indices(self, row_index, col_index, *batch_indices): indices = [*batch_indices, row_index, col_index] target_shape = _mul_broadcast_shape(*[index.shape for index in indices]) indices = [index.expand(target_shape).reshape(-1) for index in indices] cat_dim_indices = indices[self.cat_dim] # Find out for which indices we switch to different tensors target_tensors = self.idx_to_tensor_idx[cat_dim_indices] does_switch_tensor = torch.ones(target_tensors.numel() + 1, dtype=bool_compat, device=self.device) torch.ne(target_tensors[:-1], target_tensors[1:], out=does_switch_tensor[1:-1]) # Get the LazyTensors that will comprise the new LazyTensor lazy_tensor_indices = target_tensors[does_switch_tensor[:-1]].tolist() lazy_tensors = [self.lazy_tensors[idx] for idx in lazy_tensor_indices] # Get the new set of indices for each of the LazyTensors switch_tensor = does_switch_tensor.nonzero().squeeze(-1) split_sizes = (switch_tensor[1:] - switch_tensor[:-1]).tolist() sub_indices = zip( *[ list(index.split(split_sizes)) if torch.is_tensor(index) else [index] * len(split_sizes) for index in indices ] ) # Make everything a list sub_indices = [list(sub_index) for sub_index in sub_indices] # Make sure that we have adjusted the start and ends of the indices that correspond to the cat dim for lazy_tensor_idx, sub_index in zip(lazy_tensor_indices, sub_indices): sub_index[self.cat_dim] = sub_index[self.cat_dim] - self.cat_dim_cum_sizes[lazy_tensor_idx] res_list = [ lazy_tensor._get_indices(sub_index[-2], sub_index[-1], *sub_index[:-2]) for lazy_tensor, sub_index in zip(lazy_tensors, sub_indices) ] if len(res_list) == 1: return res_list[0].view(target_shape).to(self.device) else: return torch.cat(res_list).view(target_shape).to(self.device)
Example #4
Source File: io.py From TAKG with MIT License | 5 votes |
def _pad(self, input_list): input_list_lens = [len(l) for l in input_list] max_seq_len = max(input_list_lens) padded_batch = self.pad_idx * np.ones((len(input_list), max_seq_len)) for j in range(len(input_list)): current_len = input_list_lens[j] padded_batch[j][:current_len] = input_list[j] padded_batch = torch.LongTensor(padded_batch) input_mask = torch.ne(padded_batch, self.pad_idx) input_mask = input_mask.type(torch.FloatTensor) return padded_batch, input_list_lens, input_mask
Example #5
Source File: model.py From lightKG with Apache License 2.0 | 5 votes |
def loss(self, x, sent_lengths, pos, rel, y): mask = torch.ne(x, self.pad_index) emissions = self.lstm_forward(x, pos, rel, sent_lengths) return self.crflayer(emissions, y, mask=mask)
Example #6
Source File: model.py From lightKG with Apache License 2.0 | 5 votes |
def forward(self, x, sent_lengths): mask = torch.ne(x, self.pad_index) emissions = self.lstm_forward(x, sent_lengths) return self.crflayer.decode(emissions, mask=mask)
Example #7
Source File: model.py From lightKG with Apache License 2.0 | 5 votes |
def loss(self, x, sent_lengths, y): mask = torch.ne(x, self.pad_index) emissions = self.lstm_forward(x, sent_lengths) return self.crflayer(emissions, y, mask=mask)
Example #8
Source File: model.py From lightKG with Apache License 2.0 | 5 votes |
def forward(self, x, poses, rels, sent_lengths): mask = torch.ne(x, self.pad_index) emissions = self.lstm_forward(x, poses, rels, sent_lengths) return self.crflayer.decode(emissions, mask=mask)
Example #9
Source File: io.py From keyphrase-gan with MIT License | 5 votes |
def _pad(self, input_list): input_list_lens = [len(l) for l in input_list] max_seq_len = max(input_list_lens) padded_batch = self.pad_idx * np.ones((len(input_list), max_seq_len)) for j in range(len(input_list)): current_len = input_list_lens[j] padded_batch[j][:current_len] = input_list[j] padded_batch = torch.LongTensor(padded_batch) input_mask = torch.ne(padded_batch, self.pad_idx) input_mask = input_mask.type(torch.FloatTensor) return padded_batch, input_list_lens, input_mask
Example #10
Source File: mask.py From keyphrase-gan with MIT License | 5 votes |
def forward(self, x): mask = torch.ne(x, self.pad_idx).float() return mask
Example #11
Source File: cifar_trainer.py From ssl_bad_gan with MIT License | 5 votes |
def eval(self, data_loader, max_batch=None): self.gen.eval() self.dis.eval() self.enc.eval() loss, incorrect, cnt = 0, 0, 0 for i, (images, labels) in enumerate(data_loader.get_iter()): images = Variable(images.cuda(), volatile=True) labels = Variable(labels.cuda(), volatile=True) pred_prob = self.dis(images) loss += self.d_criterion(pred_prob, labels).data[0] cnt += 1 incorrect += torch.ne(torch.max(pred_prob, 1)[1], labels).data.sum() if max_batch is not None and i >= max_batch - 1: break return loss / cnt, incorrect
Example #12
Source File: svhn_trainer.py From ssl_bad_gan with MIT License | 5 votes |
def eval(self, data_loader, max_batch=None): self.gen.eval() self.dis.eval() loss, incorrect, cnt = 0, 0, 0 for i, (images, labels) in enumerate(data_loader.get_iter()): images = Variable(images.cuda(), volatile=True) labels = Variable(labels.cuda(), volatile=True) pred_prob = self.dis(images) loss += self.d_criterion(pred_prob, labels).data[0] cnt += 1 incorrect += torch.ne(torch.max(pred_prob, 1)[1], labels).data.sum() if max_batch is not None and i >= max_batch - 1: break return loss / cnt, incorrect
Example #13
Source File: mnist_trainer.py From ssl_bad_gan with MIT License | 5 votes |
def eval(self, data_loader, max_batch=None): self.gen.eval() self.dis.eval() loss, incorrect, cnt = 0, 0, 0 for i, (images, labels) in enumerate(data_loader.get_iter()): images = Variable(images.cuda(), volatile=True) labels = Variable(labels.cuda(), volatile=True) pred_prob = self.dis(images) loss += self.d_criterion(pred_prob, labels).data[0] cnt += 1 incorrect += torch.ne(torch.max(pred_prob, 1)[1], labels).data.sum() if max_batch is not None and i >= max_batch - 1: break return loss / cnt, incorrect
Example #14
Source File: main.py From pytorch-mula with MIT License | 5 votes |
def dist_accuracy(dists, th=0.5): if torch.ne(dists, -1).sum() > 0: return (dists.le(th).eq(dists.ne(-1)).sum()) * 1.0 / dists.ne(-1).sum() else: return -1
Example #15
Source File: attack_mnist.py From Hands-On-Generative-Adversarial-Networks-with-PyTorch-1.x with MIT License | 5 votes |
def adv_test(model, device, test_loader, epsilon): model.eval() correct = 0 adv_examples = [] #* grads of params are needed for data, target in test_loader: data, target = data.to(device), target.to(device) # Set requires_grad attribute of tensor. Important for Attack data.requires_grad = True output = model(data) init_pred = output.max(1, keepdim=True)[1] init_pred = init_pred.view_as(target) loss = criterion(output, target) model.zero_grad() loss.backward() perturbed_data = fgsm_attack(data, epsilon, data.grad.data) output = model(perturbed_data) final_pred = output.max(1, keepdim=True)[1] # final_pred has shape [1000, 1], target has shape [1000]. Must reshape final_pred final_pred = final_pred.view_as(target) correct += final_pred.eq(target).sum().item() if len(adv_examples) < 5 and not (final_pred == target).all(): indices = torch.ne(final_pred.ne(target), init_pred.ne(target)).nonzero() # indices = torch.arange(5) for i in range(indices.shape[0]): adv_ex = perturbed_data[indices[i]].squeeze().detach().cpu().numpy() adv_examples.append((init_pred[indices[i]].item(), final_pred[indices[i]].item(), adv_ex)) if (len(adv_examples) >= 5): break final_acc = 100. * correct / len(test_data) print("Epsilon: {}\tTest Accuracy = {}/{} = {:.4f}".format( epsilon, correct, len(test_data), final_acc)) return final_acc, adv_examples # Train
Example #16
Source File: sparsemax.py From sparsemax-pytorch with MIT License | 5 votes |
def backward(self, grad_output): """Backward function.""" dim = 1 nonzeros = torch.ne(self.output, 0) sum = torch.sum(grad_output * nonzeros, dim=dim) / torch.sum(nonzeros, dim=dim) self.grad_input = nonzeros * (grad_output - sum.expand_as(grad_output)) return self.grad_input
Example #17
Source File: filter.py From spectre with Apache License 2.0 | 5 votes |
def compute(self, left, right) -> torch.Tensor: return torch.ne(left, right)
Example #18
Source File: relational.py From heat with MIT License | 5 votes |
def ne(t1, t2): """ Element-wise rich comparison of non-equality between values from two operands, commutative. Takes the first and second operand (scalar or tensor) whose elements are to be compared as argument. Parameters ---------- t1: tensor or scalar The first operand involved in the comparison t2: tensor or scalar The second operand involved in the comparison Returns ------- result: ht.DNDarray A uint8-tensor holding 1 for all elements in which values of t1 are not equal to values of t2, 0 for all other elements Examples: --------- >>> import heat as ht >>> T1 = ht.float32([[1, 2],[3, 4]]) >>> ht.ne(T1, 3.0) tensor([[1, 1], [0, 1]]) >>> T2 = ht.float32([[2, 2], [2, 2]]) >>> ht.ne(T1, T2) tensor([[1, 0], [1, 1]]) """ return operations.__binary_op(torch.ne, t1, t2)
Example #19
Source File: mgru_rte_model.py From Recognizing-Textual-Entailment with MIT License | 5 votes |
def forward(self, premise, hypothesis, training=False): ''' inputs: premise : batch x T hypothesis : batch x T outputs : pred : batch x num_classes ''' self.train(training) batch_size = premise.size(0) mask_p = torch.ne(premise, 0).type(dtype) mask_h = torch.ne(hypothesis, 0).type(dtype) encoded_p = self.embedding(premise) # batch x T x n_embed encoded_p = F.dropout(encoded_p, p=self.options['DROPOUT'], training=training) encoded_h = self.embedding(hypothesis) # batch x T x n_embed encoded_h = F.dropout(encoded_h, p=self.options['DROPOUT'], training=training) encoded_p = encoded_p.transpose(1, 0) # T x batch x n_embed encoded_h = encoded_h.transpose(1, 0) # T x batch x n_embed mask_p = mask_p.transpose(1, 0) # T x batch mask_h = mask_h.transpose(1, 0) # T x batch h_p_0, h_n_0 = self.init_hidden(batch_size) # 1 x batch x n_dim o_p, h_n = self._gru_forward(self.p_gru, encoded_p, mask_p, h_p_0) # o_p : T x batch x n_dim # h_n : 1 x batch x n_dim o_h, h_n = self._gru_forward(self.h_gru, encoded_h, mask_h, h_n_0) # o_h : T x batch x n_dim # h_n : 1 x batch x n_dim r_0 = self.attn_gru_init_hidden(batch_size) h_star, alpha_vec = self._attn_gru_forward(o_h, mask_h, r_0, o_p, mask_p) h_star = self.out(h_star) # batch x num_classes if self.options['LAST_NON_LINEAR']: h_star = F.leaky_relu(h_star) # Non linear projection pred = F.log_softmax(h_star) return pred
Example #20
Source File: utils.py From simple-HRNet with GNU General Public License v3.0 | 5 votes |
def dist_acc(dists, thr=0.5): """ Return percentage below threshold while ignoring values with a -1 """ dist_cal = torch.ne(dists, -1) num_dist_cal = dist_cal.sum() if num_dist_cal > 0: return torch.lt(dists[dist_cal], thr).float().sum() / num_dist_cal else: return -1
Example #21
Source File: Evaluation.py From pose-adv-aug with Apache License 2.0 | 5 votes |
def dist_acc(dists, thr=0.5): ''' Return percentage below threshold while ignoring values with a -1 ''' if dists.ne(-1).sum() > 0: # denominator = dists.ne(-1).sum() # numerator = 0 # for i in range(0, dists.size(0)): # if dists[i] < thr and dists[i] != -1: # numerator += 1 return dists.le(thr).eq(dists.ne(-1)).sum()*1.0 / dists.ne(-1).sum() # return numerator / denominator else: return -1
Example #22
Source File: HumanAcc.py From pose-adv-aug with Apache License 2.0 | 5 votes |
def correct_predicted_joints(pred, target, res): # pred: b x n x 2 tensor # target: b x n x 2 tensor assert(pred.size()==target.size()) sample_num = pred.size(0) joint_num = pred.size(1) target = target.float() # distances between prediction and groundtruth coordinates dists = torch.zeros((joint_num, sample_num)) normalize = res/10 for i in range(pred.size(1)): for j in range(pred.size(0)): if target[j][i][0] > 0 and target[j][i][1] > 0: dists[i][j] = torch.dist(target[j][i], pred[j][i]) / normalize else: dists[i][j] = -1 # accuracies based on the distances threshold = 0.5 # accuracies = torch.zeros(sample_num) correct_masks = torch.ByteTensor(sample_num, joint_num).zero_() # joint_indexes = torch.arange(0, pred.size(1)) for i in range(0, sample_num): # per_person_dists = dists[idxs, i] per_person_dists = dists[:, i] if torch.ne(per_person_dists, -1).sum() > 0: correct_masks[i] = per_person_dists.le(threshold).eq(per_person_dists.ne(-1)) # all_count = per_person_dists.ne(-1).sum() # print(valid_count) # print(type(valid_count)) # exit() # accuracies[i] = float(valid_count) / float(all_count) # correct_joints.append(joint_indexes[one_correct_msk]) # print(per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum()) # print('joint {0} accuracy is {1}' .format(idxs[i]+1, per_joint_acc)) # else: # accuracies[i] = 0 # correct_joints.append([]) # exit() # return accuracies return correct_masks
Example #23
Source File: HumanAcc.py From pose-adv-aug with Apache License 2.0 | 5 votes |
def approx_PCKh_samples(pred, target, res): # pred: b x n x 2 tensor # target: b x n x 2 tensor assert(pred.size()==target.size()) sample_num = pred.size(0) target = target.float() # distances between prediction and groundtruth coordinates dists = torch.zeros((pred.size(1), pred.size(0))) normalize = res/10 for i in range(pred.size(1)): for j in range(pred.size(0)): if target[j][i][0] > 0 and target[j][i][1] > 0: dists[i][j] = torch.dist(target[j][i], pred[j][i]) / normalize else: dists[i][j] = -1 # accuracies based on the distances threshold = 0.5 # accuracies = torch.zeros(sample_num) counts = torch.zeros(sample_num) bad_idx_count = 0 for i in range(0, sample_num): # per_person_dists = dists[idxs, i] per_person_dists = dists[:, i] if torch.ne(per_person_dists, -1).sum() > 0: valid_count = per_person_dists.le(threshold).eq(per_person_dists.ne(-1)).sum() # all_count = per_person_dists.ne(-1).sum() # print(valid_count) # print(type(valid_count)) # exit() # accuracies[i] = float(valid_count) / float(all_count) counts[i] = valid_count # print(per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum()) # print('joint {0} accuracy is {1}' .format(idxs[i]+1, per_joint_acc)) else: # accuracies[i] = 0 counts[i] = 0 # exit() # return accuracies return counts
Example #24
Source File: HumanAcc.py From pose-adv-aug with Apache License 2.0 | 5 votes |
def approx_PCKh_per(pred, target, idxs, res): # pred: b x n x 2 tensor # target: b x n x 2 tensor assert(pred.size()==target.size()) target = target.float() # distances between prediction and groundtruth coordinates dists = torch.zeros((pred.size(1), pred.size(0))) normalize = res/10 for i in range(pred.size(1)): for j in range(pred.size(0)): if target[j][i][0] > 0 and target[j][i][1] > 0: dists[i][j] = torch.dist(target[j][i], pred[j][i]) / normalize else: dists[i][j] = -1 # accuracies based on the distances threshold = 0.5 avg_acc = 0 pckhs = torch.zeros(len(idxs)) bad_idx_count = 0 for i in range(len(idxs)): per_joint_dists = dists[idxs[i]] if torch.ne(per_joint_dists, -1).sum() > 0: valid_count = per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum() all_count = per_joint_dists.ne(-1).sum() # print(valid_count) # print(type(valid_count)) # exit() pckhs[i] = float(valid_count) / float(all_count) # print(per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum()) # print('joint {0} accuracy is {1}' .format(idxs[i]+1, per_joint_acc)) else: pckhs[i] = -1 if pckhs[i] >= 0: avg_acc += pckhs[i] else: bad_idx_count += 1 avg_acc = avg_acc / (len(idxs)-bad_idx_count) # exit() return avg_acc, pckhs
Example #25
Source File: HumanAcc.py From pose-adv-aug with Apache License 2.0 | 5 votes |
def approx_PCKh(pred, target, idxs, res): # pred: b x n x 2 tensor # target: b x n x 2 tensor assert(pred.size()==target.size()) target = target.float() # distances between prediction and groundtruth coordinates dists = torch.zeros((pred.size(1), pred.size(0))) normalize = res/10 for i in range(pred.size(1)): for j in range(pred.size(0)): if target[j][i][0] > 0 and target[j][i][1] > 0: dists[i][j] = torch.dist(target[j][i], pred[j][i]) / normalize else: dists[i][j] = -1 # accuracies based on the distances threshold = 0.5 avg_acc = 0 bad_idx_count = 0 for i in range(len(idxs)): per_joint_dists = dists[idxs[i]] if torch.ne(per_joint_dists, -1).sum() > 0: valid_count = per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum() all_count = per_joint_dists.ne(-1).sum() # print(valid_count) # print(type(valid_count)) # exit() per_joint_acc = float(valid_count) / float(all_count) # print(per_joint_dists.le(threshold).eq(per_joint_dists.ne(-1)).sum()) # print('joint {0} accuracy is {1}' .format(idxs[i]+1, per_joint_acc)) else: per_joint_acc = -1 if per_joint_acc >= 0: avg_acc += per_joint_acc else: bad_idx_count += 1 avg_acc = avg_acc / (len(idxs)-bad_idx_count) # exit() return avg_acc
Example #26
Source File: main.py From pytorch-pil with MIT License | 5 votes |
def dist_accuracy(dists, th=0.5): if torch.ne(dists, -1).sum() > 0: return (dists.le(th).eq(dists.ne(-1)).sum()) * 1.0 / dists.ne(-1).sum() else: return -1
Example #27
Source File: functions.py From Match-LSTM with MIT License | 5 votes |
def compute_mask(v, padding_idx=0): """ compute mask on given tensor v :param v: :param padding_idx: :return: """ mask = torch.ne(v, padding_idx).float() return mask
Example #28
Source File: mask.py From TAKG with MIT License | 5 votes |
def forward(self, x): mask = torch.ne(x, self.pad_idx).float() return mask
Example #29
Source File: hinge.py From dfw with MIT License | 5 votes |
def _augmented_scores(self, s, y): if self._range is None: delattr(self, '_range') self.register_buffer('_range', torch.arange(s.size(1), device=s.device)[None, :]) delta = torch.ne(y[:, None], self._range).detach().float() return s + delta - s.gather(1, y[:, None])
Example #30
Source File: eric_temp_layers.py From MatchLSTM-PyTorch with MIT License | 5 votes |
def compute_mask(self, x): mask = torch.ne(x, 0).float() return mask