Java教程

bp神经网络

本文主要是介绍bp神经网络,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import math
import numpy as np
import pandas as pd
from pandas import DataFrame,Series

def sigmoid(x):
return 1/(1+math.exp(-x))
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 ]
yita=0.1
for i in range(9):
Net_in =DataFrame(0.6,index=['input1','input2','theata'],columns=['a'])
Out_in = DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
Net_in.loc['input1'] =x1[i]
Net_in.loc['input2']=x2[i]
real=y[i]
Net_in.loc['theata'] = -1
Out_in.loc['theata'] = -1
W_mid=DataFrame(0.7,index=['input1','input2','theata'],columns=['mid1','mid2','mid3','mid4'])
W_out=DataFrame(0.7,index=['input1','input2','input3','input4','theata'],columns=['a'])
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,0] = 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)
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*(1-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
testx1=[0.38,0.29]
testx2=[0.49,0.47]
for i in range(2):
Net_in =DataFrame(0.6,index=['input1','input2','theata'],columns=['a'])
Out_in = DataFrame(0,index=['input1','input2','input3','input4','theata'],columns=['a'])
Net_in.loc['input1'] =testx1[i]
Net_in.loc['input2']=testx2[i]
Net_in.loc['theata'] = -1
Out_in.loc['theata'] = -1
for i in range(0,4):
Out_in.iloc[i,0] = sigmoid(sum(W_mid.iloc[:,i]*Net_in.iloc[:,0]))
res = sigmoid(sum(Out_in.iloc[:,0]*W_out.iloc[:,0]))
print(res)

这篇关于bp神经网络的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!