scipy.optimize.fmin_bfgs单个函数同时计算f和fprime [英] scipy.optimize.fmin_bfgs single function computes both f and fprime
问题描述
我正在使用scipy.optimize.fmin_bfgs(f, init_theta, fprime)
最小化具有渐变fprime
的f
.我在单个函数中计算f
和fprime
,因为大多数计算是相同的,因此无需重复两次.
I'm using scipy.optimize.fmin_bfgs(f, init_theta, fprime)
to minimize f
, which has gradient fprime
. I compute f
and fprime
in a single function because most of the computation is the same so there's no need to do it twice.
是否有任何方法可以调用fmin_bfgs()
并指定一个同时返回f
和fprime
的单个函数?
Is there any way to call fmin_bfgs()
specifying a single function that returns both f
and fprime
?
推荐答案
如果您要节省计算时间,而不仅仅是为了代码方便而组合f
和f'
的计算,那么您似乎需要函数周围有一个额外的包装器来缓存值,因为fmin_bfgs
似乎不允许您传递这样的函数(与其他一些优化函数不同).
If you're trying to save on computation time rather than just combine the calculation of f
and f'
for code convenience, it seems like you need an extra wrapper around your function to cache values, since fmin_bfgs
doesn't seem to allow you to pass such a function (unlike some other optimization functions).
这是一种方法,可以在一个小缓存中保留最近评估的10个点. (我不确定对该函数的调用是否需要是线程安全的:可能不是,但是,如果是这样,我想您可能需要在此处添加一些锁定.)
Here's one way to do that, maintaining the 10 most recently evaluated points in a little cache. (I'm not sure whether calls to this function need to be thread-safe: probably not, but if so, you'll probably need to add some locking in here, I guess.)
def func_wrapper(f, cache_size=10):
evals = {}
last_points = collections.deque()
def get(pt, which):
s = pt.tostring() # get binary string of numpy array, to make it hashable
if s not in evals:
evals[s] = f(pt)
last_points.append(s)
if len(last_points) >= cache_size:
del evals[last_points.popleft()]
return evals[s][which]
return functools.partial(get, which=0), functools.partial(get, which=1)
如果可以的话
>>> def f(x):
... print "evaluating", x
... return (x-3)**2, 2*(x-3)
>>> f_, fprime = func_wrapper(f)
>>> optimize.fmin_bfgs(f_, 1000, fprime)
evaluating [ 994.93480441]
evaluating [ 974.67402207]
evaluating [ 893.63089268]
evaluating [ 665.93446894]
evaluating [ 126.99931561]
evaluating [ 3.]
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 4
Function evaluations: 7
Gradient evaluations: 7
array([ 3.])
我们可以看到我们没有重复任何评估.
we can see that we don't repeat any evaluations.
这篇关于scipy.optimize.fmin_bfgs单个函数同时计算f和fprime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!