Keras搭建序贯式模型
生活随笔
收集整理的這篇文章主要介紹了
Keras搭建序贯式模型
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學(xué)習(xí)Keras搭建序貫式模型,并學(xué)習(xí)使用MNIST手寫數(shù)字識別例子
from tensorflow.keras import layers, models# 創(chuàng)建一個序貫式模型對象 model = models.Sequential()# 添加第一個網(wǎng)絡(luò)層(作為輸入層) # 參數(shù)1:本層神經(jīng)元個數(shù),2:激活函數(shù)類型,3:輸入樣本的形狀 model.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))# 添加第二個網(wǎng)絡(luò)層(作為輸出層) # 參數(shù)1:輸出個數(shù),2:激活函數(shù)類型 model.add(layers.Dense(10, activation='softmax'))# 輸出模型的概覽 model.summary()使用MNIST手寫數(shù)字識別例子,MNIST是經(jīng)典的手寫數(shù)字圖片數(shù)據(jù)集,下載地址:https://s3.amazonaws.com/img-datasets/mnist.npz,下載后存放于python代碼的目錄下
from tensorflow.keras import datasets# 1.加載MNIST數(shù)據(jù)集# 加載數(shù)據(jù)集 (X_train, y_train),(X_test, y_test)=datasets.mnist.load_data()# 查看拆分結(jié)果 print(X_train.shape, y_train.shape) print(X_test.shape, y_test.shape)# 2.使用Matplotlib查看圖片 import matplotlib.pyplot as plt# 查看訓(xùn)練集前10張圖片 fig = plt.figure(figsize=(12,4)) #定義畫板 for i in range(10):ax = fig.add_subplot(2,5, i+1)ax.matshow(X_train[i]) plt.show()# 3.數(shù)據(jù)預(yù)處理,搭建前饋神經(jīng)網(wǎng)絡(luò) #搭建序貫式模型,第一個網(wǎng)絡(luò)層作為輸入層,使用512個神經(jīng)元(可以自定義),第二個網(wǎng)絡(luò)層作為輸出層,使用10個神經(jīng)元(對應(yīng)10個類別),激活函數(shù)使用softmax from tensorflow.keras import layers, models# 轉(zhuǎn)換數(shù)據(jù)集的形狀,(轉(zhuǎn)成二維) X_train = X_train.reshape(60000, 28*28) X_test = X_test.reshape(10000, 28*28) print(X_train.shape)# 創(chuàng)建一個序貫式模型對象 model = models.Sequential()# 添加第一個網(wǎng)絡(luò)層(作為輸入層) # 參數(shù)1:本層神經(jīng)元個數(shù),2:激活函數(shù)類型,3:輸入樣本的形狀 model.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))# 添加第二個網(wǎng)絡(luò)層(作為輸出層) model.add(layers.Dense(10, activation='softmax'))# 輸出模型的概覽 model.summary()# 4.編譯、訓(xùn)練模型,評估準(zhǔn)確率 # 編譯參數(shù)包括優(yōu)化器、損失函數(shù)、評價指標(biāo) # 編譯模型 model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy',metrics=['accuracy'])# 訓(xùn)練模型 # 訓(xùn)練參數(shù)包括:訓(xùn)練集數(shù)據(jù)、訓(xùn)練集標(biāo)簽、訓(xùn)練迭代次數(shù)、批尺寸 model.fit(X_train, y_train, epochs = 5, batch_size = 128)# 在測試集上評估模型 # 評估參數(shù)包括:測試集數(shù)據(jù)、測試集標(biāo)簽 test_loss,test_acc = model.evaluate(X_test, y_test) print(test_acc)# 5.使用模型識別手寫數(shù)字圖片 import numpy as np index = 9plt.matshow(X_test[index].reshape(28,28)) # 查看圖片 plt.show()# 使用模型識別該圖片 result = model.predict(X_test[index].reshape(-1,28*28)) print(np.around(result)) print(np.argmax(result))# 保存模型 # 策略一:保存全模型(網(wǎng)絡(luò)結(jié)構(gòu)+權(quán)重+編譯配置) # model.save('mnist_model.h5') # model = models.load_model('mnist_model.h5') # 策略二:僅保存權(quán)重 # model.save_weights('mnist_weight_model.h5') # model.load_weights('mnist_weight_model.h5') # 策略三:僅保存網(wǎng)絡(luò)結(jié)構(gòu) # json_string = model.to_json() # with open('mnist_model_json.json', 'w') as f: # f.write(json_string) # with open('mnist_model_json.json', 'r') as f: # json_string = f.read() # model = models.model_from_json(json_string)總結(jié)
以上是生活随笔為你收集整理的Keras搭建序贯式模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【物联网毕设】基于Arduino与树莓派
- 下一篇: 负荷需求响应模型 基于Logistic函