当前位置:网站首页>13.模型的保存和載入
13.模型的保存和載入
2022-07-08 00:54:00 【booze-J】
我們以保存3.MNIST數據集分類中訓練的模型為例,來演示模型的保存與載入。
第一種模型保存和載入方式
1.保存方式
保存模型只需要在模型訓練完之後添加上
# 保存模型 可以同時保存模型的結構和參數
model.save("model.h5") # HDF5文件,pip install h5py
這種保存方式可以同時保存模型的結構和參數。
2.載入方式
載入模型之前需要先導入load_model方法
from keras.models import load_model
然後載入的代碼就是簡單一句:
# 載入模型
model = load_model("../model.h5")
這種載入方法可以同時載入模型的結構和參數。
第二種模型保存和載入方式
1.保存方式
模型參數和模型結構分開來保存:
# 保存參數
model.save_weights("my_model_weights.h5")
# 保存網絡結構
json_string = model.to_json()
2.載入方式
在載入模型結構之前,需要先導入model_from_json()方法
from keras.models import model_from_json
分別載入網絡參數和網絡結構:
# 載入參數
model.load_weights("my_model_weights.h5")
# 載入模型結構
model = model_from_json(json_string)
模型再訓練
代碼運行平臺為jupyter-notebook,文章中的代碼塊,也是按照jupyter-notebook中的劃分順序進行書寫的,運行文章代碼,直接分單元粘入到jupyter-notebook即可。
其實模型載入之後是可以進行再訓練的。
1.導入第三方庫
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.models import load_model
2.加載數據及數據預處理
# 載入數據
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) 還未進行one-hot編碼 需要後面自己操作
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,784) reshape()中參數填入-1的話可以自動計算出參數結果 除以255.0是為了歸一化
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)
3.模型再訓練
# 載入模型
model = load_model("../model.h5")
# 評估模型
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("accuracy:",accuracy)
運行結果:
對比首次保存的模型:
可以發現再訓練模型在測試集上的准確率有所提高。
边栏推荐
- Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
- v-for遍历元素样式失效
- 新库上线 | CnOpenData中国星级酒店数据
- 华泰证券官方网站开户安全吗?
- Which securities company has a low, safe and reliable account opening commission
- The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
- "An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
- How to add automatic sorting titles in typora software?
- What has happened from server to cloud hosting?
- Summary of the third course of weidongshan
猜你喜欢

Qt不同类之间建立信号槽,并传递参数
![[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output](/img/79/f5cffe62d5d1e4a69b6143aef561d9.png)
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output

Fofa attack and defense challenge record
![[necessary for R & D personnel] how to make your own dataset and display it.](/img/50/3d826186b563069fd8d433e8feefc4.png)
[necessary for R & D personnel] how to make your own dataset and display it.

基于微信小程序开发的我最在行的小游戏

Play sonar

What if the testing process is not perfect and the development is not active?

An error is reported during the process of setting up ADG. Rman-03009 ora-03113

大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?

AI遮天传 ML-初识决策树
随机推荐
从服务器到云托管,到底经历了什么?
Which securities company has a low, safe and reliable account opening commission
浪潮云溪分布式数据库 Tracing(二)—— 源码解析
Marubeni official website applet configuration tutorial is coming (with detailed steps)
股票开户免费办理佣金最低的券商,手机上开户安全吗
C# 泛型及性能比较
Leetcode brush questions
Handwriting a simulated reentrantlock
What is load balancing? How does DNS achieve load balancing?
How does the markdown editor of CSDN input mathematical formulas--- Latex syntax summary
Four stages of sand table deduction in attack and defense drill
NVIDIA Jetson测试安装yolox过程记录
国外众测之密码找回漏洞
Vscode software
ReentrantLock 公平锁源码 第0篇
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
What has happened from server to cloud hosting?
STL--String类的常用功能复写
韦东山第三期课程内容概要
AI遮天传 ML-初识决策树