Demo Project
- 手寫辨識系統 by MNIST
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
print("train_images original shape: ", train_images.shape)
print("train_labels original shape: ", train_labels.shape)
from keras import models
from keras import layers
network = models.Sequential()
# Hidden layer
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
# Output layer
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer='rmsprop', # 優化器選擇
loss='categorical_crossentropy', # 損失函數
metrics=['accuracy']) # 用準確度做評量
fix_train_images = train_images.reshape((60000, 28 * 28)).astype('float32') / 255
fix_test_images = test_images.reshape((10000, 28 * 28)).astype('float32') / 255
from keras.utils import to_categorical
fix_train_labels = to_categorical(train_labels)
fix_test_labels = to_categorical(test_labels)
result = network.fit(
fix_train_images, # training data
fix_train_labels, # 正確答案
epochs=12, # 代表要跑幾次
batch_size=128, # 每次訓練使用幾筆資料, 所以 60000 /128 約等於 469 組
validation_data=(fix_test_images, fix_test_labels))
test_loss, test_acc = network.evaluate(fix_test_images, fix_test_labels)
print('test_loss:', test_loss)
print('test_acc:', test_acc)
===== 以下是廢 code ====
# After training the model, it is time to use it to do predictions on testing data.
y_pred = classifier.predict(xtest)
plt.title('Classifier (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()