在pytorch中计算角度

时间:2021-07-14 22:28:43

标签: python pytorch

如果我们有一组点 Rs,我们可以使用 torch.cdist 获得所有对距离。

dists_ij = torch.cdist(Rs, Rs)

是否有一个函数可以像这样获取两组向量 Vs 之间的角度:

angs_ij = torch.angs(Vs, Vs)

1 个答案:

答案 0 :(得分:3)

您可以使用 relation between the dot product of two vectors and the angle between them 手动执行此操作:

# normalize the vectors
nVs = Vs / torch.norm(Vs, p=2, dim=-1, keepdim=True)
# compute cosine of the angles using dot product
cos_ij = torch.einsum('bni,bmi->bnm', nVs, nVs)