欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python神經(jīng)網(wǎng)絡(luò)編程(手寫數(shù)字識別)

系統(tǒng) 1617 0

代碼來源:《Python神經(jīng)網(wǎng)絡(luò)編程》
手寫數(shù)據(jù)集下載地址: 1.訓(xùn)練數(shù)據(jù)集 2.測試數(shù)據(jù)集

摘要

本文代碼主要講解基于Python的簡單神經(jīng)網(wǎng)絡(luò)構(gòu)建用于識別手寫數(shù)據(jù)集,類模塊具有通用性,在分析清楚問題后可以加以改動,運(yùn)用于其他方面。

代碼

            
              
                import
              
               numpy

              
                # scipy.special for the sigmoid function expit()
              
              
                import
              
               scipy
              
                .
              
              special

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt



              
                # neural network class definition
              
              
                class
              
              
                neuralNetwork
              
              
                :
              
              
                # initialise the neural network
              
              
                def
              
              
                __init__
              
              
                (
              
              self
              
                ,
              
               inputnodes
              
                ,
              
               hiddennodes
              
                ,
              
               outputnodes
              
                ,
              
               learningrate
              
                )
              
              
                :
              
              
                # set number of nodes in each input, hidden, output layer
              
              
        self
              
                .
              
              inodes 
              
                =
              
               inputnodes
        self
              
                .
              
              hnodes 
              
                =
              
               hiddennodes
        self
              
                .
              
              onodes 
              
                =
              
               outputnodes

        
              
                # link weight matrices, wih and who
              
              
                # weight 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
              
              
                # 創(chuàng)建的兩個鏈接權(quán)重矩陣
              
              
                # self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
              
              
                # self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)
              
              
                # 正態(tài)分布初始化值,第一個參數(shù)表示正態(tài)分布中心,第二個參數(shù)表示標(biāo)準(zhǔn)方差,第三個參數(shù)表示形狀
              
              
        self
              
                .
              
              wih 
              
                =
              
               numpy
              
                .
              
              random
              
                .
              
              normal
              
                (
              
              
                0.0
              
              
                ,
              
              
                pow
              
              
                (
              
              self
              
                .
              
              hnodes
              
                ,
              
              
                -
              
              
                0.5
              
              
                )
              
              
                ,
              
              
                (
              
              self
              
                .
              
              hnodes
              
                ,
              
               self
              
                .
              
              inodes
              
                )
              
              
                )
              
              
        self
              
                .
              
              who 
              
                =
              
               numpy
              
                .
              
              random
              
                .
              
              normal
              
                (
              
              
                0.0
              
              
                ,
              
              
                pow
              
              
                (
              
              self
              
                .
              
              onodes
              
                ,
              
              
                -
              
              
                0.5
              
              
                )
              
              
                ,
              
              
                (
              
              self
              
                .
              
              onodes
              
                ,
              
               self
              
                .
              
              hnodes
              
                )
              
              
                )
              
              
                # learning rate
              
              
        self
              
                .
              
              lr 
              
                =
              
               learningrate

        
              
                # activation function is the sigmoid function
              
              
                # 相當(dāng)于創(chuàng)建一個函數(shù),函數(shù)接收x,返回scipy.special.expit(x),調(diào)用時使用self.activation_function(...)即可
              
              
        self
              
                .
              
              activation_function 
              
                =
              
              
                lambda
              
               x
              
                :
              
               scipy
              
                .
              
              special
              
                .
              
              expit
              
                (
              
              x
              
                )
              
              
                pass
              
              
                # train the neural network
              
              
                def
              
              
                train
              
              
                (
              
              self
              
                ,
              
               inputs_list
              
                ,
              
               targets_list
              
                )
              
              
                :
              
              
                # convert inputs list to 2d array
              
              
        inputs 
              
                =
              
               numpy
              
                .
              
              array
              
                (
              
              inputs_list
              
                ,
              
               ndmin
              
                =
              
              
                2
              
              
                )
              
              
                .
              
              T
        targets 
              
                =
              
               numpy
              
                .
              
              array
              
                (
              
              targets_list
              
                ,
              
               ndmin
              
                =
              
              
                2
              
              
                )
              
              
                .
              
              T

        
              
                # calculate signals into hidden layer
              
              
        hidden_inputs 
              
                =
              
               numpy
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              wih
              
                ,
              
               inputs
              
                )
              
              
                # calculate the signals emerging from hidden layer
              
              
        hidden_outputs 
              
                =
              
               self
              
                .
              
              activation_function
              
                (
              
              hidden_inputs
              
                )
              
              
                # calculate signals into final output layer
              
              
        final_inputs 
              
                =
              
               numpy
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              who
              
                ,
              
               hidden_outputs
              
                )
              
              
                # calculate the signals emerging 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 
              
                =
              
               numpy
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              who
              
                .
              
              T
              
                ,
              
               output_errors
              
                )
              
              
                # update the weights for the links between the hidden and output layer
              
              
        self
              
                .
              
              who 
              
                +=
              
               self
              
                .
              
              lr 
              
                *
              
               numpy
              
                .
              
              dot
              
                (
              
              
                (
              
              output_errors 
              
                *
              
               final_outputs 
              
                *
              
              
                (
              
              
                1.0
              
              
                -
              
               final_outputs
              
                )
              
              
                )
              
              
                ,
              
               numpy
              
                .
              
              transpose
              
                (
              
              hidden_outputs
              
                )
              
              
                )
              
              
                # update the weights for the links between the input and hidden layers
              
              
        self
              
                .
              
              wih 
              
                +=
              
               self
              
                .
              
              lr 
              
                *
              
               numpy
              
                .
              
              dot
              
                (
              
              
                (
              
              hidden_errors 
              
                *
              
               hidden_outputs 
              
                *
              
              
                (
              
              
                1.0
              
              
                -
              
               hidden_outputs
              
                )
              
              
                )
              
              
                ,
              
               numpy
              
                .
              
              transpose
              
                (
              
              inputs
              
                )
              
              
                )
              
              
                pass
              
              
                # query the network
              
              
                def
              
              
                query
              
              
                (
              
              self
              
                ,
              
               inputs_list
              
                )
              
              
                :
              
              
                # convert inputs list to 2d array
              
              
                # ndmin指定數(shù)組最小維度,ndmin=2表示強(qiáng)制將數(shù)組轉(zhuǎn)換為2維
              
              
        inputs 
              
                =
              
               numpy
              
                .
              
              array
              
                (
              
              inputs_list
              
                ,
              
               ndmin
              
                =
              
              
                2
              
              
                )
              
              
                .
              
              T

        
              
                # calculate signals into hidden layer
              
              
        hidden_inputs 
              
                =
              
               numpy
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              wih
              
                ,
              
               inputs
              
                )
              
              
                # calculate the signals emerging from hidden layer
              
              
        hidden_outputs 
              
                =
              
               self
              
                .
              
              activation_function
              
                (
              
              hidden_inputs
              
                )
              
              
                # calculate signals into final output layer
              
              
        final_inputs 
              
                =
              
               numpy
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              who
              
                ,
              
               hidden_outputs
              
                )
              
              
                # calculate the signals emerging from final output layer
              
              
        final_outputs 
              
                =
              
               self
              
                .
              
              activation_function
              
                (
              
              final_inputs
              
                )
              
              
                return
              
               final_outputs



              
                # number of input, hidden and output nodes
              
              
