最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年05月08日 11:47:47   作者:神仙盼盼  
這篇文章主要介紹了python神經(jīng)網(wǎng)絡編程之手寫數(shù)字識別,文中有非常詳細的代碼示例,對正在學習python神經(jīng)網(wǎng)絡編程的小伙伴們有很好地幫助,需要的朋友可以參考下

寫在之前

首先是寫在之前的一些建議:

首先是關于這本書,我真的認為他是將神經(jīng)網(wǎng)絡里非常棒的一本書,但你也需要注意,如果你真的想自己動手去實現(xiàn),那么你一定需要有一定的python基礎,并且還需要有一些python數(shù)據(jù)科學處理能力

然后希望大家在看這邊博客的時候對于神經(jīng)網(wǎng)絡已經(jīng)有一些了解了,知道什么是輸入層,什么是輸出層,并且明白他們的一些理論,在這篇博客中我們僅僅是展開一下代碼;

然后介紹一下本篇博客的環(huán)境等:

語言:Python3.8.5

環(huán)境:jupyter

庫文件: numpy | matplotlib | scipy

一、代碼框架

我們即將設計一個神經(jīng)網(wǎng)絡對象,它可以幫我們?nèi)プ鰯?shù)據(jù)的訓練,以及數(shù)據(jù)的預測,所以我們將具有以下的三個方法:

首先我們需要初始化這個函數(shù),我們希望這個神經(jīng)網(wǎng)絡僅有三層,因為再多也不過是在隱藏層去做文章,所以先做一個簡單的。那么我們需要知道我們輸入層、隱藏層和輸出層的節(jié)點個數(shù);訓練函數(shù),我們需要去做訓練,得到我們需要的權重。通過我們已有的權重,將給定的輸入去做輸出。

二、準備工作

現(xiàn)在我們需要準備一下:

1.將我們需要的庫導入

import numpy as np
import scipy.special as spe
import matplotlib.pyplot as plt

2.構建一個類

class neuralnetwork:
    # 我們需要去初始化一個神經(jīng)網(wǎng)絡
    
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        pass
        
        
    def train(self, inputs_list, targets_list):
        pass
        
    
    def query(self, inputs_list):
        pass

3.我們的主函數(shù)

input_nodes = 784    # 輸入層的節(jié)點數(shù)
hidden_nodes = 88    # 隱藏層的節(jié)點數(shù)
output_nodes = 10    # 輸出層的節(jié)點數(shù)

learn_rate = 0.05    # 學習率

n = neuralnetwork(input_nodes, hidden_nodes, output_nodes, learn_rate)

4.導入文件

data_file = open("E:\sklearn_data\神經(jīng)網(wǎng)絡數(shù)字識別\mnist_train.csv", 'r')
data_list = data_file.readlines()
data_file.close()
file2 = open("E:\sklearn_data\神經(jīng)網(wǎng)絡數(shù)字識別\mnist_test.csv")
answer_data = file2.readlines()
file2.close()

這里需要介紹以下這個數(shù)據(jù)集,訓練集在這里,測試集在這里

三、框架的開始

def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        
        self.inodes = inputnodes   # 輸入層節(jié)點設定
        self.hnodes = hiddennodes  # 影藏層節(jié)點設定
        self.onodes = outputnodes  # 輸出層節(jié)點設定
        
        self.lr = learningrate     # 學習率設定,這里可以改進的
        
        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))) # 這里是隱藏層與輸出層之間的連接
        self.activation_function = lambda x: spe.expit(x)           # 返回sigmoid函數(shù)

Δw j,k ​ =α∗E k ​ ∗ sigmoid (O k ​ )∗(1−sigmoid(O k ​ ))⋅O j ⊤

def query(self, inputs_list):
        inputs = np.array(inputs_list, ndmin=2).T # 輸入進來的二維圖像數(shù)據(jù)
        
        hidden_inputs = np.dot(self.wih, inputs)  # 隱藏層計算,說白了就是線性代數(shù)中的矩陣的點積
        hidden_outputs = self.activation_function(hidden_inputs) # 將隱藏層的輸出是經(jīng)過sigmoid函數(shù)處理
        final_inputs = np.dot(self.who, hidden_outputs) # 原理同hidden_inputs
        final_outputs = self.activation_function(final_inputs) # 原理同hidden_outputs 
        
        return final_outputs # 最終的輸出結果就是我們預測的數(shù)據(jù)

這里我們對預測這一部分做一個簡單的解釋:我們之前的定義輸出的節(jié)點是10個,對應的是十個數(shù)字。
而為什么會通過神經(jīng)網(wǎng)絡能達到這個亞子,我推薦這本書深度學習的數(shù)學 這本書的理論講解非常不錯?。?!

四、訓練模型構建

