gpt4 book ai didi

python - scipy.optimize.minimize : compute hessian and gradient together

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:51 27 4
gpt4 key购买 nike

scipy.optimize.minimize 函数基本上相当于 MATLAB 的“fminunc”函数,用于查找函数的局部最小值。

在 scipy 中,梯度函数和 Hessian 函数是分开的。

res = minimize(rosen, x0, method='Newton-CG',
... jac=rosen_der, hess=rosen_hess,
... options={'xtol': 1e-30, 'disp': True})

但是,我有一个函数,其 Hessian 和梯度共享相当多的计算,为了提高效率,我想一起计算 Hessian 和梯度。在 fminunc 中,目标函数可以写成返回多个值,即:

function [ q, grad, Hessian ] = rosen(x)

有没有一种好的方法可以将函数传递给 scipy.optimize.minimize 来一起计算这些元素?

最佳答案

您可以寻求缓存解决方案,但首先 numpy 数组不可哈希,其次您只需要缓存一些值,具体取决于算法是否在 x 上来回多次。如果算法只从一个点移动到下一个点,你可以用这种方式只缓存最后一个计算点,你的 f_hesf_jac 只是一个更长的 lambda 接口(interface)函数计算:

import numpy as np

# I choose the example f(x,y) = x**2 + y**2, with x,y the 1st and 2nd element of x below:
def f(x):
return x[0]**2+x[1]**2

def f_jac_hess(x):
if all(x==f_jac_hess.lastx):
print('fetch cached value')
return f_jac_hess.lastf
print('new elaboration')
res = array([2*x[0],2*x[1]]),array([[2,0],[0,2]])

f_jac_hess.lastx = x
f_jac_hess.lastf = res

return res

f_jac_hess.lastx = np.empty((2,)) * np.nan

f_jac = lambda x : f_jac_hess(x)[0]
f_hes = lambda x : f_jac_hess(x)[1]

现在第二次调用将缓存保存的值:

>>> f_jac([3,2])
new elaboration
Out: [6, 4]
>>> f_hes([3,2])
fetch cached value
Out: [[2, 0], [0, 2]]

然后你称它为:

minimize(f,array([1,2]),method='Newton-CG',jac = f_jac, hess= f_hes, options={'xtol': 1e-30, 'disp': True})

关于python - scipy.optimize.minimize : compute hessian and gradient together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003543/

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