(一)神经网络项目:手写数字识别

代码部分

模块导入

#!/usr/bin/env python
# coding: utf-8
import 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')

神经网络定义

class neuralNetwork:
    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)

        pass
    
    def train(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))
        
        pass
    
    def query(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=5

for e in range(epochs):
    #go through all records in the training data set
    for 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)
        pass
    pass

测试神经网络并计算准确率


#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 set
for 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 list
    if label==correct_label:
        #network's answer matches correct answer,add 1 to scorecard
        scorecard.append(1)
    else:
        scorecard.append(0)
        pass
    pass


#calculate the performance score , the fraction of correct answers 
scorecard_array=np.asarray(scorecard)
print('performance=',scorecard_array.sum()/scorecard_array.size)


测试自己的手写数字


#测试自己的手写数字
import scipy.misc
img_array=scipy.misc.imread(r'C:\Users\Yumgur\Desktop\c.png',flatten=True)
img_data=255.0-img_array.reshape(784)
img_data=(img_data/255.0*0.99)+0.01
outputs=n.query(img_data)
label=np.argmax(outputs)
print(label)

附:用matplotlib查看图片

#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 label
print(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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值