我试图定义一个类,在这个类中,许多变量的函数都得到了优化。通常我要处理500-1000个变量。在这个类中,我需要将函数及其导数传递给scipy
中的minimize
,以找到最小化这个函数的x0
。你知道吗
下面是这个概念的一个简单的工作示例,它工作得很好。但正如您所见,函数(f
)及其导数(df
)都依赖于另一个函数g
(在本例中,它看起来很简单,可以用另一种方式编写,但实际函数要复杂得多)。你知道吗
我想知道是否可以在每次迭代中只计算一次g
,然后在类中使用该值。考虑到f
和df
在minimize
中被多次更新,因此在每个步骤g
也应该重新评估。你知道吗
谢谢!你知道吗
from scipy.optimize import minimize
class Minimization(object):
'''A class to optimizae a function'''
def __init__(self,x,y):
self.x = x
self.y = y
self.p = np.array([x,y])
def g(self,x,y):
return x-y
def f(self,p):
return (self.g(*p) - 1)**2
def df(self,p):
fprime = 2*(self.g(*p) - 1)
return np.array([fprime,-fprime])
def optimize(self):
P1 = minimize(fun=self.f, x0=self.p, args=(), method='Newton-CG',jac=self.df)
return P1
m = Minimization(2,4)
m.optimize()
#fun: 0.0
# jac: array([ 0., -0.])
#message: 'Optimization terminated successfully.'
# nfev: 3
# nhev: 0
# nit: 2
#njev: 6
#status: 0
#success: True
# x: array([ 3.5, 2.5])
你想要的是所谓的“记忆化”。当函数g计算一个值时,它将结果存储在一个字典中,由参数x,y索引。每次调用g时,它都会检查字典,看它需要的值是否已经存储在那里。如果需要重置值,请清除字典。像这样:
在没有深入研究代码本身的情况下,下面是一个示例类,演示如何一次计算一个值并避免在每次调用时重新计算它。你也可以把它变成一个财产。你知道吗
为了补充Paul的回答,您可以定义一个类聚合类似缓存的方法,然后(重新)使用这些方法作为装饰器。你知道吗
然后使用类
Cache
作为Minimization
的祖先,如下所示用例(在Python 2.7.11和3.6.1下测试)
相关问题 更多 >
编程相关推荐