本文整理匯總了Python中torch.acos方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.acos方法的具體用法?Python torch.acos怎麽用?Python torch.acos使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.acos方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def forward(self, input1):
self.batchgrid3d = torch.zeros(torch.Size([input1.size(0)]) + self.grid3d.size())
for i in range(input1.size(0)):
self.batchgrid3d[i] = self.grid3d
self.batchgrid3d = Variable(self.batchgrid3d)
#print(self.batchgrid3d)
x = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,0:4]), 3)
y = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,4:8]), 3)
z = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,8:]), 3)
#print(x)
r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5
#print(r)
theta = torch.acos(z/r)/(np.pi/2) - 1
#phi = torch.atan(y/x)
phi = torch.atan(y/(x + 1e-5)) + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
phi = phi/np.pi
output = torch.cat([theta,phi], 3)
return output
示例2: batch_mat2angle
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def batch_mat2angle(R):
""" Calcuate the axis angles (twist) from a batch of rotation matrices
Ethan Eade's lie group note:
http://ethaneade.com/lie.pdf equation (17)
function tested in 'test_geometry.py'
:input
:param Rotation matrix Bx3x3 \in \SO3 space
--------
:return
:param the axis angle B
"""
R1 = [torch.trace(R[i]) for i in range(R.size()[0])]
R_trace = torch.stack(R1)
# clamp if the angle is too large (break small angle assumption)
# @todo: not sure whether it is absoluately necessary in training.
angle = acos( ((R_trace - 1)/2).clamp(-1,1))
return angle
示例3: rotation_error
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def rotation_error(self, net_roll, net_pitch, tar_roll, tar_pitch ):
cx1 = torch.cos(net_roll)
sx1 = torch.sin(net_roll)
cy1 = torch.cos(net_pitch)
sy1 = torch.sin(net_pitch)
cx2 = torch.cos(tar_roll)
sx2 = torch.sin(tar_roll)
cy2 = torch.cos(tar_pitch)
sy2 = torch.sin(tar_pitch)
m00 = cy1*cy2 + sy1*sy2
m11 = cx1*cx2 + cy1*cy2*sx1*sx2 + sx1*sx2*sy1*sy2
m22 = sx1*sx2 + cx1*cx2*sy1*sy2 + cx1*cx2*cy1*cy2
self.acos = ( m00 + m11 + m22 - 1)/2.0 * 0.99999
return torch.acos(self.acos)
示例4: arccos
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def arccos(x, out=None):
"""
Return the trigonometric arccos, element-wise.
Parameters
----------
x : ht.DNDarray
The value for which to compute the trigonometric cosine.
out : ht.DNDarray or None, optional
A location in which to store the results. If provided, it must have a broadcastable shape. If not provided
or set to None, a fresh tensor is allocated.
Returns
-------
arccos : ht.DNDarray
A tensor of the same shape as x, containing the trigonometric arccos of each element in this tensor.
Input elements outside [-1., 1.] are returned as nan. If out was provided, arccos is a reference to it.
Examples
--------
>>> ht.arccos(ht.array([-1.,-0., 0.83]))
tensor([3.1416, 1.5708, 0.5917])
"""
return local_op(torch.acos, x, out)
示例5: interpolate_sphere
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def interpolate_sphere(self, z1, z2, t):
''' Interpolates on a sphere.
Args:
z1 (tensor): start latent code
z2 (tensor): end latent code
t (tensor): time steps
'''
p = (z1 * z2).sum(dim=-1, keepdim=True)
p = p / z1.pow(2).sum(dim=-1, keepdim=True).sqrt()
p = p / z2.pow(2).sum(dim=-1, keepdim=True).sqrt()
omega = torch.acos(p)
s1 = torch.sin((1-t)*omega)/torch.sin(omega)
s2 = torch.sin(t*omega)/torch.sin(omega)
z = s1 * z1 + s2 * z2
return z
示例6: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def forward(self, input, label):
# normalize features
x = F.normalize(input)
# normalize weights
W = F.normalize(self.weight)
# dot product
logits = F.linear(x, W)
# add margin
theta = torch.acos(torch.clamp(logits, -1.0 + 1e-7, 1.0 - 1e-7))
target_logits = torch.cos(theta + self.m)
one_hot = torch.zeros_like(logits)
one_hot.scatter_(1, label.view(-1, 1).long(), 1)
if self.ls_eps > 0:
one_hot = (1 - self.ls_eps) * one_hot + self.ls_eps / self.out_features
output = logits * (1 - one_hot) + target_logits * one_hot
# feature re-scale
with torch.no_grad():
B_avg = torch.where(one_hot < 1, torch.exp(self.s * logits), torch.zeros_like(logits))
B_avg = torch.sum(B_avg) / input.size(0)
theta_med = torch.median(theta)
self.s = torch.log(B_avg) / torch.cos(torch.min(self.theta_zero * torch.ones_like(theta_med), theta_med))
output *= self.s
return output
示例7: ts_ss_
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def ts_ss_(v, eps=1e-15, eps2=1e-4):
# reusable compute
v_inner = torch.mm(v, v.t())
vs = v.norm(dim=-1, keepdim=True)
vs_dot = vs.mm(vs.t())
# compute triangle(v)
v_cos = v_inner / vs_dot
v_cos = v_cos.clamp(-1. + eps2, 1. - eps2) # clamp to avoid backprop instability
theta_ = torch.acos(v_cos) + math.radians(10)
theta_rad = theta_ * math.pi / 180.
tri = (vs_dot * torch.sin(theta_rad)) / 2.
# compute sector(v)
v_norm = (v ** 2).sum(-1, keepdim=True)
euc_dist = v_norm + v_norm.t() - 2.0 * v_inner
euc_dist = torch.sqrt(torch.abs(euc_dist) + eps) # add epsilon to avoid srt(0.)
magnitude_diff = (vs - vs.t()).abs()
sec = math.pi * (euc_dist + magnitude_diff) ** 2 * theta_ / 360.
return tri * sec
示例8: compute_geodesic_distance_from_two_matrices
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def compute_geodesic_distance_from_two_matrices(m1, m2):
batch=m1.shape[0]
m = torch.bmm(m1, m2.transpose(1,2)) #batch*3*3
cos = ( m[:,0,0] + m[:,1,1] + m[:,2,2] - 1 )/2
cos = torch.min(cos, torch.autograd.Variable(torch.ones(batch).cuda()) )
cos = torch.max(cos, torch.autograd.Variable(torch.ones(batch).cuda())*-1 )
theta = torch.acos(cos)
#theta = torch.min(theta, 2*np.pi - theta)
return theta
#matrices batch*3*3
#both matrix are orthogonal rotation matrices
#out theta between 0 to pi batch
示例9: compute_geodesic_distance_from_two_matrices
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def compute_geodesic_distance_from_two_matrices(m1, m2):
batch=m1.shape[0]
m = torch.bmm(m1, m2.transpose(1,2)) #batch*3*3
cos = ( m[:,0,0] + m[:,1,1] + m[:,2,2] - 1 )/2
cos = torch.min(cos, torch.autograd.Variable(torch.ones(batch).cuda()) )
cos = torch.max(cos, torch.autograd.Variable(torch.ones(batch).cuda())*-1 )
theta = torch.acos(cos)
#theta = torch.min(theta, 2*np.pi - theta)
return theta
#matrices batch*3*3
#both matrix are orthogonal rotation matrices
#out theta between 0 to 180 degree batch
示例10: log
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def log(g):
eps = 1.0e-7
R = g.view(-1, 3, 3)
tr = btrace(R)
c = (tr - 1) / 2
t = torch.acos(c)
sc = sinc1(t)
idx0 = (torch.abs(sc) <= eps)
idx1 = (torch.abs(sc) > eps)
sc = sc.view(-1, 1, 1)
X = torch.zeros_like(R)
if idx1.any():
X[idx1] = (R[idx1] - R[idx1].transpose(1, 2)) / (2*sc[idx1])
if idx0.any():
# t[idx0] == math.pi
t2 = t[idx0] ** 2
A = (R[idx0] + torch.eye(3).type_as(R).unsqueeze(0)) * t2.view(-1, 1, 1) / 2
aw1 = torch.sqrt(A[:, 0, 0])
aw2 = torch.sqrt(A[:, 1, 1])
aw3 = torch.sqrt(A[:, 2, 2])
sgn_3 = torch.sign(A[:, 0, 2])
sgn_3[sgn_3 == 0] = 1
sgn_23 = torch.sign(A[:, 1, 2])
sgn_23[sgn_23 == 0] = 1
sgn_2 = sgn_23 * sgn_3
w1 = aw1
w2 = aw2 * sgn_2
w3 = aw3 * sgn_3
w = torch.stack((w1, w2, w3), dim=-1)
W = mat(w)
X[idx0] = W
x = vec(X.view_as(g))
return x
示例11: dist
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def dist(self, x: torch.Tensor, y: torch.Tensor, *, keepdim=False) -> torch.Tensor:
inner = self.inner(x, x, y, keepdim=keepdim).clamp(
-1 + EPS[x.dtype], 1 - EPS[x.dtype]
)
return torch.acos(inner)
示例12: __call__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import acos [as 別名]
def __call__(self, data):
(row, col), pos, pseudo = data.edge_index, data.pos, data.edge_attr
assert pos.dim() == 2 and pos.size(1) == 3
cart = pos[col] - pos[row]
rho = torch.norm(cart, p=2, dim=-1).view(-1, 1)
theta = torch.atan2(cart[..., 1], cart[..., 0]).view(-1, 1)
theta = theta + (theta < 0).type_as(theta) * (2 * PI)
phi = torch.acos(cart[..., 2] / rho.view(-1)).view(-1, 1)
if self.norm:
rho = rho / (rho.max() if self.max is None else self.max)
theta = theta / (2 * PI)
phi = phi / PI
spher = torch.cat([rho, theta, phi], dim=-1)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.edge_attr = torch.cat([pseudo, spher.type_as(pos)], dim=-1)
else:
data.edge_attr = spher
return data