Python科学优化.f琰min琰l琰bfgs琰b算法:如何有额外的输出?

2024-04-26 07:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Scipy包中的L-BFGS-B优化器。这是documentation.

我的代码具有以下结构。目标函数是用来最小化“a”的目标函数。为此,obj_func只能返回“a”。你知道吗

问:有没有办法从objèu 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

Tags: 函数代码obj目标documentationargsfunctionscipy