scipy 最小化函数的输入结构 [英] Structure of inputs to scipy minimize function

查看:20
本文介绍了scipy 最小化函数的输入结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些试图使用 scipy.optimize.minimize 最小化函数的代码.我无法理解 funjac 参数

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 如下所示:

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

jac_func 结构如下:

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

我不明白funcjac_funcG 输入来自哪里.这是在 minimize 函数中以某种方式指定的,还是 method 被指定为 TNC 的事实?我试图对这个优化函数的结构进行一些研究,但我无法找到我需要的答案.非常感谢任何帮助

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 由优化器维护作为最小化过程的一部分,而 (D_neg,D, 和 C) 参数按原样从 args 元组传入.

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.

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

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.

但是如果你碰巧有一个函数 fun(x, y) 有一些额外的参数 y 需要单独传入(但被认为是一个为优化目的而保持不变)?这就是 args 元组的用途.文档 尝试解释如何使用 args 元组,但可能有点难以解析:

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:

参数:元组,可选

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

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

实际上,scipy.optimize.minimize 将使用星号参数将 args 中的任何内容作为参数的其余部分传递给 fun表示法:然后在优化过程中将该函数称为 fun(x, *args).x 部分由优化器传入,args 元组作为剩余参数给出.

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.

因此,在您的代码中,G 元素的值由优化器维护,同时评估 G 的可能值,而 (D_neg, D, C) 元组按原样传入.

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.

这篇关于scipy 最小化函数的输入结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