import numpy as np
# sigmoid函数 及其 求导
def sigmoid(x, deriv=False):
if(deriv == True):
return x*(1-x)
return 1/(1+np.exp(-x))
# 初始化x, y ,w0, w1
x = np.array([[0, 0, 1],
[0, 1, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1]])
print(x.shape)
y = np.array([[0],
[1],
[1],
[0],
[0]])
print(y.shape)
np.random.seed(1)
# w0, w1 取值范围为(-1, 1)
w0 = 2 * np.random.random((3, 4)) - 1
w1 = 2 * np.random.random((4, 1)) - 1
# 开始传播
for j in range(60000):
# Forward propagation
l0 = x
l1 = sigmoid(np.dot(l0, w0))
l2 = sigmoid(np.dot(l1, w1))
l2_error = y - l2
if (j % 10000) == 0:
print('Error' + str(np.mean(np.abs(l2_error))))
# Back propagation
# 对应相乘
l2_delta = l2_error * sigmoid(l2, deriv=True)
# print(l2_delta.shape)
l1_error = l2_delta.dot(w1.T)
l1_delta = l1_error * sigmoid(l1, deriv=True)
# print(l1_delta.shape)
w1 += l1.T.dot(l2_delta)
w0 += l0.T.dot(l1_delta)
输出为
(5, 3)
(5, 1)
[[-0.16595599 0.44064899 -0.99977125 -0.39533485]
[-0.70648822 -0.81532281 -0.62747958 -0.30887855]
[-0.20646505 0.07763347 -0.16161097 0.370439 ]]
Error0.4918739397364802
Error0.009794284463972347
Error0.006493076538438722
Error0.005144157678377004
Error0.004372749181697061
Error0.0038603867449685143
此图描述矩阵的传递过程,以及w0 和 w1 的更新方式
推荐看唐宇迪老师的讲课