本文整理汇总了Python中torch.ger方法的典型用法代码示例。如果您正苦于以下问题:Python torch.ger方法的具体用法?Python torch.ger怎么用?Python torch.ger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.ger方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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)
示例2: outer
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例3: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例4: bilinear_pooling
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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)
示例5: _kernel_matching
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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) ) ]
示例6: _fix
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例7: create_grid
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例8: decode
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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])
del score_s, score_e
return pred_s, pred_e, pred_score
示例9: _apply_transforms
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例10: decode
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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
示例11: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
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,:]
示例12: rodrigues
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
def rodrigues(self, r):
"""
Rodrigues' rotation formula that turns axis-angle tensor into rotation
matrix in a batch-ed manner.
Parameter:
----------
r: Axis-angle rotation tensor of shape [N, 1, 3].
Return:
-------
Rotation matrix of shape [N, 3, 3].
"""
theta = torch.norm(r, dim=(1, 2), keepdim=True)
# avoid division by zero
torch.max(theta, theta.new_full((1,), torch.finfo(theta.dtype).tiny), out=theta)
#The .tiny has to be uploaded to GPU, but self.regress_joints is such a big bottleneck it is not felt.
r_hat = r / theta
z_stick = torch.zeros_like(r_hat[:, 0, 0])
m = torch.stack(
(z_stick, -r_hat[:, 0, 2], r_hat[:, 0, 1],
r_hat[:, 0, 2], z_stick, -r_hat[:, 0, 0],
-r_hat[:, 0, 1], r_hat[:, 0, 0], z_stick), dim=1)
m = m.reshape(-1, 3, 3)
dot = torch.bmm(r_hat.transpose(1, 2), r_hat) # Batched outer product.
# torch.matmul or torch.stack([torch.ger(r, r) for r in r_hat.squeeze(1)] works too.
cos = theta.cos()
R = cos * self.eye + (1 - cos) * dot + theta.sin() * m
return R
示例13: kronecker_product
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
def kronecker_product(mat1, mat2):
return torch.ger(mat1.view(-1), mat2.view(-1)).reshape(*(mat1.size() + mat2.size())).permute(
[0, 2, 1, 3]).reshape(mat1.size(0) * mat2.size(0), mat1.size(1) * mat2.size(1))
示例14: _scores_to_text
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
def _scores_to_text(self, text, score_s, score_e):
max_len = self.config['max_answer_len'] or score_s.size(1)
scores = torch.ger(score_s.squeeze(), score_e.squeeze())
scores.triu_().tril_(max_len - 1)
scores = scores.cpu().detach().numpy()
s_idx, e_idx = np.unravel_index(np.argmax(scores), scores.shape)
return ' '.join(text[s_idx: e_idx + 1]), (int(s_idx), int(e_idx))
示例15: _scores_to_raw_text
# 需要导入模块: import torch [as 别名]
# 或者: from torch import ger [as 别名]
def _scores_to_raw_text(self, raw_text, offsets, score_s, score_e):
max_len = self.config['max_answer_len'] or score_s.size(1)
scores = torch.ger(score_s.squeeze(), score_e.squeeze())
scores.triu_().tril_(max_len - 1)
scores = scores.cpu().detach().numpy()
s_idx, e_idx = np.unravel_index(np.argmax(scores), scores.shape)
return raw_text[offsets[s_idx][0]: offsets[e_idx][1]], (offsets[s_idx][0], offsets[e_idx][1])