天天看點

tensorflow開發之 基于softmax的mnist識别

文章目錄

  • 訓練過程
  • 定義神經網絡
  • 模型訓練
  • 可視化訓練結果
  • 模型存儲
  • 模型加載
  • 模型使用
  • 源碼

 ​

tensorflow開發之 基于softmax的mnist識别

tensorflow開發之 基于softmax的mnist識别

tensorflow開發之 基于softmax的mnist識别

tensorflow開發之 基于softmax的mnist識别

tensorflow開發之 基于softmax的mnist識别

tensorflow開發之 基于softmax的mnist識别

# 讀取資料
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(imgTrain, labelTrain),(imgTest, labelTest) = mnist.load_data(path='mnist.npz')
      
# 将2維矩陣變為1維向量
print('source data structure')
print(imgTrain.shape, type(imgTrain))
print(imgTest.shape, type(imgTest))

imgTrain = imgTrain.reshape(60000, 784)
imgTest = imgTest.reshape(10000, 784)

print('data structure after reshape')
print(imgTrain.shape, type(imgTrain))
print(imgTest.shape, type(imgTest))

# 資料歸一化處理
imgTrain = imgTrain.astype('float32')
imgTest = imgTest.astype('float32')
imgTrain /= 255
imgTest /= 255
      
# 資料編碼為one-hot
from keras.utils import np_utils

n_classes = 10
rstTrain = np_utils.to_categorical(labelTrain, n_classes)
rstTest = np_utils.to_categorical(labelTest, n_classes)

print(rstTrain[0], rstTrain.shape)
print(labelTrain[0], labelTrain.shape)
      
# 定義神經網絡(資料流圖)
from keras.models import Sequential
from keras.layers.core import Dense, Activation

# 第一層隐藏層
model = Sequential()
model.add(Dense(512, input_shape = (784,)))
model.add(Activation('relu'))

# 第二層隐藏層(上一層輸出作為本層輸入)
model.add(Dense(512))
model.add(Activation('relu'))

#最後一層輸出層為10個數值
model.add(Dense(10))
model.add(Activation('softmax'))
      
# 編譯模型
# compile(optimizer, loss=None, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
      
# 模型訓練
#fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)

history = model.fit(imgTrain, rstTrain, batch_size=128, epochs=5, verbose=2, validation_data=(imgTest, rstTest))
      
# 可視化展示訓練過程
import matplotlib.pyplot as plt
fig = plt.figure()
# 展示模型訓練過程精度變化
plt.subplot(2, 1, 1)
plt.plot(history.history['accuracy']) # 訓練準确率
plt.plot(history.history['val_accuracy']) #測試集上的準确率
plt.title('Model Accuracy')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['train', 'test'], loc='lower right')
# 展示損失值變化
plt.subplot(2, 1, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['train', 'test'], loc='upper right')
plt.tight_layout()
plt.show()
      
# 模型存儲
# model.save(filepath),存儲模型為一個獨立的HDF5檔案
# 其中含有模型訓練結果、模型自身、配置參數以及用于恢複訓練的優化器等
import os

modelName = ".\model\kerasMnist.h5"
if os.path.exists(modelName):
os.remove(modelName)    
model.save(modelName)
print('save the trained model in %s'% modelName)
      
# 加載模型
# keras.models.load_model(filepath)
from keras.models import load_model

mnistModel = load_model(modelName)
      
# 模型在測試集上的結果評估
import numpy as np
lossMetrics = mnistModel.evaluate(imgTest, rstTest, verbose = 2)
print("測試資料最終損失值:{}".format(lossMetrics[0]))
print("測試資料精度:{}".format(lossMetrics[1] * 100))

# 模型對測試集進行預測
rstPredict = mnistModel.predict(imgTest)
rstPredict = np.argmax(rstPredict,axis=1)
correctDatas = np.nonzero(rstPredict == labelTest)[0]
wrongDatas = np.nonzero(rstPredict != labelTest)[0]
print("預測正确個數:{}".format(len(correctDatas)))
print("預測錯誤個數:{}".format(len(wrongDatas)))