input_nodes 
              
                =
              
              
                784
              
              
                # 784個輸入節(jié)點(diǎn)
              
              
hidden_nodes 
              
                =
              
              
                200
              
              
                # 100個隱藏節(jié)點(diǎn)
              
              
output_nodes 
              
                =
              
              
                10
              
              
                # 10個輸出節(jié)點(diǎn)
              
              
                # learning_rate is 0.5
              
              
learning_rate 
              
                =
              
              
                0.1
              
              
                # create instance of neural network
              
              
n 
              
                =
              
               neuralNetwork
              
                (
              
              input_nodes
              
                ,
              
               hidden_nodes
              
                ,
              
               output_nodes
              
                ,
              
               learning_rate
              
                )
              
              
                # load the mnist training data CSV file into a list
              
              
training_data_file 
              
                =
              
              
                open
              
              
                (
              
              
                "mnist_data/mnist_train.csv"
              
              
                ,
              
              
                'r'
              
              
                )
              
              
training_data_list 
              
                =
              
               training_data_file
              
                .
              
              readlines
              
                (
              
              
                )
              
              
                # 讀入文件,將之變?yōu)橐粋€列表,以行為單位
              
              
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 
              
                =
              
              
                (
              
              numpy
              
                .
              
              asfarray
              
                (
              
              all_values
              
                [
              
              
                1
              
              
                :
              
              
                ]
              
              
                )
              
              
                /
              
              
                255.0
              
              
                *
              
              
                0.99
              
              
                )
              
              
                +
              
              
                0.01
              
              
                # 1*784,輸入層有784個節(jié)點(diǎn)
              
              
                # create the target output values (all 0.01, except the desired label which is 0.99)
              
              
        targets 
              
                =
              
               numpy
              
                .
              
              zeros
              
                (
              
              output_nodes
              
                )
              
              
                +
              
              
                0.01
              
              
                # all_values[0] is the target label for this record
              
              
        targets
              
                [
              
              
                int
              
              
                (
              
              all_values
              
                [
              
              
                0
              
              
                ]
              
              
                )
              
              
                ]
              
              
                =
              
              
                0.99
              
              
        n
              
                .
              
              train
              
                (
              
              inputs
              
                ,
              
               targets
              
                )
              
              
                pass
              
              
                pass
              
              
                # test the neural network
              
              
