当使用牛顿克雷洛夫时,期望的方阵

时间:2019-05-26 07:42:39

标签: python numpy optimization scipy

我指的是*拟牛顿(https://en.wikipedia.org/wiki/Quasi-Newton_method)工具箱     * scipy.optimize.newton_krylov,这是一个寻根求解器,不一定是最小化器     * https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.newton_krylov.html

scipy.optimize.newton_krylov我遇到类似的错误:

我的代码如下

def eval_with_power(input_path):
    pkl_ids = os.listdir(input_path)
    N = len(pkl_ids)
    x0 = np.ones(N)
    x_star = newton_krylov(F, x0, iter=None, rdiff=None, method='lgmres', inner_maxiter=20, inner_M=None, outer_k=10, verbose=False, maxiter=None, f_tol=None, f_rtol=None, x_tol=None, x_rtol=None, tol_norm=None, line_search='armijo', callback=None)
    return x_star

# F returns a scalar, yet for sure it can also return np.arrary([.]) but same error
def F(x):
    return DetectionBenchmark.compute_aps_arr_from_positive_cache_with_power(input_path, x)['nmsth_0.3'][0.5]['mean_max_f1']

if __name__ == '__main__':
    input_path = '...'
    x_star = eval_with_power(input_path)
    print("The optimal powers are ", x_star, ", respectively.")
    print("The optimal F score is ", F(x_star))

我的错误如下。

python eval_with_power.py 
Traceback (most recent call last):
  File "eval_with_power.py", line 23, in <module>
    x_star = eval_with_power(input_path)
  File "eval_with_power.py", line 14, in eval_with_power
    x_rtol=None, tol_norm=None, line_search='armijo', callback=None)
  File "<string>", line 8, in newton_krylov
  File "/home/ubuntu/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/scipy/optimize/nonlin.py", line 310, in nonlin_solve
dx = -jacobian.solve(Fx, tol=tol)
  File "/home/ubuntu/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/scipy/optimize/nonlin.py", line 1465, in solve
sol, info = self.method(self.op, rhs, tol=tol, **self.method_kw)
  File "/home/ubuntu/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/scipy/sparse/linalg/isolve/lgmres.py", line 125, in lgmres
A,M,x,b,postprocess = make_system(A,M,x0,b)
  File "/home/ubuntu/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/scipy/sparse/linalg/isolve/utils.py", line 69, in make_system
raise ValueError('expected square matrix, but got shape=%s' % (A.shape,))
ValueError: expected square matrix, but got shape=(1, 1705)

触发错误的地方:

34 def make_system(A, M, x0, b):
35     """Make a linear system Ax=b
36 
37     Parameters
38     ----------
39     A : LinearOperator
40         sparse or dense matrix (or any valid input to aslinearoperator)
41     M : {LinearOperator, Nones}
42         preconditioner
43         sparse or dense matrix (or any valid input to aslinearoperator)
44     x0 : {array_like, None}
45         initial guess to iterative method
46     b : array_like
47         right hand side
48 
49     Returns
50     -------
51     (A, M, x, b, postprocess)
52         A : LinearOperator
53             matrix of the linear system
54         M : LinearOperator
55             preconditioner
56         x : rank 1 ndarray
57             initial guess
58         b : rank 1 ndarray
59             right hand side
60         postprocess : function
61             converts the solution vector to the appropriate
62             type and dimensions (e.g. (N,1) matrix)
63 
64     """
65     A_ = A
66     A = aslinearoperator(A)
67     ipdb.set_trace()
68     if A.shape[0] != A.shape[1]:
69         raise ValueError('expected square matrix, but got shape=%s' % (A.shape,))
70 

在另一个类似的问题中指出,“ Krylov方法要求第一个参数(在您的情况下为函数F(x))为方阵”(Scipy Newton Krylov Expects Square Matrix),但我想问为什么吗?

有指针吗?

0 个答案:

没有答案