原文鏈接:https://data-flair.training/blogs/python-deep-learning-project-handwritten-digit-recognition/
原文講得很詳細,這里補充一些注釋。由於直接從庫導入mnist數據集需要的時間非常久,因此這里導入的是本地已下載好的mnist數據集。(但我懷疑我下了假的數據集,咋驗證准確率這么低,所以這里不提供了)
import keras from keras import backend as K import numpy as np from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D batch_size = 128 #一次訓練所選取的樣本數 num_classes = 10 #分類個數 epochs = 10 #訓練輪數 #讀取已下載到本地的數據集 f=np.load('C:/Users/Administrator/.keras/datasets/mnist.npz') x_train,y_train=f['x_train'],f['y_train'] x_test,y_test=f['x_test'],f['y_test'] #print(x_train.shape, y_train.shape) #數據預處理 x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) input_shape = (28, 28, 1) x_train = x_train.astype('float32') #轉換數據類型 x_test = x_test.astype('float32') x_train /= 255 #歸一化 x_test /= 255 y_train = keras.utils.to_categorical(y_train, num_classes) #將整形數組轉化為二元類型矩陣 y_test = keras.utils.to_categorical(y_test, num_classes) #print('x_train shape:', x_train.shape) #print(x_train.shape[0], 'train samples') #print(x_test.shape[0], 'test samples') #創建CNN模型 model = Sequential() #這里采用順序模型構建CNN #輸入層,這里指定輸入數據形狀為28*28*1 卷積核數量為32 形狀為3*3 model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape)) #添加中間層 model.add(Conv2D(64, (3, 3), activation='relu')) #卷積層 model.add(MaxPooling2D(pool_size=(2, 2))) #最大池化層 model.add(Dropout(0.25)) #通過Dropout防止過擬合 model.add(Flatten()) #展平層 model.add(Dense(256, activation='relu')) #全連接層 model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) #損失函數 model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy']) #訓練模型 hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=2,validation_data=(x_test, y_test)) print("模型訓練完成") #模型評估 score = model.evaluate(x_test, y_test, verbose=0) print('test loss: ', score[0]) print('test accuracy: ', score[1])
