Code:
import numpy as np
"""Sigmod激活函数"""
def sigmod(x):
return 1.0/(1.0 + np.exp(-x))
def BackpropCE(W1,W2,X,D):
alpha = 0.9 #学习率
N=4 #4层网络
for k in range(N):
x = X[k,:].T #对数据每行转置
d = D[k] #每行对应结果(标签)
v1 = np.matmul(W1, x)
y1 = sigmod(v1)
v = np.matmul(W2,y1)
y = sigmod(v)
e = d - y
delta = e*y*(1-y)
e1 = np.matmul(W2.T,delta)
delta1 = y1*(1-y1)*e1
dW1 = (alpha*delta1).reshape(4,1)
W1 += dW1
dW2 = alpha * delta *y1
W2 += dW2
return W1,W2
def TestBackpropCE():
X = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
D = np.array([[0],[1],[1],[0]])
W1 = 2*np.random.random((4,3))-1
W2 = 2*np.random.random((1,4))-1
for epoch in range(10000):
W1,W2 = BackpropCE(W1, W2, X, D)
N = 4
for k in range(N):
x = X[k,:].T
v1 = np.matmul(W1,x)
y1 = sigmod(v1)
v = np.matmul(W2,y1)
y = sigmod(v)
print(y)
if __name__ == "__main__":
TestBackpropCE()