Python/Numpy MemoryError [英] Python/Numpy MemoryError

查看:186
本文介绍了Python/Numpy MemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,尝试在numpy矩阵上执行代数运算时,我在python中遇到内存错误.变量u是一个大的double矩阵(在失败的情况下,它是288x288x156的double矩阵.在这种情况下,我只会出现此错误,但是我可以在其他大型矩阵上执行此操作,但不是那么大) ).这是Python错误:

Basically, I am getting a memory error in python when trying to perform an algebraic operation on a numpy matrix. The variable u, is a large matrix of double (in the failing case its a 288x288x156 matrix of doubles. I only get this error in this huge case, but I am able to do this on other large matrices, just not this big). Here is the Python error:

 Traceback (most recent call last):

 File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc
t perim erosion flattop\SwSim.py", line 121, in __init__
   self.mainSimLoop()

 File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc
t perim erosion flattop\SwSim.py", line 309, in mainSimLoop
   u = solver.solve_cg(u,b,tensors,param,fdHold,resid) # Solve the left hand si
de of the equation Au=b with conjugate gradient method to approximate u

 File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc
t perim erosion flattop\conjugate_getb.py", line 47, in solv
e_cg

u = u + alpha*p

MemoryError

u = u + alpha*p是失败的代码行.

alpha只是双精度,而ur是上述大型矩阵(大小相同).

alpha is just a double, while u and r are the large matrices described above (both of the same size).

我对内存错误了解不多,尤其是在Python中.任何有识之士/解决这个问题的技巧将不胜感激!

I don't know that much about memory errors especially in Python. Any insight/tips into solving this would be very appreciated!

谢谢

推荐答案

重写为

p *= alpha
u += p

,这将使用更少的内存.而p = p*alphap*alpha的结果分配一个全新的矩阵,然后丢弃旧的pp*= alpha就地做同样的事情.

and this will use much less memory. Whereas p = p*alpha allocates a whole new matrix for the result of p*alpha and then discards the old p; p*= alpha does the same thing in place.

通常,对于大型矩阵,请尝试使用op=分配.

In general, with big matrices, try to use op= assignment.

这篇关于Python/Numpy MemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