In my problem, I need to update the args value inside the cost function, but the args is a function argument and also has the tuple structure. I was wondering is there a way to change the element of args and update it to use it by jac function? For example, in the below codes
paraList = [detValVec, projTrans, MeasVec,
coeMat, resVec, absCoeRec]
res = optimize.minimize(costFunc, x0, args=(paraList,), method='BFGS', jac=gradientFunc, options={'gtol': 1e-6, 'disp': True})
def costFunc(x0,arg):
para = list(arg)
para[3], para[4], para[5] = forwardModelFunc(para[0], para[1], para[2])
return para[5]
I would like to update para[3], para[4], para[5] in the args argument.
In order to minimize
costFuncyou must be able to vary the input parameters (otherwise it'll always have the same value!). Theoptimize.minimizefunction will vary ("update") thexbut it will leaveargsuntouched as it callscostFunc, which means that yourparaListshould really be given asx, notargs.Since
costFuncdepends only on the first three values in your parameter listpara[:3], updating the last threepara[3:]will have no effect, so you can usex = para[:3]andargs = para[3:]. In fact, you don't even needargsat all, since it has no effect.Something like:
So the optimal result you'll get (returned in
res.x) will be the best values for the first three parameters inparaList:detValVec,projTrans, andMeasVec. If you want to get the last three values that they imply, you can just callforwardModelFunconres.x:Of course, it's important to understand the limitations of
optimize.minimize: it can only minimize over an arrayxif it is a 1d array of scalars, so hopefully the values in yourparamListare scalars. If not, you'll have to flatten and concatenate them. Also, it will pass the samexandargsto the jacobiangradientFunc, so be sure that it is properly formatted as well.