BP神经网络在python下的自主搭建梳理

本实验使用mnist数据集完成手写数字识别的测试。识别正确率认为是95%

完整代码如下:

 #!/usr/bin/env python
# coding: utf-8 # In[1]: import numpy
import scipy.special
import matplotlib.pyplot # In[2]: class neuralNetwork:
def __init__(self, inputNodes, hiddenNodes, outputNodes,learningRate):
self.iNodes = inputNodes
self.oNodes = outputNodes
self.hNodes = hiddenNodes
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)
#print(self.wih)
pass def train(self,inputs_list, target_list):
inputs = numpy.array(inputs_list, ndmin=2).T
targets = numpy.array(target_list, ndmin=2).T
#print(inputs)
#print(targets)
hidden_inputs = numpy.dot(self.wih,inputs)
#print(self.wih.shape)
#print(inputs.shape)
hidden_outputs = self.activation_function(hidden_inputs)
#print(hidden_inputs)
final_inputs = numpy.dot(self.who,hidden_outputs)
#print(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, 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_outpus = self.activation_function(final_inputs)
return final_outpus
pass # In[3]: inputNodes = 784
outputNodes = 10
hiddenNodes = 100
learningRate = 0.1
nN = neuralNetwork(inputNodes, hiddenNodes, outputNodes, learningRate) # In[4]: data_file = open("mnist_train.csv",'r')
data_list = data_file.readlines()
data_file.close() # In[5]: epochs = 1
for e in range(epochs) :
for record in data_list:
all_values = record.split(',')
inputs = numpy.asfarray( all_values [1:])/255.0*0.99+0.01
targets = numpy.zeros(outputNodes) + 0.01
targets[int (all_values[0])] = 0.99
nN.train(inputs,targets)
pass
pass # In[6]: test_data_file = open("mnist_test.csv",'r')
test_data_list = test_data_file.readlines()
test_data_file.close() # In[7]: scorecard = []
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 = nN.query(inputs)
label = numpy.argmax(outputs)
if(label == correct_label):
scorecard.append(1)
else:
scorecard.append(0)
pass
pass # In[8]: scorecard_array = numpy.asarray(scorecard)
print ("performance = " ,scorecard_array.sum()/scorecard_array.size) # In[9]: import scipy.misc
img_array = scipy.misc.imread('test.png',flatten="True")
img_data = 255.0 - img_array . reshape(784)
img_data = (img_data /255.0 * 0.99 ) + 0.01
op=nN.query(img_data)
print(op)
print(numpy.argmax(op)) # In[10]: all_values = data_list[1].split(',')
image_array = numpy.asfarray( all_values [1:]).reshape((28,28))
matplotlib.pyplot.imshow(image_array, cmap = 'Greys',interpolation='None')

IN[9]到IN[10]的代码分别用于测试自己制作的数字识别效果和显示图像。可去掉。

代码运行过程需要mnist数据集,链接:https://pan.baidu.com/s/120GTdZ8Tivkp1KD9VQ_XeQ

BP神经网络的结构:https://www.cnblogs.com/bai2018/p/10353768.html

在输入层的神经元数据选取上,和像素数量一致。MNIST采用28X28的像素点,则输入层的神经元数量为28*28=784个

输入层和隐层,输出层和隐层之间的权值选取为随机数。使用正态分布的随机数较好。

隐层的神经元数量合适即可,取值为经验法,假设为100个

输出层神经元表示数据0-9,则使用10个神经元,分别表示数字0-9的可能性概率。

训练过程中使用的学习效率,取0.2吧。。。

将权重,各层神经元值,误差等,表示为矩阵数据进行处理。

正向传递数据查询结果,误差的反向传递改变权重等过程,涉及到的数学推导:https://www.cnblogs.com/bai2018/p/10353814.html

上一篇:Java基础学习-Eclipse综述和运算符的使用


下一篇:ODP.NET 之访问 Oracle 数据库