The implementation of the BP algorithm python

Source: Internet
Author: User

0-9 digital recognition, nmist data recognition.

The specific code includes nmist see attachment.

Reference is Tom's machine learning BP chapter.

#Coding:utf-8#not considering the size endImportstructImportNumPydefloadimages (filename):Try: F= open (filename,'RB')    exceptException as instance:Printtype (instance) exit () Allimage=[] Bins=f.read () index=0 Magicnum,imagenum,rownum,colnum= Struct.unpack_from ('>IIII', Bins,index) index= index + struct.calcsize ('>IIII')        assert2051 = = Magicnum,'DataSet damaged | Little Endian'         forCtinchxrange (imagenum): Allimage.append (Struct.unpack_from ('>784b', Bins,index)) Index= index + struct.calcsize ('>784b')        returnNumpy.array (allimage,dtype='float32')defloadlabels (filename):Try: F= open (filename,'RB')    exceptException as instance:Printtype (instance) exit () Alllabels=[] Bins=f.read () index=0 Magicnum,labelnum= Struct.unpack_from ('>ii', Bins,index) index= index + struct.calcsize ('>ii')        assert2049 = = Magicnum,'DataSet damaged | Little Endian'         forCtinchxrange (labelnum): Alllabels.append (Struct.unpack_from ('B', Bins,index)) Index= index + struct.calcsize ('B')    returnNumpy.array (alllabels,dtype='float32')                if '__main__'==__name__: Images= Loadimages ('T10k-images.idx3-ubyte') Labels= Loadlabels ('T10k-labels.idx1-ubyte')    ImportMatplotlib.pyplot as Plt forXinchRange (3): Plt.figure () shown= Images[x].reshape (28,28)        #shown 28*28 numpy matrixPlt.imshow (shown,cmap='Gray') Plt.title (str (labels[x)) plt.show ()
Read Nmist
#-*-coding:utf-8-*-ImportDataloadImportNumPy as NPImportSYSImportWarningsdefBP (Trainset,eta=0.01,nin=none,nhid=none,nout=none,iternum = 10):    " "[(Instance,label), ((784,1) array, (10,1) array) ...]" "Wkh= (Np.random.rand (nout,nhid)-0.5)/10.0Whi= (Np.random.rand (nhid,nin)-0.5)/10.0Iteration=0 er= 1" "Iteration" "     whileIteration < Iternum andEr > 0.04:        Print 'iteration=', Iteration er=Testann ((whi,wkh)) Iteration+ = 1 for(X,label)inchtrainset:#Maximum minimum normalizationx = (X-x.min ())/x.max ()-x.min ()#forwardNeth = Np.dot (whi,x)#nhid*nin nin*1-nhid*1Oh = sigmoid (Neth)#nhid*1NETK = Np.dot (Wkh,oh)#Nout*nhid nhid*1-nout*1OK = sigmoid (NETK)#nout*1            #Seeking ErrorDK = ok* (1-OK) * (label-OK) DH= oh* (1-oh) *np.dot (WKH.T,DK)#(nhid,1) = (Nout,nhid). T * (nout,1)            #Update weights MatrixWkh = Wkh + eta * NP.DOT (Dk,oh. T#Nout*nhid + Nout*1*1*hidWhi = Whi + eta * NP.DOT (DH,X.T)#Nhid*nin + nhid*1*1*nin    Print 'Iteration over'     returnWkh,whidefTestann (model): Err=0 forIinchRange (len (testlabels)): Res= Fit (Model,testimages[i].reshape (784,1))        ifi > 9990:            Print '\ t', int (testlabels[i][0]),'was recognized as', res[1]        ifTESTLABELS[I][0]! = res[1]: Err+ = 1errorrate= Float (err)/float (len (testlabels))Print 'Error Rate', Errorrate,'\ n'    returnerrorratedefFit (model,image): Whi,wkh=model OK=list (sigmoid (sigmoid (Image.t.dot (WHI.T)). dot (wkh.t)) [0])returnOk,ok.index (max (OK))defsigmoid (y):#Nasty Overflow WarningWarnings.filterwarnings ("Ignore")    return1/(1+np.exp (-y))if '__main__'==__name__: Np.random.seed (207) Trainimages= Dataload.loadimages ('Train-images.idx3-ubyte') Trainlabels= Dataload.loadlabels ('Train-labels.idx1-ubyte') Testimages= Dataload.loadimages ('T10k-images.idx3-ubyte') Testlabels= Dataload.loadlabels ('T10k-labels.idx1-ubyte') DataSet= []     forIinchRange (len (trainlabels)): TMP= Np.zeros ((10,1), dtype='float32') Tmp[int (trainlabels[i]), 0]= 1dataset.append ((Trainimages[i].reshape (784,1) (TMP)) BP (trainset=dataset,eta=0.05,nin=784,nhid=20,nout=10,iternum=20)  

The implementation of the BP algorithm python

Related Article

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.