-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keras_Mnist_LSTM
85 lines (69 loc) · 3 KB
/
Keras_Mnist_LSTM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import numpy as np
from keras.utils import np_utils
np.random.seed(10)
from keras.datasets import mnist
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import LSTM,Dropout,Dense
def show_images_labels_predictions(images,labels,
predictions,start_id,num=10):
plt.gcf().set_size_inches(12, 14)
if num>25: num=25
for i in range(0, num):
ax=plt.subplot(5,5, 1+i)
#顯示黑白圖片
ax.imshow(images[start_id], cmap='binary')
# 有 AI 預測結果資料, 才在標題顯示預測結果
if( len(predictions) > 0 ) :
title = 'ai = ' + str(predictions[i])
# 預測正確顯示(o), 錯誤顯示(x)
title += (' (o)' if predictions[i]==labels[i] else ' (x)')
title += '\nlabel = ' + str(labels[i])
# 沒有 AI 預測結果資料, 只在標題顯示真實數值
else :
title = 'label = ' + str(labels[i])
# X, Y 軸不顯示刻度
ax.set_title(title,fontsize=12)
ax.set_xticks([]);ax.set_yticks([])
start_id+=1
plt.show()
#建立訓練資料和測試資料,包括訓練特徵集、訓練標籤和測試特徵集、測試標籤
(train_feature, train_label),\
(test_feature, test_label) = mnist.load_data()
#將 Features 特徵值換為 60000*28*28 的 3 維矩陣
train_feature_vector =train_feature.reshape(len(train_feature),28,28).astype('float32')
test_feature_vector = test_feature.reshape(len(test_feature),28,28).astype('float32')
#Features 特徵值標準化
train_feature_normalize = train_feature_vector/255
test_feature_normalize = test_feature_vector/255
#label 轉換為 One-Hot Encoding 編碼
train_label_onehot = np_utils.to_categorical(train_label)
test_label_onehot = np_utils.to_categorical(test_label)
# 建立簡單的線性執行的模型
model = Sequential()
model.add(LSTM(
input_shape=( 28, 28),
units=256,
unroll=True
))
# Dropout層防止過度擬合,拋棄比例:0.1
model.add(Dropout(0.1))
# 輸出層
model.add(Dense(units=10, kernel_initializer='normal', activation='softmax'))
#定義訓練方式
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
#以(train_feature_normalize,train_label_onehot)資料訓練,
#訓練資料保留 20% 作驗證,訓練10次、每批次讀取200筆資料,顯示簡易訓練過程
train_history =model.fit(x=train_feature_normalize,
y=train_label_onehot,validation_split=0.2,
epochs=10, batch_size=200,verbose=2)
#評估準確率
scores = model.evaluate(test_feature_normalize, test_label_onehot)
print('\n準確率=',scores[1])
#預測
#prediction=model.predict_classes(test_feature_normalize)
prediction = model.predict(test_feature_normalize)
prediction = np.argmax(prediction,axis=1)
#顯示圖像、預測值、真實值
show_images_labels_predictions(test_feature,test_label,prediction,0)