optimizie.minimize 使用 CG

optimizie.minimize using CG

我一直在使用以下代码:

options = {'maxiter':50, 'disp':True}
res = optimize.minimize(
        fun=lrCostFunction,
        x0=theta_k,
        args=(X, y_k, lambda_),
        method='CG',
        options=options
)

这给了我以下错误:

    TypeError: only size-1 arrays can be converted to Python scalars
    The above exception was the direct cause of the following exception:
    ValueError: setting an array element with a sequence.

但是当我设置 jac = True 如下:


options = {'maxiter':50, 'disp':True}

res = optimize.minimize(
        fun=lrCostFunction,
        x0=theta_k,
        jac=True,
        args=(X, y_k, lambda_),
        method='CG',
        options=options
)

一切正常,但文档没有说明我们必须设置 jac = True,所以这里出了什么问题?

我猜 objective 函数 lrCostFunction returns 函数值和梯度。根据 minimize

的文档

If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function. If False, the gradient will be estimated using '2-point' finite difference estimation.

因此,如果 jac=False 而 objective 函数 returns 也是梯度,您将收到一些错误。例如

# objective function 
def obj_func(x,a,b,c):
    
    func_val = a*x[0]**2 + b*x[1] + c
    func_grad = 2*a*x[0] + b
    return func_val,func_grad

x0 = np.random.rand(3)
res = minimize(obj_func,x0,args=(1,2,3),method='CG')

如果我运行这个代码,我会收到一个TypeError。如果我设置 jac=True,一切正常。