用简单的python代码帮助理解神经网络反向传播

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 的更新方式
推荐看唐宇迪老师的讲课

参考

B站 唐宇迪 深度学习课程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值