简单的神经元算法实现(python)

image

 

 

image

 

 

image

 

 

 

参考python代码如下

#perceptron
x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]]
y=[-1,1,1,1,-1,-1,1,-1]#真值
y_pre=[0,0,0,0,0,0,0,0]#初始化
_DEBUG=True


w=[0 ,0 ,0 ,0.0]

lamu=0.1#学习因子


while 1:
    sum=0
    for i in range(len(y)):
        y_pre[i]=((x[i][1]*w[1]+x[i][2]*w[2]+x[i][0]*w[0]+w[3])>=0)*2-1
        for j in range(len(x[1])):


            w[j]=w[j]+lamu*(y[i]-y_pre[i])*x[i][j]

        w[3]=w[3]+lamu*(y[i]-y_pre[i])
    
    for i in range(len(y)):
 
        sum=sum+abs(y[i]-(((x[i][1]*w[1]+x[i][2]*w[2]+x[i][0]*w[0]+w[3])>=0)*2-1))



    
    if sum<0.01:
    
        break

print(w)
posted @ 2013-11-25 21:52  joey周琦  阅读(1187)  评论(0编辑  收藏  举报