[英]Python/Numpy MemoryError
Basically, I am getting a memory error in python when trying to perform an algebraic operation on a numpy matrix. 基本上,当我尝试在numpy矩阵上执行代数运算时,我在python中遇到内存错误。 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). 变量
u
,是一个双倍的大矩阵(在失败的情况下,它是一个288x288x156的双精度矩阵。我只在这个巨大的情况下得到这个错误,但我能够在其他大矩阵上做到这一点,只是不是这么大)。 Here is the Python error: 这是Python错误:
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
is the line of code that fails. u = u + alpha*p
是失败的代码行。
alpha
is just a double, while u
and r
are the large matrices described above (both of the same size). alpha
只是一个double,而u
和r
是上面描述的大矩阵(两者都是相同的大小)。
I don't know that much about memory errors especially in Python. 我对内存错误知之甚少,特别是在Python中。 Any insight/tips into solving this would be very appreciated!
任何有关解决此问题的见解/提示都将非常感激!
Thanks 谢谢
Rewrite to 改写为
p *= alpha
u += p
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 = p*alpha
为p*alpha
的结果分配一个全新的矩阵,然后丢弃旧的p
; p*= alpha
does the same thing in place. p*= alpha
做同样的事情。
In general, with big matrices, try to use op=
assignment. 通常,对于大矩阵,请尝试使用
op=
assignment。
Another tip I have found to avoid memory errors is to manually control garbage collection . 我发现避免内存错误的另一个技巧是手动控制垃圾收集 。 When objects are deleted or go our of scope, the memory used for these variables isn't freed up until a garbage collection is performed.
当删除对象或使用我们的范围时,在执行垃圾收集之前,不会释放用于这些变量的内存。 I have found with some of my code using large numpy arrays that I get a MemoryError, but that I can avoid this if I insert calls to gc.collect() at appropriate places.
我发现我的一些代码使用了大型的numpy数组,我得到了一个MemoryError,但是如果我在适当的地方插入对gc.collect()的调用,我可以避免这种情况。
You should only look into this option if using "op=" style operators etc doesn't solve your problem as it's probably not the best coding practice to have gc.collect() calls everywhere. 如果使用“op =”样式运算符等不能解决您的问题,您应该只考虑此选项,因为它可能不是最好的编码实践,让gc.collect()调用到处。
Your matrix has 288x288x156=12,939,264 entries, which for double
could come out to 400MB in memory. 你的矩阵有288x288x156 = 12,939,264个条目,其中
double
可以在内存中达到400MB。 numpy
throwing a MemoryError
at you just means that in the function you called the memory needed to perform the operation wasn't available from the OS. numpy
抛出一个MemoryError
就意味着在你调用的函数中,操作系统无法获得执行操作所需的内存。
If you can work with sparse matrices this might save you a lot of memory. 如果您可以使用稀疏矩阵,这可能会为您节省大量内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.