Computing Jacobian And Passing to Scipy Minimize

眉间皱痕 提交于 2020-01-15 10:39:27

问题


I know the jacobian is the first derivative, but I don't know how to compute it for my simple function (I tried online derivative calculators) and pass it to my scipy minimize function.

In code, here is the objective function (guess arrays can contain thousands of variables):

def objective(current_guesses_array, first_guesses_array):
        return np.sum(np.divide(np.square(current_guesses_array - first_guesses_array), first_guesses_array))

I think the Jacobian is like this, but definitely may have messed up here:

dx = 2x/y - 2

dy = 1 - (x^2/y^2)

In code:

def jacobian_for_minimize(self, x,a):
    dx = (2*x)/a - 2
    dy = 1 - (np.square(x) / np.square(a))
    return np.array([dx,dy])

minimize(objective,initial_guesses,args=initial_guesses,jac=jacobian_for_minimize,method='SLSQP')

After calling minimize, I get an error: _slsqp.error: failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array

According to another Stack overflow page, it means the objective function isn't returning a scalar, and it has to.

Can anyone tell where I went wrong with this?

来源:https://stackoverflow.com/questions/54641003/computing-jacobian-and-passing-to-scipy-minimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!