Implementation of BP Neural network recognition mnist data set by Python

Source: Internet
Author: User

Title: "Python realizes BP neural network recognition mnist data Set"
date:2018-06-18t14:01:49+08:00
Tags: [""]
Categories: ["Python"]

Objective

The training set read in the. MAT format when testing the correct rate with a PNG-formatted picture

Code
#!/usr/bin/env Python3# Coding=utf-8ImportMathImportSysImportOsImportNumPy asNp fromPILImportImageImportScipy.io asSiodefSigmoid (x):returnNp.array (List(Map(LambdaI1 /(1 +Math.exp (-i)) (x)))defGet_train_pattern ():# Return the features and tags of the training set    # Current_dir = OS.GETCWD ()Current_dir= "/home/lxp/f/developing_folder/intelligence_system/bpneuralnet/"Train=Sio.loadmat (Current_dir+ "Mnist_train.mat")["Mnist_train"] Train_label=Sio.loadmat (Current_dir+ "Mnist_train_labels.mat")["Mnist_train_labels"] Train=Np.where (Train>  the,1,0)# Binary Value    returnTrain, Train_labeldefGet_test_pattern ():# Return to test set    # Base_url = OS.GETCWD () + "/test/"Base_url= "/home/lxp/f/developing_folder/intelligence_system/bpneuralnet/mnist_test/"Test_img_pattern=[] forIinch Range(Ten): Img_url=Os.listdir (Base_url+ Str(i)) t=[] forUrlinchImg_url:img=Image.Open(Base_url+ Str(i)+ "/" +URL) img=Img.convert (' 1 ')# Binary ValueImg_array=Np.asarray (IMG,' I ')# Convert to int arrayImg_vector=Img_array.reshape (img_array.shape[0]*img_array.shape[1])# Expand into a one-dimensional arrayT.append (Img_vector) test_img_pattern.append (t)returnTest_img_patternclassBpnetwork:# Neural Network class    def __init__( Self, In_count, Hiden_count, Out_count, In_rate, hiden_rate):""":p Aram In_count: Number of input layers:p Aram Hiden_count: Number of hidden layers:p Aram Out_count: Number of output layers:p Aram In_rate: Input layer Learning rate:p Aram Hiden_rate: Hidden layer Learning rate        """        # Number of nodes in each layer         Self. in_count=In_count Self. hiden_count=Hiden_count Self. out_count=Out_count# Random initialization of weights for input layer to hidden layer lines         Self. W1= 0.2 * \Np.random.random (( Self. In_count, Self. Hiden_count))- 0.1        # Random initialization of weights for hidden layer to output layer lines         Self. W2= 0.2 * \Np.random.random (( Self. Hiden_count, Self. Out_count))- 0.1        # hidden layer bias vector         Self. hiden_offset=Np.zeros ( Self. Hiden_count)# output layer bias vector         Self. out_offset=Np.zeros ( Self. Out_count)# Input Layer Learning rate         Self. in_rate=In_rate# Hidden Layer Learning rate         Self. hiden_rate=Hiden_ratedefTrain Self, Train_img_pattern, Train_label):if  Self. in_count!= Len(train_img_pattern[0]): Sys.exit ("The input layer dimension is not equal to the number of sample dimensions")# for NUM in range:        # for NUM in range:         forIinch Range(Len(Train_img_pattern)):ifI%  the == 0:Print(i)# generate target vectorsTarget=[0]* Tentarget[train_label[i][0]]= 1            # for T in range (len (Train_img_pattern[num])):            # Forward Propagation            # hidden layer value equals input layer *w1+ hidden layer biasHiden_value=Np.dot (Train_img_pattern[i], Self. W1)+  Self. Hiden_offset Hiden_value=Sigmoid (Hiden_value)# Calculate output layer outputOut_value=Np.dot (Hiden_value, Self. W2)+  Self. Out_offset Out_value=Sigmoid (Out_value)# Reverse UpdateError=Target-Out_value# Calculate output layer ErrorOut_error=Out_value*(1 -Out_value)*Error# Calculating hidden layer errorsHiden_error=Hiden_value* \(1 -Hiden_value)*Np.dot ( Self. W2, Out_error)# Update W2,W2 is a J-row K-column matrix that stores the weights of the hidden layer to the output layer             forKinch Range( Self. Out_count):# Update the value of W2 k column, connecting all nodes in the hidden layer to the side of the K node of the output layer                # Hidden Layer Learning rate x input layer error x hidden layer output value                 Self. w2[:, K]+=  Self. hiden_rate*OUT_ERROR[K]*Hiden_value# Update W1             forJinch Range( Self. Hiden_count): Self. w1[:, J]+=  Self. in_rate* \HIDEN_ERROR[J]*Train_img_pattern[i]# update offset vector             Self. out_offset+=  Self. hiden_rate*Out_error Self. hiden_offset+=  Self. in_rate*Hiden_errordefTest Self, Test_img_pattern):"""testing the correct rate of neural networks:p Aram Test_img_pattern[num][t] represents the T-picture of digital num: return:        """Right=Np.zeros (Ten) test_sum= 0         forNuminch Range(Ten):# 10 Digits            # Print ("Recognizing", num)Num_count= Len(Test_img_pattern[num]) test_sum+=Num_count forTinch Range(Num_count):# T-picture of number numHiden_value=Np.dot (Test_img_pattern[num][t], Self. W1)+  Self. Hiden_offset Hiden_value=Sigmoid (Hiden_value) out_value=Np.dot (Hiden_value, Self. W2)+  Self. Out_offset Out_value=Sigmoid (Out_value)# Print (out_value)                ifNp.argmax (Out_value)==Num:# Correct identificationRight[num]+= 1            Print("Digital%dthe correct rate of recognition%f" %(Num, Right[num]/Num_count))# Average recognition rate        Print("Average recognition rate is:",sum(right)/Test_sum)"""def test1:    """defRun ():# Read in training setTrain, Train_label=Get_train_pattern ()# Read in test pictureTest_pattern=Get_test_pattern ()# Neural Network configuration ParametersIn_count=  - *  -Hiden_count= 6Out_count= TenIn_rate= 0.1Hiden_rate= 0.1Bpnn=Bpnetwork (In_count, Hiden_count, Out_count, In_rate, hiden_rate) Bpnn.train (Train, Train_label) Bpnn.test (Test_patte Tno# Single-sheet test    # Identify a single picture and return the recognition result    """While True:img_name = input ("Enter a picture to recognize \ n")base_url = "/home/lxp/f/developing_folder/intelligence_system/bpneuralnet/"Img_url = base_url + img_nameimg = image.open (img_url)img = Img.convert (' 1 ') # binaryImg_array = Np.asarray (img, ' I ') # converted to an int array# Get the feature vector of the pictureImg_v = Img_array.reshape (img_array.shape[0] * img_array.shape[1]) # expands into a one-dimensional arrayBpnn.test1 (Img_v)    """if __name__ == "__main__": Run ()# train, Train_label = Get_train_pattern ()    # Print (train_label[5][0])# test = Get_test_pattern ()

Data Set Download:
Http://ot0ucj3at.bkt.clouddn.com/o_1cg8o6k59muv3fg1kel9qg1i4ca.zip

Implementation of BP Neural network recognition mnist data set by Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.