Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)
我們以MNIST手寫數(shù)字識別為例
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
# 載入數(shù)據(jù)
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000,28,28)
print('x_shape:',x_train.shape)
# (60000)
print('y_shape:',y_train.shape)
# (60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# 換one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
# 創(chuàng)建模型,輸入784個神經(jīng)元,輸出10個神經(jīng)元
model = Sequential([
Dense(units=10,input_dim=784,bias_initializer='one',activation='softmax')
])
# 定義優(yōu)化器
sgd = SGD(lr=0.2)
# 定義優(yōu)化器,loss function,訓(xùn)練過程中計(jì)算準(zhǔn)確率
model.compile(
optimizer = sgd,
loss = 'mse',
metrics=['accuracy'],
)
# 訓(xùn)練模型
model.fit(x_train,y_train,batch_size=64,epochs=5)
# 評估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('\ntest loss',loss)
print('accuracy',accuracy)
# 保存模型
model.save('model.h5') # HDF5文件,pip install h5py


載入初次訓(xùn)練的模型,再訓(xùn)練
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.models import load_model
# 載入數(shù)據(jù)
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000,28,28)
print('x_shape:',x_train.shape)
# (60000)
print('y_shape:',y_train.shape)
# (60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# 換one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
# 載入模型
model = load_model('model.h5')
# 評估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('\ntest loss',loss)
print('accuracy',accuracy)
# 訓(xùn)練模型
model.fit(x_train,y_train,batch_size=64,epochs=2)
# 評估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('\ntest loss',loss)
print('accuracy',accuracy)
# 保存參數(shù),載入?yún)?shù)
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
# 保存網(wǎng)絡(luò)結(jié)構(gòu),載入網(wǎng)絡(luò)結(jié)構(gòu)
from keras.models import model_from_json
json_string = model.to_json()
model = model_from_json(json_string)
print(json_string)
關(guān)于compile和load_model()的使用順序
這一段落主要是為了解決我們fit、evaluate、predict之前還是之后使用compile。想要弄明白,首先我們要清楚compile在程序中是做什么的?都做了什么?
compile做什么?
compile定義了loss function損失函數(shù)、optimizer優(yōu)化器和metrics度量。它與權(quán)重?zé)o關(guān),也就是說compile并不會影響權(quán)重,不會影響之前訓(xùn)練的問題。
如果我們要訓(xùn)練模型或者評估模型evaluate,則需要compile,因?yàn)橛?xùn)練要使用損失函數(shù)和優(yōu)化器,評估要使用度量方法;如果我們要預(yù)測,則沒有必要compile模型。
是否需要多次編譯?
除非我們要更改其中之一:損失函數(shù)、優(yōu)化器 / 學(xué)習(xí)率、度量
又或者我們加載了尚未編譯的模型?;蛘吣募虞d/保存方法沒有考慮以前的編譯。
再次compile的后果?
如果再次編譯模型,將會丟失優(yōu)化器狀態(tài).
這意味著您的訓(xùn)練在開始時(shí)會受到一點(diǎn)影響,直到調(diào)整學(xué)習(xí)率,動量等為止。但是絕對不會對重量造成損害(除非您的初始學(xué)習(xí)率如此之大,以至于第一次訓(xùn)練步驟瘋狂地更改微調(diào)的權(quán)重)。
到此這篇關(guān)于Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Keras保存模型并加載模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)對切片命名清除索引的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)對切片命名清除索引的方法,結(jié)合實(shí)例形式分析了Python字符串截取及indices方法映射序列的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Python應(yīng)用利器之緩存機(jī)制的妙用詳解
在 Python 應(yīng)用程序中,使用緩存能夠顯著提高性能并降低資源消耗,本文將詳細(xì)介紹如何在 Python 中實(shí)現(xiàn)緩存機(jī)制,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
這篇文章主要介紹了Python面向?qū)ο箢惥帉懠?xì)節(jié),較為詳細(xì)的分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類,方法,繼承,超類,接口等相關(guān)概念、使用技巧與注意事項(xiàng),需要的朋友可以參考下2019-01-01
在Python的Django框架中創(chuàng)建語言文件
這篇文章主要介紹了在Python的Django框架中創(chuàng)建語言文件的方法,以語言代碼來表示語言區(qū)域種類,需要的朋友可以參考下2015-07-07
詳解Python的Django框架中manage命令的使用與擴(kuò)展
這篇文章主要介紹了Python的Django框架中manage命令的使用與擴(kuò)展,manage.py使得用戶借助manage命令在命令行中能實(shí)現(xiàn)諸多簡便的操作,需要的朋友可以參考下2016-04-04

