Scipy.optimize check_grad函数给出“未知关键字参数:['args']”错误

时间:2016-10-27 14:01:02

标签: python scipy args minimization

我想使用scipy.optimize.check_grad来评估渐变的正确性。我指定

def func(x, a):
    return x[0]**2 - 0.5 * x[1]**3 + a**2 

def grad(x, a):
        return [2 * x[0], -1.5 * x[1]**2 + 2*a]

from scipy.optimize import check_grad
a = 5 
check_grad(func, grad, [1.5, -1.5], args = (a))

并获得错误

Unknown keyword arguments: ['args']

值得注意的args被列为help file中的争论者。这不应该起作用吗?

1 个答案:

答案 0 :(得分:1)

*args只需将位置参数传递给funcgrad函数。

您只想将元参数a的值作为x0之后的参数传递。

def func(x, a, b):
    return x[0]**2 - 0.5 * x[1]**3 + a**2 + b

def grad(x, a, b):
        return [2 * x[0], -1.5 * x[1]**2 + 2*a + b]

from scipy.optimize import check_grad
a = 5 
b = 10
check_grad(func, grad, [1.5, -1.5], a, b)

请参阅https://github.com/scipy/scipy/blob/a81bc79ba38825139e97b14c91e158f4aabc0bed/scipy/optimize/optimize.py#L736-L737了解实施情况。