无法区分dtype int64的wrt numpy数组?

时间:2018-10-20 19:00:59

标签: python numpy

我是numpy的新手。今天,当我使用它进行线性回归时,它显示如下:

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 

我真的不知道发生了什么。我猜可能与numpy中数组的结构有关。还是我忘记下载任何软件包?下面是我的原始代码。

# 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")

有人可以帮我弄清楚吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

这是错误的重要部分:

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

您的梯度函数说它不喜欢区分整数数组,这是有道理的,因为它可能想要比整数可以提供的精度更高的精度。您可能需要它们为双精度或浮点型。对于此问题的简单解决方案,我相信您可以从以下位置更改初始化器:

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

这将自动将这些0转换为整数,以:

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

0后的那些小数将让您知道要浮点数。还可以通过其他方法告诉您所需的数组类型(https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.array.html),但这是一种简单的方法。