带有固定种子的 scipy.sparse.linalg.eigsh [英] scipy.sparse.linalg.eigsh with fixed seed
问题描述
我正在尝试使用 scipy.sparse.linalg.eigsh 带有固定种子.
I am trying to use scipy.sparse.linalg.eigsh with fixed seed.
为此,我需要指定 v0 参数.但是,我无法弄清楚到底需要什么进入 v0,因为这里的文档非常少(它只是说 numpy.ndarray),而且错误消息对我来说没有任何信息.
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.
代码:
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)
错误:
错误:无法将 _arpack.dsaupd 的第 10 个参数workd"转换为C/Fortran 数组
error: failed in converting 10th argument `workd' of _arpack.dsaupd to C/Fortran array
推荐答案
从 eigsh
获得可重现结果的正确方法是:
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)
每次结果都一样.(感谢@hpaulj 的正确评论)
Same results everytime. (Credit to @hpaulj for the correct comment)
请注意,在不设置 v0
的情况下修复种子是不够的:
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)
每次都有不同的结果.
这篇关于带有固定种子的 scipy.sparse.linalg.eigsh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!