我似乎无法调试以下Adaline神经元的实现…我希望有人能发现我看不到的问题。我认为问题出在我train方法的最后几行?
from numpy import random, array, dotimport numpy as npimport matplotlib.pyplot as pltfrom random import choiceimport mathimport sympyclass adalineANN(object): def __init__(self, gamma=.2, trials=500, errors=[], weights=[]): self.gamma = gamma self.trials = trials self.errors = errors self.weights = weightsdef train(self): self.weights = random.rand(3) coordinates_class1 = [] coordinates_class2 = [] for x in np.random.normal(2, .5, 20): for y in np.random.normal(3, .5, 20): coordinates_class1.append(([x, y, 1], 1)) break for x in np.random.normal(2, .25, 20): for y in np.random.normal(-1, .25, 20): coordinates_class2.append(([x, y, 1], -1)) break trainingData = coordinates_class1 + coordinates_class2 for i in range(self.trials): x, target = choice(trainingData) y = np.dot(x, self.weights) error, errors = [], [] error = (target - y) self.errors.append(error) for i in range(0, 3): self.weights[i] += self.gamma * x[i] * (target - y) #????* (sympy.cosh(y)**(-1))**2def plot(self): plt.plot(self.errors) plt.show()A = adalineANN()A.train()A.plot()
我是否也需要我的阈值函数的导数?参见上面的注释。
回答:
实际上并没有什么大问题,但是:
-
你在两个循环中都使用了“i”作为迭代器,在这段代码中这并不重要,因为你没有在外部循环中真正使用它,但一般来说这可能会导致非常棘手的错误。
-
你的gamma值太高了,把它改为0.01
-
不要绘制符号错误,adaline是在最小化平方误差,因此你应该绘制
error = (target - y)**2
;你甚至可以使用error = target != np.sign(y)
来查看它是否能很好地区分,以提高准确性。