`f0` passed has more than 1 dimension. - fmin_l_bfgs_b

心已入冬 提交于 2021-01-28 11:48:17

问题


I want to find parameters which minimalize a function but I get an error. So far use scipy.optimize.fmin but I want to add bounds for every argument. This is my code

def Kou_calibration_full():
    i=0
    global opt
    p0 = spo.brute(Kou_error_function, ((0.10,0.31, 0.1),(0.01,2.6, 0.5), (0.1,0.92,0.2), (1.1,20,7),(0.1,20,7)), finish=None)
    opt = spo.minimize(Kou_error_function, p0, bounds=((0.10,0.31),(0.01,2.6), (0.1,0.92), (1.1,20),(0.1,20)))
    return opt

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<timed eval> in <module>

<ipython-input-127-e458cef75641> in Kou_calibration_full()
      3     global opt
      4     p0 = spo.brute(Kou_error_function, ((0.10,0.31, 0.1),(0.01,2.6, 0.5), (0.1,0.92,0.2), (1.1,20,7),(0.1,20,7)), finish=None)
----> 5     opt = spo.minimize(Kou_error_function, p0, bounds=((0.10,0.31),(0.01,2.6), (0.1,0.92), (1.1,20),(0.1,20)))
      6     return opt

~\anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    615                                   **options)
    616     elif meth == 'l-bfgs-b':
--> 617         return _minimize_lbfgsb(fun, x0, args, jac, bounds,
    618                                 callback=callback, **options)
    619     elif meth == 'tnc':

~\anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, finite_diff_rel_step, **unknown_options)
    304             iprint = disp
    305 
--> 306     sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
    307                                   bounds=new_bounds,
    308                                   finite_diff_rel_step=finite_diff_rel_step)

~\anaconda3\lib\site-packages\scipy\optimize\optimize.py in _prepare_scalar_function(fun, x0, jac, args, bounds, epsilon, finite_diff_rel_step, hess)
    259     # ScalarFunction caches. Reuse of fun(x) during grad
    260     # calculation reduces overall function evaluations.
--> 261     sf = ScalarFunction(fun, x0, args, grad, hess,
    262                         finite_diff_rel_step, bounds, epsilon=epsilon)
    263 

~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in __init__(self, fun, x0, args, grad, hess, finite_diff_rel_step, finite_diff_bounds, epsilon)
     93 
     94         self._update_grad_impl = update_grad
---> 95         self._update_grad()
     96 
     97         # Hessian Evaluation

~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in _update_grad(self)
    169     def _update_grad(self):
    170         if not self.g_updated:
--> 171             self._update_grad_impl()
    172             self.g_updated = True
    173 

~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in update_grad()
     89                 self._update_fun()
     90                 self.ngev += 1
---> 91                 self.g = approx_derivative(fun_wrapped, self.x, f0=self.f,
     92                                            **finite_diff_options)
     93 

~\anaconda3\lib\site-packages\scipy\optimize\_numdiff.py in approx_derivative(fun, x0, method, rel_step, abs_step, f0, bounds, sparsity, as_linear_operator, args, kwargs)
    386         f0 = np.atleast_1d(f0)
    387         if f0.ndim > 1:
--> 388             raise ValueError("`f0` passed has more than 1 dimension.")
    389 
    390     if np.any((x0 < lb) | (x0 > ub)):

ValueError: `f0` passed has more than 1 dimension.

Can anyone help? I don't know what to do :(

(I have too much code so I have to add some text: abcdabcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd


回答1:


Maybe it's due to the dimension of the return matrix of your Kou_error_function function, sample add .ravel() or .flatten() to the end of function return. Like:

def Kou_error_function(x,obs,arg):
    err = x*arg-obs
    return y.ravel()


来源:https://stackoverflow.com/questions/63783638/f0-passed-has-more-than-1-dimension-fmin-l-bfgs-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!