python - 超过 2 维的 numpy 点

标签 python numpy matrix-multiplication dot-product

在 numpy dot(...) 中功能documentation据说:

If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

这与 matmul(...) 明显不同函数,其中最后两个维度上的矩阵乘积简单地沿第一个维度广播(如果矩阵超过 2 个维度)。

然而,虽然 matmul 行为的实用性很明显,但我想知道 dot(...) 给出的结果可能有哪些应用。对于二维以上的矩阵。有没有实际应用dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])有用吗?

最佳答案

我不记得有任何关于为什么选择这种方法的讨论。可能只是为了计算方便。它的局限性导致了 einsum 和后来的 matmul 的发展。

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

einsum 等价

np.einsum('ijl,klm->ijkm', a, b)

通过转置和 reshape ,它可以写成

np.einsum('pl,lo', A, B)    # p=ij, o=km
然后可以将

A,B 传递给传统的 BLAS 函数。该功能甚至可能内置到 BLAS 中。

关于python - 超过 2 维的 numpy 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73544747/

相关文章:

python - 使用 numpy 和/或 cv2 在深度图中找到最暗的区域

python - recv_into 一个 numpy 数组

c++ - Laderman 的 3x3 矩阵乘法只有 23 次乘法,值得吗?

python - 嗨,我有一项赋值,可以使用lambda并在python中映射,从而将一个数字的所有数字相加

python - 如何将 BOW 以外的功能添加到 scikit-learn 分类模型中

python - 读取 pandas 中的所有列作为字符串

python - BigQuery : Is there a way to get the schema of a SELECT statement without running it (dry_run)?

python - rospy 中的实时 2D 激光扫描仪数据

gpu - AMD Polaris 上某些尺寸的矩阵乘法性能下降

opencl - 优化批处理矩阵乘法opencl代码