编程语言
首页 > 编程语言> > 感知器算法

感知器算法

作者:互联网

import numpy as np
import random
w0=0.2
w1=0.2
w2=0.2
n=0.01
e=float(2.72)
#E=float(10*e^(-4))
E=float(0.18)
e0=10
e1=10
e2=10
x=np.array([[0,0,0],
           [0,1,0],
           [1,0,0],
           [1,1,1]])
# t=np.array([[0],
#            [0],
#            [0],
#            [1]])

for a in range(0,100):
    if e0<=E and e1<=E and e2<=E:
        w0=w0-n*e0
        w1=w1-n*e1
        w2=w2-n*e2
    else:
        j=random.randint(0,3)
        i=j
        z=w1*x[i,0]+w2*x[i,1]+w0
        s=1/(1+e**(-z))
        e0=2*(s-x[i,2])*(s**2)*(e**z)
        e1=2*(s-x[i,2])*(s**2)*(e**z)*x[i,0]
        e2=2*(s-x[i,2])*(s**2)*(e**z)*x[i,1]
print(w0,w1,w2)

#以下代码为尝试画图,但没有成功实现
# import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D   #绘制3D坐标的函数  
# fig1=plt.figure()#创建一个绘图对象  
# ax=Axes3D(fig1)#用这个绘图对象创建一个Axes对象(有3D坐标)  
# X1=random.randint(0,2)
# X2=random.randint(0,2)
# #X1,X2,T=np.random.randint(0,2,(100,2))
# if X1==X2:
#     T=X1
# else:
#     T=0
# X1,X2=np.meshgrid(X1,X2)
# h=(w0+w1*a[0]+w2*a[1]-T)**2
# plt.title("tu xiang") #图像标题
# ax.plot_surface(X1, X2, h , rstride=1, cstride=1, cmap=plt.cm.coolwarm, alpha=0.5) #用取样点(x,y,z)去构建曲面
# ax.set_xlabel('X1', color='r')
# ax.set_ylabel('X2', color='g')
# ax.set_zlabel('h', color='b')
# plt.show()#显示模块中的所有绘图对象

运行结果:
0.7331438354808711 0.7331438354808711 0.7331438354808711

标签:10,感知器,float,0.7331438354808711,算法,np,array,0.2
来源: https://blog.csdn.net/liangzai07/article/details/120205094