python - Numpy 性能 - 向量与其转置的外积
问题描述
据我了解,向量及其转置的外积在值上是对称的。
Numpy 是否考虑到这一点,只对输出的上三角部分进行乘法运算,还是计算整个输出矩阵(即使它是对称的并且时间 + 内存可能会浪费?)
解决方案
探索一些替代方案:
In [162]: x=np.arange(100)
In [163]: np.outer(x,x)
Out[163]:
array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 1, 2, ..., 97, 98, 99],
[ 0, 2, 4, ..., 194, 196, 198],
...,
[ 0, 97, 194, ..., 9409, 9506, 9603],
[ 0, 98, 196, ..., 9506, 9604, 9702],
[ 0, 99, 198, ..., 9603, 9702, 9801]])
In [164]: x1=x[:,None]
In [165]: x1*x1.T
Out[165]:
array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 1, 2, ..., 97, 98, 99],
[ 0, 2, 4, ..., 194, 196, 198],
...,
[ 0, 97, 194, ..., 9409, 9506, 9603],
[ 0, 98, 196, ..., 9506, 9604, 9702],
[ 0, 99, 198, ..., 9603, 9702, 9801]])
In [166]: np.dot(x1,x1.T)
Out[166]:
array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 1, 2, ..., 97, 98, 99],
[ 0, 2, 4, ..., 194, 196, 198],
...,
[ 0, 97, 194, ..., 9409, 9506, 9603],
[ 0, 98, 196, ..., 9506, 9604, 9702],
[ 0, 99, 198, ..., 9603, 9702, 9801]])
比较他们的时间:
In [167]: timeit np.outer(x,x)
40.8 µs ± 63.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [168]: timeit x1*x1.T
36.3 µs ± 22 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [169]: timeit np.dot(x1,x1.T)
60.7 µs ± 6.86 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
是否dot
使用转置捷径?我不这么认为,或者如果确实如此,在这种情况下也无济于事。我有点惊讶,dot
它变慢了。
In [170]: x2=x1.T
In [171]: timeit np.dot(x1,x2)
61.1 µs ± 30 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
另一种方法
In [172]: timeit np.einsum('i,j',x,x)
28.3 µs ± 19.4 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
einsum
withx1
和x2
具有相同的时间。
有趣的是matmul
,einsum
在这种情况下(也许einsum
是委托给matmul
?)
In [178]: timeit x1@x2
27.3 µs ± 1.09 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [179]: timeit x1@x1.T
27.2 µs ± 14.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Numpy 高效矩阵自乘法(gram 矩阵)演示了如何dot
通过聪明来节省时间(对于 1000x1000 数组)。
如链接中所述,dot
可以检测一个参数何时是另一个参数的转置(可能通过检查数据缓冲区指针以及形状和步幅),并且可以使用针对对称计算优化的 BLAS 函数。但我没有看到outer
这样做的证据。广播乘法不太可能采取这样的步骤。
推荐阅读
- azure - 尝试通过 Ansible 使用托管磁盘的快照创建新的 Azure VM
- python - Python subprocess aws copy - 最大化网络带宽
- ansible - 需要协助编写脚本以在所有成员交换机(不仅仅是主交换机)中查找可用空间
- javascript - 从按钮表单指定 javascript 函数
- powerbi - 计算power bi中两行之间的差异
- linux - 带有 curl 和命令行参数的 Sudo sh 脚本
- python - 如果只有单个值最常见,则熊猫返回模式
- c# - 网络上的 CQRS 示例中是否过度使用了 MediatR 库?
- amazon-web-services - AWS - 自定义授权人
- c# - 如何在我的游戏中让我的蛇成为在 c# 中在整个控制台上打印的一个角色?