原理部分
BP神经网络(算法整体思路及原理+手写公式推导)_秃头小苏的博客-CSDN博客_bp神经网络算法流程图
非常感谢这位博主
import timeimport numpy as np
from BPNETpredata import traindata
from BPNETpredata import labledata
# BPNNdef sigmoid(x):return 1.0 / (1.0 + np.exp(-x))def input(traindata):for i in traindata:return len(i)def var(y1, y2, rate): # y1:out y2:static outreturn -rate * (y2 - y1) * y1 * (1 - y1)rate = 0.6
w1 = -1 + 2 * np.random.rand(9, 3)
w2 = -1 + 2 * np.random.rand(3, 1)
b1 = -1 + 2 * np.random.rand(1, 3)
b2 = -1 + 2 * np.random.rand(1, 1)
epoch = 9999999999999# forward 3hidden 9input 1.1bias 1 out 3level
# INTALIZING
def bpnntrain(w1, w2, b1, b2, a, c, epoch, rate):for epoch in range(epoch):# step foward 1hidden = np.dot(a, w1)hidden = hidden - b1shidden = []for j in hidden:shidden.append(sigmoid(j))out = sigmoid(np.dot(shidden, w2)[0] - b2[0][0])# work out errorloss = 0.5 * pow((c[0] - out[0]), 2) # loss functionaccurate = (out[0] - c[0]) / c[0]if pow(pow(accurate, 2), 0.5) < 0.01:print(epoch, out[0], c[0], accurate)break# update b2varb2 = var(out[0], c[0], rate)b2[0][0] = b2[0][0] + varb2# update w2for i in range(len(shidden[0])):w2[i][0] = w2[i][0] - var(out[0], c[0], rate) * shidden[0][i]# update b1for i in range(len(shidden[0])):b1[0][i] = b1[0][i] + var(out[0], c[0], rate) * w2[i][0] * shidden[0][i] * (1 - shidden[0][i])# update w1for i in range(len(w1)):for j in range(len(shidden[0])):w1[i][j] = w1[i][j] + var(out[0], c[0], rate) * w2[j][0] * shidden[0][j] * (1 - shidden[0][j]) * a[i]return [w1, w2, b1, b2]
#train
for i in traindata:for j in labledata:a=ic=[j]qaq=bpnntrain(w1, w2, b1, b2, a, c, epoch, rate)
#predict
def bpnnpredict(w1,w2,b1,b2,a,c):hidden = np.dot(a, w1)hidden = hidden - b1shidden = []for j in hidden:shidden.append(sigmoid(j))out = sigmoid(np.dot(shidden, w2)[0] - b2[0][0])# work out errorloss = 0.5 * pow((c[0] - out[0]), 2) # loss functionaccurate = (out[0] - c[0]) / c[0]print(out[0],c[0],accurate)
list1=[0.123,0.124,0.125,0.124,0.1256,0.111,0.12312,0.1285,0.1231245]
list2=[1]bpnnpredict(qaq[0],qaq[1],qaq[2],qaq[3],list1,list2)