BP神经网络-python代码实现

神经网络模型
在这里插入图片描述
代码实现

import random

import math

rate = 0.01
x = [1, 1, 1]
print("迭代前")
weight1 = [random.random(), random.random(), random.random(), random.random()]  # 表示输入层第一个元素与隐藏层每个元素之间的权值
weight2 = [random.random(), random.random(), random.random(), random.random()]
weight3 = [random.random(), random.random(), random.random(), random.random()]
weight4 = [random.random(), random.random(), random.random(), random.random()]  # 表示输出层第一个元素与隐藏层每个元素之间的权值
weight5 = [random.random(), random.random(), random.random(), random.random()]  # 表示输出层第二个元素与隐藏层每个元素之间的权值
t = [10, 10]

print(weight1)
print(weight2)
print(weight3)
print(weight4)
print(weight5)

print("********************")


def sigmoid(m):
    return 1 / (1 + math.exp(-m))


# 对输出层
def delta():
    y = []

    y11 = sigmoid(y1) * (1 - sigmoid(y1)) * (sigmoid(y1) - t[0])
    y21 = sigmoid(y2) * (1 - sigmoid(y2)) * (sigmoid(y2) - t[1])

    y.append(y11)
    y.append(y21)

    return y


def sigma():
    res = 0
    for j in range(4):
        res += delta()[0] * weight4[j] + delta()[1] * weight5[j]
    return res


for i in range(100):
    # 利用前向传播进行预测
    a1 = sigmoid(weight1[0] * x[0] + weight2[0] * x[1] + weight3[0] * x[2])
    a2 = sigmoid(weight1[1] * x[0] + weight2[1] * x[1] + weight3[1] * x[2])
    a3 = sigmoid(weight1[2] * x[0] + weight2[2] * x[1] + weight3[2] * x[2])
    a4 = sigmoid(weight1[3] * x[0] + weight2[3] * x[1] + weight3[3] * x[2])

    y1 = sigmoid(a1 * weight4[0] + a2 * weight4[1] + a3 * weight4[2] + a4 * weight4[3])

    y2 = sigmoid(a1 * weight5[0] + a2 * weight5[1] + a3 * weight5[2] + a4 * weight5[3])

    # 利用后向传播进行训练
    # 对隐藏层
    delta1 = a1 * (1 - a1) * sigma()
    delta2 = a2 * (1 - a2) * sigma()
    delta3 = a3 * (1 - a3) * sigma()
    delta4 = a4 * (1 - a4) * sigma()

    weight1[0] = weight1[0] - rate * delta1
    weight1[1] = weight1[1] - rate * delta2
    weight1[2] = weight1[2] - rate * delta3
    weight1[3] = weight1[3] - rate * delta4

    weight2[0] = weight2[0] - rate * delta1
    weight2[1] = weight2[1] - rate * delta2
    weight2[2] = weight2[2] - rate * delta3
    weight2[3] = weight2[3] - rate * delta4

    weight3[0] = weight3[0] - rate * delta1
    weight3[1] = weight3[1] - rate * delta2
    weight3[2] = weight3[2] - rate * delta3
    weight3[3] = weight3[3] - rate * delta4

    weight4[0] = weight4[0] - rate * delta()[0]
    weight4[1] = weight4[1] - rate * delta()[0]
    weight4[2] = weight4[2] - rate * delta()[0]
    weight4[3] = weight4[3] - rate * delta()[0]

    weight5[0] = weight5[0] - rate * delta()[1]
    weight5[1] = weight5[1] - rate * delta()[1]
    weight5[2] = weight5[2] - rate * delta()[1]
    weight5[3] = weight5[3] - rate * delta()[1]

print("迭代后")
print(weight1)
print(weight2)
print(weight3)
print(weight4)
print(weight5)


运行结果

"D:\py project\exer\venv\Scripts\python.exe" "D:/py project/exer/BPNeuralNet.py"
迭代前
[0.6749607312139871, 0.72473923486996, 0.09567769814501037, 0.19543508716273783]
[0.9480136217394588, 0.78660703084072, 0.9494596065013234, 0.03326308822834956]
[0.03979060320446026, 0.8702010897572475, 0.24010512416547514, 0.8433444512401135]
[0.6136627683014735, 0.8112779287264847, 0.08326044079467965, 0.360091538512604]
[0.31328870558368593, 0.08221450543994302, 0.3888970740339499, 0.9522872815161906]
********************
迭代后
[1.4926069142352807, 1.3376534106464741, 1.0260221454441008, 1.1904378925716226]
[1.7656598047607523, 1.3995212066172344, 1.8798040538004144, 1.0282658936372342]
[0.8574367862257543, 1.4831152655337618, 1.170449571464566, 1.8383472566489978]
[2.4582950326913084, 2.6559101931163203, 1.9278927051845134, 2.2047238029024383]
[2.161910787519616, 1.930836587375873, 2.2375191559698795, 2.800909363452121]

Process finished with exit code 0

由于没有合适数据进行检验,所以无法确定代码的准确性。

参考资料
参考资料

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值