資源簡介
用于手寫字體識別的卷積神經(jīng)網(wǎng)絡源碼,python語言,包含注釋
代碼片段和文件信息
#coding:utf-8
import?numpy?as?np
import?os
import?gzip
import?urllib.request
import?struct
import?matplotlib.pyplot?as?plt
import?sys
import?pickle
filefolder?=?r‘C:\Users\子坤\Desktop\machinelearning?learning‘
filename_list?=?(r‘\train-images-idx3-ubyte.gz‘
????????????r‘\train-labels-idx1-ubyte.gz‘
????????????r‘\t10k-images-idx3-ubyte.gz‘
????????????r‘\t10k-labels-idx1-ubyte.gz‘)
file_net_name?=?r‘\net.pkl‘
def?storeNet(filename?net):
????fw?=?open(filename‘wb‘)
????pickle.dump(net?fw)
????fw.close()
def?grabNet(filename):
????fr?=?open(filename‘rb‘)
????net?=?pickle.load(fr)
????fr.close()
????return?net
def?load_dataset(pathfile_train_image?file_train_label?file_test_image?file_test_label):
????“““
????函數(shù)功能:讀取mnist數(shù)據(jù)集,并返回4個np數(shù)組
????path:手寫數(shù)字數(shù)據(jù)集路徑名
????file_train_image:訓練集image壓縮包文件名
????file_train_label:訓練集label壓縮包文件名
????file_test_image:測試集image壓縮包文件名
????file_test_label:測試集label壓縮包文件名
????“““
????train_imgs?=?load_imageset(path?+?file_train_image)
????train_lbls?=?load_labelset(path?+?file_train_label)
????test_imgs?=?load_imageset(path?+?file_test_image)
????test_lbls?=?load_labelset(path?+?file_test_label)
????return?train_imgs?train_lbls?test_imgs?test_lbls
def?load_imageset(filename):
????“““
????函數(shù)功能:讀取mnist數(shù)據(jù)的中的images壓縮包到np數(shù)組中并返回
????函數(shù)工作原理解釋:該二進制文件的內(nèi)容以“位”為單位,前16個字節(jié)是4個整形數(shù)組,每個數(shù)字(分別是魔數(shù)、圖
????片數(shù)、圖片橫軸像素數(shù)、圖片縱軸像素數(shù))占4個字節(jié),接下來每個字節(jié)代表一個像素值。
????filename:待讀取文件絕對路徑
????“““
????with?gzip.open(filename?‘rb‘)?as?binfile:
????????buffers?=?binfile.read()
????binfile.close()
????head?=?struct.unpack_from(‘>IIII‘?buffers?0)?#?取前4個整數(shù),返回一個元組
????offset?=?struct.calcsize(‘>IIII‘)??#?定位到data開始的位置
????imgNum?width?height?=?head[1]?head[2]?head[3]
????bits?=?imgNum?*?width?*?height??#?data一共有60000*28*28個像素值
????bitsString?=?‘>‘?+?str(bits)?+?‘B‘??#?fmt格式:‘>47040000B‘
????imgs?=?struct.unpack_from(bitsString?buffers?offset)?#?取data數(shù)據(jù),返回一個元組
????imgs?=?np.reshape(imgs?[imgNum?1?width?height])?#?reshape為[60000784]型數(shù)組
????print(imgs.shape)
????return?imgs
def?load_labelset(filename):
????“““
????函數(shù)功能:讀取mnist數(shù)據(jù)的中的label壓縮包到np數(shù)組中并返回
????函數(shù)工作原理解釋:該二進制文件的內(nèi)容以“位”為單位,前8個字節(jié)是2個整形數(shù)組,每個數(shù)字(分別是魔數(shù)、
????laebl數(shù))占4個字節(jié),接下來每個字節(jié)代表一個label值。
????filename:待讀取文件絕對路徑
????“““
????with?gzip.open(filename?‘rb‘)?as?binfile:
????????buffers?=?binfile.read()
????binfile.close()
????head?=?struct.unpack_from(‘>II‘?buffers?0)?#?取前2個整數(shù),返回一個元組
????offset?=?struct.calcsize(‘>II‘)??#?定位到label數(shù)據(jù)開始的位置
????lblNum?=?head[1]?#?data一共有60000個像素值
????bitsString?=?‘>‘?+?str(lblNum)?+?‘B‘??#?fmt格式:‘>600000B‘
????lbls?=?struct.unpack_from(bitsString?buffers?offset)?#?取label數(shù)據(jù),返回一個元組
????lbls?=?np.reshape(lbls?[lblNum])?#?reshape為[60000]型列表
????print(lbls.shape)
????return?lbls
def?conv(inputdata?kernel):
????“““
????計算兩個np.adarray矩陣的卷積
????“““
????in_row?in_column?=?inputdata.shape
????kernel_width?kernel_height?=?kernel.shape
????re_row?re_clunm?=?in_row?-?kernel_width?+?1?in_column?-?kerne
- 上一篇:深度學習testCNN的python實現(xiàn)
- 下一篇:騎士周游問題源碼
評論
共有 條評論