Python:将参数传递给函数
Python: passing arguments to a function
我正在使用 dlib
的 find_min_global
函数,这是一种优化算法,有助于找到使函数输出最小化的值。例如
import dlib
def holder_table(x0,x1):
return -abs(sin(x0)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
x,y = dlib.find_min_global(holder_table,
[-10,-10], # Lower bound constraints on x0 and x1 respectively
[10,10], # Upper bound constraints on x0 and x1 respectively
80) # The number of times find_min_global() will call holder_table()
这里的holder_table
函数returns对于x0
和x1
的不同值需要最小化的值。
这里的holder_table
函数只接受需要优化的值,即x0
和x1
。但是我想用 dlib
函数使用的函数比 x0
和 x1
还要多。函数定义看起来像这样
def holder_table(a,b,x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
值a, b
不固定,是另一个函数的输出。现在,我可以直接在 holder_table
中调用 returns a, b
函数,但我不想最终重新计算它们,因为每次 holder_table
都被调用 a, b
被重新计算,这个过程很耗时。
如何将 a, b
传递给 holder_table
函数?
您的问题不是 100% 清楚,但看起来您想要一个 partial application. In Python this can be done using the dedicated functools.partial
object, or quite simply with a closure(使用内部函数或 lambda)
def holder_table(a,b,x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
def main():
a, b = some_heavy_function(...)
holder_table_partial = lambda ax, ay: holder_table(a, b, ax, ay)
x, y = dlib.find_min_global(
holder_table_partial, [-10,-10], [10,10], 80
)
仅根据您对规范的介绍,holder_table
是一个带有两个参数的函数和 returns 可用于帮助指导优化步骤的最终结果。此外,如果我理解正确,a
和 b
是 objective 公式的组成部分,但可能需要一段时间才能计算,并且您不希望调用它们的逻辑计算比必要的更频繁 - 所以包括他们的推导 inside 和 holder_table
似乎效率低下。
怎么样:
def build_objective_function(a,b):
def holder_table(x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
return holder_table
你会这样称呼它:
a = <compute a>
b = <compute b>
holder_table = build_objective_function(a,b) # holder_table will be a function
x,y = dlib.find_min_global(holder_table,
[-10,-10], # Lower bound constraints on x0 and x1 respectively
[10,10], # Upper bound constraints on x0 and x1 respectively
80) # The number of times find_min_global() will call holder_table()
我正在使用 dlib
的 find_min_global
函数,这是一种优化算法,有助于找到使函数输出最小化的值。例如
import dlib
def holder_table(x0,x1):
return -abs(sin(x0)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
x,y = dlib.find_min_global(holder_table,
[-10,-10], # Lower bound constraints on x0 and x1 respectively
[10,10], # Upper bound constraints on x0 and x1 respectively
80) # The number of times find_min_global() will call holder_table()
这里的holder_table
函数returns对于x0
和x1
的不同值需要最小化的值。
这里的holder_table
函数只接受需要优化的值,即x0
和x1
。但是我想用 dlib
函数使用的函数比 x0
和 x1
还要多。函数定义看起来像这样
def holder_table(a,b,x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
值a, b
不固定,是另一个函数的输出。现在,我可以直接在 holder_table
中调用 returns a, b
函数,但我不想最终重新计算它们,因为每次 holder_table
都被调用 a, b
被重新计算,这个过程很耗时。
如何将 a, b
传递给 holder_table
函数?
您的问题不是 100% 清楚,但看起来您想要一个 partial application. In Python this can be done using the dedicated functools.partial
object, or quite simply with a closure(使用内部函数或 lambda)
def holder_table(a,b,x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
def main():
a, b = some_heavy_function(...)
holder_table_partial = lambda ax, ay: holder_table(a, b, ax, ay)
x, y = dlib.find_min_global(
holder_table_partial, [-10,-10], [10,10], 80
)
仅根据您对规范的介绍,holder_table
是一个带有两个参数的函数和 returns 可用于帮助指导优化步骤的最终结果。此外,如果我理解正确,a
和 b
是 objective 公式的组成部分,但可能需要一段时间才能计算,并且您不希望调用它们的逻辑计算比必要的更频繁 - 所以包括他们的推导 inside 和 holder_table
似乎效率低下。
怎么样:
def build_objective_function(a,b):
def holder_table(x0,x1):
return -abs(sin(b*x0/a)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
return holder_table
你会这样称呼它:
a = <compute a>
b = <compute b>
holder_table = build_objective_function(a,b) # holder_table will be a function
x,y = dlib.find_min_global(holder_table,
[-10,-10], # Lower bound constraints on x0 and x1 respectively
[10,10], # Upper bound constraints on x0 and x1 respectively
80) # The number of times find_min_global() will call holder_table()