pythonでNN(多層パーセプトロン)

線形分類不可能のXor問題を解いてみました。重み更新ははバックプロパゲーションによって行っています。中間層のユニットは2です

    # -*- coding: utf-8 -*-
import random
import math

studyFlg = True
weight =[[0.1,0.1,0.06],[-0.04,0.01,-0.1]]
deltaWeight = [[0,0,0],[0,0,0]]
vWeight = [0.02,-0.009,-0.051]
deltaVWeight = [0,0,0]
hidden = [0,0,1]
thetaz = 0
thetay = [0,0]

n = 0.3#学習係数
a = 0.9#慣性係数

def out(parameter,w):
    s = 0
    for index,value in enumerate(parameter):
        s += value*w[index]
    e = math.exp(-s)
    return 1/(1+e)

def output(x):
    for index,value in enumerate(weight):
        hidden[index] = out(x,value)
    return out(hidden,vWeight)

def study(x,t):
    z = output(x)
    thetaz = z*(1-z)*(t-z)
    thetay[0] = hidden[0]*(1-hidden[0])*thetaz
    thetay[1] = hidden[1]*(1-hidden[1])*thetaz
    for index,value in enumerate(vWeight):
        deltaVWeight[index] = n*thetaz*hidden[index] + deltaVWeight[index]*a
        vWeight[index] = vWeight[index] + deltaVWeight[index]

    for i in range(2):
        for j in range(3):
            deltaWeight[i][j] = n*thetay[i]*x[j] + deltaWeight[i][j]*a
            weight[i][j] = weight[i][j] + deltaWeight[i][j]

    #print weight
    #print deltaWeight
    return z 

arrayX = [[0,0,1],[1,0,1],[0,1,1],[1,1,1]]
arrayT = [0,1,1,0]
for i,v in enumerate(arrayX):
    print output(v)
while studyFlg:
    flg = False
    s = 0
    for x,t in zip(arrayX,arrayT):
        answer = study(x,t)
        answer = (answer - t)*(answer - t)
        s += answer
        flg = s > 0.0001 or flg
        studyFlg = flg
        #print output([0,0,1])

for i,v in enumerate(arrayX):
    print output(v)

出力結果

0.488758075607
0.488905017624
0.48887700069
0.489022967732
0.00499997290096
0.995327822571
0.995327066708
0.00559726403334