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
Structure of inputs to scipy minimize function
提问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 fun
and jac
arguments
我继承了一些试图使用scipy.optimize.minimize
. 我无法理解fun
和jac
参数的一些输入
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:
func
如下所示:
def func(G, D_neg, D, C):
#do stuff
jac_func
has 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 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
我不明白的是 G 输入的来源func
和jac_func
来源。这是在minimize
函数中以某种方式指定的,还是method
被指定为TNC
?我试图对这个优化函数的结构进行一些研究,但我无法找到我需要的答案。任何帮助是极大的赞赏
采纳答案by lmjohns3
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.
简短的回答是G
优化器将其作为最小化过程的一部分进行维护,而(D_neg, D, and C)
参数则按原样从args
元组传入。
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.
默认情况下,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 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 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.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.
有效地,scipy.optimize.minimize
将使用星号参数符号将任何args
作为参数的其余部分传递给fun
:然后fun(x, *args)
在优化期间调用该函数。该x
部分由优化器传入,args
元组作为剩余参数给出。
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.
因此,在您的代码中,G
优化器在评估 的可能值时维护元素的值G
,并且(D_neg, D, C)
元组按原样传递。