python - Scipy.optimize check_grad 函数给出 "Unknown keyword arguments: [' args']"错误

标签 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']

值得注意的参数在 help file 中被列为参数。 。这不应该起作用吗?

最佳答案

*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以便实现。

关于python - Scipy.optimize check_grad 函数给出 "Unknown keyword arguments: [' args']"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40286648/

相关文章:

python - 平滑离散数据集

python *args 和 **kwargs

Python截取屏幕截图并将其保存到缓冲区

python - TensorFlow独立使用CPU和GPU

python - 如何从文件中打印单行

linux - vim:通过 ftp 替换文件

java - 使用扫描仪 JAVA 初始化数组

python - PyModule_New 的目的和用法

python - 嘈杂的 Scipy Wavfile 输出问题

python - 规范化/标准化 numpy recarray