iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 16
0
自我挑戰組

Tensorflow學習日記系列 第 16

tensorflow學習日記Day16 多元感知器模型預測(下)

  • 分享至 

  • xImage
  •  

from keras.utils import np_utils
import numpy as np
np.random.seed(10)
from keras.datasets import mnist
#預處理
(x_train_image,y_train_label),
(x_test_image,y_test_label)=mnist.load_data()
x_Train = x_train_image.reshape(60000,784).astype('float32')
x_Test = x_test_image.reshape(10000,784).astype('float32')
x_Train_normalize = x_Train / 255
x_Test_normalize = x_Test / 255
y_Train_OneHot = np_utils.to_categorical(y_train_label)
y_Test_OneHot = np_utils.to_categorical(y_test_label)

#建立模型
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=256,input_dim=784,kernel_initializer='normal',activation='relu'))
model.add(Dense(units=10,kernel_initializer='normal',activation='softmax'))
#print(model.summary())

#訓練
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
train_history = model.fit(x=x_Train_normalize,y = y_Train_OneHot,validation_split=0.2,epochs = 10,batch_size=200,verbose=2)

#建立show_train_history來顯示訓練過程
import matplotlib.pyplot as plt#定義show_train_history(之前訓練產生的)(train_history,訓練執行結果,驗證資料執行結果)
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')#圖形標題
plt.ylabel(train)#顯示y軸標籤
plt.xlabel('Epoch')#設定x軸標籤是'Epoch'
plt.legend(['train','validation'],loc='upper left')#設定圖例顯示'train','validation'在左上角
plt.show()
#畫出accuracy執行結果(這裡有一個要注意的地方是,windows在顯示訓練結果的時候是以acc表現accuracy所以要將accuracy改為acc,mac則是要寫accuracy)
#show_train_history(train_history,'accuracy','val_accuracy')
#畫出loss執行結果
#show_train_history(train_history,'loss','val_loss')

#使用model.evaluate進行評估模型準確率,評估後的準確率會存在scores(features,label)
scores = model.evaluate(x_Test_normalize,y_Test_OneHot)
print()
#顯示準確率
print('accuracy',scores[1])
#執行預測
prediction = model.predict_classes(x_Test)
prediction
plot_images_labels_prediction(x_test_image,y_test_label,prediction,idx=340)

輸出:
https://scontent-hkg3-1.xx.fbcdn.net/v/t1.15752-9/71263299_410786479579585_8501644697347817472_n.png?_nc_cat=110&_nc_oc=AQkPlrWmATpiY5cYbaKmxgXM1jv7VJXrKkFoY2PBgJz-NzfenEO6L5-olEGtdTRPN4c&_nc_ht=scontent-hkg3-1.xx&oh=32b54ae0a3aaa9669297b240d05b76e3&oe=5E3503BA

#使用pandas建立混淆矩陣

import pandas as pd
#使用pd.crosstab建立混淆矩陣(測試資料label,預測值,設定行的名稱,設定列的名稱)
pd.crosstab(y_test_label,prediction,rownames=['label'],colnames=['predict'])

輸出:
https://scontent-hkg3-1.xx.fbcdn.net/v/t1.15752-9/71734990_538865786867810_9130825155302391808_n.png?_nc_cat=106&_nc_oc=AQlCeLBnZm9nSZ6Pf9iC0imxspXrrJY8eQ19U9d9Yq_uKoOyd_iixkpv0U9qIDkAgFA&_nc_ht=scontent-hkg3-1.xx&oh=698218bb8dffb63d33e77779c9fa6360&oe=5E3043AB

對角線是預測正確,可以看到5和3最容易搞混

#建立df查看label和prediction
df = pd.DataFrame({'label':y_test_label,'predict':prediction})
df[:2]

輸出:
https://scontent-hkg3-1.xx.fbcdn.net/v/t1.15752-9/71515446_1662845703851789_1968994981955764224_n.png?_nc_cat=110&_nc_oc=AQk04X69r4gnCrgDHXiaUpLbweJMLDVHbnv3BklwdfvHFNGzNUJ-hunpq4TpV-ZZTRQ&_nc_ht=scontent-hkg3-1.xx&oh=2dca5e347e2dd3392d91579daa778f07&oe=5DF995B7

#建立完成後,查看當label=5,predict=3的資料
df[(df.label==5)&(df.predict==3)]

輸出:
https://scontent-hkg3-1.xx.fbcdn.net/v/t1.15752-9/71310074_531353820956891_5304905521097080832_n.png?_nc_cat=110&_nc_oc=AQl5WlAwFLRa_Qc09ImSlhv3RZJJq1SooJxVTwtReFDwNFWDnaQ6OHRUJmnvPu7MbSI&_nc_ht=scontent-hkg3-1.xx&oh=6bfbe0c90b52787a65bacde8b1a32b7c&oe=5DF68E56

可以看到真實值是5預測卻是3的資料為340,1003,1393...等

#然後我們想知道是哪些圖被誤判
plot_images_labels_prediction(x_test_image,y_test_label,prediction,idx=第幾筆資料,num)
https://scontent-hkg3-1.xx.fbcdn.net/v/t1.15752-9/72133511_314428029404926_7162069437384753152_n.png?_nc_cat=107&_nc_oc=AQnpAVFXRCJ_2WPfH9mHO8h_9e9c35m0FcItUUcLAXlRh8156jMHZJd69CvB4vu79gM&_nc_ht=scontent-hkg3-1.xx&oh=462be55d208ab7b348b187a359e4b7c6&oe=5DF262D4

可以看到都是一些被寫得很像3的5


上一篇
tensorflow學習日記Day15 多元感知資料預處理
下一篇
tensorflow學習日記Day17 多元感知器模型 增加神經元
系列文
Tensorflow學習日記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言