純numpy卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字識別的實踐
前面講解了使用純numpy實現(xiàn)數(shù)值微分和誤差反向傳播法的手寫數(shù)字識別,這兩種網(wǎng)絡(luò)都是使用全連接層的結(jié)構(gòu)。全連接層存在什么問題呢?那就是數(shù)據(jù)的形狀被“忽視”了。比如,輸入數(shù)據(jù)是圖像時,圖像通常是高、長、通道方向上的3維形狀。但是,向全連接層輸入時,需要將3維數(shù)據(jù)拉平為1維數(shù)據(jù)。實際上,前面提到的使用了MNIST數(shù)據(jù)集的例子中,輸入圖像就是1通道、高28像素、長28像素的(1, 28, 28)形狀,但卻被排成1列,以784個數(shù)據(jù)的形式輸入到最開始的Affine層。
圖像是3維形狀,這個形狀中應(yīng)該含有重要的空間信息。比如空間上鄰近的像素為相似的值、RBG的各個通道之間分別有密切的關(guān)聯(lián)性、相距較遠(yuǎn)的像素之間沒有什么關(guān)聯(lián)等,3維形狀中可能隱藏有值得提取的本質(zhì)模式。但是,因為全連接層會忽視形狀,將全部的輸入數(shù)據(jù)作為相同的神經(jīng)元(同一維度的神經(jīng)元)處理,所以無法利用與形狀相關(guān)的信息。而卷積層可以保持形狀不變。當(dāng)輸入數(shù)據(jù)是圖像時,卷積層會以3維數(shù)據(jù)的形式接收輸入數(shù)據(jù),并同樣以3維數(shù)據(jù)的形式輸出至下一層。因此,在CNN中,可以(有可能)正確理解圖像等具有形狀的數(shù)據(jù)。
在全連接神經(jīng)網(wǎng)絡(luò)中,除了權(quán)重參數(shù),還存在偏置。CNN中,濾波器的參數(shù)就對應(yīng)之前的權(quán)重,并且,CNN中也存在偏置。
三維數(shù)據(jù)的卷積運(yùn)算,通道方向上有多個特征圖時,會按通道進(jìn)行輸入數(shù)據(jù)和濾波器的卷積運(yùn)算,然后將結(jié)果相加,從而得到輸出。
在上面的圖中,輸出的是一張?zhí)卣鲌D,換句話說,就是通道數(shù)為1的特征圖。那么,如果要在通道方向上也擁有多個卷積運(yùn)算的輸出,就應(yīng)該使用多個濾波器(權(quán)重)。
卷積運(yùn)算的處理流如下:
卷積運(yùn)算的處理流,批處理如下:
而池化層是縮小高、長空間上的運(yùn)算。
上圖是Max池化,取出2x2區(qū)域中的最大值元素。除了Max池化外,還有Average池化,在圖像識別領(lǐng)域,主要使用Max池化。
網(wǎng)絡(luò)的構(gòu)成是“Convolution - ReLU - Pooling -Affine - ReLU - Affine - Softmax”,訓(xùn)練代碼如下:
import numpy as np from collections import OrderedDict import matplotlib.pylab as plt from dataset.mnist import load_mnist import pickle def im2col(input_data, filter_h, filter_w, stride=1, pad=0): """ Parameters ---------- input_data : 由(數(shù)據(jù)量, 通道, 高, 長)的4維數(shù)組構(gòu)成的輸入數(shù)據(jù) filter_h : 濾波器的高 filter_w : 濾波器的長 stride : 步幅 pad : 填充 Returns ------- col : 2維數(shù)組 """ N, C, H, W = input_data.shape out_h = (H + 2*pad - filter_h)//stride + 1 out_w = (W + 2*pad - filter_w)//stride + 1 img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant') col = np.zeros((N, C, filter_h, filter_w, out_h, out_w)) for y in range(filter_h): y_max = y + stride*out_h for x in range(filter_w): x_max = x + stride*out_w col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride] col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1) return col def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0): """ Parameters ---------- col : input_shape : 輸入數(shù)據(jù)的形狀(例:(10, 1, 28, 28)) filter_h : filter_w stride pad Returns ------- """ N, C, H, W = input_shape out_h = (H + 2*pad - filter_h)//stride + 1 out_w = (W + 2*pad - filter_w)//stride + 1 col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2) img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1)) for y in range(filter_h): y_max = y + stride*out_h for x in range(filter_w): x_max = x + stride*out_w img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :] return img[:, :, pad:H + pad, pad:W + pad] class Relu: def __init__(self): self.mask = None def forward(self, x): self.mask = (x <= 0) out = x.copy() out[self.mask] = 0 return out def backward(self, dout): dout[self.mask] = 0 dx = dout return dx def softmax(x): if x.ndim == 2: x = x.T x = x - np.max(x, axis=0) y = np.exp(x) / np.sum(np.exp(x), axis=0) return y.T x = x - np.max(x) # 溢出對策 return np.exp(x) / np.sum(np.exp(x)) def cross_entropy_error(y, t): if y.ndim == 1: t = t.reshape(1, t.size) y = y.reshape(1, y.size) # 監(jiān)督數(shù)據(jù)是one-hot-vector的情況下,轉(zhuǎn)換為正確解標(biāo)簽的索引 if t.size == y.size: t = t.argmax(axis=1) batch_size = y.shape[0] return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size class SoftmaxWithLoss: def __init__(self): self.loss = None self.y = None # softmax的輸出 self.t = None # 監(jiān)督數(shù)據(jù) def forward(self, x, t): self.t = t self.y = softmax(x) self.loss = cross_entropy_error(self.y, self.t) return self.loss def backward(self, dout=1): batch_size = self.t.shape[0] if self.t.size == self.y.size: # 監(jiān)督數(shù)據(jù)是one-hot-vector的情況 dx = (self.y - self.t) / batch_size else: dx = self.y.copy() dx[np.arange(batch_size), self.t] -= 1 dx = dx / batch_size return dx #Affine層的實現(xiàn) class Affine: def __init__(self,W,b): self.W=W self.b=b self.x=None self.dW=None self.db=None self.original_x_shape = None def forward(self,x): #對于卷積層 需要把數(shù)據(jù)先展平 self.original_x_shape = x.shape x=x.reshape(x.shape[0],-1) self.x=x out=np.dot(x,self.W)+self.b return out def backward(self,dout): dx=np.dot(dout,self.W.T) self.dW=np.dot(self.x.T,dout) self.db=np.sum(dout,axis=0) # 還原輸入數(shù)據(jù)的形狀(對應(yīng)張量) dx = dx.reshape(*self.original_x_shape) return dx #卷積層的實現(xiàn) class Convolution: def __init__(self,W,b,stride=1,pad=0): self.W=W self.b=b self.stride=stride self.pad=pad # 中間數(shù)據(jù)(backward時使用) self.x = None self.col = None self.col_W = None # 權(quán)重和偏置參數(shù)的梯度 self.dW = None self.db = None def forward(self,x): #濾波器的數(shù)目、通道數(shù)、高、寬 FN,C,FH,FW=self.W.shape #輸入數(shù)據(jù)的數(shù)目、通道數(shù)、高、寬 N,C,H,W=x.shape #輸出特征圖的高、寬 out_h=int(1+(H+2*self.pad-FH)/self.stride) out_w=int(1+(W+2*self.pad-FW)/self.stride) #輸入數(shù)據(jù)使用im2col展開 col=im2col(x,FH,FW,self.stride,self.pad) #濾波器的展開 col_W=self.W.reshape(FN,-1).T #計算 out=np.dot(col,col_W)+self.b #變換輸出數(shù)據(jù)的形狀 #(N,h,w,C)->(N,c,h,w) out=out.reshape(N,out_h,out_w,-1).transpose(0,3,1,2) self.x = x self.col = col self.col_W = col_W return out def backward(self, dout): FN, C, FH, FW = self.W.shape dout = dout.transpose(0,2,3,1).reshape(-1, FN) self.db = np.sum(dout, axis=0) self.dW = np.dot(self.col.T, dout) self.dW = self.dW.transpose(1, 0).reshape(FN, C, FH, FW) dcol = np.dot(dout, self.col_W.T) dx = col2im(dcol, self.x.shape, FH, FW, self.stride, self.pad) return dx #池化層的實現(xiàn) class Pooling: def __init__(self,pool_h,pool_w,stride=1,pad=0): self.pool_h=pool_h self.pool_w=pool_w self.stride=stride self.pad=pad self.x = None self.arg_max = None def forward(self,x): #輸入數(shù)據(jù)的數(shù)目、通道數(shù)、高、寬 N,C,H,W=x.shape #輸出數(shù)據(jù)的高、寬 out_h=int(1+(H-self.pool_h)/self.stride) out_w=int(1+(W-self.pool_w)/self.stride) #展開 col=im2col(x,self.pool_h,self.pool_w,self.stride,self.pad) col=col.reshape(-1,self.pool_h*self.pool_w) #最大值 arg_max = np.argmax(col, axis=1) out=np.max(col,axis=1) #轉(zhuǎn)換 out=out.reshape(N,out_h,out_w,C).transpose(0,3,1,2) self.x = x self.arg_max = arg_max return out def backward(self, dout): dout = dout.transpose(0, 2, 3, 1) pool_size = self.pool_h * self.pool_w dmax = np.zeros((dout.size, pool_size)) dmax[np.arange(self.arg_max.size), self.arg_max.flatten()] = dout.flatten() dmax = dmax.reshape(dout.shape + (pool_size,)) dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1) dx = col2im(dcol, self.x.shape, self.pool_h, self.pool_w, self.stride, self.pad) return dx #SimpleNet class SimpleConvNet: def __init__(self,input_dim=(1,28,28), conv_param={'filter_num':30,'filter_size':5,'pad':0,'stride':1}, hidden_size=100, output_size=10, weight_init_std=0.01): filter_num=conv_param['filter_num']#30 filter_size=conv_param['filter_size']#5 filter_pad=conv_param['pad']#0 filter_stride=conv_param['stride']#1 input_size=input_dim[1]#28 conv_output_size=int((1+input_size+2*filter_pad-filter_size)/filter_stride)#24 #pool 默認(rèn)的是2x2最大值池化 池化層的大小變?yōu)榫矸e層的一半30*12*12=4320 pool_output_size=int(filter_num*(conv_output_size/2)*(conv_output_size/2)) #權(quán)重參數(shù)的初始化部分 濾波器和偏置 self.params={} #(30,1,5,5) self.params['W1']=np.random.randn(filter_num,input_dim[0],filter_size,filter_size)*weight_init_std #(30,) self.params['b1']=np.zeros(filter_num) #(4320,100) self.params['W2']=np.random.randn(pool_output_size,hidden_size)*weight_init_std #(100,) self.params['b2']=np.zeros(hidden_size) #(100,10) self.params['W3']=np.random.randn(hidden_size,output_size)*weight_init_std #(10,) self.params['b3']=np.zeros(output_size) #生成必要的層 self.layers=OrderedDict() #(N,1,28,28)->(N,30,24,24) self.layers['Conv1']=Convolution(self.params['W1'],self.params['b1'],conv_param['stride'],conv_param['pad']) #(N,30,24,24) self.layers['Relu1']=Relu() #池化層的步幅大小和池化應(yīng)用區(qū)域大小相等 #(N,30,12,12) self.layers['Pool1']=Pooling(pool_h=2,pool_w=2,stride=2) #全連接層 #全連接層內(nèi)部有個判斷 首先是把數(shù)據(jù)展平 #(N,30,12,12)->(N,4320)->(N,100) self.layers['Affine1']=Affine(self.params['W2'],self.params['b2']) #(N,100) self.layers['Relu2']=Relu() #(N,100)->(N,10) self.layers['Affine2']=Affine(self.params['W3'],self.params['b3']) self.last_layer=SoftmaxWithLoss() def predict(self,x): for layer in self.layers.values(): x=layer.forward(x) return x def loss(self,x,t): y=self.predict(x) return self.last_layer.forward(y,t) def gradient(self,x,t): #forward self.loss(x,t) #backward dout=1 dout=self.last_layer.backward(dout) layers=list(self.layers.values()) layers.reverse() for layer in layers: dout=layer.backward(dout) #梯度 grads={} grads['W1']=self.layers['Conv1'].dW grads['b1']=self.layers['Conv1'].db grads['W2']=self.layers['Affine1'].dW grads['b2']=self.layers['Affine1'].db grads['W3']=self.layers['Affine2'].dW grads['b3']=self.layers['Affine2'].db return grads #計算準(zhǔn)確率 def accuracy(self,x,t): y=self.predict(x) y=np.argmax(y,axis=1) if t.ndim !=1: t=np.argmax(t,axis=1) accuracy=np.sum(y==t)/float(x.shape[0]) return accuracy #保存模型參數(shù) def save_params(self, file_name="params.pkl"): params = {} for key, val in self.params.items(): params[key] = val with open(file_name, 'wb') as f: pickle.dump(params, f) #載入模型參數(shù) def load_params(self, file_name="params.pkl"): with open(file_name, 'rb') as f: params = pickle.load(f) for key, val in params.items(): self.params[key] = val for i, key in enumerate(['Conv1', 'Affine1', 'Affine2']): self.layers[key].W = self.params['W' + str(i+1)] self.layers[key].b = self.params['b' + str(i+1)] if __name__=='__main__': (x_train,t_train),(x_test,t_test)=load_mnist(flatten=False) # 處理花費時間較長的情況下減少數(shù)據(jù) x_train, t_train = x_train[:5000], t_train[:5000] x_test, t_test = x_test[:1000], t_test[:1000] net=SimpleConvNet(input_dim=(1,28,28), conv_param = {'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1}, hidden_size=100, output_size=10, weight_init_std=0.01) train_loss_list=[] #超參數(shù) iter_nums=1000 train_size=x_train.shape[0] batch_size=100 learning_rate=0.1 #記錄準(zhǔn)確率 train_acc_list=[] test_acc_list=[] #平均每個epoch的重復(fù)次數(shù) iter_per_epoch=max(train_size/batch_size,1) for i in range(iter_nums): #小批量數(shù)據(jù) batch_mask=np.random.choice(train_size,batch_size) x_batch=x_train[batch_mask] t_batch=t_train[batch_mask] #計算梯度 #誤差反向傳播法 計算很快 grad=net.gradient(x_batch,t_batch) #更新參數(shù) 權(quán)重W和偏重b for key in ['W1','b1','W2','b2']: net.params[key]-=learning_rate*grad[key] #記錄學(xué)習(xí)過程 loss=net.loss(x_batch,t_batch) print('訓(xùn)練次數(shù):'+str(i)+' loss:'+str(loss)) train_loss_list.append(loss) #計算每個epoch的識別精度 if i%iter_per_epoch==0: #測試在所有訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)上的準(zhǔn)確率 train_acc=net.accuracy(x_train,t_train) test_acc=net.accuracy(x_test,t_test) train_acc_list.append(train_acc) test_acc_list.append(test_acc) print('train acc:'+str(train_acc)+' test acc:'+str(test_acc)) # 保存參數(shù) net.save_params("params.pkl") print("模型參數(shù)保存成功!") print(train_acc_list) print(test_acc_list) # 繪制圖形 markers = {'train': 'o', 'test': 's'} x = np.arange(len(train_acc_list)) plt.plot(x, train_acc_list, label='train acc') plt.plot(x, test_acc_list, label='test acc', linestyle='--') plt.xlabel("epochs") plt.ylabel("accuracy") plt.ylim(0, 1.0) plt.legend(loc='lower right') plt.show()
訓(xùn)練過程如下:
訓(xùn)練的結(jié)果如圖所示:
到此這篇關(guān)于純numpy實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字識別的實踐的文章就介紹到這了,更多相關(guān)numpy手寫數(shù)字識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python全局設(shè)置id 自動化測試元素定位過程解析
這篇文章主要介紹了基于python全局設(shè)置id 自動化測試元素定位過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-092020新版本pycharm+anaconda+opencv+pyqt環(huán)境配置學(xué)習(xí)筆記,親測可用
這篇文章主要介紹了2020新版本pycharm+anaconda+opencv+pyqt環(huán)境配置學(xué)習(xí)筆記,親測可用,特此分享到腳本之家平臺,需要的朋友可以參考下2020-03-03python中數(shù)字列表轉(zhuǎn)化為數(shù)字字符串的實例代碼
先前學(xué)習(xí)過,數(shù)字和字符串都可以存儲到變量當(dāng)中,下面這篇文章主要給大家介紹了關(guān)于python中數(shù)字列表轉(zhuǎn)化為數(shù)字字符串的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02關(guān)于Python中的if __name__ == __main__詳情
在學(xué)習(xí)Python的過程中發(fā)現(xiàn)即使把if __name__ == ‘__main__’ 去掉,程序還是照樣運(yùn)行。很多小伙伴只知道是這么用的,也沒有深究具體的作用。這篇文字就來介紹一下Python中的if __name__ == ‘__main__’的作用,需要的朋友參考下文2021-09-09Python實現(xiàn)把回車符\r\n轉(zhuǎn)換成\n
這篇文章主要介紹了Python實現(xiàn)把回車符\r\n轉(zhuǎn)換成\n,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-04-04Python?Web開發(fā)通信協(xié)議WSGI?uWSGI?uwsgi使用對比全面介紹
這篇文章主要為大家介紹了Python?Web開發(fā)通信協(xié)議WSGI?uWSGI?uwsgi使用對比全面介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12