pydrake.forwarddiff.jacobian的参数格式?

2024-09-27 07:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题最初是在这里提出的。问题是如何使用这个函数

功能描述: 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”

所以问题是,我应该如何正确使用它?谢谢

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

多亏了埃里克的发现。我把代码改成: ''

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回溯(最近一次调用) 在() 11打印雅可比(f,x)#接受 12#打印雅可比矩阵(f,0.)#不接受 ---&燃气轮机;13打印雅可比(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。这很尴尬


Tags: inimportsizeobjectnpfunctionarrayad
1条回答
网友
1楼 · 发布于 2024-09-27 07:32:17

你能保证xnp.ndarray类型吗?为此,请尝试执行x = np.asarray(x)

看来我们在德雷克方面也可以做到这一点,所以我提交了一份公关(尽管它可能在新的一年登陆): https://github.com/RobotLocomotion/drake/pull/12511

相关问题 更多 >

    热门问题