使用numpy进行有效的矩阵切片

时间:2018-12-23 13:17:16

标签: python-3.x numpy matrix-multiplication

我正在尝试在子向量上相乘子矩阵。看来这样的乘法应该比整个矩阵在整个矢量上的乘法要快,但是时间测量却相反:

B = np.random.randn(26200, 2000)
h = np.random.randn(2000)
%time z = B @ h
CPU times: user 56 ms, sys: 4 ms, total: 60 ms
Wall time: 29.4 ms

%time z = B[:, :256] @ h[:256]
CPU times: user 44 ms, sys: 28 ms, total: 72 ms
Wall time: 54.5 ms 

%timeit的结果:

%timeit z = B @ h
100 loops, best of 3: 18.8 ms per loop

%timeit z = B[:, :256] @ h[:256]
10 loops, best of 3: 38.2 ms per loop

再次运行:

%timeit z = B @ h
10 loops, best of 3: 18.7 ms per loop 

%timeit z = B[:, :256] @ h[:256]
10 loops, best of 3: 36.8 ms per loop

也许有一些使用numpy的有效方法,或者我需要使用例如tenserflow来使这种切片有效吗?

1 个答案:

答案 0 :(得分:1)

这是内存布局和时间访问的问题。默认情况下,arrays are stored line by line like in C order='C')。您可以像在Fortran(order='F')中一样逐列存储数据,这与您的受限问题更加兼容,因为您只选择了很少的列。

说明:

In [107]: BF=np.asfortranarray(B)

In [108]: np.equal(B,BF).all()
Out[108]: True


In [110]: %timeit B@h
78.5 ms ± 20.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [111]: %timeit BF@h
89.3 ms ± 7.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [112]: %timeit B[:,:256]@h[:256]
150 ms ± 18.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [113]: %timeit BF[:,:256]@h[:256]
10.5 ms ± 893 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

这样,时间执行就随大小而变化。