NgdZddlZddlZddlZddlZGddeZGddeZGddeZ d Z d Z d Z d Z y) anetwork2.py ~~~~~~~~~~~~~~ An improved version of network.py, implementing the stochastic gradient descent learning algorithm for a feedforward neural network. Improvements include the addition of the cross-entropy cost function, regularisation, and better initialisation of network weights. Note that Nielsen has focessed on making the code simple, easily readable, and easily modifiable. It is _not_ optimised, and omits many desirable features. Nc,eZdZedZedZy) QuadraticCostcRdtjj||z dzzS)zJReturn the cost associated with an output 'a' and desired output 'y'. ?nplinalgnormays a/Users/aayushbajaj/Documents/new-site/static/code/10khrs-ai-ml-dl/learning/nielsen-dl/network2.pyfnzQuadraticCost.fns% ryy~~ac"A% %%c$||z t|zS)z1Return the error delta from the output layer ) sigmoid_primezr rs rdeltazQuadraticCost.deltas aCq! !!rN__name__ __module__ __qualname__ staticmethodrrrrrrs(&& ""rrc,eZdZedZedZy)CrossEntropyCostc tjtj| tj|zd|z tjd|z zz SN)r sum nan_to_numlogr s rrzCrossEntropyCost.fn#sB 66"--266!9 ac266!A#;-> >? @@rc ||z SNrrs rrzCrossEntropyCost.delta's aCLrNrrrrrr"s*AArrc^eZdZdZefdZdZdZdZ d dZ dZ d Z dd Z dd Z d Zy)NetworkzUi think it is important to write docstrings for code that other people will see. c`t||_||_|j||_yr&)len num_layerssizesdefault_weight_initialisercost)selfr,r.s r__init__zNetwork.__init__0s'%jDODJ##%DIrc~|jddDcgc]"}tjj|d$c}|_t |jdd|jddDcgc];\}}tjj||tj |z =c}}|_ycc}wcc}}w)z init each weight with mean 0 and standard dev = 1 over the sequare root of the number of weights connecting to the same neuron. init each bias using a gaussian dist with mean 0 and std dev = 1. no biases for the input layer r!N)r,r randomrandnbiaseszipsqrtweightsr/rxs rr-z"Network.default_weight_initialiser6s37**QR.A.Q299??1a(.ADK $**Sb/4::ab>*,* !QIIOOAq)"''!*4*,DLB,s 'B4*AB9cR|jddDcgc]"}tjj|d$c}|_t |jdd|jddDcgc]%\}}tjj||'c}}|_ycc}wcc}}w)z-same as above, but with unnormalised weights.r!Nr2)r,r r3r4r5r6r8r9s rlarge_weight_initialiserz Network.large_weight_initialiserEs26**QR.A.Q299??1a(.ADK $**Sb/4::ab>*,* !QIIOOAq)*,DLB,s 'B**B#ct|j|jD]'\}}tt j |||z})|S)z0return the output of the network if 'a' is input)r6r5r8sigmoidr dot)r/r bws r feedforwardzNetwork.feedforwardLs<DKK.1 "&&A,q. !a/ HrNc l|r t|} t|} gg}} gg}}t|D]{}tj|td| |Dcgc] }||||z }}|D]}|j |||t|!t d|z| r=|j ||}|j|t dj|| r?|j|d}|j|t dj|| |r?|j ||d}| j|t dj||s0|j|}|j|t dj|j| ~| |||fScc}w) NrzEpoch %s training completezCost on training data: {}T)convertz"Accuracy on training data: {} / {}zCost on evaluation data: {}z$Accuracy on evaluation data: {} / {}) r*ranger3shuffleupdate_mini_batchprint total_costappendformataccuracy)r/ training_dataepochsmini_batch_sizeetalmbdaevaluation_datamonitor_evaluation_costmonitor_evaluation_accuracymonitor_training_costmonitor_training_accuracyn_datanevaluation_costevaluation_accuracy training_costtraining_accuracyjk mini_batches mini_batchr.rLs rSGDz Network.SGDRs?#f MA+-r(O')2$M 6] nn]#q?+-+A a/)*+-%* z3s=7IJ% (1 ,- }e4T" )0067 "====  * 299(AFG tDt$ +22489 $==1""8, 4;;DMM/r.r transposerEr+r)r/r:rr@rfrArg activation activationszsrrlsps rrezNetwork.backprops*.++6+Qrxx +G6*.,,7,Qrxx ,G7J#K BDKK.1 &&J ! #aiil1:j$ / YY  bfk"oa 8EGBK&& B 9 9 ;\}}tj|j|tj|f@}}}n8|Dcgc]+\}}tj|j||f-}}}td|DScc}}wcc}}w)zireturns the number of inputs in ``data`` for which the nueral network outputs the correct result c3>K|]\}}t||k(ywr&)int).0r:rs r z#Network.accuracy..s.gUas1a4ygs)r argmaxrBr")r/datarDr:rresultss rrLzNetwork.accuracysMQRT6Aq"))D,,Q/01>TgRAEF1"))D,,Q/03gF .g. ..SFs AB0Bcd}|D]\}}|j|}|r t}||jjt |z z }|d|t |z zt d|j Dzz }|S)Nrc3`K|]&}tjj|dz(yw)rNr)ryrAs rrzz%Network.total_cost..s#%QLqbiinnQ&7&:Ls,.)rBvectorised_resultr.rr*r"r8)r/r|rQrDr.r:rr s rrIzNetwork.total_costs D1   1 a A aDIILL1 c$i ''DCs4y !#%QDLL%Q"Q QQD Krcp|j|jDcgc]}|jc}|jDcgc]}|jc}t |j j d}t|d}tj|||jycc}wcc}w)z(save the neural net to file ``filename``)r,r8r5r.rAN) r,r8tolistr5strr.ropenjsondumpclose)r/filenamerAr@r|fs rsavez Network.savesZZ,0LL9Lq L9+/;;7;aqxxz;7 **+ -D XsAIIdAGGI :7s B.B3)rNFFFF)F)rrr__doc__rr0r-r<rBrarGrerLrIrrrrr(r(+sP"2 ,, FI8=CH$)!RF ;4/rr(ct|d}tj|}|jt t j t|d}t|d|}|dDcgc]}tj|c}|_ |dDcgc]}tj|c}|_ |Scc}wcc}w)z"returns an instance of the networkrr.r,)r.r8r5) rrloadrgetattrsysmodulesrr(r arrayr8r5)rrr|r.netrAr@s rrrs 8S! 1$'') X&V 5$W D)#&*9o6o!o6#+%)(^4^ ^4#* *74s -B>Cc:tjd}d||<|S)zis the e_i vector) r!?)r rc)r]es rrrshhv! !A$ (rc:ddtj| zz S)Nr)r exprs rr>r>s c"&&!*n rc6t|dt|z zSr )r>rs rrrs Qwqz\ ""r)rrr3rnumpyr objectrrr(rrr>rrrrrsS  "F "vMfM^   #r