#!/usr/bin/env python# coding: utf-8import numpy as np
#scipy.special for the sigmoid function expit()import scipy.special as ss
import matplotlib.pyplot
get_ipython().run_line_magic('matplotlib','inline')
神经网络定义
classneuralNetwork:def__init__(self,inputnodes,hiddennodes,outputnodes,learningrate):#set number of nodes in each inpput,hidden,output layer
self.inodes=inputnodes
self.hnodes=hiddennodes
self.onodes=outputnodes
#link weight matrices wih-input_to_hidden and who-hidden_to_output#weights inside the arrays are w_i_j , where link is from node i to node j in the next layer#w11 w21#w12 w22 etc
self.wih=np.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
self.who=np.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))#learning rate
self.lr=learningrate
#activation function is the sigmoid function
self.activation_function=lambda x:ss.expit(x)passdeftrain(self,inputs_list,targets_list):#conver inputs list to 2d array
inputs=np.array(inputs_list,ndmin=2).T
targets=np.array(targets_list,ndmin=2).T
#calculate signals into hidden layer
hidden_inputs=np.dot(self.wih,inputs)#calculate the signals merging from hidden layer
hidden_outputs=self.activation_function(hidden_inputs)#calculate signals into final output layer
final_inputs=np.dot(self.who,hidden_outputs)#calculate the signals merging from final output layer
final_outputs=self.activation_function(final_inputs)#error is the (target-actual)
output_errors=targets-final_outputs
#hidden layer error is the output_errors,split by weights,recombined at hidden nodes
hidden_errors=np.dot(self.who.T,output_errors)#update the weights for the links between the hidden and output layers
self.who+=self.lr*np.dot((output_errors*final_outputs*(1-final_outputs)),np.transpose(hidden_outputs))#update the weights for the links between the hidden and output layers
self.wih+=self.lr*np.dot((hidden_errors*hidden_outputs*(1-hidden_outputs)),np.transpose(inputs))passdefquery(self,input_list):#conver inputs list to 2d array
inputs=np.array(input_list,ndmin=2).T
#calculate signals into hidden layer
hidden_inputs=np.dot(self.wih,inputs)#calculate the signals merging from hidden layer
hidden_outputs=self.activation_function(hidden_inputs)#calculate signals into final output layer
final_inputs=np.dot(self.who,hidden_outputs)#calculate the signals merging from final output layer
final_outputs=self.activation_function(final_inputs)return final_outputs
神经网络初始设置
#number of input,hidden and output nodes
input_nodes=784
hidden_nodes=200
output_nodes=10#learning rate
learning_rate=0.2#create instance of neural network
n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)#load the mnist traing data CSV file into a list
path='D:/data_analysis/jupyter_notebook/machine_learning/dataset/mnist图片/'
training_data_file=open(path+'mnist_train.csv','r')
training_data_list=training_data_file.readlines()
training_data_file.close()
训练神经网络’
# train the neural network#epochs is the number of times the training data set is used for training
epochs=5for e inrange(epochs):#go through all records in the training data setfor record in training_data_list:#split the record by the ',' commas
all_values=record.split(',')#scale and shift the inputs
inputs=(np.asfarray(all_values[1:])/255.0*0.99)+0.01#create the target output values (all 0.01,except the desired label which is 0.99)
targets=np.zeros(output_nodes)+0.01
targets[int(all_values[0])]=0.99
n.train(inputs,targets)passpass
测试神经网络并计算准确率
#load the mnist test data CSV file into a list
path='D:/data_analysis/jupyter_notebook/machine_learning/dataset/mnist图片/'
test_data_file=open(path+'mnist_test.csv','r')
test_data_list=test_data_file.readlines()
test_data_file.close()#test the neural network #scorecard for how well the network performs,initially empty scorecard=[]
scorecard=[]#go through all the records in the test data setfor record in test_data_list:#split the record by the ',' commas
all_values=record.split(',')#correct answer is first value
correct_label=int(all_values[0])# print(correct_label,'correct label')#scalr and shift the inputs
inputs=(np.asfarray(all_values[1:])/255.0*0.99)+0.01#query the net work
outputs=n.query(inputs)#the index of the highest value vorresponds to the label
label=np.argmax(outputs)#print(label,"network's answer" )#append correct or incorrect to listif label==correct_label:#network's answer matches correct answer,add 1 to scorecard
scorecard.append(1)else:
scorecard.append(0)passpass#calculate the performance score , the fraction of correct answers
scorecard_array=np.asarray(scorecard)print('performance=',scorecard_array.sum()/scorecard_array.size)
#load the mnist test data CSV file into a list
path='D:/data_analysis/jupyter_notebook/machine_learning/dataset/mnist图片/'
test_data_file=open(path+'mnist_test.csv','r')
test_data_list=test_data_file.readlines()
test_data_file.close()#get the first test record
all_values=test_data_list[8].split(',')#print the labelprint(all_values[0])
image_array=np.asfarray(all_values[1:]).reshape((28,28))
matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')
a=n.query((np.asfarray(all_values[1:])/255.0*0.99)+0.01)
人工神经网络-手写数字识别代码部分模块导入神经网络定义神经网络初始设置训练神经网络’测试神经网络并计算准确率测试自己的手写数字附:用matplotlib查看图片代码部分模块导入#!/usr/bin/env python# coding: utf-8import numpy as np#scipy.special for the sigmoid function expit()import scipy.special as ssimport matplotlib.pyplotget_ipyt