Python使用numpy实现BP神经网络
本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此处不再介绍。
importnumpyasnp classNeuralNetwork(object): def__init__(self,input_nodes,hidden_nodes,output_nodes,learning_rate): #Setnumberofnodesininput,hiddenandoutputlayers.设定输入层、隐藏层和输出层的node数目 self.input_nodes=input_nodes self.hidden_nodes=hidden_nodes self.output_nodes=output_nodes #Initializeweights,初始化权重和学习速率 self.weights_input_to_hidden=np.random.normal(0.0,self.hidden_nodes**-0.5, (self.hidden_nodes,self.input_nodes)) self.weights_hidden_to_output=np.random.normal(0.0,self.output_nodes**-0.5, (self.output_nodes,self.hidden_nodes)) self.lr=learning_rate #隐藏层的激励函数为sigmoid函数,Activationfunctionisthesigmoidfunction self.activation_function=(lambdax:1/(1+np.exp(-x))) deftrain(self,inputs_list,targets_list): #Convertinputslistto2darray inputs=np.array(inputs_list,ndmin=2).T#输入向量的shape为[feature_diemension,1] targets=np.array(targets_list,ndmin=2).T #向前传播,Forwardpass #TODO:Hiddenlayer hidden_inputs=np.dot(self.weights_input_to_hidden,inputs)#signalsintohiddenlayer hidden_outputs=self.activation_function(hidden_inputs)#signalsfromhiddenlayer #输出层,输出层的激励函数就是y=x final_inputs=np.dot(self.weights_hidden_to_output,hidden_outputs)#signalsintofinaloutputlayer final_outputs=final_inputs#signalsfromfinaloutputlayer ###反向传播Backwardpass,使用梯度下降对权重进行更新### #输出误差 #Outputlayererroristhedifferencebetweendesiredtargetandactualoutput. output_errors=(targets_list-final_outputs) #反向传播误差Backpropagatederror #errorspropagatedtothehiddenlayer hidden_errors=np.dot(output_errors,self.weights_hidden_to_output)*(hidden_outputs*(1-hidden_outputs)).T #更新权重Updatetheweights #更新隐藏层与输出层之间的权重updatehidden-to-outputweightswithgradientdescentstep self.weights_hidden_to_output+=output_errors*hidden_outputs.T*self.lr #更新输入层与隐藏层之间的权重updateinput-to-hiddenweightswithgradientdescentstep self.weights_input_to_hidden+=(inputs*hidden_errors*self.lr).T #进行预测 defrun(self,inputs_list): #Runaforwardpassthroughthenetwork inputs=np.array(inputs_list,ndmin=2).T ####实现向前传播Implementtheforwardpasshere#### #隐藏层Hiddenlayer hidden_inputs=np.dot(self.weights_input_to_hidden,inputs)#signalsintohiddenlayer hidden_outputs=self.activation_function(hidden_inputs)#signalsfromhiddenlayer #输出层Outputlayer final_inputs=np.dot(self.weights_hidden_to_output,hidden_outputs)#signalsintofinaloutputlayer final_outputs=final_inputs#signalsfromfinaloutputlayer returnfinal_outputs
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。