“牛顿-CG方法需要雅可比"当 jac=False 时不使用雅可比行列式的近似值? [英] "Jacobian is required for Newton-CG method" when doing a approximation to a Jacobian not being used when jac=False?

查看:162
本文介绍了“牛顿-CG方法需要雅可比"当 jac=False 时不使用雅可比行列式的近似值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scipy.optimize.minimize 进行优化,并尝试使用以下方法:Newton-CG"、dogleg"和trust-ncg".据我了解,对于这些方法,需要目标函数的雅可比.但是,文档 建议如果 jac 设置为 False,则梯度将以数值方式计算.

I'm doing an optimization with scipy.optimize.minimize, and attempting to use the following methods: 'Newton-CG', 'dogleg', and 'trust-ncg'. As I understand, for these methods a jacobian of the objective function is needed. However, the documentation suggests that if jac is set to False, that the gradient will be computed numerically.

所以我试图像这样调用函数:

So I'm trying to call the function like so:

scipy.optimize.minimize(fun,x0,method='Newton-CG',jac=False,options={'disp':True}

当我调用它时,我收到以下错误消息:

When I call this, I get the following error message:

  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 1351, in _minimize_newtoncg
raise ValueError('Jacobian is required for Newton-CG method')

这很令人惊讶,因为我以为我只是将它设置为 False(如果 jac 设置为 None,则此异常仅发生在 */optimize.py 中).于是我进入/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py 看看函数

That's surprising, since I thought I just set it to False (and this exception only occurs in */optimize.py if jac is set to None). So I go into /usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py and look at the function

def _minimize_newtoncg(fun, x0, args=(), jac=None, hess=None, hessp=None,
                   callback=None, xtol=1e-5, eps=_epsilon, maxiter=None,
                   disp=False, return_all=False,
                   **unknown_options):

在这个函数的开头我写了以下打印语句:

At the beginning of this function I write the following print statements:

print (jac)
_check_unknown_options(unknown_options)
print(jac)
if jac is None:
    raise ValueError('Jacobian is required for Newton-CG method')

令人惊讶的是,'None' 被打印出来而不是 False!所以我查看了/usr/lib/python2.7/dist-packages/scipy/optimize/_minimize.py 中的调用函数,我找到了将其设置为 None 的代码片段:

Surprisingly, 'None' is printed and not False! So I look at the calling function, which is in /usr/lib/python2.7/dist-packages/scipy/optimize/_minimize.py, and I find the code snippet that is setting this to None:

    if not callable(jac):
    if bool(jac):
        fun = MemoizeJac(fun)
        jac = fun.derivative
    else:
        jac = None

所以 jac 被设置为 None 是有道理的(尽管它似乎与文档不兼容,该文档表明我将通过在原始函数调用中将 jac 设置为 False 来获得 jacobian 的数值近似值).

So that makes sense why jac is being set to None (though it seems incompatible with the documentation that suggests I'm going to get a numerical approximation to the jacobian by setting jac to False in the original function call).

我错过了什么?我是否可以像上面那样使用 Scipy 为我计算雅可比矩阵的数值近似值来调用牛顿-CG"方法?

What am I missing? Is it possible for me to call the 'Newton-CG' method like I'm doing above with Scipy computing the numerical approximation for the Jacobian for me?

推荐答案

显然这个 bug 仍然存在,三年后.

Apparently the bug is still there, three years later.

对于 Newton-CG,最小化器只需要一个可调用的雅可比行列式.获得一个的快速方法是使用 scipy.optimize.approx_fprime 如下:

For Newton-CG, the minimizer only takes a callable Jacobian. A quick way of obtaining one is to use scipy.optimize.approx_fprime as follows:

# x0 is your initial guess.
fprime = lambda x: optimize.approx_fprime(x, f, 0.01)
result = optimize.minimize(f, x0, method = 'Newton-CG', jac = fprime)

据我所知,这应该是两点"方法的实现方式.

As my understanding this should be how "2-point" method is implemented.

这篇关于“牛顿-CG方法需要雅可比"当 jac=False 时不使用雅可比行列式的近似值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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