BP神经网络搭建代码

 如下程序为经典BP算法,BP算法的主要工作为:

  1. 根据输入结合权重计算隐藏层输入值 ,并对隐藏层输入值使用激活函数处理,压缩输入值,然后使用同样的方式计算输出层的值。
  2. 根据输出层的值计算误差,并进行误差的反向传播,根据梯度下降法获取误差值最小时的权重变化,更新权重,不断训练,权重不断优化,最终达到良好的训练效果。

此程序主要对手写数据集mnist进行训练,欢迎大家一起讨论!(点击数据集下载

import numpy
#sigmoid function:scipy.special.expit
import scipy.special
import matplotlib.pyplot
%matplotlib inline

class neuralNetwork:
    #initialise the neural network
    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
        #set nodes and learningrate
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        self.lr = learningrate
        
        #set weight,incloud weight_input_hidden,weight_hidden_output (random)
        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))
        
        #sigmoid function
        self.activation_function = lambda x: scipy.special.expit(x)
        pass
    def train(self,inputs_list,targets_list):
        #convert inputs list to 2d array
        inputs = numpy.array(inputs_list,ndmin=2).T
        targets = numpy.array(targets_list,ndmin=2).T
        
        #calculate signals into hidden layer
        hidden_inputs = numpy.dot(self.wih,inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        
        #calculate signals into output layer
        final_inputs = numpy.dot(self.who,hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        
        output_errors = targets - final_outputs
        #hidden_errors = who.T * output_errors
        hidden_errors = numpy.dot(self.who.T,output_errors)
        
        #update the weights for the links between the hidden and output layers
        self.who += self.lr*numpy.dot((output_errors*final_outputs*(1.0-final_outputs)),numpy.transpose(hidden_outputs))
        #updata the weights for the links between the input and hidden layers
        self.wih += self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)),numpy.transpose(inputs))
        pass
    def query(self,inputs_list):
        inputs = numpy.array(inputs_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

input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learning_rate = 0.1
#create instance of neural network
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)

#load train data
training_data_file = open('mnist_dataset/mnist_train_100.csv','r')
training_data_list = training_data_file.readlines()
training_data_file.close()

#train the neural network
#train times
epochs = 5
for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
        targets = numpy.zeros(output_nodes) +0.01
        targets[int(all_values[0])] = 0.99
        n.train(inputs,targets)
        pass
    pass

#load test data
test_data_file = open('mnist_dataset/mnist_test_10.csv','r')
test_data_list = test_data_file.readlines()
test_data_file.close()

scorecard = []
#test the neural network
for record in test_data_list:
    all_values = record.split(',')
    correct_label = int(all_values[0])
    inputs = (numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
    outputs = n.query(inputs)
    label = numpy.argmax(outputs)
    print('correct_label = ',correct_label,'predict_label = ',label)
    if(label == correct_label):
        scorecard.append(1)
    else:
        scorecard.append(0)
        pass
    pass

scorecard_array = numpy.asarray(scorecard)
print("rate = ",scorecard_array.sum()/scorecard_array.size)

如需转载请注明出处!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带鱼工作室

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值