matlab - Suppyling a hessian to fmin_ncg in python -
for scipy function fmin_ncg, there way of suppling hessian , gradient variable rather function?
i'm trying rewrite matlab code in python. code involves using optimisation routine fit parameters set of data. this, i've supplied gradient , hessian. e.g in matlab have this:
fmincon(@myfunc,x0,[],[],[],[],lb,ub,[],options); where myfunc returns 3 values: function evaluation, gradient, , hessian.
however fmin_ncg in python appears gradiant , hessian must supplied separate functions.
to me seems inefficient code has go through large dataset, , there calculations common function, gradient , hessian. e.g. imagine function f(x) = a(x)*b(x) gradient g(x) = a(x)*c(x), hessian h(x) = a(x)*d(x) ... in matlab can calculate a(x) once, appears have calculate 3 times in python.
have misunderstood how fmin_ncg works or there way around this?
you can create class includes of functions. each iteration, common variables calculated during first function call, reused other calls. callback feature of fmin_ncg can used reset common variables @ end of each iteration.
class function(object): def __init__(self): self.commonvarsdirty = true def calcfunction(self,x,*args,**kwargs): if self.commonvarsdirty: self.calccommonvars() return self.a*b def calcgradient(self,x,*args,**kwargs): if self.commonvarsdirty: self.calccommonvars() return self.a*c def calchessian(self,x,*args,**kwargs): if self.commonvarsdirty: self.calccommonvars() return self.a*d def resetcommonvars(self,*args,**kwargs): self.commonvarsdirty = true def calccommonvars(self): self.commonvarsdirty = false # calculate common variables , save them class attributes self.a = 1+1 you use this.
f = function() fmin_ncg(f.calcfunction,x0,f.calcgradient,fhess=f.calchessian,callback=f.resetcommonvars) this adds overhead worth if computation effort calculate common variables significant.
Comments
Post a Comment