当您想与目标函数一起计算梯度时,如何使用 scipy.optimize.minimize 函数? [英] How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function?

查看:60
本文介绍了当您想与目标函数一起计算梯度时,如何使用 scipy.optimize.minimize 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scipy.optimize.minimzeobjjac 函数作为输入.我相信它会在需要时分别调用它们.但我们经常会遇到目标函数,其梯度计算共享来自目标函数的大量计算.所以理想情况下,我想同时计算 objgrad .但是这个库似乎不是这种情况?如果有的话,如果仍然想使用 scipy.optimize.minimze 有什么办法来处理?

scipy.optimize.minimze takes obj and jac functions as input. and I believe it will call them separately as and when needed. But more often than not we come across objective functions whose gradient computation shares a lot of computations from the objective function. So ideally I would like to compute the obj and grad simultaneously. But this doesn't seem to be the case with this library? What is the way to deal with it if one still wants to use scipy.optimize.minimze if at all there is?

推荐答案

你完全可以.只需使用 jac=True:

You totally can. Just use jac=True:

In [1]: import numpy as np

In [2]: from scipy.optimize import minimize

In [3]: def f_and_grad(x):
   ...:     return x**2, 2*x
   ...: 

In [4]: minimize(f_and_grad, [1], jac=True)
Out[4]: 
      fun: 1.8367099231598242e-40
 hess_inv: array([[ 0.5]])
      jac: array([  2.71050543e-20])
  message: 'Optimization terminated successfully.'
     nfev: 4
      nit: 2
     njev: 4
   status: 0
  success: True
        x: array([  1.35525272e-20])

它实际上是记录:

jac : bool 或 callable, 可选的 Jacobian (gradient) 目标功能.仅适用于 CG、BFGS、Newton-CG、L-BFGS-B、TNC、SLSQP、狗腿、信任-ncg.如果 jac 是布尔值且为 True,则假定 fun 返回梯度和目标函数. 如果为 False,梯度将进行数值估算.jac 也可以是一个可调用的返回目标的梯度.在这种情况下,它必须接受相同的争论很有趣.

jac : bool or callable, optional Jacobian (gradient) of objective function. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg. If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function. If False, the gradient will be estimated numerically. jac can also be a callable returning the gradient of the objective. In this case, it must accept the same arguments as fun.

(强调我的)

这篇关于当您想与目标函数一起计算梯度时,如何使用 scipy.optimize.minimize 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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