BP人工神经网络
2022/3/19 23:29:48
本文主要是介绍BP人工神经网络,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import math import numpy as np import pandas as pd from pandas import DataFrame y = [0.14, 0.64, 0.28, 0.33, 0.12, 0.03, 0.02, 0.11, 0.08] x1 = [0.29, 0.50, 0.00, 0.21, 0.10, 0.06, 0.13, 0.24, 0.28] x2 = [0.23, 0.62, 0.53, 0.53, 0.33, 0.15, 0.03, 0.23, 0.03] theata = [-1, -1, -1, -1, -1, -1, -1, -1, -1] x = np.array([x1, x2, theata]) W_mid = DataFrame(0.5, index=['input1', 'input2', 'theata'], columns=['mid1', 'mid2', 'mid3', 'mid4']) W_out = DataFrame(0.5, index=['input1', 'input2', 'input3', 'input4', 'theata'], columns=['a']) def sigmoid(x): # 映射函数 return 1 / (1 + math.exp(-x)) # 训练神经元 def train(W_out, W_mid, data, real): # 中间层神经元输入和输出层神经元输入 Net_in = DataFrame(data, index=['input1', 'input2', 'theata'], columns=['a']) Out_in = DataFrame(0, index=['input1', 'input2', 'input3', 'input4', 'theata'], columns=['a']) Out_in.loc['theata'] = -1 # 中间层和输出层神经元权值 W_mid_delta = DataFrame(0, index=['input1', 'input2', 'theata'], columns=['mid1', 'mid2', 'mid3', 'mid4']) W_out_delta = DataFrame(0, index=['input1', 'input2', 'input3', 'input4', 'theata'], columns=['a']) # 中间层的输出 for i in range(0, 4): Out_in.iloc[i] = sigmoid(sum(W_mid.iloc[:, i] * Net_in.iloc[:, 0])) # 输出层的输出/网络输出 res = sigmoid(sum(Out_in.iloc[:, 0] * W_out.iloc[:, 0])) # 误差 error = abs(res - real) # 输出层权值变化量 # yita =学习率 yita = 0.8 W_out_delta.iloc[:, 0] = yita * res * (1 - res) * (real - res) * Out_in.iloc[:, 0] W_out_delta.iloc[4, 0] = -(yita * res * (1 - res) * (real - res)) W_out = W_out + W_out_delta # 输出层权值更新 # 中间层权值变化量 for i in range(0, 4): W_mid_delta.iloc[:, i] = yita * Out_in.iloc[i, 0] * (1 - Out_in.iloc[i, 0]) * W_out.iloc[i, 0] * res * ( - res) * (real - res) * Net_in.iloc[:, 0] W_mid_delta.iloc[2, i] = -( yita * Out_in.iloc[i, 0] * (1 - Out_in.iloc[i, 0]) * W_out.iloc[i, 0] * res * (1 - res) * ( real - res)) W_mid = W_mid + W_mid_delta # 中间层权值更新 return W_out, W_mid, res, error def reault(data, W_out, W_mid): Net_in = DataFrame(data, index=['input1', 'input2', 'theata'], columns=['a']) Out_in = DataFrame(0, index=['input1', 'input2', 'input3', 'input4', 'theata'], columns=['a']) Out_in.loc['theata'] = -1 # 中间层的输出 for i in range(0, 4): Out_in.iloc[i] = sigmoid(sum(W_mid.iloc[:, i] * Net_in.iloc[:, 0])) # 输出层的输出/网络输出 res = sigmoid(sum(Out_in.iloc[:, 0] * W_out.iloc[:, 0])) return res for i in range(0, 9): W_out, W_mid, res, error = train(W_out, W_mid, x[0:, i], y[i]) res1 = reault([0.38, 0.49, -1], W_out, W_mid) res2 = reault([0.29, 0.47, -3], W_out, W_mid) print(res1, res2)
这篇关于BP人工神经网络的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24怎么修改Kafka的JVM参数?-icode9专业技术文章分享
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?