我正在尝试优化一些执行大量顺序矩阵运算的代码。
我认为numpy.linalg.multi_dot
(docs here)会执行C或BLAS中的所有操作,因此它会比arr1.dot(arr2).dot(arr3)
之类的更快,等等。
我真的很惊讶在笔记本上运行这段代码:
v1 = np.random.rand(2,2)
v2 = np.random.rand(2,2)
%%timeit
v1.dot(v2.dot(v1.dot(v2)))
The slowest run took 9.01 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.14 µs per loop
%%timeit
np.linalg.multi_dot([v1,v2,v1,v2])
The slowest run took 4.67 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 32.9 µs per loop
使用multi_dot
找出相同的操作速度大约慢10倍。
我的问题是:
答案 0 :(得分:6)
这是因为你的测试矩阵太小而且太规律;确定最快评估订单的开销可能会超过潜在的性能提升。
使用文档中的示例:
import numpy as snp
from numpy.linalg import multi_dot
# Prepare some data
A = np.random.rand(10000, 100)
B = np.random.rand(100, 1000)
C = np.random.rand(1000, 5)
D = np.random.rand(5, 333)
%timeit -n 10 multi_dot([A, B, C, D])
%timeit -n 10 np.dot(np.dot(np.dot(A, B), C), D)
%timeit -n 10 A.dot(B).dot(C).dot(D)
结果:
10 loops, best of 3: 12 ms per loop
10 loops, best of 3: 62.7 ms per loop
10 loops, best of 3: 59 ms per loop
multi_dot
通过评估标量乘法最少的最快乘法顺序来提高性能。
在上述情况下,默认的常规乘法顺序((AB)C)D
被评估为A((BC)D)
- 因此1000x100 @ 100x1000
乘法减少到1000x100 @ 100x333
,至少减少2/3
标量乘法。
您可以通过测试来验证这一点
%timeit -n 10 np.dot(A, np.dot(np.dot(B, C), D))
10 loops, best of 3: 19.2 ms per loop