使用scipy进行SVD时出现内存错误
我尝试使用LSI来生成向量,以表示文档。我正在使用Scipy库中的svd包。但是程序出现了内存错误。我的矩阵大小是100*13057。这对我8G的内存来说是不是太大了?
我在StackOverflow上搜索了这个问题。有人说我只需要在64位的操作系统上安装64位的Python。(现在,我在64位的操作系统上使用的是32位的Python)。但是重新安装所有库太麻烦了。还有一种说法是转换稀疏矩阵。
那么大家对这个问题有什么想法吗?谢谢!
raw_matrix = []
for text in forest_lsi:
raw_matrix.append( text.get_vector() )
from svd import compute_svd
print("The size of raw matrix: "+str(len(raw_matrix))+" * "+str(len(raw_matrix[0])))
matrix = compute_svd( raw_matrix )
控制台中的信息如下:
The size of raw matrix: 100 * 13057
Original matrix:
[[1 1 2 ..., 0 0 0]
[0 3 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 1 ..., 0 0 0]
[0 0 2 ..., 1 1 3]]
Traceback (most recent call last):
File "D:\workspace\PyQuEST\src\Practice\baseline_lsi.py", line 93, in <module>
matrix = compute_svd( raw_matrix )
File "D:\workspace\PyQuEST\src\Practice\svd.py", line 12, in compute_svd
U, s, V = linalg.svd( matrix )
File "D:\Program\Python26\lib\site-packages\scipy\linalg\decomp_svd.py", line 79, in svd
full_matrices=full_matrices, overwrite_a = overwrite_a)
MemoryError
1 个回答
0
如果你使用默认的 dtype=np.float
,那么你的 V
矩阵会占用 13057*13057*8
字节的内存,大约是 1.4GB。我猜这对于你的 32 位 Python 来说太大了。试试使用 32 位浮点数,也就是 dtype=np.float32
,这样可以把内存使用量减半,或者开始使用 scipy.sparse
(对于信息检索问题,这几乎总是个好主意)。