佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)

Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)

熱門(mén)標(biāo)簽:語(yǔ)音系統(tǒng) 硅谷的囚徒呼叫中心 客戶服務(wù) Win7旗艦版 企業(yè)做大做強(qiáng) 呼叫中心市場(chǎng)需求 百度AI接口 電話運(yùn)營(yíng)中心

我們以MNIST手寫(xiě)數(shù)字識(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個(gè)神經(jīng)元,輸出10個(gè)神經(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)練過(guò)程中計(jì)算準(zhǔn)確率
model.compile(
  optimizer = sgd,
  loss = 'mse',
  metrics=['accuracy'],
)
 
# 訓(xùn)練模型
model.fit(x_train,y_train,batch_size=64,epochs=5)
 
# 評(píng)估模型
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')
 
# 評(píng)估模型
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)
 
# 評(píng)估模型
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),也就是說(shuō)compile并不會(huì)影響權(quán)重,不會(huì)影響之前訓(xùn)練的問(wèn)題。

如果我們要訓(xùn)練模型或者評(píng)估模型evaluate,則需要compile,因?yàn)橛?xùn)練要使用損失函數(shù)和優(yōu)化器,評(píng)估要使用度量方法;如果我們要預(yù)測(cè),則沒(méi)有必要compile模型。

是否需要多次編譯?

除非我們要更改其中之一:損失函數(shù)、優(yōu)化器 / 學(xué)習(xí)率、度量

又或者我們加載了尚未編譯的模型。或者您的加載/保存方法沒(méi)有考慮以前的編譯。

再次compile的后果?

如果再次編譯模型,將會(huì)丟失優(yōu)化器狀態(tài).

這意味著您的訓(xùn)練在開(kāi)始時(shí)會(huì)受到一點(diǎn)影響,直到調(diào)整學(xué)習(xí)率,動(dòng)量等為止。但是絕對(duì)不會(huì)對(duì)重量造成損害(除非您的初始學(xué)習(xí)率如此之大,以至于第一次訓(xùn)練步驟瘋狂地更改微調(diào)的權(quán)重)。

到此這篇關(guān)于Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Keras保存模型并加載模型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • keras修改backend的簡(jiǎn)單方法
  • keras的get_value運(yùn)行越來(lái)越慢的解決方案
  • 基于keras中訓(xùn)練數(shù)據(jù)的幾種方式對(duì)比(fit和fit_generator)
  • 淺談Keras中fit()和fit_generator()的區(qū)別及其參數(shù)的坑
  • TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn)
  • tensorflow2.0教程之Keras快速入門(mén)
  • 淺析關(guān)于Keras的安裝(pycharm)和初步理解
  • 基于Keras的擴(kuò)展性使用

標(biāo)簽:山西 濟(jì)南 長(zhǎng)沙 山西 崇左 海南 安康 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    读书| 获嘉县| 哈巴河县| 玛曲县| 宁河县| 日喀则市| 齐齐哈尔市| 门源| 中方县| 桃江县| 宁安市| 汉川市| 吉水县| 英吉沙县| 策勒县| 子洲县| 论坛| 黄冈市| 枣强县| 扶风县| 宁武县| 浦东新区| 西乌珠穆沁旗| 阳东县| 通道| 务川| 富裕县| 漳平市| 卢龙县| 密云县| 泾源县| 沭阳县| 龙泉市| 涟源市| 南投县| 富川| 石台县| 怀宁县| 肇源县| 名山县| 福清市|