7

I am trying to apply SVD on my matrix (3241 x 12596) that was obtained after some text processing (with the ultimate goal of performing Latent Semantic Analysis) and I am unable to understand why this is happening as my 64-bit machine has 16GB RAM. The moment svd(self.A) is called, it throws an error. The precise error is given below:

Traceback (most recent call last):
  File ".\SVD.py", line 985, in <module>
    _svd.calc()
  File ".\SVD.py", line 534, in calc
    self.U, self.S, self.Vt = svd(self.A)
  File "C:\Python26\lib\site-packages\scipy\linalg\decomp_svd.py", line 81, in svd
    overwrite_a = overwrite_a)
MemoryError

So I tried using

self.U, self.S, self.Vt = svd(self.A, full_matrices= False)

and this time, it throws the following error:

Traceback (most recent call last):
  File ".\SVD.py", line 985, in <module>
    _svd.calc()
  File ".\SVD.py", line 534, in calc
    self.U, self.S, self.Vt = svd(self.A, full_matrices= False)
  File "C:\Python26\lib\site-packages\scipy\linalg\decomp_svd.py", line 71, in svd
    return numpy.linalg.svd(a, full_matrices=0, compute_uv=compute_uv)
  File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 1317, in svd
    work = zeros((lwork,), t)
MemoryError

Is this supposed to be such a large matrix that Numpy cannot handle and is there something that I can do at this stage without changing the methodology itself?

Legend
  • 113,822
  • 119
  • 272
  • 400
  • How much memory is Python using at the point it tries to compute the SVD? Are you running 32-bit or 64-bit Python? – Ferdinand Beyer Aug 22 '11 at 06:35
  • @Ferdinand Beyer: It is using 380 MB when it crashes. Ah! :( I am using a 32-bit Python. I will go ahead and install the 64-bit version. – Legend Aug 22 '11 at 06:42
  • @Ferdinand Beyer: You are a real savior! Somehow that point skipped my mind. It works perfectly fine now! Thank you very much. – Legend Aug 22 '11 at 07:13

2 Answers2

13

Yes, the full_matrices parameter to scipy.linalg.svd is important: your input is highly rank-deficient (rank max 3,241), so you don't want to allocate the entire 12,596 x 12,596 matrix for V!

More importantly, matrices coming from text processing are likely very sparse. The scipy.linalg.svd is dense and doesn't offer truncated SVD, which results in a) tragic performance and b) lots of wasted memory.

Have a look at the sparseSVD package from PyPI, which works over sparse input and you can ask for top K factors only. Or try scipy.sparse.linalg.svd, though that's not as efficient and only available in newer versions of scipy.

Or, to avoid the gritty details completely, use a package that does efficient LSA for you transparently, such as gensim.

Radim
  • 4,208
  • 3
  • 27
  • 38
2

Apparently, as it turns out, thanks to @Ferdinand Beyer, I did not notice that I was using a 32-bit version of Python on my 64-bit machine.

Using a 64-bit version of Python and reinstalling all the libraries solved the problem.

Legend
  • 113,822
  • 119
  • 272
  • 400