之前的部分相對而言還是比較簡單的,那么接下來就是如何去構建訓練模型了。

 def train(self, inputs_list, targets_list):
        # 前期和識別過程是一樣的,說白了我們與要先看看現(xiàn)在的預測結果如何,只有根據(jù)這次的預期結果才能去修改之前的權重
        inputs = np.array(inputs_list, ndmin=2).T
        
        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = np.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        
        # 接下來將標簽拿遲來
        targets = np.array(targets_list, ndmin=2).T

		# 得到我們的數(shù)據(jù)預測的誤差,這個誤差將是向前反饋的基礎
        output_errors = targets - final_outputs
        # 這部分是根據(jù)公式得到的反向傳播參數(shù)
        hidden_errors = np.dot(self.who.T, output_errors)
        
        # 根據(jù)我們的反饋參數(shù)去修改兩個權重
        self.who += self.lr * np.dot((output_errors * final_outputs * ( 1.0-final_outputs)), np.transpose(hidden_outputs))
        self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0-hidden_outputs)), np.transpose(inputs))

如此我們的基礎神經(jīng)網(wǎng)絡構建完成了。

五、手寫數(shù)字的識別

接下來神經(jīng)網(wǎng)絡是完成的,那么我們究竟該如何去將數(shù)據(jù)輸入呢?
csv文件我們并不陌生【或許陌生?】,他是逗號分割文件,顧名思義,它是通過逗號分隔的,所以我們可以打開看一下:

在這里插入圖片描述

眼花繚亂!!

但是細心的我們可以發(fā)現(xiàn)他的第一個數(shù)字都是0~9,說明是我們的標簽,那么后面的應該就是圖像了,通過了解我們知道這個后面的數(shù)據(jù)是一個28*28的圖像。

all_value = data_list[0].split(',') # split分割成列表
image_array = np.asfarray(all_value[1:]).reshape((28,28)) # 將數(shù)據(jù)reshape成28*28的矩陣
plt.imshow(image_array, cmap='Greys', interpolation='None') # 展示一下

通過這段代碼,我們可以簡單的看一下每個數(shù)字是什么:

在這里插入圖片描述

很好,知道這里就足夠了,那么我們接下來就是將這些數(shù)據(jù)傳入了!

我們在訓練的時候,需要將他們都轉化成數(shù)字列表,方便處理

data = []     # 用來保存訓練過程的數(shù)據(jù)
sum_count = 0 # 統(tǒng)計總識別的正確的個數(shù)
for i in range(15): # 訓練的輪數(shù)
    count = 0         # 單次訓練識別正確的個數(shù)
    for j in range(len(data_list)):   # 對60000張圖片開始訓練, 沒有劃分數(shù)據(jù)集的過程主要是別人直接給了,我也懶得自己去做了,主要就是展示一下神經(jīng)網(wǎng)絡嘛~
        target = np.zeros(10)+0.01 # 生成初始標簽集合,用來和結果對比
        line_ = data_list[j].split(',')    # 對每一行的數(shù)據(jù)處理切割
        imagearray = np.asfarray(line_)  # 將切割完成的數(shù)據(jù)轉換成數(shù)字列表
        target[int(imagearray[0])] = 1.0    # 將正確答案挑出來
        n.train(imagearray[1:]/255*0.99+0.01, target) # 丟入訓練,丟入的時候注意將數(shù)據(jù)轉換成0.01~1.0之間的結果
    for line in answer_data: # 對10000組測試集測試
        all_values = line.split(',')
        answer = n.query((np.asfarray(all_values[1:])/255*0.99)+0.01)
        if answer[int(all_values[0])] > 0.85:  # 查看對應位置是否達到自定義的閾值?
            count += 1
    sum_count += count
    string = "訓練進度 %05f\n本輪準確度 %05f\n總準確度 %05f\n\n"%(i/120,count/len(answer_data), sum_count/(len(answer_data)*(i+1)))
    data.append([i/120,count/len(answer_data), sum_count/(len(answer_data)*(i+1))])  # 將數(shù)據(jù)保存方便生成訓練曲線
    print(string)
    ```
接下來我們將結果圖片展示以下吧~

```python
data = np.array(data)
plt.plot(range(len(data)), data[:, 1:])

在這里插入圖片描述

六、源碼

把源碼整理一下貼出來

import numpy as np
import scipy.special as spe
import matplotlib.pyplot as plt

class neuralnetwork:
    # 我們需要去初始化一個神經(jīng)網(wǎng)絡
    
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        
        self.lr = learningrate
        
        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)))
        self.activation_function = lambda x: spe.expit(x)           # 返回sigmoid函數(shù)
        
        
    def train(self, inputs_list, targets_list):
        inputs = np.array(inputs_list, ndmin=2).T
        
        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = np.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        
        targets = np.array(targets_list, ndmin=2).T
        output_errors = targets - final_outputs
        hidden_errors = np.dot(self.who.T, output_errors)
        
        self.who += self.lr * np.dot((output_errors * final_outputs * ( 1.0-final_outputs)), np.transpose(hidden_outputs))
        self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0-hidden_outputs)), np.transpose(inputs))
        
    
    def query(self, inputs_list):
        inputs = np.array(inputs_list, ndmin=2).T
        
        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = np.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        
        return final_outputs
    
        
input_nodes = 784
hidden_nodes = 88
output_nodes = 10

