这是对这个问题的跟进:
How to do a sum of sums of the square of sum of sums?
我在寻求帮助时使用einsum(以提高速度)并获得了很好的答案。
我还有一个suggestion来使用numba
。我已经离开并尝试了两种方式,似乎在某一点之后numba
的速度提升要好得多。
那么如何在不遇到内存问题的情况下加快速度呢?
答案 0 :(得分:2)
下面的解决方案提供了3种不同的方法来进行简单的和和4种不同的方法来进行平方和。
总和3个方法 - for循环,JIT for循环,einsum(没有遇到内存问题)
总和平方和4方法 - for循环,JIT for循环,扩展einsum,中间einsum
这里前三个不会遇到内存问题,而for循环和扩展的einsum会遇到速度问题。这使得JIT解决方案看起来最好。
import numpy as np
import time
from numba import jit
def fun1(Fu, Fv, Fx, Fy, P, B):
Nu = Fu.shape[0]
Nv = Fv.shape[0]
Nx = Fx.shape[0]
Ny = Fy.shape[0]
Nk = Fu.shape[1]
Nl = Fv.shape[1]
I1 = np.zeros([Nu, Nv])
for iu in range(Nu):
for iv in range(Nv):
for ix in range(Nx):
for iy in range(Ny):
S = 0.
for ik in range(Nk):
for il in range(Nl):
S += Fu[iu,ik]*Fv[iv,il]*Fx[ix,ik]*Fy[iy,il]*P[ix,iy]*B[ik,il]
I1[iu, iv] += S
return I1
def fun2(Fu, Fv, Fx, Fy, P, B):
Nu = Fu.shape[0]
Nv = Fv.shape[0]
Nx = Fx.shape[0]
Ny = Fy.shape[0]
Nk = Fu.shape[1]
Nl = Fv.shape[1]
I2 = np.zeros([Nu, Nv])
for iu in range(Nu):
for iv in range(Nv):
for ix in range(Nx):
for iy in range(Ny):
S = 0.
for ik in range(Nk):
for il in range(Nl):
S += Fu[iu,ik]*Fv[iv,il]*Fx[ix,ik]*Fy[iy,il]*P[ix,iy]*B[ik,il]
I2[iu, iv] += S**2.
return I2
if __name__ == '__main__':
Nx = 30
Ny = 40
Nk = 50
Nl = 60
Nu = 70
Nv = 8
Fx = np.random.rand(Nx, Nk)
Fy = np.random.rand(Ny, Nl)
Fu = np.random.rand(Nu, Nk)
Fv = np.random.rand(Nv, Nl)
P = np.random.rand(Nx, Ny)
B = np.random.rand(Nk, Nl)
fjit1 = jit(fun1)
fjit2 = jit(fun2)
# For loop - becomes too slow so commented out
# t = time.time()
# I1 = fun1(Fu, Fv, Fx, Fy, P, B)
# print 'fun1 :', time.time() - t
# JIT compiled for loop - After a certain point beats einsum
t = time.time()
I1jit = fjit1(Fu, Fv, Fx, Fy, P, B)
print 'jit1 :', time.time() - t
# einsum great solution when no squaring is needed
t = time.time()
I1_ = np.einsum('uk, vl, xk, yl, xy, kl->uv', Fu, Fv, Fx, Fy, P, B)
print '1 einsum:', time.time() - t
# For loop - becomes too slow so commented out
# t = time.time()
# I2 = fun2(Fu, Fv, Fx, Fy, P, B)
# print 'fun2 :', time.time() - t
# JIT compiled for loop - After a certain point beats einsum
t = time.time()
I2jit = fjit2(Fu, Fv, Fx, Fy, P, B)
print 'jit2 :', time.time() - t
# Expanded einsum - As the size increases becomes very very slow
# t = time.time()
# I2_ = np.einsum('uk,vl,xk,yl,um,vn,xm,yn,kl,mn,xy->uv', Fu,Fv,Fx,Fy,Fu,Fv,Fx,Fy,B,B,P**2)
# print '2 einsum:', time.time() - t
# Intermediate einsum - As the sizes increase memory can become an issue
t = time.time()
temp = np.einsum('uk, vl, xk, yl, xy, kl->uvxy', Fu, Fv, Fx, Fy, P, B)
I2__ = np.einsum('uvxy->uv', np.square(temp))
print '2 einsum:', time.time() - t
# print 'I1 == I1_ :', np.allclose(I1, I1_)
print 'I1_ == Ijit1_:', np.allclose(I1_, I1jit)
# print 'I2 == I2_ :', np.allclose(I2, I2_)
print 'I2_ == Ijit2_:', np.allclose(I2__, I2jit)
注释: 请随时编辑/改进此答案。如果有人提出任何关于使这种并行的建议,那就太好了。
答案 1 :(得分:1)
您可以先对一个索引求和,然后继续乘法。我也尝试过使用numexpr进行最后的乘法和缩减操作的版本,但它似乎没有太多帮助。
def fun3(Fu, Fv, Fx, Fy, P, B):
P = P[None, None, ...]
Fu = Fu[:, None, None, None, :]
Fx = Fx[None, None, :, None, :]
Fv = Fv[:, None, None, :]
Fy = Fy[None, :, None, :]
B = B[None, None, ...]
return np.sum((P*np.sum(Fu*Fx*np.sum(Fv*Fy*B, axis=-1)[None, :, None, :, :], axis=-1))**2, axis=(2, 3))
我的电脑速度要快得多:
jit2:7.06 s
fun3:0.144 s
编辑:小改进 - 先乘以正方形。
EDIT2: 利用每个人最擅长的东西(numexpr - 乘法,numpy - dot / tensordot,求和),你仍然可以在fun3上进行超过20次的改进。
def fun4(Fu, Fv, Fx, Fy, P, B):
P = P[None, None, ...]
Fu = Fu[:, None, :]
Fx = Fx[None, ...]
Fy = Fy[:, None, :]
B = B[None, ...]
s = ne.evaluate('Fu*Fx')
r = np.tensordot(Fv, ne.evaluate('Fy*B'), axes=(1, 2))
I = np.tensordot(s, r, axes=(2, 2)).swapaxes(1, 2)
r = ne.evaluate('(P*I)**2')
r = np.sum(r, axis=(2, 3))
return r
fun4:0.007 s
此外,由于fun8不再是内存饥饿(由于智能的数字转换),你可以将更大的阵列相乘,并看到它使用多个内核。