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))
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]
y=[0.14,0.64,0.28,0.33,0.12,0.03,0.02,0.11,0.08]
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)
import numpy
import scipy.special
import scipy.misc
import matplotlib.pyplot
import scipy.ndimage
import math
import pandas as pd
from pandas import DataFrame,Series
class NeuralNetwork():
def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
self.lr = learningrate
self.wih = numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))
self.activation_function = lambda x: scipy.special.expit(x)
pass
def train(self,input_list,target_list):
inputs=numpy.array(input_list,ndmin=2).T
targets=numpy.array(target_list,ndmin=2).T
hidden_inputs=numpy.dot(self.wih,inputs)
hidden_outputs=self.activation_function(hidden_inputs)
hidden_outputs1=numpy.append(hidden_outputs,-1)
final_inputs=numpy.dot(self.who,hidden_outputs)
final_outputs=self.activation_function(final_inputs)
output_errors=targets-final_outputs
hidden_errors=numpy.dot(self.who.T,output_errors)
self.who+=self.lr*numpy.dot((output_errors*final_outputs*(1.0-final_outputs)),numpy.transpose(hidden_outputs))
self.wih+=self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)),numpy.transpose(inputs))
pass
def query(self,input_list):
inputs=numpy.array(input_list,ndmin=2).T
hidden_inputs=numpy.dot(self.wih,inputs)
hidden_outputs=self.activation_function(hidden_inputs)
final_inputs=numpy.dot(self.who,hidden_outputs)
final_outputs=self.activation_function(final_inputs)
return final_outputs
print('n')
input_nodes=2
hidden_nodes=3
output_nodes=1
learning_rate=0.5
n=NeuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
training_data_file=open(r'D:\wx\WeChat Files\wxid_jiprlm7rzg9u11\FileStorage\File\2022-03\3.3 data_tr.txt')
training_data_list=training_data_file.readlines();
training_data_file.close()
for record in training_data_list[1:]:
all_values=record.split(',')
inputs=(numpy.asfarray(all_values[0:2]))
targets=numpy.zeros(output_nodes)+0.01
targets[0]=all_values[2]
n.train(inputs,targets)
pass
test_data_file=open(r'D:\wx\WeChat Files\wxid_jiprlm7rzg9u11\FileStorage\File\2022-03\3.3 data_te.txt')
test_data_list=test_data_file.readlines()
test_data_file.close()
scorecard=[]
total=0
correct=0
for record in test_data_list[1:]:
total+=1
all_values=record.split(',')
correct_label=all_values[2]
inputs=(numpy.asfarray(all_values[0:2]))
outputs=n.query(inputs)
print(outputs)
标签:inputs,res,self,神经网络,bp,outputs,iloc,numpy 来源: https://www.cnblogs.com/nesum6/p/16027755.html