我正在使用 Scipy 包中的 L-BFGS-B 优化器。这是 文档。
我的代码具有以下结构。obj_func 是用于最小化“a”的目标函数。为此,obj_func 应该只返回“a”。
问题:有没有办法从 obj_func 中获取“b”和“c”?
目前我正在使用函数属性。但我不确定这是首选方式。
def obj_func(x, *args)
a, b, c = compute(x) # compute is where major computation happens
# I want to access b and c from outside the function
obj_func.result = [b,c] # use function attribute
return a
result = optimize.fmin_l_bfgs_b(obj_func, x0, bounds=bounds, args=args)
b, c = obj_func.result