# bp神经网络怎么实际应用啊？Python

``````
import numpy as np

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def main():
# 14条数据
data = np.array([
[1, 0, 0, 1, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 0],
[1, 1, 0, 1, 1, 1, 0, 1],
[0, 1, 0, 1, 0, 0, 1, 1],
[1, 0, 1, 1, 0, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 1],
[1, 0, 1, 1, 0, 1, 1, 1],
[1, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 1, 1],
[1, 0, 0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 1, 1, 1]])
print("原始数据：\n", data)
# 十四条数据的跳高成绩
highJump = np.array(
[0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0])
print("十四条数据的跳高成绩：\n", highJump)
# 第十五条数据的输入
data15 = np.array([0, 1, 0, 1, 1, 0, 1, 0])
print("第十五条数据的输入：\n", data15)
# 设置输入层与隐藏层之间的权值和阈值
wInput = np.random.random(size=(6, 8))/10
print("输入层与隐藏层之间的六组权值:\n", wInput)
bInput = np.random.random(size=(6, 8))/10
print("输入层与隐藏层之间的六组阈值:\n", bInput)
# 设置隐藏层与输出层之间的权值和阈值
wOutput = np.random.random(size=6)/10
print("隐藏层与输出层之间的一组权值", wOutput)
bOutput = np.random.random(size=6)/10
print("隐藏层与输出层之间的一组阈值", bOutput)
loss = 1
count = 0
while loss > 0.1:
count = count + 1
loss = 0
outputNode = []
for i in range(0, 14):

# 正向传播
# 计算隐藏层节点输入
hide = []
for j in range(0, 6):
hideNode = 0
for k in range(0, 8):
hideNode = data[i, k] * wInput[j, k] + \
bInput[j, k] + hideNode
# print(hideNode)
hideNode = sigmoid(hideNode)  # 激活函数
hide.append(hideNode)
hide = np.array(hide)
# print("隐藏层结点", hide)
output = 0
for j in range(0, 6):
output = hide[j] * wOutput[j] + bOutput[j] + output
output = sigmoid(output)
outputNode.append(output)
# print("输出层结点", output)
loss = ((output - highJump[i]) * (output - highJump[i])) / 2 + loss
outputNode = np.array(outputNode)
# 反向传播
# print("隐藏层结点", hide)
for i in range(0, 14):
# 隐藏层与输出层之间权值阈值更新
wOutputLoss = []
for j in range(0, 6):
wOutputLoss.append((outputNode[i] - highJump[i]) *
outputNode[i] * (1 - outputNode[i])
* hide[j])
wOutputLoss = np.array(wOutputLoss)
# print("wOutputLoss", wOutputLoss)
bOutputLoss = []
for j in range(0, 6):
bOutputLoss.append((outputNode[i] - highJump[i]) *
outputNode[i] * (1 - outputNode[i]))
bOutputLoss = np.array(bOutputLoss)
# print("bOutputLoss", bOutputLoss)
for j in range(0, 6):
wOutput[j] = wOutput[j] - 0.1 * wOutputLoss[j]
bOutput[j] = bOutput[j] - 0.1 * bOutputLoss[j]
# print("隐藏层与输出层更新后权值和阈值", wOutput, bOutput)
# 输入层与隐藏层之间权值更新
wInputLoss = np.ones((6, 8)) * 0
for j in range(0, 6):
for k in range(0, 8):
wInputLoss[j][k] = ((outputNode[i] - highJump[i]) *
outputNode[i] *
(1 - outputNode[i]) * wOutput[j]
* hide[j] * (1 - hide[j]) * data[i][k])
wInputLoss = np.array(wInputLoss)
# print("wIutputLoss", wInputLoss)
bInputLoss = np.ones((6, 8)) * 0
for j in range(0, 6):
for k in range(0, 8):
bInputLoss[j][k] = ((outputNode[i] - highJump[i]) *
outputNode[i] * (1 - outputNode[i]) *
wOutput[j] * hide[j] * (1 - hide[j]))
bInputLoss = np.array(bInputLoss)
# print("bIutputLoss", bInputLoss)
for j in range(0, 6):
for k in range(0, 8):
wInput[j][k] = wInput[j][k] - 0.1 * wInputLoss[j][k]
bInput[j][k] = bInput[j][k] - 0.1 * bInputLoss[j][k]
# print("输入层与隐藏层之间更新后的权值和阈值", wInput, bInput)
# print("输出", output)
print("学习前的loss", loss)
loss = 0
for i in range(0, 14):
# 正向传播
# 计算隐藏层节点输入
hide = []
for j in range(0, 6):
hideNode = 0
for k in range(0, 8):
hideNode = data[i, k] * wInput[j, k] + \
bInput[j, k] + hideNode
hideNode = sigmoid(hideNode)  # 激活函数
hide.append(hideNode)
hide = np.array(hide)
output = 0
for j in range(0, 6):
output = hide[j] * wOutput[j] + bOutput[j] + output
output = sigmoid(output)
loss = ((output - highJump[i]) * (output - highJump[i])) / 2 + loss
# print("输出", output)
print("学习后的loss", loss)

# 预测
hide = []
for j in range(0, 6):
hideNode = 0
for k in range(0, 8):
hideNode = data15[k] * wInput[j, k] + \
bInput[j, k] + hideNode
hideNode = sigmoid(hideNode)  # 激活函数
hide.append(hideNode)
hide = np.array(hide)
output = 0
for j in range(0, 6):
output = hide[j] * wOutput[j] + bOutput[j] + output
output = sigmoid(output)
print(output)
print(loss)
print(count)

if __name__ == '__main__':
main()

``````
• 写回答

#### 2条回答默认 最新

• 好好学习搞钱要紧 2018-10-28 12:31
关注

解决了。。。。。。。

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 cv::resize不同线程时间不同
• ¥15 web课程，怎么做啊😭没好好听课 根本不知道怎么下手
• ¥15 做一个关于单片机的比较难的代码，然后搞一个PPT进行解释
• ¥15 python提取.csv文件中的链接会经常出现爬取失败
• ¥15 数据结构中的数组地址问题
• ¥15 maya的mel里，怎样先选择模型A，然后利用mel脚本自动选择有相同名字的模型B呢。
• ¥15 Python题，根本不会啊
• ¥15 会会信号与系统和python的来
• ¥15 关于#python#的问题
• ¥20 oracle RAC 怎么配置啊，配置