我想使用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中的争论者。这不应该起作用吗?
答案 0 :(得分:1)
*args
只需将位置参数传递给func
和grad
函数。
您只想将元参数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)