简体   繁体   中英

Can't differentiate wrt numpy arrays of dtype int64?

I am a newbie to numpy. Today when I use it to work with linear regression, it shows as below:

KeyError                                  Traceback (most recent call 
last)
~/anaconda3/lib/python3.6/site-packages/autograd/numpy/numpy_extra.py 
in new_array_node(value, tapes)
     84     try:
---> 85         return array_dtype_mappings[value.dtype](value, tapes)
     86     except KeyError:

KeyError: dtype('int64')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call 
last)
<ipython-input-4-aebe8f7987b0> in <module>()
     24     return cost/float(np.size(y))
     25 
---> 26 weight_h, cost_h = gradient_descent(least_squares, alpha, 
max_its, w)
     27 
     28 # a)

<ipython-input-2-1b74c4f818f4> in gradient_descent(g, alpha, max_its, 
w)
     12     for k in range(max_its):
     13         # evaluate the gradient
---> 14         grad_eval = gradient(w)
     15 
     16         # take gradient descent step

~/anaconda3/lib/python3.6/site-packages/autograd/core.py in 
gradfun(*args, **kwargs)
     19     @attach_name_and_doc(fun, argnum, 'Gradient')
     20     def gradfun(*args,**kwargs):
---> 21         return 
backward_pass(*forward_pass(fun,args,kwargs,argnum))
     22     return gradfun
     23 

~/anaconda3/lib/python3.6/site-packages/autograd/core.py in 
forward_pass(fun, args, kwargs, argnum)
     57         tape = CalculationTape()
     58         arg_wrt = args[argnum]
---> 59         start_node = new_node(safe_type(getval(arg_wrt)), 
[tape])
     60         args = list(args)
     61         args[argnum] = merge_tapes(start_node, arg_wrt)

~/anaconda3/lib/python3.6/site-packages/autograd/core.py in 
new_node(value, tapes)
    185 def new_node(value, tapes=[]):
    186     try:
--> 187         return Node.type_mappings[type(value)](value, tapes)
    188     except KeyError:
    189         return NoDerivativeNode(value, tapes)

 ~/anaconda3/lib/python3.6/site-packages/autograd/numpy/numpy_extra.py 
in new_array_node(value, tapes)
     85         return array_dtype_mappings[value.dtype](value, tapes)
     86     except KeyError:
---> 87         raise TypeError("Can't differentiate wrt numpy arrays 
of dtype {0}".format(value.dtype))
     88 Node.type_mappings[anp.ndarray] = new_array_node
     89 

TypeError: Can't differentiate wrt numpy arrays of dtype int64 

I really have no idea about what is happened. I guess it might be related to the structure of array in numpy. Or did I forget to download any packages? Below is my original codes.

# import statements
datapath = 'datasets/'
from autograd import numpy as np

# import automatic differentiator to compute gradient module
from autograd import grad 

# gradient descent function 
def gradient_descent(g,alpha,max_its,w):
    # compute gradient module using autograd
    gradient = grad(g)

    # run the gradient descent loop
    weight_history = [w] # weight history container
    cost_history = [g(w)] # cost function history container
    for k in range(max_its):
        # evaluate the gradient
        grad_eval = gradient(w)

        # take gradient descent step
        w = w - alpha*grad_eval

        # record weight and cost
        weight_history.append(w)
        cost_history.append(g(w))
    return weight_history,cost_history

# load in dataset
csvname = datapath + 'kleibers_law_data.csv'
data = np.loadtxt(csvname,delimiter=',')

# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:] 

x = np.log(x)
y = np.log(y)

#Data Initiation
alpha = 0.01
max_its = 1000
w = np.array([0,0])

#linear model
def model(x, w):
    a = w[0] + np.dot(x.T, w[1:])
    return a.T

def least_squares(w):
    cost = np.sum((model(x,w)-y)**2)
    return cost/float(np.size(y))

weight_h, cost_h = gradient_descent(least_squares, alpha, max_its, w)

# a)
k = np.linspace(-5.5, 7.5, 250)
y = weight_h[max_its][0] + k*weight_h[max_its][1]
plt.figure()
plt.plot(x, y, label='Linear Line', color='g')
plt.xlabel('log of mass')
plt.ylabel('log of metabolic rate')
plt.title("Answer Of a")
plt.legend()
plt.show()

# b)
w0 = weight_h[max_its][0]
w1 = weight_h[max_its][1]

print("Nonlinear relationship between the body mass x and the metabolic 
rate y is " /
      + str(w0) + " + " + "log(xp)" + str(w1) + " = " + "log(yp)")

# c)
x2 = np.log(10)
Kj = np.exp(w0 + w1*x2)*1000/4.18
print("It needs " + str(Kj) + " calories")

Could someone help me to figure it out? Thanks a lot.

Here's the important parts of your error:

---> 14         grad_eval = gradient(w)
...    
Type Error: Can't differentiate wrt numpy arrays of dtype int64 

Your gradient function is saying it doesn't like to differentiate arrays of ints, which makes some sense, since it probably wants more precision than an int can give. You probably need them to be doubles or floats. For a simple solution to this, I believe you can just change your initializer from:

w = np.array([0,0])

which is going to automatically cast those 0s as ints, to:

w = np.array([0.0,0.0])

Those decimals after the 0 will let it know you want floats. There's other ways to go about telling it what kind of array you want ( https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.array.html ), but this is a simple way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM