朱莉娅:最小化带有多个参数的函数(BFGS) [英] Julia: Minimise a function with multiple arguments (BFGS)
问题描述
我正在尝试使用BFGS算法在Optim.jl库中最小化具有多个参数的函数.
I am trying to minimise a function with multiple arguments with the Optim.jl library, using a BFGS algorithm.
在Optim库的GitHub网站上,我找到了以下工作示例:
In the GitHub website of the Optim library, I found the following working example:
using Optim
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
result = optimize(rosenbrock, zeros(2), BFGS())
假设我的目标函数是:
fmin(x, a) = (1.0 - x[1])^a + 100.0 * (x[2] - x[1]^2)^(1-a)
如何使用 optimize 传递附加的-常数-参数 a ?
How can I pass the additional - constant - argument a using optimize?
推荐答案
最简单的方法是传递一个变量的匿名函数,该变量使用参数集调用原始函数.例如,使用fmin的变体:
The simplest way is to pass an anonymous function of one variable which calls your original function with the parameters set. For example, using a variant of your fmin:
julia> fmin(x, a) = (1.0 - x[1])^a + 100.0 * (x[2] - x[1]^2)^(a)
fmin (generic function with 1 method)
julia> r = optimize(x->fmin(x, 2), zeros(2), BFGS());
julia> r.minimizer, r.minimum
([1.0,1.0],5.471432684244042e-17)
或者,您可以为一个变量创建一个单独的命名函数,该函数将关闭您喜欢的任何参数.没有与Python中scipy.optimize.minimize
中的args
等效的功能,在Python中,您将不变的参数分别作为元组AFAIK传递.
Alternatively you can make a separate named function of one variable which closes over whatever parameters you like. There's no equivalent to the args
in scipy.optimize.minimize
in Python where you pass the non-varying arguments separately as a tuple, AFAIK.
这篇关于朱莉娅:最小化带有多个参数的函数(BFGS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!