1

我编写了一个机器学习模型,其中导数是使用 autograd 库计算的。

import autograd.numpy as np
from autograd import grad
# take gradient
# import tangent

# Prepare the data
input = np.array(list(range(100)), dtype=np.float64)
output = np.array([x**2 + 2 for x in list(range(100))])

# Visualize Data
import matplotlib.pyplot as plt
plt.plot(input, output, 'ro')
plt.show()

# Define your Model
a = 1.0
b = 1.0

parameters = [a, b]
parameters = np.array(parameters)

def model(x, parameters):
    return parameters[0]*x + parameters[1]*b

def loss(args):
    x, y, parameters = args[0], args[1], args[2]
    return 0.5*(y-model(x, parameters))**2

# y = ax + b # we put a bias in the model based on our knowledge

# Loss array for later viewing
losses = []

# grad_loss = tangent.grad(loss)
grad_loss = grad(loss)

# Train your model == Optimize the parameters so that they give very less loss
for e in range(1000):
    epoch_loss = 0
    for x, y in zip(input, output):
        cur_loss =  loss([x, y, parameters])
        epoch_loss += cur_loss
        grad = grad_loss([x, y, parameters])
        parameters -= 0.01*grad
    losses.append(epoch_loss)
    print("Epoch {0} Training loss = {1}".format(e, epoch_loss))

# Training Summary
plt.plot(list(range(1,len(losses)+1)), losses, '-') 
plt.show()

发生的事情是我收到此错误:

Traceback (most recent call last):
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\tracer.py", line 139, in new_box
    return box_type_mappings[type(value)](value, trace, node)
KeyError: <class 'numpy.int32'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ml.py", line 43, in <module>
    grad = grad_loss([float(x), y, parameters])
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\differential_operators.py", line 24, in grad
    vjp, ans = _make_vjp(fun, x)
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\tracer.py", line 10, in trace
    end_box = fun(start_box)
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
  File "ml_zero2.py", line 26, in loss
    x, y, parameters = args[0], args[1], args[2]
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\tracer.py", line 46, in f_wrapped
    return new_box(ans, trace, node)
  File "C:\Program Files\Anaconda2\envs\py35\lib\site-packages\autograd\tracer.py", line 141, in new_box
    raise TypeError("Can't differentiate w.r.t. type {}".format(type(value)))
TypeError: Can't differentiate w.r.t. type <class 'numpy.int32'>

我尝试了什么:

我尝试将输入数组设置为 float64,仍然得到相同的错误。

我尝试在 for 循环中将 x 转换为 float(x) ,但仍然得到相同的错误。

仍然无法弄清楚如何解决错误并运行代码?谢谢你的帮助。

4

0 回答 0