介绍
这是一个简单的三层全连接网络,所谓全连接网络,就是每一层的每一个节点均与前一层的每一个节点相连,如下所示:、均与、相连 ;、也均与、相连
计算步骤
初始状态:
前向计算:
根据初始状态图可得以下公式:
第一层到第二层的节点间计算:
使用激活函数得到、:
第二层到第三层的节点间计算:
使用激活函数得到、:
根据公式可得以下值:
以 为例:
同理可得
以 为例:
同理可得
通过得到的结果可以看到输出的结果和预期输出的结果存在差异,则需要更新参数
反向计算(更新参数):
更新参数、、、
一次反向传播完成,再次正向计算可得到新的实际输出
迭代上述运算过程10000次后,输出为[0.015914607588423745, 0.9840961824613796],已经
非常接近期望输出
代码
import numpy as np
# i1 = 0.05; i2 = 0.10
# out1 = 0.01;out2 = 0.99
# w1 = 0.15 ; w2 = 0.20; w3 = 0.25; w4 = 0.30; b1 = 0.35
# w5 = 0.4 ; w6 = 0.45; w7 = 0.5; w8 = 0.55; b2 = 0.6
def cacu():
i1 = 0.05; i2 = 0.10;
out1 = 0.01;out2 = 0.99
w1 = 0.15; w2 = 0.20; w3 = 0.25; w4 = 0.30; b1 = 0.35
w5 = 0.4; w6 = 0.45; w7 = 0.5; w8 = 0.55; b2 = 0.6
for i in range(10000):
net_h1 = w1 * i1 + w2 * i2 + b1
net_h2 = w3 * i1 + w4 * i2 + b1
h1 = 1 / ( np.exp(-net_h1)+1 )
h2 = 1 / ( np.exp(-net_h2)+1 )
net_o1 = w5 * h1 + w6 * h2 + b2
net_o2 = w7 * h1 + w8 * h2 + h2
o1 = 1 / ( np.exp(-net_o1)+1 )
o2 = 1 / ( np.exp(-net_o2)+1 )
print("o1:",o1,o2 )
E_total = (o1-out1)*(o1-out1)*0.5 + (o2-out2)*(o2-out2)*0.5
print("total:",E_total)
Etotal_O1 = (out1 - o1) * -1
Eo1_neto1 = o1 *( 1-o1 )
neto1_w5 = h1
change_w5 = Etotal_O1 * Eo1_neto1 * neto1_w5
w5_new = w5 - change_w5 *0.5
print("w5: ",w5_new)
neto1_w6 = h2
change_w6 = Etotal_O1 * Eo1_neto1 * neto1_w6
w6_new = w6 - change_w6 * 0.5
print("w6: ",w6_new)
Etotal_O2 = (out2 - o2) * -1
Eo2_neto2 = o2 * (1 - o2)
neto2_w7 = h1
change_w7 = Etotal_O2 * Eo2_neto2 * neto2_w7
w7_new = w7 - change_w7 * 0.5
print("w7: ",w7_new)
neto2_w8 = h2
change_w8 = Etotal_O2 * Eo2_neto2 * neto2_w8
w8_new = w8 - change_w8 * 0.5
print("w8: ",w8_new)
#计算 Etotal_w1
# 计算 Etotal_outh1
# 计算 E01_outh1
E01_outh1 = Etotal_O1 * Eo1_neto1 * w5
print("E01_outh1 : ", E01_outh1)
# 计算 E02_outh1
E02_outh1 = Etotal_O2 * Eo2_neto2 * w7
print("E02_outh1 : ", E02_outh1)
Etotal_outh1 = E02_outh1 + E01_outh1
print("Etotal_outh1 : ", Etotal_outh1)
# 计算 outh1_neth1
outh1_neth1 = h1 * ( 1- h1 )
print("out1_neth1: ",outh1_neth1)
# 计算neth1_w1
neth1_w1 = i1
Etotal_w1 = Etotal_outh1 * outh1_neth1 * neth1_w1
print("Etotal_w1:",Etotal_w1)
w1_new = w1 - 0.5 * Etotal_w1
print("w1_new:",w1_new)
# 计算neth1_w2
neth1_w2 = i2
Etotal_w2 = Etotal_outh1 * outh1_neth1 * neth1_w2
print("Etotal_w1:", Etotal_w2)
w2_new = w2 - 0.5 * Etotal_w2
print("w2_new:", w2_new)
# 计算 Etotal_w3
# 计算 Etotal_outh2
# 计算 E01_outh2
E01_outh2 = Etotal_O1 * Eo1_neto1 * w6
print("E01_outh1 : ", E01_outh1)
# 计算 E02_outh2
E02_outh2 = Etotal_O2 * Eo2_neto2 * w8
print("E02_outh1 : ", E02_outh1)
Etotal_outh1 = E02_outh2 + E01_outh2
print("Etotal_outh1 : ", Etotal_outh1)
# 计算 outh2_neth2
outh2_neth2 = h2 * (1 - h2)
print("out1_neth1: ", outh2_neth2)
# 计算neth1_w3
neth1_w3 = i1
Etotal_w3 = Etotal_outh1 * outh1_neth1 * neth1_w3
print("Etotal_w3:", Etotal_w3)
w3_new = w3 - 0.5 * Etotal_w3
print("w3_new:", w3_new)
# 计算neth1_w4
neth1_w4 = i2
Etotal_w4 = Etotal_outh1 * outh1_neth1 * neth1_w4
print("Etotal_w4:", Etotal_w4)
w4_new = w4 - 0.5 * Etotal_w4
print("w4_new:", w4_new)
w1 = w1_new; w2 = w2_new; w3 = w3_new; w4 = w4_new;
w5 = w5_new; w6 = w6_new; w7 = w7_new; w8 = w8_new;
print("o1: ", o1 )
print("o2: ", o2)
def main():
cacu()
if __name__ == "__main__":
main()
示例输出:
来源:CSDN
作者:油醋三椒
链接:https://blog.csdn.net/weixin_42664622/article/details/103670903