[英]MemoryError with large sparse matrices
For a project I have built a program that constructs large matrices. 对于一个项目,我构建了一个构建大型矩阵的程序。
def ExpandSparse(LNew):
SpId = ssp.csr_matrix(np.identity(MS))
Sz = MS**LNew
HNew = ssp.csr_matrix((Sz,Sz))
Bulk = dict()
for i in range(LNew-1):
for j in range(LNew-1):
if i == j:
Bulk[(i,j)]=H2
else:
Bulk[(i,j)]=SpId
Ha = ssp.csr_matrix((8,8))
try:
for i in range(LNew-1):
for j in range(LNew-2):
if j < 1:
Ha = ssp.csr_matrix(ssp.kron(Bulk[(i,j)],Bulk[(i,j+1)]))
else:
Ha = ssp.csr_matrix(ssp.kron(Ha,Bulk[(i,j+1)]))
HNew = HNew + Ha
except MemoryError:
print('The matrix you tried to build requires too much memory space.')
return
return HNew
This does the job, however it does not work as well as I would have expected. 这可以完成工作,但是效果不如我预期。 The problem is that it won't allow for really large matrices.
问题在于它不允许使用非常大的矩阵。 When
LNew
is larger than 13 I will get a MemoryError. 当
LNew
大于13时,我将收到MemoryError。 My experiences with numpy suggest that, memorywise, I should be able to get LNew
up to 18 or 19 before I get this error. 我对numpy的经验表明,从内存
LNew
,在出现此错误之前,我应该能够使LNew
达到18或19。 Does this have to do with my code, or with the way scipy.sparse.kron()
works with these matrices? 这与我的代码或
scipy.sparse.kron()
与这些矩阵的工作方式scipy.sparse.kron()
吗?
Another note that might be important is that I use Windows not Linux. 另一个可能很重要的注意事项是,我使用Windows而不是Linux。
After some more reading on the working of the scipy.sparse.kron()
function I have noticed that there is a third term named format you can enter. 在进一步阅读了
scipy.sparse.kron()
函数的工作后,我注意到可以输入第三个名为格式的术语。 The default setting is None, but when it is put on 'csr' or another supported format it will only use the sparse format making it a lot more efficient, now for me it can build a 2097152 x 2097152 matrix. 默认设置为“无”,但是当将其设置为“ csr”或其他受支持的格式时,它将仅使用稀疏格式,使其效率更高,现在对我而言,它可以构建2097152 x 2097152矩阵。 Here
LNew
is 21. 这里
LNew
是21。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.