本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此处不再介绍。
import numpy as np class NeuralNetwork(object): def __init__(self,input_nodes,hIDden_nodes,output_nodes,learning_rate): # Set number of nodes in input,hIDden and output layers.设定输入层、隐藏层和输出层的node数目 self.input_nodes = input_nodes self.hIDden_nodes = hIDden_nodes self.output_nodes = output_nodes # Initialize weights,初始化权重和学习速率 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函数,Activation function is the sigmoID function self.activation_function = (lambda x: 1/(1 + np.exp(-x))) def train(self,inputs_List,targets_List): # Convert inputs List to 2d array inputs = np.array(inputs_List,ndmin=2).T # 输入向量的shape为 [feature_dIEmension,1] targets = np.array(targets_List,ndmin=2).T # 向前传播,Forward pass # Todo: HIDden layer hIDden_inputs = np.dot(self.weights_input_to_hIDden,inputs) # signals into hIDden layer hIDden_outputs = self.activation_function(hIDden_inputs) # signals from hIDden layer # 输出层,输出层的激励函数就是 y = x final_inputs = np.dot(self.weights_hIDden_to_output,hIDden_outputs) # signals into final output layer final_outputs = final_inputs # signals from final output layer ### 反向传播 Backward pass,使用梯度下降对权重进行更新 ### # 输出误差 # Output layer error is the difference between desired target and actual output. output_errors = (targets_List-final_outputs) # 反向传播误差 Backpropagated error # errors propagated to the hIDden layer hIDden_errors = np.dot(output_errors,self.weights_hIDden_to_output)*(hIDden_outputs*(1-hIDden_outputs)).T # 更新权重 Update the weights # 更新隐藏层与输出层之间的权重 update hIDden-to-output weights with gradIEnt descent step self.weights_hIDden_to_output += output_errors * hIDden_outputs.T * self.lr # 更新输入层与隐藏层之间的权重 update input-to-hIDden weights with gradIEnt descent step self.weights_input_to_hIDden += (inputs * hIDden_errors * self.lr).T # 进行预测 def run(self,inputs_List): # Run a forward pass through the network inputs = np.array(inputs_List,ndmin=2).T #### 实现向前传播 Implement the forward pass here #### # 隐藏层 HIDden layer hIDden_inputs = np.dot(self.weights_input_to_hIDden,inputs) # signals into hIDden layer hIDden_outputs = self.activation_function(hIDden_inputs) # signals from hIDden layer # 输出层 Output layer final_inputs = np.dot(self.weights_hIDden_to_output,hIDden_outputs) # signals into final output layer final_outputs = final_inputs # signals from final output layer return final_outputs
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:python构建深度神经网络(DNN)python实现简单神经网络算法Python实现的径向基(RBF)神经网络示例Python编程实现的简单神经网络算法示例python实现神经网络感知器算法python机器学习之神经网络(三)python机器学习之神经网络(二)Python实现感知器模型、两层神经网络Python与人工神经网络:使用神经网络识别手写图像介绍python构建深度神经网络(续) 总结以上是内存溢出为你收集整理的Python使用numpy实现BP神经网络全部内容,希望文章能够帮你解决Python使用numpy实现BP神经网络所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)