0

This question is originally presented here https://github.com/RobotLocomotion/drake/issues/12484. the question is about how to use the function.

the function description: https://drake.mit.edu/pydrake/pydrake.forwarddiff.html#pydrake.forwarddiff.jacobian

my code: '''

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.)

''' The returned error info:

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' object has no attribute 'shape'

So the question is, how should I use it correctly? Thanks

*****************update line*****************************************

Thanks to Eric's finding. I change the code into: '''

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

''' The result output is: '''

[[-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...

N.Li
  • 47
  • 7

1 Answers1

1

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

Eric Cousineau
  • 1,944
  • 14
  • 23