scipy.sparse.linalg.eigsh with fixed seed

Viewed 690

I am trying to use scipy.sparse.linalg.eigsh with fixed seed.

In order to that, I need to specify the v0 parameter. However, I am unable to figure out what exactly needs to go into v0, as the documentation is very meagre here (it merely says numpy.ndarray) and the error message is not informative for me.

Code:

import numpy as np
import scipy.sparse.linalg

A = scipy.sparse.rand(10,10)
# v0 = np.random.rand(10,10)
v0 = np.random.rand(10,5)
w, v = scipy.sparse.linalg.eigsh(A, k=5, v0=v0)

Error:

error: failed in converting 10th argument `workd' of _arpack.dsaupd to C/Fortran array

2 Answers

First of all the documentation do not mention anywhere that parameter v0 has to do anything with seed. It says

v0 : ndarray, optional Starting vector for iteration. Default: random

And from my naive understanding its the initial vector when it starts to find eigenvalues and eigenvectors it takes this parameter v0 as initial vector to start from, now to the seed thing, we use seed to fix the numbers generated for these vectors. So your question really doesn't make sense. Even if you get this program running, you will have different results and to avoid that we use seed to make the results reproducible.

Again, I might be wrong here.

Secondly if you want to fix the seed for your method I suggest using numpy to fix the seed as scipy uses numpy to generate random numbers.

So the code will look something like this

import numpy as np 
np.random.seed(seed= 13)

and then if parameter v0 is seed you can avoid it completely

w, v = scipy.sparse.linalg.eigsh(A, k=5)

Again I could have posted this in comments but it is always better to put in some code to make your point clear.

P.S

I may have misunderstood your question, if that is the case please feel free to downvote.

The correct way to get reproducible results from eigsh is:

import numpy as np
import scipy.sparse.linalg

np.random.seed(0)
A = scipy.sparse.rand(10,10)
v0 = np.random.rand(min(A.shape))
w, v = scipy.sparse.linalg.eigsh(A, k=5, v0=v0)

Same results everytime. (Credit to @hpaulj for the correct comment)

Note that fixing the seed without setting v0 is not sufficient:

np.random.seed(0)
A = scipy.sparse.rand(10,10)
w, v = scipy.sparse.linalg.eigsh(A, k=5)

Different results every time.

Related