我想尝试不同的策略来将我的数据拟合到给定的模型中,并提供边界并提供分析雅可比行列以改进结果。我可以运行optimize.least_squares
,例如:
sol = optimize.least_squares(cost_function, x0=x0, args=(data, sys_path, configfile), loss='soft_l1', f_scale=0.1, max_nfev=500, jac=grad_V , bounds = param_bounds)
但是当我尝试使用使用相同参数和数据的任何可用方法进行优化时,我发现了几个错误(见下文):
method='L-BFGS-B'
:
sol = optimize.minimize(cost_function, x0=x0, args=(data, sys_path, configfile), method='L-BFGS-B', jac=grad_V, bounds = param_bounds)
[..] line 594, in fitting_model
sol = optimize.minimize(cost_function_vector, x0=x0, args=(data, sys_path, configfile), method='L-BFGS-B', jac=grad_V, bounds = param_bounds)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/_minimize.py", line 600, in minimize
callback=callback, **options)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py", line 267, in _minimize_lbfgsb
raise ValueError('length of x0 != length of bounds')
ValueError: length of x0 != length of bounds
- 如果我这样做,例如
np.transpose(param_bounds)
:
sol = optimize.minimize(cost_function, x0=x0, args=(data, sys_path, configfile), method='L-BFGS-B', jac=grad_V, bounds = np.transpose(param_bounds))
[...], line 594, in fitting_model
sol = optimize.minimize(cost_function_vector, x0=x0, args=(data, sys_path, configfile), method='L-BFGS-B', jac=grad_V, bounds = np.transpose(param_bounds))
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/_minimize.py", line 600, in minimize
callback=callback, **options)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py", line 328, in _minimize_lbfgsb
isave, dsave, maxls)
ValueError: too many axes: 2 (effrank=2), expected rank=1
同样,它适用于 least_squares,所以我想问题在于我如何传递参数(为什么它们与 least_squares 不同??)而不是在代码中。但是,按照找到的文档和示例,我无法理解错误在哪里。
我真的很感激这方面的任何帮助。提前致谢。
我正准备放代码、数据示例等,但为了避免让它太长,我认为以下信息就足够了。让我知道我是否错了并且需要其他东西。
有 5 个参数:
S0, d, theta, phi, f
. 这些和其他需要的参数(例如数组bvals
和bvecs
)是从 aconfigfile
和其他所需的函数中读取的sys_path
(传入args
)。cost_function
返回一个向量F
(data, Sj
因此F
是形状为 (65,) 的数组)。基本上,它是这样的:
def cost_function(data, sys_path, configfile):
[...]
v = get_v(theta,phi)
Sj = s0 * ((1 - f) * np.exp(-bvals * d) + f * np.exp(-bvals * d * np.transpose(np.square((np.transpose(bvecs) * v)))) ) ##vector v depends on theta and phi
F = Sj - data
return F
x0
(5 个参数的初始化)在形状 (5,) 的数组中提供。例如:array([ 1.46732834e+02, 1.00000000e-03, 6.81814424e-01, -2.07406186e-01, 1.27062049e+01])
下边界和上边界作为 param_bounds 提供,形状为 (2,5)。例如,
array([[ 0.0e+00, 1.0e-06, -1.0e+04, -1.0e+04, 0.001],
[ 1.0e+05, 1.0e-02, 1.0e+04, 1.0e+04, 1]])
grad_V
is 是一个用户定义的 jacobian 函数,它返回一个形状为 (65,5) 的数组,并且作为cost_function
,args=(data, sys_path, configfile)
作为输入参数。