pydrake.forwarddiff.jacobian 参数的格式?
the format of argument of pydrake.forwarddiff.jacobian?
这个问题最初出现在这里https://github.com/RobotLocomotion/drake/issues/12484。问题是关于如何使用该功能。
功能说明:‖https://drake.mit.edu/pydrake/pydrake.forwarddiff.html#pydrake.forwarddiff.jacobian
我的代码:
'''
from pydrake.forwarddiff import jacobian
import math
import numpy as np
def f(t):
return np.array([1.5 - 0.5 * sigmoid(t - 2.5), 0.5, 0.0])
print jacobian(f,0.)
'''
返回的错误信息:
AttributeError Traceback (most recent call last)
in ()
48
49
---> 50 print jacobian(f,0.)
51 #print jacobian(f,np.array[0.])
52
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'float' object has no attribute 'shape'
If I changed that line into
print jacobian(f,np.array[0.])
then the error becomes:
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
TypeError Traceback (most recent call last)
in ()
49
50 #print jacobian(f,0.)
---> 51 print jacobian(f,np.array[0.])
52
53
TypeError: 'builtin_function_or_method' object has no attribute 'getitem'
If I changed that line into:
print jacobian(f,[0.])
the error:
AttributeError Traceback (most recent call last)
in ()
50 #print jacobian(f,0.)
51 #print jacobian(f,np.array[0.])
---> 52 print jacobian(f,[0.])
53
54 # Run the simulation. Parameters not described above
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'list' 对象没有属性 'shape'
那么问题来了,我该如何正确使用呢?谢谢
******************更新行************************** ***************
感谢 Eric 的发现。我把代码改成:
'''
from pydrake.forwarddiff import jacobian
import math
import numpy as np
def f(t):
return np.array([1.5 - 0.5 *(t - 2.5)])
def g(t):
return np.array([1.5 - 0.5 *(t - 2.5), 0.5, 0.0])
x=0.
x = np.asarray(x)
print jacobian(f,x) # accepted
print jacobian(f,0.) # not accepted
print jacobian(g,x) # not accepted
'''
结果输出为:
'''
[[-0.5]]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-1b94cb728eed> in <module>()
9 x = np.asarray(x)
10 print jacobian(f,x)
---> 11 print jacobian(f,0.)
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'float' object has no attribute 'shape'
'''
所以,这里至少有两个问题:
1. 每当我想使用 print jacobian(f,0.) 时,我必须先将 float 常量转换为 np.asarray。这很尴尬。
2.注意g(t)的雅可比,该行的错误是:
'''
---------------------------------------------- --------------------------
AttributeError Traceback(最后一次调用)
在 ()
11 print jacobian(f,x) # 接受
12 #print jacobian(f,0.) # 不被接受
---> 13 print jacobian(g,x) # 不被接受
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
45 y_ad = function(x_ad)
46 return np.vstack(
---> 47 [y.derivatives() for y in y_ad.flat]).reshape(y_ad.shape + (-1,))
48
49
AttributeError: 'float' object has no attribute 'derivatives'
'''
如果我猜对了,必须将 g(x) 中的所有内容更改为 np.asarray。这很尴尬...
你能保证x
是np.ndarray
类型吗?为此,请尝试 x = np.asarray(x)
.
看来我们也可以在 Drake 方面做到这一点,所以我提交了一个 PR(尽管它可能会在新年登陆):
https://github.com/RobotLocomotion/drake/pull/12511
这个问题最初出现在这里https://github.com/RobotLocomotion/drake/issues/12484。问题是关于如何使用该功能。
功能说明:‖https://drake.mit.edu/pydrake/pydrake.forwarddiff.html#pydrake.forwarddiff.jacobian
我的代码: '''
from pydrake.forwarddiff import jacobian
import math
import numpy as np
def f(t):
return np.array([1.5 - 0.5 * sigmoid(t - 2.5), 0.5, 0.0])
print jacobian(f,0.)
''' 返回的错误信息:
AttributeError Traceback (most recent call last)
in ()
48
49
---> 50 print jacobian(f,0.)
51 #print jacobian(f,np.array[0.])
52
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'float' object has no attribute 'shape'
If I changed that line into
print jacobian(f,np.array[0.])
then the error becomes:
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
TypeError Traceback (most recent call last)
in ()
49
50 #print jacobian(f,0.)
---> 51 print jacobian(f,np.array[0.])
52
53
TypeError: 'builtin_function_or_method' object has no attribute 'getitem'
If I changed that line into:
print jacobian(f,[0.])
the error:
AttributeError Traceback (most recent call last)
in ()
50 #print jacobian(f,0.)
51 #print jacobian(f,np.array[0.])
---> 52 print jacobian(f,[0.])
53
54 # Run the simulation. Parameters not described above
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'list' 对象没有属性 'shape'
那么问题来了,我该如何正确使用呢?谢谢
******************更新行************************** ***************
感谢 Eric 的发现。我把代码改成: '''
from pydrake.forwarddiff import jacobian
import math
import numpy as np
def f(t):
return np.array([1.5 - 0.5 *(t - 2.5)])
def g(t):
return np.array([1.5 - 0.5 *(t - 2.5), 0.5, 0.0])
x=0.
x = np.asarray(x)
print jacobian(f,x) # accepted
print jacobian(f,0.) # not accepted
print jacobian(g,x) # not accepted
''' 结果输出为: '''
[[-0.5]]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-1b94cb728eed> in <module>()
9 x = np.asarray(x)
10 print jacobian(f,x)
---> 11 print jacobian(f,0.)
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
38 The function should be vector-input and vector-output.
39 """
---> 40 x_ad = np.empty(x.shape, dtype=np.object)
41 for i in range(x.size):
42 der = np.zeros(x.size)
AttributeError: 'float' object has no attribute 'shape'
''' 所以,这里至少有两个问题: 1. 每当我想使用 print jacobian(f,0.) 时,我必须先将 float 常量转换为 np.asarray。这很尴尬。 2.注意g(t)的雅可比,该行的错误是: ''' ---------------------------------------------- -------------------------- AttributeError Traceback(最后一次调用) 在 () 11 print jacobian(f,x) # 接受 12 #print jacobian(f,0.) # 不被接受 ---> 13 print jacobian(g,x) # 不被接受
/drake/lib/python2.7/site-packages/pydrake/forwarddiff.py in jacobian(function, x)
45 y_ad = function(x_ad)
46 return np.vstack(
---> 47 [y.derivatives() for y in y_ad.flat]).reshape(y_ad.shape + (-1,))
48
49
AttributeError: 'float' object has no attribute 'derivatives'
''' 如果我猜对了,必须将 g(x) 中的所有内容更改为 np.asarray。这很尴尬...
你能保证x
是np.ndarray
类型吗?为此,请尝试 x = np.asarray(x)
.
看来我们也可以在 Drake 方面做到这一点,所以我提交了一个 PR(尽管它可能会在新年登陆): https://github.com/RobotLocomotion/drake/pull/12511