感觉是权值更新部分出错了
运行出来都是0.5左右 不知道为啥
import math
import numpy as np
import random as rd
def sigmoid(x):
f = 1/(1+np.exp(-1*x))
return f
def sigmoid_(x):
f = [[sigmoid(x[i][j])*(1-sigmoid(x[i][j])) for j in range(len(x[i]))] for i in range(len(x))]
return f
def differentail_matrix(x):
f = np.diag(x[0])
return f
input_X = np.array([[0,0],[1,0],[0,1],[1,1]])
#print(input_X.shape)
input_Y = np.array([[0],[1],[1],[0]])
#print(input_Y.shape)
W1 = np.random.rand(2,2) #later input,former output
b1 = np.zeros([2,1])
#W1 = np.array([[2,2],[-1,-1]])
#b1 = np.array([[-1],[1.5]])
W2 = np.random.rand(1,2)
b2 = np.random.rand(1,1)
# W2 = np.array([1,1])
# W2.shape = 1,2
# b2 = np.array([-1.0])
#print(W1,b1)
alpha = 0.05 #learn rate
for k in range(10000):
r = rd.sample([0,1,2,3], 1) #随机抽取
X = np.array(input_X[r])
X.shape = 2,1 #transpose
Y = np.array(input_Y[r])
out1 = sigmoid(np.dot(W1,X) + b1)
pred_y = sigmoid(np.dot(W2,out1) + b2)
#print('predy:',pred_y)
err = Y-pred_y
#print('err:',err)
#back propagation
s2 = -2*sigmoid(pred_y)*(1-sigmoid(pred_y))*err #计算敏感度
temp = sigmoid_(out1)
temp = np.array(temp)
temp.shape = 1, 2
temp2 = np.array(differentail_matrix(temp))
s1 = np.dot(temp2, W2.T)*s2
W2 = W2-alpha*s2*np.transpose(out1) #权值更新
b2 = b2 - alpha*s2
W1 = W1 - alpha*np.dot(s1,np.transpose(X))
b1 = b1 - alpha*s1
#print('第:',i,'次迭代','\n','权值1',W1,'\n',b1)
#print()
if k%500 == 0:
out1 = sigmoid(np.dot(W1,np.transpose(input_X)) + b1)
pred_y = sigmoid(np.dot(W2,out1) + b2)
print(pred_y)