scipy linalg deterministic/non-deterministic 代码
scipy linalg deterministic/non-deterministic code
我运行使用下面的代码从scipy
中使用这个SVD求解器:
import numpy as np
from scipy.sparse.linalg import svds
features = np.arange(9,dtype=np.float64).reshape((3,3))
for i in range(10):
_,_,V = svds(features,2)
print i,np.mean(V)
我希望打印的平均值每次都相同,但是它会发生变化并且似乎会循环显示一些最喜欢的值。由于 optimisation/random 播种级别低,我很乐意接受这种行为。
我不太明白为什么每次我 运行 该脚本时它都会以相同的顺序输出相同的值。对我来说,这似乎是半确定性和半非确定性的。
这个问题正在影响一些更复杂的处理,如果能理解它就好了,这样我至少可以做一些 hacky 解决方法。
我自己没有测试(现在在没有 Python shell 的平板电脑上),我相信这是由于与近似值使用的初始化起点相关的一些奇怪的行为svds
最终调用的特征求解器库 ARPACK。
如果您在第一次调用 svds
, v0
(the starting point in question) is handled only in _ArpackParams
, where it's set to zeros and the info
parameter set to 0
if v0 is None
; otherwise, v0
is kept as its value and info
is 1
. Then we go into the realm of dragons Fortran, calling (if the matrix is doubles) the function dsaupd
, which I didn't fully check but I'm assuming eventually calls cgetv0
when a random starting point is requested. This function appears to initialize the LAPACK RNG seed to 1357 时遵循 Python 代码。
因此,如果您不对 ARPACK 进行任何其他调用(或者可能是其他 LAPACK 事物,不确定它们如何相互作用),那么您每次都会使用相同的种子启动 RNG,从而得到每次都是相同的初始化点;因此,假设这是算法中唯一的随机性来源,您每次都会得到相同的答案序列。
您可以通过在代码开头随机调用一个小矩阵的 eigs
来解决这个问题。
我运行使用下面的代码从scipy
中使用这个SVD求解器:
import numpy as np
from scipy.sparse.linalg import svds
features = np.arange(9,dtype=np.float64).reshape((3,3))
for i in range(10):
_,_,V = svds(features,2)
print i,np.mean(V)
我希望打印的平均值每次都相同,但是它会发生变化并且似乎会循环显示一些最喜欢的值。由于 optimisation/random 播种级别低,我很乐意接受这种行为。
我不太明白为什么每次我 运行 该脚本时它都会以相同的顺序输出相同的值。对我来说,这似乎是半确定性和半非确定性的。
这个问题正在影响一些更复杂的处理,如果能理解它就好了,这样我至少可以做一些 hacky 解决方法。
我自己没有测试(现在在没有 Python shell 的平板电脑上),我相信这是由于与近似值使用的初始化起点相关的一些奇怪的行为svds
最终调用的特征求解器库 ARPACK。
如果您在第一次调用 svds
, v0
(the starting point in question) is handled only in _ArpackParams
, where it's set to zeros and the info
parameter set to 0
if v0 is None
; otherwise, v0
is kept as its value and info
is 1
. Then we go into the realm of dragons Fortran, calling (if the matrix is doubles) the function dsaupd
, which I didn't fully check but I'm assuming eventually calls cgetv0
when a random starting point is requested. This function appears to initialize the LAPACK RNG seed to 1357 时遵循 Python 代码。
因此,如果您不对 ARPACK 进行任何其他调用(或者可能是其他 LAPACK 事物,不确定它们如何相互作用),那么您每次都会使用相同的种子启动 RNG,从而得到每次都是相同的初始化点;因此,假设这是算法中唯一的随机性来源,您每次都会得到相同的答案序列。
您可以通过在代码开头随机调用一个小矩阵的 eigs
来解决这个问题。