本文主要使用下面的网络结构来完成 异或运算 异或运算 : 0^0 = 0, 1^0 = 1, 0^1 = 1, 1^1 = 0 。
上图的公式推导可以参考博文: 三层神经网络前向后向传播示意图
import numpy as np
def nonlin(x, deriv = False):
if(deriv == True):
return x*(1-x)
else:
return 1/(1+np.exp(-x))
#input dataset
X = np.array([[0,0],
[1,0],
[0,1],
[1,1]])
#output dataset
y = np.array([[0,1,1,0]]).T
1 #the first-hidden layer weight value
syn0 = 2*np.random.random((2,3)) -
#the hidden-hidden layer weight value
syn1 = 2*np.random.random((3,2)) - 1
#the hidden-output layer weight value
syn2 = 2*np.random.random((2,1)) - 1
for j in range(60000):
#the first layer,and the input layer
l0 = X
# the first hidden layer
l1 = nonlin(np.dot(l0,syn0))
# the second hidden layer
l2 = nonlin(np.dot(l1,syn1))
# the output layer
l3 = nonlin(np.dot(l2,syn2))
l3_error = y-l3 #the hidden-output layer error
if(j%10000) == 0:
print ("Error:"+str(np.mean(l3_error)))
l3_delta = l3_error*nonlin(l3,deriv = True)
l2_error = l3_delta.dot(syn2.T)
l2_delta = l2_error*nonlin(l2,deriv = True)
#the first-hidden layer error
l1_error = l2_delta.dot(syn1.T)
l1_delta = l1_error*nonlin(l1,deriv = True)
syn2 += l2.T.dot(l3_delta)
syn1 += l1.T.dot(l2_delta)
syn0 += l0.T.dot(l1_delta)
print( "outout after Training:")
print (l3)