gpt4 book ai didi

python - 当你想计算梯度和目标函数时,如何使用 scipy.optimize.minimize 函数?

转载 作者:太空狗 更新时间:2023-10-29 22:12:45 24 4
gpt4 key购买 nike

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

最佳答案

你完全可以。只需使用 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])

实际上是documented :

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.

(强调我的)

关于python - 当你想计算梯度和目标函数时,如何使用 scipy.optimize.minimize 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37734430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com