learn_rate = 0.05

n = neuralnetwork(input_nodes, hidden_nodes, output_nodes, learn_rate)

data_file = open("E:\sklearn_data\神經(jīng)網(wǎng)絡數(shù)字識別\mnist_train.csv", 'r')
data_list = data_file.readlines()
data_file.close()
file2 = open("E:\sklearn_data\神經(jīng)網(wǎng)絡數(shù)字識別\mnist_test.csv")
answer_data = file2.readlines()
file2.close()

data = []

sum_count = 0
for i in range(15):
    count = 0
    for j in range(len(data_list)):
        target = np.zeros(10)+0.01
        line_ = data_list[j].split(',')
        imagearray = np.asfarray(line_)
        target[int(imagearray[0])] = 1.0
        n.train(imagearray[1:]/255*0.99+0.01, target)
    for line in answer_data:
        all_values = line.split(',')
        answer = n.query((np.asfarray(all_values[1:])/255*0.99)+0.01)
        if answer[int(all_values[0])] > 0.85:
            count += 1
    sum_count += count
    string = "訓練進度 %05f\n本輪準確度 %05f\n總準確度 %05f\n\n"%(i/120,count/len(answer_data), sum_count/(len(answer_data)*(i+1)))
    data.append([i/120,count/len(answer_data), sum_count/(len(answer_data)*(i+1))])
    print(string)


data = np.array(data)

plt.plot(range(len(data)), data[:, 1:])

可以說是相對簡單的一個程序,但卻是包含著神經(jīng)網(wǎng)絡最基礎的思想!值得好好康康~

七、思考

如何識別其他手寫字體等?

我的想法:通過圖像處理,將像素規(guī)定到相近大小【尺度放縮】

圖像大小運行速度問題

我的想法:如何快速的矩陣運算,通過C語言是否可以加速?相較于darknet這個神經(jīng)網(wǎng)絡僅有三層,運算速度并不是十分理想。當然cuda編程對于GPU加速肯定是最好的選擇之一。

到此這篇關于python神經(jīng)網(wǎng)絡編程之手寫數(shù)字識別的文章就介紹到這了,更多相關python手寫數(shù)字識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python安裝selenium包詳細過程

    Python安裝selenium包詳細過程

    在本篇文章里小編給大家整理了關于Python安裝selenium包詳細過程,需要的朋友們可以學習下。
    2019-07-07
  • Python調(diào)用Zoomeye搜索接口的實現(xiàn)

    Python調(diào)用Zoomeye搜索接口的實現(xiàn)

    本文主要介紹了Python調(diào)用Zoomeye搜索接口的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • python使用nntp讀取新聞組內(nèi)容的方法

    python使用nntp讀取新聞組內(nèi)容的方法

    這篇文章主要介紹了python使用nntp讀取新聞組內(nèi)容的方法,實例分析了Python操作nntp讀取新聞組內(nèi)容的相關技巧,需要的朋友可以參考下
    2015-05-05
  • 在Python中處理XML的教程

    在Python中處理XML的教程

    這篇文章主要介紹了在Python中處理XML的教程,是Python網(wǎng)絡編程中的基礎知識,需要的朋友可以參考下
    2015-04-04
  • 詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖)

    詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖)

    這篇文章主要介紹了詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Python多線程編程(四):使用Lock互斥鎖

    Python多線程編程(四):使用Lock互斥鎖

    這篇文章主要介紹了Python多線程編程(四):使用Lock互斥鎖,本文講解了互斥鎖概念、同步阻塞、代碼示例等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • python入門課程第一講之安裝與優(yōu)缺點介紹

    python入門課程第一講之安裝與優(yōu)缺點介紹

    這篇文章主要介紹了python入門課程第一講之安裝與優(yōu)缺點,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • python科學計算之narray對象用法

    python科學計算之narray對象用法

    今天小編就為大家分享一篇python科學計算之narray對象用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python編程基礎之函數(shù)和模塊

    Python編程基礎之函數(shù)和模塊

    這篇文章主要為大家介紹了Python函數(shù)和模塊,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Python中的getter和setter的方法使用詳解

    Python中的getter和setter的方法使用詳解

    基本上,在面向對象編程語言中,使用setter和getter方法的主要目的是為了確保數(shù)據(jù)的封裝,這篇文章主要介紹了Python的getter和setter的方法使用詳解,需要的朋友可以參考下
    2022-12-12

最新評論

叶城县| 澎湖县| 泽库县| 衡山县| 兴安县| 贺兰县| 青田县| 北川| 秦皇岛市| 抚顺市| 翁源县| 宜都市| 蒙城县| 梨树县| 姚安县| 江安县| 定结县| 马关县| 林州市| 清河县| 翁源县| 牡丹江市| 淮安市| 岐山县| 彝良县| 禹州市| 阿拉善右旗| 门头沟区| 马山县| 拉萨市| 双城市| 祁阳县| 朔州市| 都安| 南漳县| 杨浦区| 岑巩县| 南昌市| 原阳县| 清镇市| 阜宁县|