Python torch.t() Examples
The following are 30
code examples of torch.t().
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: GromovWassersteinLearning.py From gwl with GNU General Public License v3.0 | 6 votes |
def tensor_times_mat(self, cost_s, cost_t, trans, mu_s, mu_t): if self.loss_type == 'L2': # f1(a) = a^2, f2(b) = b^2, h1(a) = a, h2(b) = 2b # cost_st = f1(cost_s)*mu_s*1_nt^T + 1_ns*mu_t^T*f2(cost_t)^T # cost = cost_st - h1(cost_s)*trans*h2(cost_t)^T f1_st = torch.matmul(cost_s ** 2, mu_s).repeat(1, trans.size(1)) f2_st = torch.matmul(torch.t(mu_t), torch.t(cost_t ** 2)).repeat(trans.size(0), 1) cost_st = f1_st + f2_st cost = cost_st - 2 * torch.matmul(torch.matmul(cost_s, trans), torch.t(cost_t)) else: # f1(a) = a*log(a) - a, f2(b) = b, h1(a) = a, h2(b) = log(b) # cost_st = f1(cost_s)*mu_s*1_nt^T + 1_ns*mu_t^T*f2(cost_t)^T # cost = cost_st - h1(cost_s)*trans*h2(cost_t)^T f1_st = torch.matmul(cost_s * torch.log(cost_s + 1e-5) - cost_s, mu_s).repeat(1, trans.size(1)) f2_st = torch.matmul(torch.t(mu_t), torch.t(cost_t)).repeat(trans.size(0), 1) cost_st = f1_st + f2_st cost = cost_st - torch.matmul(torch.matmul(cost_s, trans), torch.t(torch.log(cost_t + 1e-5))) return cost
Example #2
Source File: models.py From models with MIT License | 6 votes |
def forward(self,iput): bin_a=None level1_rep=None [batch_size,_,_]=iput.size() for hm,hm_encdr in enumerate(self.rnn_hms): hmod=iput[:,:,hm].contiguous() hmod=torch.t(hmod).unsqueeze(2) op,a= hm_encdr(hmod) if level1_rep is None: level1_rep=op bin_a=a else: level1_rep=torch.cat((level1_rep,op),1) bin_a=torch.cat((bin_a,a),1) level1_rep=level1_rep.permute(1,0,2) final_rep_1,hm_level_attention_1=self.hm_level_rnn_1(level1_rep) final_rep_1=final_rep_1.squeeze(1) prediction_m=((self.fdiff1_1(final_rep_1))) return torch.sigmoid(prediction_m)
Example #3
Source File: runningstats.py From gandissect with MIT License | 6 votes |
def _weighted_summary(self, sort=True): if self.firstfree[0]: self._scan_extremes(self.data[0][:,:self.firstfree[0]].t()) size = sum(self.firstfree) + 2 weights = torch.FloatTensor(size) # Floating point summary = torch.zeros(self.depth, size, dtype=self.dtype, device=self.device) weights[0:2] = 0 summary[:,0:2] = self.extremes index = 2 for level, ff in enumerate(self.firstfree): if ff == 0: continue summary[:,index:index + ff] = self.data[level][:,:ff] weights[index:index + ff] = 2.0 ** level index += ff assert index == summary.shape[1] if sort: summary, order = torch.sort(summary, dim=-1) weights = weights[order.view(-1).cpu()].view(order.shape) return (summary, weights)
Example #4
Source File: sgcn.py From SGCN with GNU General Public License v3.0 | 6 votes |
def calculate_positive_embedding_loss(self, z, positive_edges): """ Calculating the loss on the positive edge embedding distances :param z: Hidden vertex representation. :param positive_edges: Positive training edges. :return loss_term: Loss value on positive edge embedding. """ self.positive_surrogates = [random.choice(self.nodes) for node in range(positive_edges.shape[1])] self.positive_surrogates = torch.from_numpy(np.array(self.positive_surrogates, dtype=np.int64).T) self.positive_surrogates = self.positive_surrogates.type(torch.long).to(self.device) positive_edges = torch.t(positive_edges) self.positive_z_i = z[positive_edges[:, 0], :] self.positive_z_j = z[positive_edges[:, 1], :] self.positive_z_k = z[self.positive_surrogates, :] norm_i_j = torch.norm(self.positive_z_i-self.positive_z_j, 2, 1, True).pow(2) norm_i_k = torch.norm(self.positive_z_i-self.positive_z_k, 2, 1, True).pow(2) term = norm_i_j-norm_i_k term[term < 0] = 0 loss_term = term.mean() return loss_term
Example #5
Source File: grassdata.py From grass_pytorch with Apache License 2.0 | 6 votes |
def __init__(self, dir, transform=None): self.dir = dir box_data = torch.from_numpy(loadmat(self.dir+u'/box_data.mat')[u'boxes']).float() op_data = torch.from_numpy(loadmat(self.dir+u'/op_data.mat')[u'ops']).int() sym_data = torch.from_numpy(loadmat(self.dir+u'/sym_data.mat')[u'syms']).float() #weight_list = torch.from_numpy(loadmat(self.dir+'/weights.mat')['weights']).float() num_examples = op_data.size()[1] box_data = torch.chunk(box_data, num_examples, 1) op_data = torch.chunk(op_data, num_examples, 1) sym_data = torch.chunk(sym_data, num_examples, 1) #weight_list = torch.chunk(weight_list, num_examples, 1) self.transform = transform self.trees = [] for i in xrange(len(op_data)) : boxes = torch.t(box_data[i]) ops = torch.t(op_data[i]) syms = torch.t(sym_data[i]) tree = Tree(boxes, ops, syms) self.trees.append(tree)
Example #6
Source File: sgcn.py From SGCN with GNU General Public License v3.0 | 6 votes |
def calculate_negative_embedding_loss(self, z, negative_edges): """ Calculating the loss on the negative edge embedding distances :param z: Hidden vertex representation. :param negative_edges: Negative training edges. :return loss_term: Loss value on negative edge embedding. """ self.negative_surrogates = [random.choice(self.nodes) for node in range(negative_edges.shape[1])] self.negative_surrogates = torch.from_numpy(np.array(self.negative_surrogates, dtype=np.int64).T) self.negative_surrogates = self.negative_surrogates.type(torch.long).to(self.device) negative_edges = torch.t(negative_edges) self.negative_z_i = z[negative_edges[:, 0], :] self.negative_z_j = z[negative_edges[:, 1], :] self.negative_z_k = z[self.negative_surrogates, :] norm_i_j = torch.norm(self.negative_z_i-self.negative_z_j, 2, 1, True).pow(2) norm_i_k = torch.norm(self.negative_z_i-self.negative_z_k, 2, 1, True).pow(2) term = norm_i_k-norm_i_j term[term < 0] = 0 loss_term = term.mean() return loss_term
Example #7
Source File: grassdata.py From grass_pytorch with Apache License 2.0 | 6 votes |
def __init__(self, dir, transform=None): self.dir = dir box_data = torch.from_numpy(loadmat(self.dir+'/box_data.mat')['boxes']).float() op_data = torch.from_numpy(loadmat(self.dir+'/op_data.mat')['ops']).int() sym_data = torch.from_numpy(loadmat(self.dir+'/sym_data.mat')['syms']).float() #weight_list = torch.from_numpy(loadmat(self.dir+'/weights.mat')['weights']).float() num_examples = op_data.size()[1] box_data = torch.chunk(box_data, num_examples, 1) op_data = torch.chunk(op_data, num_examples, 1) sym_data = torch.chunk(sym_data, num_examples, 1) #weight_list = torch.chunk(weight_list, num_examples, 1) self.transform = transform self.trees = [] for i in range(len(op_data)) : boxes = torch.t(box_data[i]) ops = torch.t(op_data[i]) syms = torch.t(sym_data[i]) tree = Tree(boxes, ops, syms) self.trees.append(tree)
Example #8
Source File: runningstats.py From gandissect with MIT License | 6 votes |
def _add_every(self, incoming): supplied = len(incoming) index = 0 while index < supplied: ff = self.firstfree[0] available = self.data[0].shape[1] - ff if available == 0: if not self._shift(): # If we shifted by subsampling, then subsample. incoming = incoming[index:] if self.samplerate >= 0.5: # First time sampling - the data source is very large. self._scan_extremes(incoming) incoming = sample_portion(incoming, self.samplerate) index = 0 supplied = len(incoming) ff = self.firstfree[0] available = self.data[0].shape[1] - ff copycount = min(available, supplied - index) self.data[0][:,ff:ff + copycount] = torch.t( incoming[index:index + copycount,:]) self.firstfree[0] += copycount index += copycount
Example #9
Source File: ReadoutFunction.py From nmp_qc with MIT License | 6 votes |
def r_duvenaud(self, h): # layers aux = [] for l in range(len(h)): param_sz = self.learn_args[l].size() parameter_mat = torch.t(self.learn_args[l])[None, ...].expand(h[l].size(0), param_sz[1], param_sz[0]) aux.append(torch.transpose(torch.bmm(parameter_mat, torch.transpose(h[l], 1, 2)), 1, 2)) for j in range(0, aux[l].size(1)): # Mask whole 0 vectors aux[l][:, j, :] = nn.Softmax()(aux[l][:, j, :].clone())*(torch.sum(aux[l][:, j, :] != 0, 1) > 0).expand_as(aux[l][:, j, :]).type_as(aux[l]) aux = torch.sum(torch.sum(torch.stack(aux, 3), 3), 1) return self.learn_modules[0](torch.squeeze(aux))
Example #10
Source File: noising.py From fairseq with MIT License | 6 votes |
def __getitem__(self, index): """ Returns a single noisy sample. Multiple samples are fed to the collater create a noising dataset batch. """ src_tokens = self.src_dataset[index] src_lengths = torch.LongTensor([len(src_tokens)]) src_tokens = src_tokens.unsqueeze(0) # Transpose src tokens to fit expected shape of x in noising function # (batch size, sequence length) -> (sequence length, batch size) src_tokens_t = torch.t(src_tokens) with data_utils.numpy_seed(self.seed + index): noisy_src_tokens = self.noiser.noising(src_tokens_t, src_lengths) # Transpose back to expected src_tokens format # (sequence length, 1) -> (1, sequence length) noisy_src_tokens = torch.t(noisy_src_tokens) return noisy_src_tokens[0]
Example #11
Source File: rfa.py From nispat with GNU General Public License v3.0 | 6 votes |
def predict(self, hyp, X, y, Xs): """ Function to make predictions from the model """ X, y, hyp = self._numpy2torch(X, y, hyp) Xs, *_ = self._numpy2torch(Xs) if (hyp != self.hyp).all() or not(hasattr(self, 'A')): self.post(hyp, X, y) # generate prediction tensors XsO = torch.mm(Xs, self.Omega) Phis = torch.exp(hyp[-1])/np.sqrt(self.Nf) * \ torch.cat((torch.cos(XsO), torch.sin(XsO)), 1) # add linear component Phis = torch.cat((Phis, Xs), 1) ys = torch.mm(Phis, self.m) # compute diag(Phis*(Phis'\A)) avoiding computing off-diagonal entries s2 = torch.exp(2*hyp[0]) + \ torch.sum(Phis * torch.t(torch.solve(torch.t(Phis), self.A)[0]), 1) # return output as numpy arrays return ys.detach().numpy().squeeze(), s2.detach().numpy().squeeze()
Example #12
Source File: train_rmc.py From relational-rnn-pytorch with Apache License 2.0 | 6 votes |
def evaluate(data_source): # Turn on evaluation mode which disables dropout. model.eval() total_loss = 0. ntokens = len(corpus.dictionary) memory = model.module.initial_state(eval_batch_size, trainable=False).to(device) with torch.no_grad(): for i in range(0, data_source.size(0) - 1, args.bptt): data, targets = get_batch(data_source, i) data = torch.t(data) loss, memory = model(data, memory, targets) loss = torch.mean(loss) # data has shape [T * B, N] total_loss += args.bptt * loss.item() return total_loss / len(data_source)
Example #13
Source File: token_predictor.py From editsql with MIT License | 6 votes |
def score_snippets(snippets, scorer): """ Scores snippets given a scorer. Inputs: snippets (list of Snippet): The snippets to score. scorer (dy.Expression): Dynet vector against which to score the snippets. Returns: dy.Expression, list of str, where the first is the scores and the second is the names of the snippets that were scored. """ snippet_expressions = [snippet.embedding for snippet in snippets] all_snippet_embeddings = torch.stack(snippet_expressions, dim=1) scores = torch.t(torch.mm(torch.t(scorer), all_snippet_embeddings)) if scores.size()[0] != len(snippets): raise ValueError("Got " + str(scores.size()[0]) + " scores for " + str(len(snippets)) + " snippets") return scores, [snippet.name for snippet in snippets]
Example #14
Source File: utils.py From latent_ode with MIT License | 6 votes |
def linspace_vector(start, end, n_points): # start is either one value or a vector size = np.prod(start.size()) assert(start.size() == end.size()) if size == 1: # start and end are 1d-tensors res = torch.linspace(start, end, n_points) else: # start and end are vectors res = torch.Tensor() for i in range(0, start.size(0)): res = torch.cat((res, torch.linspace(start[i], end[i], n_points)),0) res = torch.t(res.reshape(start.size(0), n_points)) return res
Example #15
Source File: utils.py From latent_ode with MIT License | 6 votes |
def normalize_data(data): reshaped = data.reshape(-1, data.size(-1)) att_min = torch.min(reshaped, 0)[0] att_max = torch.max(reshaped, 0)[0] # we don't want to divide by zero att_max[ att_max == 0.] = 1. if (att_max != 0.).all(): data_norm = (data - att_min) / att_max else: raise Exception("Zero!") if torch.isnan(data_norm).any(): raise Exception("nans!") return data_norm, att_min, att_max
Example #16
Source File: utils.py From latent_ode with MIT License | 6 votes |
def normalize_masked_data(data, mask, att_min, att_max): # we don't want to divide by zero att_max[ att_max == 0.] = 1. if (att_max != 0.).all(): data_norm = (data - att_min) / att_max else: raise Exception("Zero!") if torch.isnan(data_norm).any(): raise Exception("nans!") # set masked out elements back to zero data_norm[mask == 0] = 0 return data_norm, att_min, att_max
Example #17
Source File: Social_Aggregators.py From GraphRec-WWW19 with MIT License | 6 votes |
def forward(self, nodes, to_neighs): embed_matrix = torch.empty(len(nodes), self.embed_dim, dtype=torch.float).to(self.device) for i in range(len(nodes)): tmp_adj = to_neighs[i] num_neighs = len(tmp_adj) # e_u = self.u2e.weight[list(tmp_adj)] # fast: user embedding #slow: item-space user latent factor (item aggregation) #feature_neigbhors = self.features(torch.LongTensor(list(tmp_adj)).to(self.device)) #e_u = torch.t(feature_neigbhors) u_rep = self.u2e.weight[nodes[i]] att_w = self.att(e_u, u_rep, num_neighs) att_history = torch.mm(e_u.t(), att_w).t() embed_matrix[i] = att_history to_feats = embed_matrix return to_feats
Example #18
Source File: memory.py From LSH_Memory with Apache License 2.0 | 6 votes |
def predict(self, x): batch_size, dims = x.size() query = F.normalize(self.query_proj(x), dim=1) # Find the k-nearest neighbors of the query scores = torch.matmul(query, torch.t(self.keys_var)) cosine_similarity, topk_indices_var = torch.topk(scores, self.top_k, dim=1) # softmax of cosine similarities - embedding softmax_score = F.softmax(self.softmax_temperature * cosine_similarity) # retrive memory values - prediction y_hat_indices = topk_indices_var.data[:, 0] y_hat = self.values[y_hat_indices] return y_hat, softmax_score
Example #19
Source File: GromovWassersteinLearning.py From gwl with GNU General Public License v3.0 | 6 votes |
def mutual_cost_mat(self, index1, index2): embs1 = self.emb_model[0](index1) # (batch_size1, dim) embs2 = self.emb_model[1](index2) # (batch_size2, dim) if self.cost_type == 'cosine': # cosine similarity energy1 = torch.sqrt(torch.sum(embs1 ** 2, dim=1, keepdim=True)) # (batch_size1, 1) energy2 = torch.sqrt(torch.sum(embs2 ** 2, dim=1, keepdim=True)) # (batch_size2, 1) cost = 1-torch.exp(-(1-torch.matmul(embs1, torch.t(embs2))/(torch.matmul(energy1, torch.t(energy2))+1e-5))) else: # Euclidean distance embs = torch.matmul(embs1, torch.t(embs2)) # (batch_size1, batch_size2) # (batch_size1, batch_size2) embs_diag1 = torch.diag(torch.matmul(embs1, torch.t(embs1))).view(-1, 1).repeat(1, embs2.size(0)) # (batch_size2, batch_size1) embs_diag2 = torch.diag(torch.matmul(embs2, torch.t(embs2))).view(-1, 1).repeat(1, embs1.size(0)) cost = 1-torch.exp(-(embs_diag1 + torch.t(embs_diag2) - 2 * embs)/embs1.size(1)) return cost
Example #20
Source File: make_datasets.py From nice_pytorch with BSD 3-Clause "New" or "Revised" License | 6 votes |
def zca_matrix(data_tensor): """ Helper function: compute ZCA whitening matrix across a dataset ~ (N, C, H, W). """ # 1. flatten dataset: X = data_tensor.view(data_tensor.shape[0], -1) # 2. zero-center the matrix: X = rescale(X, -1., 1.) # 3. compute covariances: cov = torch.t(X) @ X # 4. compute ZCA(X) == U @ (diag(1/S)) @ torch.t(V) where U, S, V = SVD(cov): U, S, V = torch.svd(cov) return (U @ torch.diag(torch.reciprocal(S)) @ torch.t(V))
Example #21
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def LAFMagic(LAFs1, LAFs2, H1to2, xy_th = 5.0, scale_log = 0.4, t = 1.0, sc = 1.0, aff = 1.0): LHF2_in_1 = reprojectLAFs(LAFs2, torch.inverse(H1to2), True) LHF1 = LAFs_to_H_frames(LAFs1) idxs_in1, idxs_in_2 = get_closest_correspondences_idxs(LHF1, LHF2_in_1, xy_th, scale_log) if len(idxs_in1) == 0: print('Warning, no correspondences found') return None LHF1_good = LHF1[idxs_in1,:,:] LHF2_good = LHF2_in_1[idxs_in_2,:,:] scales1 = get_LHFScale(LHF1_good); scales2 = get_LHFScale(LHF2_good); max_scale = torch.max(scales1,scales2); min_scale = torch.min(scales1, scales2); mean_scale = 0.5 * (max_scale + min_scale) eps = 1e-12; if t != 0: dist_loss = torch.sqrt(torch.sum((LHF1_good[:,0:2,2] - LHF2_good[:,0:2,2])**2, dim = 1) + eps) / V(mean_scale.data); else: dist_loss = 0 if sc != 0 : scale_loss = torch.log1p( (max_scale-min_scale)/(mean_scale)) else: scale_loss = 0 if aff != 0: A1 = LHF1_good[:,:2,:2] / scales1.view(-1,1,1).expand(scales1.size(0),2,2); A2 = LHF2_good[:,:2,:2] / scales2.view(-1,1,1).expand(scales2.size(0),2,2); shape_loss = ((A1 - A2)**2).mean(dim = 1).mean(dim = 1); else: shape_loss = 0; loss = t * dist_loss + sc * scale_loss + aff *shape_loss; #print dist_loss, scale_loss, shape_loss return loss, idxs_in1, idxs_in_2, LHF2_in_1[:,0:2,:]
Example #22
Source File: Losses.py From affnet with MIT License | 5 votes |
def distance_matrix_vector(anchor, positive): """Given batch of anchor descriptors and positive descriptors calculate distance matrix""" d1_sq = torch.sum(anchor * anchor, dim=1).unsqueeze(-1) d2_sq = torch.sum(positive * positive, dim=1).unsqueeze(-1) eps = 1e-6 return torch.sqrt((d1_sq.repeat(1, anchor.size(0)) + torch.t(d2_sq.repeat(1, positive.size(0))) - 2.0 * torch.bmm(anchor.unsqueeze(0), torch.t(positive).unsqueeze(0)).squeeze(0))+eps)
Example #23
Source File: main_lena.py From D-LADMM with MIT License | 5 votes |
def l2_normalize(inputs): [batch_size, dim] = inputs.shape inputs2 = torch.mul(inputs, inputs) norm2 = torch.sum(inputs2, 1) root_inv = torch.rsqrt(norm2) tmp_var1 = root_inv.expand(dim,batch_size) tmp_var2 = torch.t(tmp_var1) nml_inputs = torch.mul(inputs, tmp_var2) return nml_inputs
Example #24
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def distance_matrix_vector(anchor, positive): """Given batch of anchor descriptors and positive descriptors calculate distance matrix""" d1_sq = torch.sum(anchor * anchor, dim=1) d2_sq = torch.sum(positive * positive, dim=1) eps = 1e-12 return torch.sqrt(torch.abs((d1_sq.expand(positive.size(0), anchor.size(0)) + torch.t(d2_sq.expand(anchor.size(0), positive.size(0))) - 2.0 * torch.bmm(positive.unsqueeze(0), torch.t(anchor).unsqueeze(0)).squeeze(0))+eps))
Example #25
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def distance_matrix_vector(anchor, positive): """Given batch of anchor descriptors and positive descriptors calculate distance matrix""" d1_sq = torch.sum(anchor * anchor, dim=1) d2_sq = torch.sum(positive * positive, dim=1) eps = 1e-12 return torch.sqrt(torch.abs((d1_sq.expand(positive.size(0), anchor.size(0)) + torch.t(d2_sq.expand(anchor.size(0), positive.size(0))) - 2.0 * torch.bmm(positive.unsqueeze(0), torch.t(anchor).unsqueeze(0)).squeeze(0))+eps))
Example #26
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def LAFMagic(LAFs1, LAFs2, H1to2, xy_th = 5.0, scale_log = 0.4, t = 1.0, sc = 1.0, aff = 1.0): LHF2_in_1 = reprojectLAFs(LAFs2, torch.inverse(H1to2), True) LHF1 = LAFs_to_H_frames(LAFs1) idxs_in1, idxs_in_2 = get_closest_correspondences_idxs(LHF1, LHF2_in_1, xy_th, scale_log) if len(idxs_in1) == 0: print('Warning, no correspondences found') return None LHF1_good = LHF1[idxs_in1,:,:] LHF2_good = LHF2_in_1[idxs_in_2,:,:] scales1 = get_LHFScale(LHF1_good); scales2 = get_LHFScale(LHF2_good); max_scale = torch.max(scales1,scales2); min_scale = torch.min(scales1, scales2); mean_scale = 0.5 * (max_scale + min_scale) eps = 1e-12; if t != 0: dist_loss = torch.sqrt(torch.sum((LHF1_good[:,0:2,2] - LHF2_good[:,0:2,2])**2, dim = 1) + eps) / V(mean_scale.data); else: dist_loss = 0 if sc != 0 : scale_loss = torch.log1p( (max_scale-min_scale)/(mean_scale)) else: scale_loss = 0 if aff != 0: A1 = LHF1_good[:,:2,:2] / scales1.view(-1,1,1).expand(scales1.size(0),2,2); A2 = LHF2_good[:,:2,:2] / scales2.view(-1,1,1).expand(scales2.size(0),2,2); shape_loss = ((A1 - A2)**2).mean(dim = 1).mean(dim = 1); else: shape_loss = 0; loss = t * dist_loss + sc * scale_loss + aff *shape_loss; #print dist_loss, scale_loss, shape_loss return loss, idxs_in1, idxs_in_2, LHF2_in_1[:,0:2,:]
Example #27
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def ratio_matrix_vector(a, p): eps = 1e-12 return a.expand(p.size(0), a.size(0)) / (torch.t(p.expand(a.size(0), p.size(0))) + eps)
Example #28
Source File: ReprojectionStuff.py From affnet with MIT License | 5 votes |
def LAFMagic(LAFs1, LAFs2, H1to2, xy_th = 5.0, scale_log = 0.4, t = 1.0, sc = 1.0, aff = 1.0): LHF2_in_1 = reprojectLAFs(LAFs2, torch.inverse(H1to2), True) LHF1 = LAFs_to_H_frames(LAFs1) idxs_in1, idxs_in_2 = get_closest_correspondences_idxs(LHF1, LHF2_in_1, xy_th, scale_log) if len(idxs_in1) == 0: print('Warning, no correspondences found') return None LHF1_good = LHF1[idxs_in1,:,:] LHF2_good = LHF2_in_1[idxs_in_2,:,:] scales1 = get_LHFScale(LHF1_good); scales2 = get_LHFScale(LHF2_good); max_scale = torch.max(scales1,scales2); min_scale = torch.min(scales1, scales2); mean_scale = 0.5 * (max_scale + min_scale) eps = 1e-12; if t != 0: dist_loss = torch.sqrt(torch.sum((LHF1_good[:,0:2,2] - LHF2_good[:,0:2,2])**2, dim = 1) + eps) / V(mean_scale.data); else: dist_loss = 0 if sc != 0 : scale_loss = torch.log1p( (max_scale-min_scale)/(mean_scale)) else: scale_loss = 0 if aff != 0: A1 = LHF1_good[:,:2,:2] / scales1.view(-1,1,1).expand(scales1.size(0),2,2); A2 = LHF2_good[:,:2,:2] / scales2.view(-1,1,1).expand(scales2.size(0),2,2); shape_loss = ((A1 - A2)**2).mean(dim = 1).mean(dim = 1); else: shape_loss = 0; loss = t * dist_loss + sc * scale_loss + aff *shape_loss; #print dist_loss, scale_loss, shape_loss return loss, idxs_in1, idxs_in_2, LHF2_in_1[:,0:2,:]
Example #29
Source File: runningstats.py From gandissect with MIT License | 5 votes |
def minmax(self): if self.firstfree[0]: self._scan_extremes(self.data[0][:,:self.firstfree[0]].t()) return self.extremes.clone()
Example #30
Source File: Losses.py From affnet with MIT License | 5 votes |
def distance_matrix_vector(anchor, positive): """Given batch of anchor descriptors and positive descriptors calculate distance matrix""" d1_sq = torch.sum(anchor * anchor, dim=1).unsqueeze(-1) d2_sq = torch.sum(positive * positive, dim=1).unsqueeze(-1) eps = 1e-6 return torch.sqrt((d1_sq.repeat(1, positive.size(0)) + torch.t(d2_sq.repeat(1, anchor.size(0))) - 2.0 * torch.bmm(anchor.unsqueeze(0), torch.t(positive).unsqueeze(0)).squeeze(0))+eps)