多層感知器識別手寫數字算法程序
阿新 • • 發佈:2018-09-27
itl cti val shape erb ase 鏈接 n) frame
1 #coding=utf-8 2 #1.數據預處理 3 import numpy as np #導入模塊,numpy是擴展鏈接庫 4 import pandas as pd 5 import tensorflow 6 import keras 7 from keras.utils import np_utils 8 np.random.seed(10) #設置seed可以產生的隨機數據 9 from keras.datasets import mnist #導入模塊,下載讀取mnist數據 10 (x_train_image,y_train_label),11 (x_test_image,y_test_label)=mnist.load_data() #下載讀取mnist數據 12 print(‘train data=‘,len(x_train_image)) 13 print(‘test data=‘,len(x_test_image)) 14 print(‘x_train_image:‘,x_train_image.shape) 15 print(‘y_train_label:‘,y_train_label.shape) 16 import matplotlib.pyplot as plt 17 def plot_image(image):18 fig=plt.gcf() 19 fig.set_size_inches(2,2) 20 plt.imshow(image,cmap=‘binary‘) 21 plt.show() 22 y_train_label[0] 23 import matplotlib.pyplot as plt 24 def plot_image_labels_prediction(image,lables,prediction,idx,num=10): 25 fig=plt.gcf() 26 fig.set_size_inches(12,14) 27if num>25:num=25 28 for i in range(0,num): 29 ax=plt.subplot(5,5,i+1) 30 ax.imshow(image[idx],cmap=‘binary‘) 31 title="lable="+str(lables[idx]) 32 if len(prediction)>0: 33 title+=",predict="+str(prediction[idx]) 34 ax.set_title(title,fontsize=10) 35 ax.set_xticks([]);ax.set_yticks([]) 36 idx+=1 37 plt.show() 38 plot_image_labels_prediction(x_train_image,y_train_label,[],0,10) 39 plot_image_labels_prediction(x_test_image,y_test_label,[],0,10) 40 x_Train=x_train_image.reshape(60000,784).astype(‘float32‘) #以reshape轉化成784個float 41 x_Test=x_test_image.reshape(10000,784).astype(‘float32‘) 42 x_Train_normalize=x_Train/255 #將features標準化 43 x_Test_normalize=x_Test/255 44 y_Train_OneHot=np_utils.to_categorical(y_train_label)#將訓練數據和測試數據的label進行one-hot encoding轉化 45 y_Test_OneHot=np_utils.to_categorical(y_test_label) 46 #2.建立模型 47 from keras.models import Sequential #可以通過Sequential模型傳遞一個layer的list來構造該模型,序慣模型是多個網絡層的線性堆疊 48 from keras.layers import Dense #全連接層 49 from keras.layers import Dropout #避免過度擬合 50 model=Sequential() 51 #建立輸入層、隱藏層 52 model.add(Dense(units=1000, 53 input_dim=784, 54 kernel_initializer=‘normal‘, 55 activation=‘relu‘)) 56 model.add(Dropout(0.5)) 57 model.add(Dense(units=1000, 58 kernel_initializer=‘normal‘, 59 activation=‘relu‘)) 60 model.add(Dropout(0.5)) 61 #建立輸出層 62 model.add(Dense(units=10, 63 kernel_initializer=‘normal‘, 64 activation=‘softmax‘)) 65 print(model.summary()) #查看模型的摘要 66 #3、進行訓練 67 #對訓練模型進行設置,損失函數、優化器、權值 68 model.compile(loss=‘categorical_crossentropy‘, 69 optimizer=‘adam‘,metrics=[‘accuracy‘]) 70 # 設置訓練與驗證數據比例,80%訓練,20%測試,執行10個訓練周期,每一個周期200個數據,顯示訓練過程2次 71 train_history=model.fit(x=x_Train_normalize, 72 y=y_Train_OneHot,validation_split=0.2, 73 epochs=10,batch_size=200,verbose=2) 74 #顯示訓練過程 75 import matplotlib.pyplot as plt 76 def show_train_history(train_history,train,validation): 77 plt.plot(train_history.history[train]) 78 plt.plot(train_history.history[validation]) 79 plt.title(‘Train History‘) 80 plt.ylabel(train) 81 plt.xlabel(‘Epoch‘) 82 plt.legend([‘train‘,‘validation‘],loc=‘upper left‘) #顯示左上角標簽 83 plt.show() 84 show_train_history(train_history,‘acc‘,‘val_acc‘) #畫出準確率評估結果 85 show_train_history(train_history,‘loss‘,‘val_loss‘) #畫出誤差執行結果 86 #以測試數據評估模型準確率 87 scores=model.evaluate(x_Test_normalize,y_Test_OneHot) #創建變量存儲評估後的準確率數據,(特征值,真實值) 88 print() 89 print(‘accuracy‘,scores[1]) 90 #進行預測 91 prediction=model.predict_classes(x_Test) 92 prediction 93 plot_image_labels_prediction(x_test_image,y_test_label,prediction,idx=340) 94 #4、建立模型提高預測準確率 95 #建立混淆矩陣 96 import pandas as pd #pandas 是基於NumPy 的一種工具,該工具是為了解決數據分析任務而創建的 97 pd.crosstab(y_test_label,prediction, 98 rownames=[‘label‘],colnames=[‘predict‘]) 99 #建立真實值與預測值dataFrame 100 df=pd.DataFrame({‘label‘:y_test_label,‘predict‘:prediction}) 101 df[:2] 102 df[(df.label==5)&(df.predict==3)] 103 plot_image_labels_prediction(x_test_image,y_test_label,prediction,idx=340,num=1)
多層感知器識別手寫數字算法程序