Keras實作練習
建置一個手寫辨識
首先匯入 Keras 及相關模組:
Code[part 1]:
import numpy as np
import pandas as pd
import sys, os
from keras.utils import np_utils
from keras.datasets import mnist
np.random.seed(10)
11.
Keras實作練習
建置一個手寫辨識
讀取與查看 mnist 資料
可知道training data 共有 60,000 筆; testing data 共有 10,000 筆
Code[ part 2]:
(X_train_image, y_train_label), (X_test_image, y_test_label) = mnist.load_data()
print("t[Info] train data={:7,}".format(len(X_train_image)))
print("t[Info] test data={:7,}".format(len(X_test_image)))
print("t[Info] Shape of train data=%s" % (str(X_train_image.shape)))
print("t[Info] Shape of train label=%s" % (str(y_train_label.shape)))
Keras實作練習
建置一個手寫辨識
評估模型準確率 、進行預測
Code[ part10]:
print("t[Info] Making prediction to x_Test_norm")
prediction = model.predict_classes(x_Test_norm) # Making prediction and save result to prediction
print()
print("t[Info] Show 10 prediction result (From 240):")
print("%sn" % (prediction[240:250]))
if isDisplayAvl():
plot_images_labels_predict(X_test_image, y_test_label, prediction, idx=240)
print("t[Info] Error analysis:")
for i in range(len(prediction)):
if prediction[i] != y_test_label[i]:
print("tAt %d'th: %d is with wrong prediction as %d!" % (i, y_test_label[i], prediction[i]))
print()