【问题标题】:Have memory error when using scipy to do SVD使用 scipy 进行 SVD 时出现内存错误
【发布时间】:2011-12-27 20:53:31
【问题描述】:

我尝试使用 LSI 生成向量来表示文档。我在 Scipy 库中使用 svd 包。但是程序会抛出内存错误。我的矩阵大小是 100*13057。这对我的 8G RAM 来说太大了吗?

我在 stackflow 中搜索了这个问题。有人说我只需要在我的 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 )

Concole 中的消息如下:

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

【问题讨论】:

    标签: python scipy svd


    【解决方案1】:

    如果您使用默认的dtype=np.float,您的V 矩阵将占用13057*13057*8 字节的内存,这大约是。 1.4GB。我的直觉是,这对于您的 32 位 Python 来说太大了。尝试使用 32 位浮点数,即 dtype=np.float32,将内存使用量减少一半,或者开始使用 scipy.sparse(对于信息检索问题几乎总是一个好主意)。

    【讨论】:

    • 谢谢!我这样做: from scipy.sparse import dok_matrix
      dok = dok_matrix(raw_matrix)
      matrix = compute_svd( dok )
      我得到另一个例外。
    • Traceback(最近一次调用最后一次):
      文件“D:\workspace\PyQuEST\src\Practice\baseline_lsi.py”,第 96 行,在
      矩阵中= compute_svd( dok )
      文件 "D:\workspace\PyQuEST\src\Practice\svd.py",第 12 行,在 compute_svd
      U, s, V = linalg.svd( 矩阵 )
      文件“D:\Program\Python26\lib\site-packages\scipy\linalg\decomp_svd.py”,第 79 行,svd full_matrices=full_matrices, overwrite_a = overwrite_a)
      TypeError: float() argument must是字符串或数字
    • @ChongWang: scipy.linalg.svd 不能神奇地与 scipy.sparse 矩阵一起使用。试试scipy.sparse.linalg.svds
    • 我在使用 scipy.sparse 时仍然遇到异常。我打开了一个新问题。我希望你能帮助我,拉斯曼斯。 stackoverflow.com/questions/8650014/…
    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2020-06-14
    • 2015-12-10
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多