test_data_file 
              
                =
              
              
                open
              
              
                (
              
              
                "mnist_data/mnist_test.csv"
              
              
                ,
              
              
                'r'
              
              
                )
              
              
test_data_list 
              
                =
              
               test_data_file
              
                .
              
              readlines
              
                (
              
              
                )
              
              
test_data_file
              
                .
              
              close
              
                (
              
              
                )
              
              
                # scorecard for how well the network performs, initially empty
              
              
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"
              
              
                )
              
              
                # scale and shift the inputs
              
              
    inputs 
              
                =
              
              
                (
              
              numpy
              
                .
              
              asfarray
              
                (
              
              all_values
              
                [
              
              
                1
              
              
                :
              
              
                ]
              
              
                )
              
              
                /
              
              
                255.0
              
              
                *
              
              
                0.99
              
              
                )
              
              
                +
              
              
                0.01
              
              
                # query the network
              
              
    outputs 
              
                =
              
               n
              
                .
              
              query
              
                (
              
              inputs
              
                )
              
              
                # the index of the highest value correesponds to the label
              
              
    label 
              
                =
              
               numpy
              
                .
              
              argmax
              
                (
              
              outputs
              
                )
              
              
                # 返回數(shù)組中最大索引值
              
              
                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
              
              
                :
              
              
                # network's answer doesn't match correct answer, add 0 to scorecard
              
              
        scorecard
              
                .
              
              append
              
                (
              
              
                0
              
              
                )
              
              
                pass
              
              
                pass
              
              
                # calculate the performance score, the fraction of correct answers
              
              
scorecard_array 
              
                =
              
               numpy
              
                .
              
              asarray
              
                (
              
              scorecard
              
                )
              
              
                # 將輸入轉(zhuǎn)化為數(shù)組
              
              
                print
              
              
                (
              
              
                "performance = "
              
              
                ,
              
               scorecard_array
              
                .
              
              
                sum
              
              
                (
              
              
                )
              
              
                /
              
               scorecard_array
              
                .
              
              size
              
                )
              
            
          

總結(jié)

  1. 網(wǎng)絡(luò)訓(xùn)練中矩陣計算起到重要作用,要特別注意兩個矩陣相乘時的矩陣形狀
  2. sigmoid函數(shù)的定義域與值域
  3. 反向誤差傳播中計算部分要深入理解,書中的三層網(wǎng)絡(luò)結(jié)構(gòu)中最后一層輸出層在計算式也要使用sigmoid函數(shù)
  4. 在一定程度上多次使用數(shù)據(jù)集訓(xùn)練網(wǎng)絡(luò)可以提高識別率

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品欧美乱码久久久久久1区2区 | 国产精品欧美日韩 | 奇米影音四色 | 亚洲成人免费视频在线观看 | 亚洲精品一区二区三区四区高清 | xxnxx中国18| 91短视频免费在线观看 | 日韩不卡一区二区 | 片在线观看免费观看视频 | 亚洲国产精品二区久久 | 天堂福利电影 | 成人精品一区久久久久 | 国产精品人成福利视频 | 久久99中文字幕 | 日本精品一二区 | 亚洲国产精品一区二区三区久久 | 欧美日韩一区不卡 | 久久夜色精品国产亚洲噜噜 | 暴操美女视频 | 欧美操穴| 国产综合在线播放 | 傲视影院午夜毛片 | a黄视频 | 欧美日韩手机在线观看 | 欧美zozozo人禽交免费观看 | 国产成人系列 | 亚洲一区二区免费看 | 亚洲欧美一区二区三区另类 | 欧美 video | 亚洲精品久 | 精品无码国产一区二区日本 | 三级毛片免费看 | 成人在线精品 | 国产高清在线精品一区αpp | 国产精品99爱免费视频 | 国产大片在线观看 | 91精品久久久久久久久网影视 | 亚洲国产日韩欧美高清片a 高清视频在线播放 | 精品国产三级在线观看 | 亚洲精品视频免费观看 | 国产亚洲福利精品一区 |