Python scipy 最小化函数的输入结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19843752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:47:31  来源:igfitidea点击:

Structure of inputs to scipy minimize function

pythonnumpyscipyminimize

提问by sedavidw

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 funand jacarguments

我继承了一些试图使用scipy.optimize.minimize. 我无法理解funjac参数的一些输入

The call to minimize looks something like this:

最小化调用看起来像这样:

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

funclooks like the following:

func如下所示:

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

jac_funchas the following structure:

jac_func具有以下结构:

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

What I don't understand is where the G input to funcand jac_funcis coming from. Is that somehow specified in the minimizefunction, or by the fact that the methodis 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

我不明白的是 G 输入的来源funcjac_func来源。这是在minimize函数中以某种方式指定的,还是method被指定为TNC?我试图对这个优化函数的结构进行一些研究,但我无法找到我需要的答案。任何帮助是极大的赞赏

采纳答案by lmjohns3

The short answer is that Gis 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 argstuple.

简短的回答是G优化器将其作为最小化过程的一部分进行维护,而(D_neg, D, and C)参数则按原样从args元组传入。

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

默认情况下,scipy.optimize.minimize接受一个函数fun(x),该函数接受一个参数x(可能是一个数组等)并返回一个标量。scipy.optimize.minimize然后找到一个参数值xp,该fun(xp)值小于fun(x)的其他值x。优化器负责创建 的值x并将它们传递给fun评估。

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

但是,如果您碰巧有一个函数fun(x, y),该函数具有一些y需要单独传入的附加参数(但出于优化的目的被视为常量),该怎么办?这就是args元组的用途。该文档试图解释如何使用 args 元组,但解析起来可能有点困难:

args: tuple, optional

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

args:元组,可选

传递给目标函数及其导数(Jacobian、Hessian)的额外参数。

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

有效地,scipy.optimize.minimize将使用星号参数符号将任何args作为参数的其余部分传递给fun:然后fun(x, *args)在优化期间调用该函数。该x部分由优化器传入,args元组作为剩余参数给出。

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

因此,在您的代码中,G优化器在评估 的可能值时维护元素的值G,并且(D_neg, D, C)元组按原样传递。