首页 > 解决方案 > 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:“列表”对象没有属性“形状”

那么问题来了,应该如何正确使用呢?谢谢

*****************更新行************************************ **********

感谢 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'

''' SO, THERE ARE TWO PROBLEMS HERE AT LEAST: 1. whenever i want to use print jacobian(f,0.), I have to transfer the float constant into np.asarray first. This is awkward. 2. Notice the jacobian of g(t), the error of that line is: ''' --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 11 print jacobian(f,x) # accepted 12 #print jacobian(f,0.) # not accepted ---> 13 print jacobian(g,x) # not accepted

/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'

''' If i'm guessing right, one has to change everything in g(x) to be np.asarray. This is awkward...

标签: pythonjupyter-notebookdrake

解决方案


Can you ensure that x is of type np.ndarray? To do so, try doing x = np.asarray(x).

Seems like we can also do this on the Drake side, so I've submitted a PR (though it may land in the New Year): https://github.com/RobotLocomotion/drake/pull/12511


推荐阅读