我正在操作 autograd 库并计算向量函数 f 的雅可比矩阵,我想计算在点 x = (-1,0.2,0.3) 评估的雅可比矩阵
import autograd.numpy as np
from autograd import grad, jacobian
def f(x):
f = np.zeros(len(x))
f[0] = np.sin(x[0])+x[1]**2 +np.log(x[2])-7
f[1] = 3.0*x[0] + 2.0**x[1] - x[2]**3 + 1.0
f[2] = x[0] + x[1] + x[2] - 5.0
return f
x = np.array([-1.0,0.2,0.3])
gradient_cost = grad(f)
jacobian_cost = jacobian(f)
gradient_cost(x)
jacobian_cost(np.array([x,x,x]))
并向我抛出以下错误:
jacobian_cost(np.array([x,x,x]))
C:\ProgramData\Anaconda3\lib\site-packages\autograd\tracer.py:48: RuntimeWarning: invalid value encountered in log
return f_raw(*args, **kwargs)
Traceback (most recent call last):
File "<ipython-input-94-f5cd2527501d>", line 1, in <module>
jacobian_cost(np.array([x,x,x]))
File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\differential_operators.py", line 57, in jacobian
vjp, ans = _make_vjp(fun, x)
File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\core.py", line 10, in make_vjp
end_value, end_node = trace(start_node, fun, x)
File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\tracer.py", line 10, in trace
end_box = fun(start_box)
File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
return fun(*subargs, **kwargs)
File "<ipython-input-90-1d8956a98276>", line 3, in f
f[0] = np.sin(x[0])+x[1]**2 +np.log(x[2])-7
TypeError: 'function' object does not support item assignment