讀書筆記(一) Keras MNIST手寫數字識別數據

《TensorFlow+Keras深度學習人工智能實踐應用》讀書筆記

選擇MNIST手寫數據識別數據集是因爲其數據量不多,而且是單色的圖像,比較簡單,很適合自己這種初學者用來練習建立模型、訓練、預測。
MNIST數據集共有數據項60000項、測試數據10000項。MNIST數據集中的每一項數據都由images(數字圖像)與label(真實的數字)所組成。MNIST數據集


一、下載MNIST數據集

  1. 導入Kears及相關模塊
import numpy as np        //導入numpy模塊,numpy支持維數組與矩陣運算
import pandas as pd       
from keras.utils import np_utils     //導入keras.utils是爲了後續要將label標籤轉換爲One-Hot Encoding(一位有效編碼)
np.random.seed(10)      //設置seed可以產生的隨機數據

運行結果會顯示Using Tensorflow backend表示Keras自動以TensorFlow作爲Backend。

  1. 導入Keras模塊
    由於Keras已經提供了現成的模塊可以幫助我們下載並讀取MNIST數據,所以先導入MNIST模塊。
from keras.datasets import mnist
  1. 第一次進行MNIST數據的下載
(x_train_image, y_train_label),\
(x_test_image, y_test_label) = mnist.load_data()

第一次執行mnist.load()方法時,程序會檢查用戶目錄下是否有MNIST數據集,如果沒有,就會下載文件,所以運行時間比較長。

  1. 查看MNIST數據
print('train data=',len(x_train_image))
print('test data=',len(x_test_image))

運行結果爲train data=60000,test data=10000
即數據分爲兩部分:train訓練數據60000項,test測試數據10000項。

二、查看訓練數據

  1. 訓練數據是由images與labels組成的
print('x_train_image:',x_train_image.shape)
print('y_train_label:',y_train_label.shape)

運行結果爲x_train_image:(60000,28,28) y_train_label:(60000,)
image與label共60000項,image是單色的數字圖像,label是數字圖像的真實值。

  1. 定義plot_image函數顯示數字圖像
    爲了能夠顯示images數字圖像,我們創建下列plot_image函數:
import matplotlib.pyplot as plt
def plot_image(image):
    fig = plt.gcf()
    fig.set_size_inches(2,2)    //設置顯示圖形的大小
    plt.imshow(image, cmap='binary')   //使用plt.imshow顯示圖形,傳入參數image是28x28的圖形,cmap參數設置爲binary,以黑白灰度顯示
    plt.show()    //開始繪圖
  1. 執行plot_image函數查看第0個數字圖像
    程序調用
    plot_image(x_train_image[0])
  2. 查看第0項label數據
y_train_label[0]

運行結果爲5
第0項label數據是第0個數字圖像的真實值,所以是5。

三、查看多項訓練數據

  1. 創建plot_images_labels_prediction()函數
    創建下列函數方便我們查看數字圖形、真實的數字與預測結果。
import matplotlib.pyplot as plt
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
//images(數字圖像)、labels(真實值)、prediction(預測結果)、idx(開始顯示的數據index)、num(要顯示的數據項數,默認是10,不超過25)
    fig = plt.gcf()       
    fig.set_size_inches(12,14)      //設置圖形的大小
    if num>25: num=25   //如果要顯示的數據項數大於25,就設置爲25,以免發生錯誤
    for i in range(0,num):    //畫出num個數字圖形
        ax=plt.subplot(5,5,1+i)     //建立subgraph子圖形爲55列
        ax.imshow(images[idx],cmap='binary')   //畫出subgraph子圖形
        title="label="+str(labels[idx])    //設置子圖形標籤title,顯示標籤字段
        if len(prediction)>0:       //如果傳入了預測結果
            title+=",prediction="+str(prediction[idx])    //標題
            
        ax.set_title(title,fontsize=10)     //設置子圖形的標題
        ax.set_xticks([]);ax.set_yticks([])    //設置不顯示刻度
        idx+=1    //讀取下一項
    plt.show      //開始畫圖
  1. 查看訓練數據的前10項數據
    執行下列函數顯示前10項訓練數據。目前還沒有預測結果(prediction),所以傳入空list[],從第0項數據開始一直顯示到第9項數據。
plot_images_labels_prediction(x_train_image,y_train_label,[],0,10)


3. 查看test測試數據

print('x_test_image:',x_test_image.shape)
print('y_test_label:',y_test_label.shape)

運行結果:x_test_image: (10000, 28, 28)
y_test_label: (10000,)

4.顯示test測試數據
執行下列函數,顯示前10項測試數據

plot_images_labels_prediction(x_test_image,y_test_label,[],0,10)

四、多層感知器模型預處理

1、features數據預處理

feature(數字圖像特徵)數據預處理可分爲下列兩個步驟:

  • 將原本28x28的數字圖像以reshape轉換爲一維的向量,其長度是784,並且轉換爲Float
  • 數字圖像image數字標準化
  1. 查看image的shape
    查看每一個數字圖像的shape是28x28
print('x_train_image:',x_train_image.shape)
print('y_train_label:',y_train_label.shape)

運行結果:x_train_image: (60000, 28, 28)
y_train_label: (60000,)

  1. 將image以reshape轉換
    將原本28x28的二維數字圖像以reshape轉換爲一維的向量,再以astype轉換爲Float,共784個浮點數
x_Train = x_train_image.reshape(60000, 784).astype('float32')
x_Test = X_test_image.reshape(10000, 784).astype('float32')
  1. 查看轉換爲一維向量的shape
    查看每一個數字圖像是784個浮點數
print('x_train:',x_Train.shape)
print('x_test:',x_Test.shape)

運行結果:x_train: (60000, 784)
x_test: (10000, 784)

  1. 查看images圖像的內容
    查看images第0項的內容
    x_train_image[0]
    在這裏插入圖片描述
    根據執行結果,大部分都是0,少部分是數字。每一個數字都是從0到255的值,代表圖形每一個點灰度的深淺

  2. 將數字圖像images的數字標準化
    images的數字標準化可以提高後續訓練模型的準確率,因此images的數字都是從0到255的值,所以最簡單的標準化方式是除以255

x_Train_normalize = x_Train/255
x_Test_normalize = x_Test/255
  1. 查看數字圖像images數字標準化後的結果
    x_Train_normalize[0]

2、label數據預處理

label(數字圖像真實的值)標籤字段原本是0~9的數字,必須以One-Hot Encoding(一位有效編碼)轉換爲10個0或1的組合,例如數字7經過One-Hot Encoding轉換後是0000000100,正好對應輸出層的10個神經元。

  1. 查看原本的label標籤字段
    查看訓練數據label標籤字段的前5項訓練數據
    y_train_label[:5]
    運行結果:array([5, 0, 4, 1, 9], dtype=uint8)

  2. label標籤字段進行One-Hot-Encoding轉換

y_TrainOneHot = np_utils.to_categorical(y_train_label)   
y_TestOneHot = np_utils.to_categorical(y_test_label)
  1. 查看進行One-Hot-Encoding轉換之後的label標籤字段
    y_TrainOneHot[:5]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章