Python torch.ger() Examples
The following are 30
code examples of torch.ger().
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: pscn.py From DeepInf with MIT License | 6 votes |
def forward(self, x, vertices, recep): emb = self.embedding(vertices) if self.inst_norm: emb = self.norm(emb.transpose(1, 2)).transpose(1, 2) x = torch.cat((x, emb), dim=2) if self.use_vertex_feature: vfeature = self.vertex_feature(vertices) x = torch.cat((x, vfeature), dim=2) bs, l = recep.size() n = x.size()[1] # x is of shape bs x n x num_feature offset = torch.ger(torch.arange(0, bs).long(), torch.ones(l).long() * n) offset = Variable(offset, requires_grad=False) offset = offset.cuda() recep = (recep.long() + offset).view(-1) x = x.view(bs * n, -1) x = x.index_select(dim=0, index=recep) x = x.view(bs, l, -1) # x is of shape bs x l x num_feature, l=w*k x = x.transpose(1, 2) # x is of shape bs x num_feature x l, l=w*k x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) x = F.dropout(x, self.dropout, training=self.training) x = x.view(bs, -1) x = self.fc(x) return F.log_softmax(x, dim=-1)
Example #2
Source File: anchors.py From zsgnet-pytorch with MIT License | 6 votes |
def create_grid(size, flatten=True): "Create a grid of a given `size`." if isinstance(size, tuple): H, W = size else: H, W = size, size grid = torch.FloatTensor(H, W, 2) linear_points = torch.linspace(-1+1/W, 1-1/W, W) if W > 1 else torch.tensor([0.]) grid[:, :, 1] = torch.ger(torch.ones( H), linear_points).expand_as(grid[:, :, 0]) linear_points = torch.linspace(-1+1/H, 1-1/H, H) if H > 1 else torch.tensor([0.]) grid[:, :, 0] = torch.ger( linear_points, torch.ones(W)).expand_as(grid[:, :, 1]) return grid.view(-1, 2) if flatten else grid
Example #3
Source File: PCACell.py From EchoTorch with GNU General Public License v3.0 | 6 votes |
def _fix(self, mtx, avg, tlen, center=True): """ Returns a triple containing the covariance matrix, the average and the number of observations. :param mtx: :param center: :return: """ mtx /= tlen - 1 # Substract the mean if center: avg_mtx = torch.ger(avg, avg) avg_mtx /= tlen * (tlen - 1) mtx -= avg_mtx # end if # Fix the average avg /= tlen return mtx, avg, tlen # end fix # Update covariance matrix
Example #4
Source File: modeling_resnet.py From tape with BSD 3-Clause "New" or "Revised" License | 6 votes |
def forward(self, input_ids): words_embeddings = self.word_embeddings(input_ids) seq_length = input_ids.size(1) position_ids = torch.arange( seq_length - 1, -1, -1.0, dtype=words_embeddings.dtype, device=words_embeddings.device) sinusoidal_input = torch.ger(position_ids, self.inverse_frequency) position_embeddings = torch.cat([sinusoidal_input.sin(), sinusoidal_input.cos()], -1) position_embeddings = position_embeddings.unsqueeze(0) embeddings = words_embeddings + position_embeddings embeddings = self.layer_norm(embeddings) embeddings = self.dropout(embeddings) return embeddings
Example #5
Source File: data_attachment.py From lddmm-ot with MIT License | 6 votes |
def _kernel_matching(q1_x, q1_mu, xt_x, xt_mu, radius) : """ Given two measures q1 and xt represented by locations/weights arrays, outputs a kernel-fidelity term and an empty 'info' array. """ K_qq, K_qx, K_xx = _cross_kernels(q1_x, xt_x, radius) cost = .5 * ( torch.sum(K_qq * torch.ger(q1_mu,q1_mu)) \ + torch.sum(K_xx * torch.ger(xt_mu,xt_mu)) \ -2*torch.sum(K_qx * torch.ger(q1_mu,xt_mu)) ) # Info = the 2D graph of the blurred distance function # Increase res if you want to get nice smooth pictures... res = 10 ; ticks = np.linspace( 0, 1, res + 1)[:-1] + 1/(2*res) X,Y = np.meshgrid( ticks, ticks ) points = Variable(torch.from_numpy(np.vstack( (X.ravel(), Y.ravel()) ).T).type(dtype), requires_grad=False) info = _k( points, q1_x , radius ) @ q1_mu \ - _k( points, xt_x , radius ) @ xt_mu return [cost , info.view( (res,res) ) ]
Example #6
Source File: lddmm_pytorch.py From lddmm-ot with MIT License | 6 votes |
def _kernel_matching(q1_x, q1_mu, xt_x, xt_mu, radius) : """ Given two measures q1 and xt represented by locations/weights arrays, outputs a kernel-fidelity term and an empty 'info' array. """ K_qq, K_qx, K_xx = _cross_kernels(q1_x, xt_x, radius) cost = .5 * ( torch.sum(K_qq * torch.ger(q1_mu,q1_mu)) \ + torch.sum(K_xx * torch.ger(xt_mu,xt_mu)) \ -2*torch.sum(K_qx * torch.ger(q1_mu,xt_mu)) ) # Info = the 2D graph of the blurred distance function # Increase res if you want to get nice smooth pictures... res = 10 ; ticks = np.linspace( 0, 1, res + 1)[:-1] + 1/(2*res) X,Y = np.meshgrid( ticks, ticks ) points = Variable(torch.from_numpy(np.vstack( (X.ravel(), Y.ravel()) ).T).type(dtype), requires_grad=False) info = _k( points, q1_x , radius ) @ q1_mu \ - _k( points, xt_x , radius ) @ xt_mu return [cost , info.view( (res,res) ) ]
Example #7
Source File: test.py From pytorch_compact_bilinear_pooling with BSD 3-Clause "New" or "Revised" License | 6 votes |
def bilinear_pooling(x,y): x_size = x.size() y_size = y.size() assert(x_size[:-1] == y_size[:-1]) out_size = list(x_size) out_size[-1] = x_size[-1]*y_size[-1] x = x.view([-1,x_size[-1]]) y = y.view([-1,y_size[-1]]) out_stack = [] for i in range(x.size()[0]): out_stack.append(torch.ger(x[i],y[i])) out = torch.stack(out_stack) return out.view(out_size)
Example #8
Source File: torchbk.py From quantumflow with Apache License 2.0 | 6 votes |
def outer(tensor0: BKTensor, tensor1: BKTensor) -> BKTensor: tensor0 = tensor0.contiguous() tensor1 = tensor1.contiguous() bits = rank(tensor0) + rank(tensor1) num0 = torch.numel(tensor0[0]) num1 = torch.numel(tensor1[0]) res = (torch.ger(tensor0[0].view(num0), tensor1[0].view(num1)) - torch.ger(tensor0[1].view(num0), tensor1[1].view(num1)), torch.ger(tensor0[0].view(num0), tensor1[1].view(num1)) + torch.ger(tensor0[1].view(num0), tensor1[0].view(num1))) tensor = torch.stack(res) tensor = tensor.resize_([2]*(bits+1)) return tensor
Example #9
Source File: model.py From RCZoo with MIT License | 5 votes |
def decode(score_s, score_e, top_n=1, max_len=None): """Take argmax of constrained score_s * score_e. Args: score_s: independent start predictions score_e: independent end predictions top_n: number of top scored pairs to take max_len: max span length to consider """ pred_s = [] pred_e = [] pred_score = [] max_len = max_len or score_s.size(1) for i in range(score_s.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_s[i], score_e[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.numpy() scores_flat = scores.flatten() if top_n == 1: idx_sort = [np.argmax(scores_flat)] elif len(scores_flat) < top_n: idx_sort = np.argsort(-scores_flat) else: idx = np.argpartition(-scores_flat, top_n)[0:top_n] idx_sort = idx[np.argsort(-scores_flat[idx])] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) pred_s.append(s_idx) pred_e.append(e_idx) pred_score.append(scores_flat[idx_sort]) return pred_s, pred_e, pred_score
Example #10
Source File: mem_transformer.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:,None,:].expand(-1, bsz, -1) else: return pos_emb[:,None,:]
Example #11
Source File: model.py From Multi-Step-Reasoning with Apache License 2.0 | 5 votes |
def decode(score_s, score_e, top_n=1, max_len=None): """Take argmax of constrained score_s * score_e. Args: score_s: independent start predictions score_e: independent end predictions top_n: number of top scored pairs to take max_len: max span length to consider """ pred_s = [] pred_e = [] pred_score = [] max_len = max_len or score_s.size(1) for i in range(score_s.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_s[i], score_e[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.numpy() scores_flat = scores.flatten() if top_n == 1: idx_sort = [np.argmax(scores_flat)] elif len(scores_flat) < top_n: idx_sort = np.argsort(-scores_flat) else: idx = np.argpartition(-scores_flat, top_n)[0:top_n] idx_sort = idx[np.argsort(-scores_flat[idx])] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) pred_s.append(s_idx) pred_e.append(e_idx) pred_score.append(scores_flat[idx_sort]) return pred_s, pred_e, pred_score
Example #12
Source File: models.py From dlgm with MIT License | 5 votes |
def kronecker(self, matrix1, matrix2): """ Arguments: ---------- - matrix1: batch-wise stacked matrices1 - matrix2: batch-wise stacked matrices2 Returns: -------- - Batchwise Kronecker product between matrix1 and matrix2 """ return torch.ger(matrix1.view(-1), matrix2.view(-1)).reshape(*(matrix1.size() + matrix2.size())).permute([0, 2, 1, 3]).reshape(matrix1.size(0) * matrix2.size(0), matrix1.size(1) * matrix2.size(1))
Example #13
Source File: model.py From sru with MIT License | 5 votes |
def predict(self, ex): # Eval mode self.network.eval() # Transfer to GPU if self.opt['cuda']: inputs = [Variable(e.cuda(async=True), volatile=True) for e in ex[:7]] else: inputs = [Variable(e, volatile=True) for e in ex[:7]] # Run forward score_s, score_e = self.network(*inputs) # Transfer to CPU/normal tensors for numpy ops score_s = score_s.data.cpu() score_e = score_e.data.cpu() # Get argmax text spans text = ex[-2] spans = ex[-1] predictions = [] max_len = self.opt['max_len'] or score_s.size(1) for i in range(score_s.size(0)): scores = torch.ger(score_s[i], score_e[i]) scores.triu_().tril_(max_len - 1) scores = scores.numpy() s_idx, e_idx = np.unravel_index(np.argmax(scores), scores.shape) s_offset, e_offset = spans[i][s_idx][0], spans[i][e_idx][1] predictions.append(text[i][s_offset:e_offset]) return predictions
Example #14
Source File: mixin.py From claf with MIT License | 5 votes |
def get_best_span(self, span_start_logits, span_end_logits, answer_maxlen=None): """ Take argmax of constrained score_s * score_e. * Args: span_start_logits: independent start logits span_end_logits: independent end logits * Kwargs: answer_maxlen: max span length to consider (default is None -> All) """ B = span_start_logits.size(0) best_word_span = span_start_logits.new_zeros((B, 2), dtype=torch.long) score_starts = F.softmax(span_start_logits, dim=-1) score_ends = F.softmax(span_end_logits, dim=-1) max_len = answer_maxlen or score_starts.size(1) for i in range(score_starts.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_starts[i], score_ends[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.detach().cpu().numpy() scores_flat = scores.flatten() idx_sort = [np.argmax(scores_flat)] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) best_word_span[i, 0] = int(s_idx[0]) best_word_span[i, 1] = int(e_idx[0]) return best_word_span
Example #15
Source File: modeling_transfo_xl.py From HPSG-Neural-Parser with MIT License | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:,None,:].expand(-1, bsz, -1) else: return pos_emb[:,None,:]
Example #16
Source File: positional_embedding.py From deq with MIT License | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(self.inv_freq, pos_seq) # C x L pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=0) # Concat at feature dimension if bsz is not None: return pos_emb[None,:,:].expand(bsz, -1, -1) else: return pos_emb[None,:,:]
Example #17
Source File: orthogonal.py From nsf with MIT License | 5 votes |
def _apply_transforms(inputs, q_vectors): """Apply the sequence of transforms parameterized by given q_vectors to inputs. Costs O(KDN), where: - K is number of transforms - D is dimensionality of inputs - N is number of inputs Args: inputs: Tensor of shape [N, D] q_vectors: Tensor of shape [K, D] Returns: A tuple of: - A Tensor of shape [N, D], the outputs. - A Tensor of shape [N], the log absolute determinants of the total transform. """ squared_norms = torch.sum(q_vectors ** 2, dim=-1) outputs = inputs for q_vector, squared_norm in zip(q_vectors, squared_norms): temp = outputs @ q_vector # Inner product. temp = torch.ger(temp, (2.0 / squared_norm) * q_vector) # Outer product. outputs = outputs - temp batch_size = inputs.shape[0] logabsdet = torch.zeros(batch_size) return outputs, logabsdet
Example #18
Source File: model.py From RCZoo with MIT License | 5 votes |
def decode(score_s, score_e, top_n=1, max_len=None): """Take argmax of constrained score_s * score_e. Args: score_s: independent start predictions score_e: independent end predictions top_n: number of top scored pairs to take max_len: max span length to consider """ pred_s = [] pred_e = [] pred_score = [] max_len = max_len or score_s.size(1) for i in range(score_s.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_s[i], score_e[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.numpy() scores_flat = scores.flatten() if top_n == 1: idx_sort = [np.argmax(scores_flat)] elif len(scores_flat) < top_n: idx_sort = np.argsort(-scores_flat) else: idx = np.argpartition(-scores_flat, top_n)[0:top_n] idx_sort = idx[np.argsort(-scores_flat[idx])] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) pred_s.append(s_idx) pred_e.append(e_idx) pred_score.append(scores_flat[idx_sort]) return pred_s, pred_e, pred_score
Example #19
Source File: model.py From RCZoo with MIT License | 5 votes |
def decode(score_s, score_e, top_n=1, max_len=None): """Take argmax of constrained score_s * score_e. Args: score_s: independent start predictions score_e: independent end predictions top_n: number of top scored pairs to take max_len: max span length to consider """ pred_s = [] pred_e = [] pred_score = [] max_len = max_len or score_s.size(1) for i in range(score_s.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_s[i], score_e[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.numpy() scores_flat = scores.flatten() if top_n == 1: idx_sort = [np.argmax(scores_flat)] elif len(scores_flat) < top_n: idx_sort = np.argsort(-scores_flat) else: idx = np.argpartition(-scores_flat, top_n)[0:top_n] idx_sort = idx[np.argsort(-scores_flat[idx])] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) pred_s.append(s_idx) pred_e.append(e_idx) pred_score.append(scores_flat[idx_sort]) return pred_s, pred_e, pred_score
Example #20
Source File: gelcd.py From torch-gel with MIT License | 5 votes |
def _hess_j(C_j, I_j, b_j, b_j_norm, a_1_j, a_2_j): """Compute the Hessian with respect to one of the coefficients.""" D_j = torch.ger(b_j, b_j) return C_j + (a_1_j / b_j_norm) * (I_j - D_j / (b_j_norm ** 2)) + a_2_j * I_j
Example #21
Source File: xlnet_utils.py From texar-pytorch with Apache License 2.0 | 5 votes |
def forward(self, # type: ignore pos_seq: torch.Tensor) -> torch.Tensor: sinusoid = torch.ger(pos_seq, self.inv_freq) pos_embed = torch.cat([sinusoid.sin(), sinusoid.cos()], dim=-1) return pos_embed
Example #22
Source File: modeling_transfo_xl.py From PPLM with Apache License 2.0 | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:,None,:].expand(-1, bsz, -1) else: return pos_emb[:,None,:]
Example #23
Source File: model.py From RCZoo with MIT License | 5 votes |
def decode(score_s, score_e, top_n=1, max_len=None): """Take argmax of constrained score_s * score_e. Args: score_s: independent start predictions score_e: independent end predictions top_n: number of top scored pairs to take max_len: max span length to consider """ pred_s = [] pred_e = [] pred_score = [] max_len = max_len or score_s.size(1) for i in range(score_s.size(0)): # Outer product of scores to get full p_s * p_e matrix scores = torch.ger(score_s[i], score_e[i]) # Zero out negative length and over-length span scores scores.triu_().tril_(max_len - 1) # Take argmax or top n scores = scores.numpy() scores_flat = scores.flatten() if top_n == 1: idx_sort = [np.argmax(scores_flat)] elif len(scores_flat) < top_n: idx_sort = np.argsort(-scores_flat) else: idx = np.argpartition(-scores_flat, top_n)[0:top_n] idx_sort = idx[np.argsort(-scores_flat[idx])] s_idx, e_idx = np.unravel_index(idx_sort, scores.shape) pred_s.append(s_idx) pred_e.append(e_idx) pred_score.append(scores_flat[idx_sort]) return pred_s, pred_e, pred_score
Example #24
Source File: modeling_transfo_xl.py From KagNet with MIT License | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:,None,:].expand(-1, bsz, -1) else: return pos_emb[:,None,:]
Example #25
Source File: cameras_cuda.py From multiview-human-pose-estimation-pytorch with MIT License | 5 votes |
def project_point_radial(x, R, T, f, c, k, p): """ Args x: Nx3 points in world coordinates R: 3x3 Camera rotation matrix T: 3x1 Camera translation parameters f: (scalar) Camera focal length c: 2x1 Camera center k: 3x1 Camera radial distortion coefficients p: 2x1 Camera tangential distortion coefficients Returns ypixel.T: Nx2 points in pixel space """ n = x.shape[0] xcam = torch.mm(R, torch.t(x) - T) y = xcam[:2] / xcam[2] kexp = k.repeat((1, n)) r2 = torch.sum(y**2, 0, keepdim=True) r2exp = torch.cat([r2, r2**2, r2**3], 0) radial = 1 + torch.einsum('ij,ij->j', kexp, r2exp) tan = 2 * p[0] * y[1] + 2 * p[1] * y[0] corr = (radial + tan).repeat((2, 1)) y = y * corr + torch.ger(torch.cat([p[1], p[0]]).view(-1), r2.view(-1)) ypixel = (f * y) + c return torch.t(ypixel)
Example #26
Source File: pytorch_backend.py From pyhf with Apache License 2.0 | 5 votes |
def outer(self, tensor_in_1, tensor_in_2): return torch.ger(tensor_in_1, tensor_in_2)
Example #27
Source File: modeling_transfo_xl.py From exbert with Apache License 2.0 | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:, None, :].expand(-1, bsz, -1) else: return pos_emb[:, None, :]
Example #28
Source File: pscn.py From GPF with MIT License | 5 votes |
def forward(self, batch_graph, node_feat, edge_feat): num_nebhors = batch_graph[0].mat.shape[0] #获取每个子图中邻节点所构成的邻接矩阵:shape = num_nebhors * num_nebhors recep = np.zeros((len(batch_graph), num_nebhors, num_nebhors)) i = 0 for batch in batch_graph: recep[i,:] = (batch.mat).todense() i += 1 #转变成张量 recep = torch.from_numpy(recep) recep = recep.sum(dim=1) #x: batch_size * nebhors * latent_dim x = node_feat.view(len(batch_graph), num_nebhors, node_feat.shape[1]) bs, l = recep.size()[:2] #print(bs,l) n = x.size()[1] # x is of shape bs x n x num_feature offset = torch.ger(torch.arange(0, bs).long(), torch.ones(l).long() * n) offset = Variable(offset, requires_grad=False) #print(recep.shape) #print(offset.shape) recep = (recep.long() + offset).view(-1) x = x.view(bs * n, -1) x = x.index_select(dim=0, index=recep) x = x.view(bs, l, -1) # x is of shape bs x l x num_feature, l=w*k x = x.transpose(1, 2) # x is of shape bs x num_feature x l, l=w*k x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) x = F.dropout(x, self.dropout, training=self.training) x = x.view(bs, -1) x = self.fc(x) #后面接其它网络层 return x #后面不接其它层,直接获取分类结果 #return F.log_softmax(x, dim=-1)
Example #29
Source File: modeling.py From unilm with MIT License | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:, None, :].expand(-1, bsz, -1) else: return pos_emb[:, None, :]
Example #30
Source File: modeling_decoding.py From unilm with MIT License | 5 votes |
def forward(self, pos_seq, bsz=None): sinusoid_inp = torch.ger(pos_seq, self.inv_freq) pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1) if bsz is not None: return pos_emb[:, None, :].expand(-1, bsz, -1) else: return pos_emb[:, None, :]