Structure of inputs to scipy minimize function

我怕爱的太早我们不能终老 提交于 2019-11-26 15:46:46

问题


I have inherited some code that is trying to minimize a function using scipy.optimize.minimize. I am having trouble understanding some of the inputs to the fun and jac arguments

The call to minimize looks something like this:

result = minimize(func, jac=jac_func, args=(D_neg, D, C), method = 'TNC' ...other arguments)

func looks like the following:

def func(G, D_neg, D, C):
#do stuff

jac_func has the following structure:

def jac_func(G, D_neg, D, C):
#do stuff

What I don't understand is where the G input to func and jac_func is coming from. Is that somehow specified in the minimize function, or by the fact that the method is specified as TNC? I've tried to do some research into the structure of this optimization function but I'm having trouble finding the answer I need. Any help is greatly appreciated


回答1:


The short answer is that G is maintained by the optimizer as part of the minimization process, while the (D_neg, D, and C) arguments are passed in as-is from the args tuple.

By default, scipy.optimize.minimize takes a function fun(x) that accepts one argument x (which might be an array or the like) and returns a scalar. scipy.optimize.minimize then finds an argument value xp such that fun(xp) is less than fun(x) for other values of x. The optimizer is responsible for creating values of x and passing them to fun for evaluation.

But what if you happen to have a function fun(x, y) that has some additional parameter y that needs to be passed in separately (but is considered a constant for the purposes of the optimization)? This is what the args tuple is for. The documentation tries to explain how the args tuple is used, but it can be a little hard to parse:

args: tuple, optional

Extra arguments passed to the objective function and its derivatives (Jacobian, Hessian).

Effectively, scipy.optimize.minimize will pass whatever is in args as the remainder of the arguments to fun, using the asterisk arguments notation: the function is then called as fun(x, *args) during optimization. The x portion is passed in by the optimizer, and the args tuple is given as the remaining arguments.

So, in your code, the value of the G element is maintained by the optimizer while evaluating possible values of G, and the (D_neg, D, C) tuple is passed in as-is.



来源:https://stackoverflow.com/questions/19843752/structure-of-inputs-to-scipy-minimize-function

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