当前位置:网站首页>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)
運行結果:
對比首次保存的模型:
可以發現再訓練模型在測試集上的准確率有所提高。
边栏推荐
- 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
- C# 泛型及性能比较
- Redis, do you understand the list
- Hotel
- 第一讲:链表中环的入口结点
- How can CSDN indent the first line of a paragraph by 2 characters?
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- 【笔记】常见组合滤波电路
- 取消select的默认样式的向下箭头和设置select默认字样
- NVIDIA Jetson test installation yolox process 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.

Course of causality, taught by Jonas Peters, University of Copenhagen

Service Mesh介绍,Istio概述

DNS 系列(一):为什么更新了 DNS 记录不生效?

玩转Sonar

QT establish signal slots between different classes and transfer parameters

FOFA-攻防挑战记录

51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

Binder core API

新库上线 | CnOpenData中国星级酒店数据
随机推荐
玩转Sonar
What if the testing process is not perfect and the development is not active?
Cve-2022-28346: Django SQL injection vulnerability
Course of causality, taught by Jonas Peters, University of Copenhagen
Basic types of 100 questions for basic grammar of Niuke
How to add automatic sorting titles in typora software?
德总理称乌不会获得“北约式”安全保障
詹姆斯·格雷克《信息简史》读后感记录
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
jemter分布式
Summary of weidongshan phase II course content
他们齐聚 2022 ECUG Con,只为「中国技术力量」
Codeforces Round #804 (Div. 2)(A~D)
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
赞!idea 如何单窗口打开多个项目?
Stock account opening is free of charge. Is it safe to open an account on your mobile phone
丸子官网小程序配置教程来了(附详细步骤)
My best game based on wechat applet development