Python 中的简单与门感知器学习
我正在尝试编写一个简单的算法,该算法将学习权重和阈值,以便能够绘制线 w1*x + w2*y = threshhold
,使其遵循任何训练集的数据(在本例中为与门训练集)。 然而,我的程序似乎没有学习,并且错误始终为-3,无论我让多少次迭代。 以下是我的代码:
import numpy
import random
w1 = random.uniform(-0.2, 0.2)
w2 = random.uniform(-0.2, 0.2)
threshhold = random.uniform(-0.2, 0.2)
training_x = numpy.asarray([[0,0], [0,1], [1,0], [1,1]])
out = [0,0,0,1]
def positive(number):
if(number >= 0):
return 1
else:
return 0
error = numpy.array([0,0,0,0])
for j in range(len(training_x)):
check = positive(numpy.dot(numpy.asarray([w1,w2]), training_x[j]) + threshhold)
error[j] = out[j] -check
errornumber = numpy.sum(error)
iterations = 1000
count = 1
eta = 0.1
values = [w1, w2, threshhold]
while count < iterations and errornumber != 0:
for j in range(len(training_x)):
check = positive(numpy.dot(numpy.asarray([w1,w2]), training_x[j]) + threshhold)
error[j] = out[j] -check
w1 = values[0] + eta * error[j]*training_x[j][0]
w2 = values[1] + eta * error[j]*training_x[j][0]
threshhold = values[2] + eta*training_x[j][0]
values = [w1, w2, threshhold]
errornumber = numpy.sum(error)
print("ERRORS: " + str(errornumber))
count += 1
print("w1 " + str(values[0]) + "w2 " + str(values[1]) + "theta " + str(values[2]))
print("count " + str(count))
我将不胜感激任何帮助。
顺便说一句,我从这个网站获得了灵感: https://medium.com/analytics-vidhya/implementing-perceptron-learning-algorithm-to-solve-and-in-python-903516300b2f
提前致谢!
I am trying to code a simple algorithm that will learn the weights and the threshold to be able to draw the line w1*x + w2*y = threshhold
such that it follows the data of any training set (in this case the AND gate training set).
However, it seems my program is not learning, and the error is always at -3, no matter how many iterations I let if have.
Below is my code:
import numpy
import random
w1 = random.uniform(-0.2, 0.2)
w2 = random.uniform(-0.2, 0.2)
threshhold = random.uniform(-0.2, 0.2)
training_x = numpy.asarray([[0,0], [0,1], [1,0], [1,1]])
out = [0,0,0,1]
def positive(number):
if(number >= 0):
return 1
else:
return 0
error = numpy.array([0,0,0,0])
for j in range(len(training_x)):
check = positive(numpy.dot(numpy.asarray([w1,w2]), training_x[j]) + threshhold)
error[j] = out[j] -check
errornumber = numpy.sum(error)
iterations = 1000
count = 1
eta = 0.1
values = [w1, w2, threshhold]
while count < iterations and errornumber != 0:
for j in range(len(training_x)):
check = positive(numpy.dot(numpy.asarray([w1,w2]), training_x[j]) + threshhold)
error[j] = out[j] -check
w1 = values[0] + eta * error[j]*training_x[j][0]
w2 = values[1] + eta * error[j]*training_x[j][0]
threshhold = values[2] + eta*training_x[j][0]
values = [w1, w2, threshhold]
errornumber = numpy.sum(error)
print("ERRORS: " + str(errornumber))
count += 1
print("w1 " + str(values[0]) + "w2 " + str(values[1]) + "theta " + str(values[2]))
print("count " + str(count))
I would appreciate any help.
By the way, I took inspiration from this website:
https://medium.com/analytics-vidhya/implementing-perceptron-learning-algorithm-to-solve-and-in-python-903516300b2f
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我重写了算法,现在可以完美运行了。
I rewrote the algorithm and it now works perfectly.
尝试调整学习率“eta”。当我设置 eta = 0.005 时,它会收敛 - 但我可能需要重新运行 3-5 次才能获得不同的随机初始化。
输出示例:
错误:1
错误:1
错误:1
错误:1
错误:0
w1 -0.142054790891668 w2 0.11580039178422052 θ -0.09818819751599175
数 6
编辑
多运行几次后,我得到了这个解决方案:
错误:1
错误:1
错误:1
...
错误:1
错误:1
错误:0
w1 0.04909721513572815 w2 0.04279261178484681 θ -0.08081630077876589
数20
它确实具有按照您希望的与门方式划分图形的属性。
Try adjusting the learning rate "eta". When I set eta = 0.005, it would converge - but I might have to re-run it 3-5 times to obtain different random initializations.
Example output:
ERRORS: 1
ERRORS: 1
ERRORS: 1
ERRORS: 1
ERRORS: 0
w1 -0.142054790891668 w2 0.11580039178422052 theta -0.09818819751599175
count 6
edit
After running it a few more times, I obtained this solution:
ERRORS: 1
ERRORS: 1
ERRORS: 1
...
ERRORS: 1
ERRORS: 1
ERRORS: 0
w1 0.04909721513572815 w2 0.04279261178484681 theta -0.08081630077876589
count 20
Which does have the property of dividing the graph the way you wanted it to for an AND gate.