I have as an assignment to use the 'trust-ncg'
method of scipy.opt.minimize
.
I have given the outputs of my function f(x,y)
. I manually find the Jacobian in a function. So, also the Hessian. Simplified, it is:
def foo(params):
x,y = params
return x*x+y
def df(params): # jacobian
x,y = params
return np.array([2*x, 1])
def hf(params): # hessian
x,y = params
return np.array([[2, 0], [0, 0])
Then, my attempt is: res = opt.minimize(foo, [1, 1], jac=df, hess=hf, method='trust-ncg', tol=1e-10)
I receive: ValueError: shapes (2,11) and (2,11) not aligned: 11 (dim 1) != 2 (dim 0)
.
I was playing with the tolerance, as in https://www.programcreek.com/python/example/57330/scipy.optimize.minimize. If I set the tolerance to 1, I would do only 1 iteration, but then I would simply return a straight line, which is not my goal. I also tried to change the order of the arguments of the function, which had no effect as expected. I tried other versions of the hessian, as 1d array, but it did not help me. I also tried the jacobian to be of the type [[0], [1]]. Again, no effect.
How to properly call scipy opt minimize with trust-ncg?