【问题标题】:np.tensordot for rotation of point clouds?np.tensordot 用于点云的旋转?
【发布时间】:2019-01-30 16:29:59
【问题描述】:

关于原点的旋转是一个矩阵乘积,可以用numpy的点函数完成,

import numpy as np
points = np.random.rand(100,3)  # 100 X, Y, Z tuples.  shape = (100,3)
rotation = np.identity(3)  # null rotation for example
out = np.empty(points.shape)
for idx, point in enumerate(points):
    out[idx,:] = np.dot(rotation, point)

这涉及到一个 for 循环,或者可以使用 numpy tile 进行矢量化。我认为有一个涉及 np.tensordot 的实现,但该功能对我来说是巫术。这可能吗?

【问题讨论】:

  • 你可以做out = (rotation @ points[:, :, np.newaxis])[:, :, 0]。或out = np.einsum('ij,nj->ni', rotation, points)。或者用张量点out = np.tensordot(points, rotation, axes=[1, 1]).
  • 谢谢! Einsum 实际上是 100 分中最快的——比 tensordot 快 5 倍,比 for 循环快 33 倍。以 1000 点 tensordot 获胜——我的机器上似乎有 ~14us 的调度开销,但对于大型数组来说,时间相对恒定。我认为 matmul 解决方案是最通用的,因为它始终具有最佳性能的竞争力。再次感谢您 -- 如果您提交答案,我可以将其标记为已接受。

标签: python numpy tensor


【解决方案1】:

有几种方法可以做到这一点。使用np.matmul,您可以:

out = np.matmul(rotation, points[:, :, np.newaxis])[:, :, 0]

或者,如果您使用的是 Python 3.5 或更高版本:

out = (rotation @ points[:, :, np.newaxis])[:, :, 0]

另一种方法是np.einsum:

out = np.einsum('ij,nj->ni', rotation, points)

最后,按照你的建议,你也可以使用np.tensordot

out = np.tensordot(points, rotation, axes=[1, 1])

请注意,在这种情况下,points 是第一个参数,rotation 是第二个参数,否则输出的尺寸会反转。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多